shared-types.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* eslint-disable no-shadow,@typescript-eslint/no-shadow */
  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. /* eslint-enable no-shadow, @typescript-eslint/no-shadow */
  18. /* eslint-disable @typescript-eslint/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. /* eslint-enable @typescript-eslint/ban-types */
  31. /**
  32. * A type representing the type rather than instance of a class.
  33. */
  34. // eslint-disable-next-line @typescript-eslint/ban-types
  35. export interface Type<T> extends Function {
  36. // eslint-disable-next-line @typescript-eslint/prefer-function-type
  37. new (...args: any[]): T;
  38. }
  39. export type Json = null | boolean | number | string | Json[] | { [prop: string]: Json };
  40. /**
  41. * @description
  42. * A type representing JSON-compatible values.
  43. * From https://github.com/microsoft/TypeScript/issues/1897#issuecomment-580962081
  44. *
  45. * @docsCategory common
  46. */
  47. export type JsonCompatible<T> = {
  48. [P in keyof T]: T[P] extends Json
  49. ? T[P]
  50. : Pick<T, P> extends Required<Pick<T, P>>
  51. ? never
  52. : JsonCompatible<T[P]>;
  53. };
  54. /**
  55. * @description
  56. * A type describing the shape of a paginated list response. In Vendure, almost all list queries
  57. * (`products`, `collections`, `orders`, `customers` etc) return an object of this type.
  58. *
  59. * @docsCategory common
  60. */
  61. export type PaginatedList<T> = {
  62. items: T[];
  63. totalItems: number;
  64. };
  65. /**
  66. * @description
  67. * An entity ID. Depending on the configured {@link EntityIdStrategy}, it will be either
  68. * a `string` or a `number`;
  69. *
  70. * @docsCategory common
  71. */
  72. export type ID = string | number;
  73. /**
  74. * @description
  75. * A data type for a custom field. The CustomFieldType determines the data types used in the generated
  76. * database columns and GraphQL fields as follows (key: m = MySQL, p = Postgres, s = SQLite):
  77. *
  78. * Type | DB type | GraphQL type
  79. * ----- |--------- |---------------
  80. * string | varchar | String
  81. * localeString | varchar | String
  82. * text | longtext(m), text(p,s) | String
  83. * localeText | longtext(m), text(p,s) | String
  84. * int | int | Int
  85. * float | double precision | Float
  86. * boolean | tinyint (m), bool (p), boolean (s) | Boolean
  87. * datetime | datetime (m,s), timestamp (p) | DateTime
  88. * struct | json (m), jsonb (p), text (s) | JSON
  89. * relation | many-to-one / many-to-many relation | As specified in config
  90. *
  91. * Additionally, the CustomFieldType also dictates which [configuration options](/guides/developer-guide/custom-fields/#custom-field-config-properties)
  92. * are available for that custom field.
  93. *
  94. * @docsCategory custom-fields
  95. */
  96. export type CustomFieldType =
  97. | 'string'
  98. | 'localeString'
  99. | 'int'
  100. | 'float'
  101. | 'boolean'
  102. | 'datetime'
  103. | 'relation'
  104. | 'text'
  105. | 'localeText'
  106. | 'struct';
  107. export type StructFieldType = 'string' | 'int' | 'float' | 'boolean' | 'datetime' | 'text';
  108. /**
  109. * @description
  110. * Certain entities (those which implement {@link ConfigurableOperationDef}) allow arbitrary
  111. * configuration arguments to be specified which can then be set in the admin-ui and used in
  112. * the business logic of the app. These are the valid data types of such arguments.
  113. * The data type influences:
  114. *
  115. * 1. How the argument form field is rendered in the admin-ui
  116. * 2. The JavaScript type into which the value is coerced before being passed to the business logic.
  117. *
  118. * @docsCategory ConfigurableOperationDef
  119. */
  120. export type ConfigArgType = 'string' | 'int' | 'float' | 'boolean' | 'datetime' | 'ID';
  121. /**
  122. * @description
  123. * The ids of the default form input components that ship with the
  124. * Admin UI.
  125. *
  126. * @docsCategory ConfigurableOperationDef
  127. */
  128. export type DefaultFormComponentId =
  129. | 'boolean-form-input'
  130. | 'currency-form-input'
  131. | 'customer-group-form-input'
  132. | 'date-form-input'
  133. | 'facet-value-form-input'
  134. | 'json-editor-form-input'
  135. | 'html-editor-form-input'
  136. | 'number-form-input'
  137. | 'password-form-input'
  138. | 'product-selector-form-input'
  139. | 'relation-form-input'
  140. | 'rich-text-form-input'
  141. | 'select-form-input'
  142. | 'text-form-input'
  143. | 'textarea-form-input'
  144. | 'product-multi-form-input'
  145. | 'combination-mode-form-input'
  146. | 'struct-form-input';
  147. /**
  148. * @description
  149. * Used to define the expected arguments for a given default form input component.
  150. *
  151. * @docsCategory ConfigurableOperationDef
  152. */
  153. type DefaultFormConfigHash = {
  154. 'boolean-form-input': Record<string, never>;
  155. 'currency-form-input': Record<string, never>;
  156. 'customer-group-form-input': Record<string, never>;
  157. 'date-form-input': { min?: string; max?: string; yearRange?: number };
  158. 'facet-value-form-input': Record<string, never>;
  159. 'json-editor-form-input': { height?: string };
  160. 'html-editor-form-input': { height?: string };
  161. 'number-form-input': { min?: number; max?: number; step?: number; prefix?: string; suffix?: string };
  162. 'password-form-input': Record<string, never>;
  163. 'product-selector-form-input': Record<string, never>;
  164. 'relation-form-input': Record<string, never>;
  165. 'rich-text-form-input': Record<string, never>;
  166. 'select-form-input': {
  167. options?: Array<{ value: string; label?: Array<Omit<LocalizedString, '__typename'>> }>;
  168. };
  169. 'text-form-input': { prefix?: string; suffix?: string };
  170. 'textarea-form-input': {
  171. spellcheck?: boolean;
  172. };
  173. 'product-multi-form-input': {
  174. selectionMode?: 'product' | 'variant';
  175. };
  176. 'combination-mode-form-input': Record<string, never>;
  177. 'struct-form-input': Record<string, never>;
  178. };
  179. export type DefaultFormComponentUiConfig<T extends DefaultFormComponentId | string> =
  180. T extends DefaultFormComponentId ? DefaultFormConfigHash[T] : any;
  181. export type DefaultFormComponentConfig<T extends DefaultFormComponentId | string> =
  182. DefaultFormComponentUiConfig<T> & {
  183. ui?: DefaultFormComponentUiConfig<T>;
  184. };
  185. export type UiComponentConfig<T extends DefaultFormComponentId | string> = T extends DefaultFormComponentId
  186. ? {
  187. component: T;
  188. tab?: string;
  189. } & DefaultFormConfigHash[T]
  190. : {
  191. component: string;
  192. tab?: string;
  193. [prop: string]: any;
  194. };
  195. export type CustomFieldsObject = { [key: string]: any };
  196. /**
  197. * @description
  198. * This interface describes JSON config file (vendure-ui-config.json) used by the Admin UI.
  199. * The values are loaded at run-time by the Admin UI app, and allow core configuration to be
  200. * managed without the need to re-build the application.
  201. *
  202. * @docsCategory common/AdminUi
  203. */
  204. export interface AdminUiConfig {
  205. /**
  206. * @description
  207. * The hostname of the Vendure server which the admin UI will be making API calls
  208. * to. If set to "auto", the Admin UI app will determine the hostname from the
  209. * current location (i.e. `window.location.hostname`).
  210. *
  211. * @default 'auto'
  212. */
  213. apiHost: string | 'auto';
  214. /**
  215. * @description
  216. * The port of the Vendure server which the admin UI will be making API calls
  217. * to. If set to "auto", the Admin UI app will determine the port from the
  218. * current location (i.e. `window.location.port`).
  219. *
  220. * @default 'auto'
  221. */
  222. apiPort: number | 'auto';
  223. /**
  224. * @description
  225. * The path to the GraphQL Admin API.
  226. *
  227. * @default 'admin-api'
  228. */
  229. adminApiPath: string;
  230. /**
  231. * @description
  232. * Whether to use cookies or bearer tokens to track sessions.
  233. * Should match the setting of in the server's `tokenMethod` config
  234. * option.
  235. *
  236. * @default 'cookie'
  237. */
  238. tokenMethod: 'cookie' | 'bearer';
  239. /**
  240. * @description
  241. * The header used when using the 'bearer' auth method. Should match the
  242. * setting of the server's `authOptions.authTokenHeaderKey` config option.
  243. *
  244. * @default 'vendure-auth-token'
  245. */
  246. authTokenHeaderKey: string;
  247. /**
  248. * @description
  249. * The name of the header which contains the channel token. Should match the
  250. * setting of the server's `apiOptions.channelTokenKey` config option.
  251. *
  252. * @default 'vendure-token'
  253. */
  254. channelTokenKey: string;
  255. /**
  256. * @description
  257. * The default language for the Admin UI. Must be one of the
  258. * items specified in the `availableLanguages` property.
  259. *
  260. * @default LanguageCode.en
  261. */
  262. defaultLanguage: LanguageCode;
  263. /**
  264. * @description
  265. * The default locale for the Admin UI. The locale affects the formatting of
  266. * currencies & dates. Must be one of the items specified
  267. * in the `availableLocales` property.
  268. *
  269. * If not set, the browser default locale will be used.
  270. *
  271. * @since 2.2.0
  272. */
  273. defaultLocale?: string;
  274. /**
  275. * @description
  276. * An array of languages for which translations exist for the Admin UI.
  277. */
  278. availableLanguages: LanguageCode[];
  279. /**
  280. * @description
  281. * An array of locales to be used on Admin UI.
  282. *
  283. * @since 2.2.0
  284. */
  285. availableLocales: string[];
  286. /**
  287. * @description
  288. * If you are using an external {@link AuthenticationStrategy} for the Admin API, you can configure
  289. * a custom URL for the login page with this option. On logging out or redirecting an unauthenticated
  290. * user, the Admin UI app will redirect the user to this URL rather than the default username/password
  291. * screen.
  292. */
  293. loginUrl?: string;
  294. /**
  295. * @description
  296. * The custom brand name.
  297. */
  298. brand?: string;
  299. /**
  300. * @description
  301. * Option to hide vendure branding.
  302. *
  303. * @default false
  304. */
  305. hideVendureBranding?: boolean;
  306. /**
  307. * @description
  308. * Option to hide version.
  309. *
  310. * @default false
  311. */
  312. hideVersion?: boolean;
  313. /**
  314. * @description
  315. * A url of a custom image to be used on the login screen, to override the images provided by Vendure's login image server.
  316. *
  317. * @since 1.9.0
  318. */
  319. loginImageUrl?: string;
  320. /**
  321. * @description
  322. * Allows you to provide default reasons for a refund or cancellation. This will be used in the
  323. * refund/cancel dialog. The values can be literal strings (e.g. "Not in stock") or translation
  324. * tokens (see [Adding Admin UI Translations](/guides/extending-the-admin-ui/adding-ui-translations/)).
  325. *
  326. * @since 1.5.0
  327. * @default ['order.cancel-reason-customer-request', 'order.cancel-reason-not-available']
  328. */
  329. cancellationReasons?: string[];
  330. }
  331. /**
  332. * @description
  333. * Configures the path to a custom-build of the Admin UI app.
  334. *
  335. * @docsCategory common/AdminUi
  336. */
  337. export interface AdminUiAppConfig {
  338. /**
  339. * @description
  340. * The path to the compiled admin UI app files. If not specified, an internal
  341. * default build is used. This path should contain the `vendure-ui-config.json` file,
  342. * index.html, the compiled js bundles etc.
  343. */
  344. path: string;
  345. /**
  346. * @description
  347. * Specifies the url route to the Admin UI app.
  348. *
  349. * @default 'admin'
  350. */
  351. route?: string;
  352. /**
  353. * @description
  354. * The function which will be invoked to start the app compilation process.
  355. */
  356. compile?: () => Promise<void>;
  357. }
  358. /**
  359. * @description
  360. * Information about the Admin UI app dev server.
  361. *
  362. * @docsCategory common/AdminUi
  363. */
  364. export interface AdminUiAppDevModeConfig {
  365. /**
  366. * @description
  367. * The path to the uncompiled UI app source files. This path should contain the `vendure-ui-config.json` file.
  368. */
  369. sourcePath: string;
  370. /**
  371. * @description
  372. * The port on which the dev server is listening. Overrides the value set by `AdminUiOptions.port`.
  373. */
  374. port: number;
  375. /**
  376. * @description
  377. * Specifies the url route to the Admin UI app.
  378. *
  379. * @default 'admin'
  380. */
  381. route?: string;
  382. /**
  383. * @description
  384. * The function which will be invoked to start the app compilation process.
  385. */
  386. compile: () => Promise<void>;
  387. }