shared-types.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // tslint:disable:no-shadowed-variable
  2. // prettier-ignore
  3. import { LanguageCode, LocalizedString } from './generated-types';
  4. /**
  5. * A recursive implementation of the Partial<T> type.
  6. * Source: https://stackoverflow.com/a/49936686/772859
  7. */
  8. export type DeepPartial<T> = {
  9. [P in keyof T]?:
  10. | null
  11. | (T[P] extends Array<infer U>
  12. ? Array<DeepPartial<U>>
  13. : T[P] extends ReadonlyArray<infer U>
  14. ? ReadonlyArray<DeepPartial<U>>
  15. : DeepPartial<T[P]>);
  16. };
  17. // tslint:enable:no-shadowed-variable
  18. // tslint:disable:ban-types
  19. /**
  20. * A recursive implementation of Required<T>.
  21. * Source: https://github.com/microsoft/TypeScript/issues/15012#issuecomment-365453623
  22. */
  23. export type DeepRequired<T, U extends object | undefined = undefined> = T extends object
  24. ? {
  25. [P in keyof T]-?: NonNullable<T[P]> extends NonNullable<U | Function | Type<any>>
  26. ? NonNullable<T[P]>
  27. : DeepRequired<NonNullable<T[P]>, U>;
  28. }
  29. : T;
  30. // tslint:enable:ban-types
  31. /**
  32. * A type representing the type rather than instance of a class.
  33. */
  34. export interface Type<T> extends Function {
  35. // tslint:disable-next-line:callable-types
  36. new (...args: any[]): T;
  37. }
  38. export type Json = null | boolean | number | string | Json[] | { [prop: string]: Json };
  39. /**
  40. * @description
  41. * A type representing JSON-compatible values.
  42. * From https://github.com/microsoft/TypeScript/issues/1897#issuecomment-580962081
  43. *
  44. * @docsCategory common
  45. */
  46. export type JsonCompatible<T> = {
  47. [P in keyof T]: T[P] extends Json
  48. ? T[P]
  49. : Pick<T, P> extends Required<Pick<T, P>>
  50. ? never
  51. : JsonCompatible<T[P]>;
  52. };
  53. /**
  54. * @description
  55. * A type describing the shape of a paginated list response.
  56. *
  57. * @docsCategory common
  58. */
  59. export type PaginatedList<T> = {
  60. items: T[];
  61. totalItems: number;
  62. };
  63. /**
  64. * @description
  65. * An entity ID. Depending on the configured {@link EntityIdStrategy}, it will be either
  66. * a `string` or a `number`;
  67. *
  68. * @docsCategory common
  69. */
  70. export type ID = string | number;
  71. /**
  72. * @description
  73. * A data type for a custom field. The CustomFieldType determines the data types used in the generated
  74. * database columns and GraphQL fields as follows (key: m = MySQL, p = Postgres, s = SQLite):
  75. *
  76. * Type | DB type | GraphQL type
  77. * ----- |--------- |---------------
  78. * string | varchar | String
  79. * localeString | varchar | String
  80. * text | longtext(m), text(p,s) | String
  81. * int | int | Int
  82. * float | double precision | Float
  83. * boolean | tinyint (m), bool (p), boolean (s) | Boolean
  84. * datetime | datetime (m,s), timestamp (p) | DateTime
  85. * relation | many-to-one / many-to-many relation | As specified in config
  86. *
  87. * Additionally, the CustomFieldType also dictates which [configuration options](/docs/typescript-api/custom-fields/#configuration-options)
  88. * are available for that custom field.
  89. *
  90. * @docsCategory custom-fields
  91. */
  92. export type CustomFieldType =
  93. | 'string'
  94. | 'localeString'
  95. | 'int'
  96. | 'float'
  97. | 'boolean'
  98. | 'datetime'
  99. | 'relation'
  100. | 'text';
  101. /**
  102. * @description
  103. * Certain entities (those which implement {@link ConfigurableOperationDef}) allow arbitrary
  104. * configuration arguments to be specified which can then be set in the admin-ui and used in
  105. * the business logic of the app. These are the valid data types of such arguments.
  106. * The data type influences:
  107. *
  108. * 1. How the argument form field is rendered in the admin-ui
  109. * 2. The JavaScript type into which the value is coerced before being passed to the business logic.
  110. *
  111. * @docsCategory ConfigurableOperationDef
  112. */
  113. export type ConfigArgType = 'string' | 'int' | 'float' | 'boolean' | 'datetime' | 'ID';
  114. /**
  115. * @description
  116. * The ids of the default form input components that ship with the
  117. * Admin UI.
  118. *
  119. * @docsCategory ConfigurableOperationDef
  120. */
  121. export type DefaultFormComponentId =
  122. | 'boolean-form-input'
  123. | 'currency-form-input'
  124. | 'date-form-input'
  125. | 'facet-value-form-input'
  126. | 'number-form-input'
  127. | 'select-form-input'
  128. | 'product-selector-form-input'
  129. | 'customer-group-form-input'
  130. | 'text-form-input'
  131. | 'textarea-form-input'
  132. | 'password-form-input'
  133. | 'relation-form-input';
  134. /**
  135. * @description
  136. * Used to defined the expected arguments for a given default form input component.
  137. *
  138. * @docsCategory ConfigurableOperationDef
  139. */
  140. type DefaultFormConfigHash = {
  141. 'date-form-input': { min?: string; max?: string; yearRange?: number };
  142. 'number-form-input': { min?: number; max?: number; step?: number; prefix?: string; suffix?: string };
  143. 'select-form-input': {
  144. options?: Array<{ value: string; label?: Array<Omit<LocalizedString, '__typename'>> }>;
  145. };
  146. 'boolean-form-input': {};
  147. 'currency-form-input': {};
  148. 'facet-value-form-input': {};
  149. 'product-selector-form-input': {};
  150. 'customer-group-form-input': {};
  151. 'text-form-input': {};
  152. 'textarea-form-input': {
  153. spellcheck?: boolean;
  154. };
  155. 'password-form-input': {};
  156. 'relation-form-input': {};
  157. };
  158. export type DefaultFormComponentConfig<T extends DefaultFormComponentId> = DefaultFormConfigHash[T];
  159. export type CustomFieldsObject = { [key: string]: any };
  160. /**
  161. * @description
  162. * This interface describes JSON config file (vendure-ui-config.json) used by the Admin UI.
  163. * The values are loaded at run-time by the Admin UI app, and allow core configuration to be
  164. * managed without the need to re-build the application.
  165. *
  166. * @docsCategory AdminUiPlugin
  167. */
  168. export interface AdminUiConfig {
  169. /**
  170. * @description
  171. * The hostname of the Vendure server which the admin ui will be making API calls
  172. * to. If set to "auto", the Admin UI app will determine the hostname from the
  173. * current location (i.e. `window.location.hostname`).
  174. *
  175. * @default 'http://localhost'
  176. */
  177. apiHost: string | 'auto';
  178. /**
  179. * @description
  180. * The port of the Vendure server which the admin ui will be making API calls
  181. * to. If set to "auto", the Admin UI app will determine the port from the
  182. * current location (i.e. `window.location.port`).
  183. *
  184. * @default 3000
  185. */
  186. apiPort: number | 'auto';
  187. /**
  188. * @description
  189. * The path to the GraphQL Admin API.
  190. *
  191. * @default 'admin-api'
  192. */
  193. adminApiPath: string;
  194. /**
  195. * @description
  196. * Whether to use cookies or bearer tokens to track sessions.
  197. * Should match the setting of in the server's `tokenMethod` config
  198. * option.
  199. *
  200. * @default 'cookie'
  201. */
  202. tokenMethod: 'cookie' | 'bearer';
  203. /**
  204. * @description
  205. * The header used when using the 'bearer' auth method. Should match the
  206. * setting of the server's `authOptions.authTokenHeaderKey` config
  207. * option.
  208. *
  209. * @default 'vendure-auth-token'
  210. */
  211. authTokenHeaderKey: string;
  212. /**
  213. * @description
  214. * The default language for the Admin UI. Must be one of the
  215. * items specified in the `availableLanguages` property.
  216. *
  217. * @default LanguageCode.en
  218. */
  219. defaultLanguage: LanguageCode;
  220. /**
  221. * @description
  222. * An array of languages for which translations exist for the Admin UI.
  223. *
  224. * @default [LanguageCode.en, LanguageCode.es]
  225. */
  226. availableLanguages: LanguageCode[];
  227. /**
  228. * @description
  229. * If you are using an external {@link AuthenticationStrategy} for the Admin API, you can configure
  230. * a custom URL for the login page with this option. On logging out or redirecting an unauthenticated
  231. * user, the Admin UI app will redirect the user to this URL rather than the default username/password
  232. * screen.
  233. */
  234. loginUrl?: string;
  235. /**
  236. * @description
  237. * The custom brand name.
  238. */
  239. brand?: string;
  240. /**
  241. * @description
  242. * Option to hide vendure branding.
  243. *
  244. * @default false
  245. */
  246. hideVendureBranding?: boolean;
  247. /**
  248. * @description
  249. * Option to hide version.
  250. *
  251. * @default false
  252. */
  253. hideVersion?: boolean;
  254. }
  255. /**
  256. * @description
  257. * Configures the path to a custom-build of the Admin UI app.
  258. *
  259. * @docsCategory common
  260. */
  261. export interface AdminUiAppConfig {
  262. /**
  263. * @description
  264. * The path to the compiled admin ui app files. If not specified, an internal
  265. * default build is used. This path should contain the `vendure-ui-config.json` file,
  266. * index.html, the compiled js bundles etc.
  267. */
  268. path: string;
  269. /**
  270. * @description
  271. * Specifies the url route to the Admin UI app.
  272. *
  273. * @default 'admin'
  274. */
  275. route?: string;
  276. /**
  277. * @description
  278. * The function which will be invoked to start the app compilation process.
  279. */
  280. compile?: () => Promise<void>;
  281. }
  282. /**
  283. * @description
  284. * Information about the Admin UI app dev server.
  285. *
  286. * @docsCategory common
  287. */
  288. export interface AdminUiAppDevModeConfig {
  289. /**
  290. * @description
  291. * The path to the uncompiled ui app source files. This path should contain the `vendure-ui-config.json` file.
  292. */
  293. sourcePath: string;
  294. /**
  295. * @description
  296. * The port on which the dev server is listening. Overrides the value set by `AdminUiOptions.port`.
  297. */
  298. port: number;
  299. /**
  300. * @description
  301. * Specifies the url route to the Admin UI app.
  302. *
  303. * @default 'admin'
  304. */
  305. route?: string;
  306. /**
  307. * @description
  308. * The function which will be invoked to start the app compilation process.
  309. */
  310. compile: () => Promise<void>;
  311. }