shared-types.ts 9.3 KB

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