shared-types.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 common
  99. * @docsPage Configurable Operations
  100. */
  101. export type ConfigArgType = 'string' | 'int' | 'float' | 'boolean' | 'datetime' | 'facetValueIds';
  102. export type ConfigArgSubset<T extends ConfigArgType> = T;
  103. export type CustomFieldsObject = { [key: string]: any };
  104. /**
  105. * @description
  106. * This interface describes JSON config file (vendure-ui-config.json) used by the Admin UI.
  107. * The values are loaded at run-time by the Admin UI app, and allow core configuration to be
  108. * managed without the need to re-build the application.
  109. *
  110. * @docsCategory AdminUiPlugin
  111. */
  112. export interface AdminUiConfig {
  113. /**
  114. * @description
  115. * The hostname of the Vendure server which the admin ui will be making API calls
  116. * to. If set to "auto", the Admin UI app will determine the hostname from the
  117. * current location (i.e. `window.location.hostname`).
  118. *
  119. * @default 'http://localhost'
  120. */
  121. apiHost: string | 'auto';
  122. /**
  123. * @description
  124. * The port of the Vendure server which the admin ui will be making API calls
  125. * to. If set to "auto", the Admin UI app will determine the port from the
  126. * current location (i.e. `window.location.port`).
  127. *
  128. * @default 3000
  129. */
  130. apiPort: number | 'auto';
  131. /**
  132. * @description
  133. * The path to the GraphQL Admin API.
  134. *
  135. * @default 'admin-api'
  136. */
  137. adminApiPath: string;
  138. /**
  139. * @description
  140. * Whether to use cookies or bearer tokens to track sessions.
  141. * Should match the setting of in the server's `tokenMethod` config
  142. * option.
  143. *
  144. * @default 'cookie'
  145. */
  146. tokenMethod: 'cookie' | 'bearer';
  147. /**
  148. * @description
  149. * The header used when using the 'bearer' auth method. Should match the
  150. * setting of the server's `authOptions.authTokenHeaderKey` config
  151. * option.
  152. *
  153. * @default 'vendure-auth-token'
  154. */
  155. authTokenHeaderKey: string;
  156. /**
  157. * @description
  158. * The default language for the Admin UI. Must be one of the
  159. * items specified in the `availableLanguages` property.
  160. *
  161. * @default LanguageCode.en
  162. */
  163. defaultLanguage: LanguageCode;
  164. /**
  165. * @description
  166. * An array of languages for which translations exist for the Admin UI.
  167. *
  168. * @default [LanguageCode.en, LanguageCode.es]
  169. */
  170. availableLanguages: LanguageCode[];
  171. }
  172. /**
  173. * @description
  174. * Configures the path to a custom-build of the Admin UI app.
  175. *
  176. * @docsCategory common
  177. */
  178. export interface AdminUiAppConfig {
  179. /**
  180. * @description
  181. * The path to the compiled admin ui app files. If not specified, an internal
  182. * default build is used. This path should contain the `vendure-ui-config.json` file,
  183. * index.html, the compiled js bundles etc.
  184. */
  185. path: string;
  186. /**
  187. * @description
  188. * The function which will be invoked to start the app compilation process.
  189. */
  190. compile?: () => Promise<void>;
  191. }
  192. /**
  193. * @description
  194. * Information about the Admin UI app dev server.
  195. *
  196. * @docsCategory common
  197. */
  198. export interface AdminUiAppDevModeConfig {
  199. /**
  200. * @description
  201. * The path to the uncompiled ui app source files. This path should contain the `vendure-ui-config.json` file.
  202. */
  203. sourcePath: string;
  204. /**
  205. * @description
  206. * The port on which the dev server is listening. Overrides the value set by `AdminUiOptions.port`.
  207. */
  208. port: number;
  209. /**
  210. * @description
  211. * The function which will be invoked to start the app compilation process.
  212. */
  213. compile: () => Promise<void>;
  214. }