shared-types.ts 12 KB

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