shared-types.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // tslint:disable:no-shadowed-variable
  2. // prettier-ignore
  3. import { LanguageCode } 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. * int | int | Int
  78. * float | double precision | Float
  79. * boolean | tinyint (m), bool (p), boolean (s) | Boolean
  80. * datetime | datetime (m,s), timestamp (p) | DateTime
  81. *
  82. * Additionally, the CustomFieldType also dictates which [configuration options](/docs/typescript-api/custom-fields/#configuration-options)
  83. * are available for that custom field.
  84. *
  85. * @docsCategory custom-fields
  86. */
  87. export type CustomFieldType = 'string' | 'localeString' | 'int' | 'float' | 'boolean' | 'datetime';
  88. /**
  89. * @description
  90. * Certain entities (those which implement {@link ConfigurableOperationDef}) allow arbitrary
  91. * configuration arguments to be specified which can then be set in the admin-ui and used in
  92. * the business logic of the app. These are the valid data types of such arguments.
  93. * The data type influences:
  94. *
  95. * 1. How the argument form field is rendered in the admin-ui
  96. * 2. The JavaScript type into which the value is coerced before being passed to the business logic.
  97. *
  98. * @docsCategory ConfigurableOperationDef
  99. */
  100. export type ConfigArgType = 'string' | 'int' | 'float' | 'boolean' | 'datetime' | 'ID';
  101. /**
  102. * @description
  103. * The ids of the default form input components that ship with the
  104. * Admin UI.
  105. *
  106. * @docsCategory ConfigurableOperationDef
  107. */
  108. export type DefaultFormComponentId =
  109. | 'boolean-form-input'
  110. | 'currency-form-input'
  111. | 'date-form-input'
  112. | 'facet-value-form-input'
  113. | 'number-form-input'
  114. | 'select-form-input'
  115. | 'product-selector-form-input'
  116. | 'customer-group-form-input'
  117. | 'text-form-input'
  118. | 'password-form-input';
  119. /**
  120. * @description
  121. * Used to defined the expected arguments for a given default form input component.
  122. *
  123. * @docsCategory ConfigurableOperationDef
  124. */
  125. type DefaultFormConfigHash = {
  126. 'date-form-input': { min?: string; max?: string; yearRange?: number };
  127. 'number-form-input': { min?: number; max?: number; step?: number; prefix?: string; suffix?: string };
  128. 'select-form-input': { options?: Array<{ value: string; label?: string }> };
  129. 'boolean-form-input': {};
  130. 'currency-form-input': {};
  131. 'facet-value-form-input': {};
  132. 'product-selector-form-input': {};
  133. 'customer-group-form-input': {};
  134. 'text-form-input': {};
  135. 'password-form-input': {};
  136. };
  137. export type DefaultFormComponentConfig<T extends DefaultFormComponentId> = DefaultFormConfigHash[T];
  138. export type CustomFieldsObject = { [key: string]: any };
  139. /**
  140. * @description
  141. * This interface describes JSON config file (vendure-ui-config.json) used by the Admin UI.
  142. * The values are loaded at run-time by the Admin UI app, and allow core configuration to be
  143. * managed without the need to re-build the application.
  144. *
  145. * @docsCategory AdminUiPlugin
  146. */
  147. export interface AdminUiConfig {
  148. /**
  149. * @description
  150. * The hostname of the Vendure server which the admin ui will be making API calls
  151. * to. If set to "auto", the Admin UI app will determine the hostname from the
  152. * current location (i.e. `window.location.hostname`).
  153. *
  154. * @default 'http://localhost'
  155. */
  156. apiHost: string | 'auto';
  157. /**
  158. * @description
  159. * The port of the Vendure server which the admin ui will be making API calls
  160. * to. If set to "auto", the Admin UI app will determine the port from the
  161. * current location (i.e. `window.location.port`).
  162. *
  163. * @default 3000
  164. */
  165. apiPort: number | 'auto';
  166. /**
  167. * @description
  168. * The path to the GraphQL Admin API.
  169. *
  170. * @default 'admin-api'
  171. */
  172. adminApiPath: string;
  173. /**
  174. * @description
  175. * Whether to use cookies or bearer tokens to track sessions.
  176. * Should match the setting of in the server's `tokenMethod` config
  177. * option.
  178. *
  179. * @default 'cookie'
  180. */
  181. tokenMethod: 'cookie' | 'bearer';
  182. /**
  183. * @description
  184. * The header used when using the 'bearer' auth method. Should match the
  185. * setting of the server's `authOptions.authTokenHeaderKey` config
  186. * option.
  187. *
  188. * @default 'vendure-auth-token'
  189. */
  190. authTokenHeaderKey: string;
  191. /**
  192. * @description
  193. * The default language for the Admin UI. Must be one of the
  194. * items specified in the `availableLanguages` property.
  195. *
  196. * @default LanguageCode.en
  197. */
  198. defaultLanguage: LanguageCode;
  199. /**
  200. * @description
  201. * An array of languages for which translations exist for the Admin UI.
  202. *
  203. * @default [LanguageCode.en, LanguageCode.es]
  204. */
  205. availableLanguages: LanguageCode[];
  206. /**
  207. * @description
  208. * If you are using an external {@link AuthenticationStrategy} for the Admin API, you can configure
  209. * a custom URL for the login page with this option. On logging out or redirecting an unauthenticated
  210. * user, the Admin UI app will redirect the user to this URL rather than the default username/password
  211. * screen.
  212. */
  213. loginUrl?: string;
  214. }
  215. /**
  216. * @description
  217. * Configures the path to a custom-build of the Admin UI app.
  218. *
  219. * @docsCategory common
  220. */
  221. export interface AdminUiAppConfig {
  222. /**
  223. * @description
  224. * The path to the compiled admin ui app files. If not specified, an internal
  225. * default build is used. This path should contain the `vendure-ui-config.json` file,
  226. * index.html, the compiled js bundles etc.
  227. */
  228. path: string;
  229. /**
  230. * @description
  231. * The function which will be invoked to start the app compilation process.
  232. */
  233. compile?: () => Promise<void>;
  234. }
  235. /**
  236. * @description
  237. * Information about the Admin UI app dev server.
  238. *
  239. * @docsCategory common
  240. */
  241. export interface AdminUiAppDevModeConfig {
  242. /**
  243. * @description
  244. * The path to the uncompiled ui app source files. This path should contain the `vendure-ui-config.json` file.
  245. */
  246. sourcePath: string;
  247. /**
  248. * @description
  249. * The port on which the dev server is listening. Overrides the value set by `AdminUiOptions.port`.
  250. */
  251. port: number;
  252. /**
  253. * @description
  254. * The function which will be invoked to start the app compilation process.
  255. */
  256. compile: () => Promise<void>;
  257. }