shared-types.ts 9.6 KB

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