shared-types.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. * localText | 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](/docs/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. | 'asset-form-input'
  141. | 'product-multi-form-input'
  142. | 'combination-mode-form-input';
  143. /**
  144. * @description
  145. * Used to define the expected arguments for a given default form input component.
  146. *
  147. * @docsCategory ConfigurableOperationDef
  148. */
  149. type DefaultFormConfigHash = {
  150. 'boolean-form-input': Record<string, never>;
  151. 'currency-form-input': Record<string, never>;
  152. 'customer-group-form-input': Record<string, never>;
  153. 'date-form-input': { min?: string; max?: string; yearRange?: number };
  154. 'facet-value-form-input': Record<string, never>;
  155. 'json-editor-form-input': { height?: string };
  156. 'html-editor-form-input': { height?: string };
  157. 'number-form-input': { min?: number; max?: number; step?: number; prefix?: string; suffix?: string };
  158. 'password-form-input': Record<string, never>;
  159. 'product-selector-form-input': Record<string, never>;
  160. 'relation-form-input': Record<string, never>;
  161. 'rich-text-form-input': Record<string, never>;
  162. 'select-form-input': {
  163. options?: Array<{ value: string; label?: Array<Omit<LocalizedString, '__typename'>> }>;
  164. };
  165. 'text-form-input': { prefix?: string; suffix?: string };
  166. 'textarea-form-input': {
  167. spellcheck?: boolean;
  168. };
  169. 'asset-form-input': Record<string, never>;
  170. 'product-multi-form-input': {
  171. selectionMode?: 'product' | 'variant';
  172. };
  173. 'combination-mode-form-input': Record<string, never>;
  174. };
  175. export type DefaultFormComponentUiConfig<T extends DefaultFormComponentId | string> =
  176. T extends DefaultFormComponentId ? DefaultFormConfigHash[T] : any;
  177. export type DefaultFormComponentConfig<T extends DefaultFormComponentId | string> =
  178. DefaultFormComponentUiConfig<T> & {
  179. ui?: DefaultFormComponentUiConfig<T>;
  180. };
  181. export type UiComponentConfig<T extends DefaultFormComponentId | string> = T extends DefaultFormComponentId
  182. ? {
  183. component: T;
  184. tab?: string;
  185. } & DefaultFormConfigHash[T]
  186. : {
  187. component: string;
  188. tab?: string;
  189. [prop: string]: any;
  190. };
  191. export type CustomFieldsObject = { [key: string]: any };
  192. /**
  193. * @description
  194. * This interface describes JSON config file (vendure-ui-config.json) used by the Admin UI.
  195. * The values are loaded at run-time by the Admin UI app, and allow core configuration to be
  196. * managed without the need to re-build the application.
  197. *
  198. * @docsCategory AdminUiPlugin
  199. */
  200. export interface AdminUiConfig {
  201. /**
  202. * @description
  203. * The hostname of the Vendure server which the admin UI will be making API calls
  204. * to. If set to "auto", the Admin UI app will determine the hostname from the
  205. * current location (i.e. `window.location.hostname`).
  206. *
  207. * @default 'http://localhost'
  208. */
  209. apiHost: string | 'auto';
  210. /**
  211. * @description
  212. * The port of the Vendure server which the admin UI will be making API calls
  213. * to. If set to "auto", the Admin UI app will determine the port from the
  214. * current location (i.e. `window.location.port`).
  215. *
  216. * @default 3000
  217. */
  218. apiPort: number | 'auto';
  219. /**
  220. * @description
  221. * The path to the GraphQL Admin API.
  222. *
  223. * @default 'admin-api'
  224. */
  225. adminApiPath: string;
  226. /**
  227. * @description
  228. * Whether to use cookies or bearer tokens to track sessions.
  229. * Should match the setting of in the server's `tokenMethod` config
  230. * option.
  231. *
  232. * @default 'cookie'
  233. */
  234. tokenMethod: 'cookie' | 'bearer';
  235. /**
  236. * @description
  237. * The header used when using the 'bearer' auth method. Should match the
  238. * setting of the server's `authOptions.authTokenHeaderKey` config
  239. * option.
  240. *
  241. * @default 'vendure-auth-token'
  242. */
  243. authTokenHeaderKey: string;
  244. /**
  245. * @description
  246. * The default language for the Admin UI. Must be one of the
  247. * items specified in the `availableLanguages` property.
  248. *
  249. * @default LanguageCode.en
  250. */
  251. defaultLanguage: LanguageCode;
  252. /**
  253. * @description
  254. * The default locale for the Admin UI. The locale affects the formatting of
  255. * currencies & dates.
  256. *
  257. * If not set, the browser default locale will be used.
  258. */
  259. defaultLocale?: string;
  260. /**
  261. * @description
  262. * An array of languages for which translations exist for the Admin UI.
  263. */
  264. availableLanguages: LanguageCode[];
  265. /**
  266. * @description
  267. * If you are using an external {@link AuthenticationStrategy} for the Admin API, you can configure
  268. * a custom URL for the login page with this option. On logging out or redirecting an unauthenticated
  269. * user, the Admin UI app will redirect the user to this URL rather than the default username/password
  270. * screen.
  271. */
  272. loginUrl?: string;
  273. /**
  274. * @description
  275. * The custom brand name.
  276. */
  277. brand?: string;
  278. /**
  279. * @description
  280. * Option to hide vendure branding.
  281. *
  282. * @default false
  283. */
  284. hideVendureBranding?: boolean;
  285. /**
  286. * @description
  287. * Option to hide version.
  288. *
  289. * @default false
  290. */
  291. hideVersion?: boolean;
  292. /**
  293. * @description
  294. * A url of a custom image to be used on the login screen, to override the images provided by Vendure's login image server.
  295. *
  296. * @since 1.9.0
  297. */
  298. loginImageUrl?: string;
  299. /**
  300. * @description
  301. * Allows you to provide default reasons for a refund or cancellation. This will be used in the
  302. * refund/cancel dialog. The values can be literal strings (e.g. "Not in stock") or translation
  303. * tokens (see [Adding Admin UI Translations](/docs/plugins/extending-the-admin-ui/adding-ui-translations/)).
  304. *
  305. * @since 1.5.0
  306. * @default ['order.cancel-reason-customer-request', 'order.cancel-reason-not-available']
  307. */
  308. cancellationReasons?: string[];
  309. }
  310. /**
  311. * @description
  312. * Configures the path to a custom-build of the Admin UI app.
  313. *
  314. * @docsCategory common
  315. */
  316. export interface AdminUiAppConfig {
  317. /**
  318. * @description
  319. * The path to the compiled admin UI app files. If not specified, an internal
  320. * default build is used. This path should contain the `vendure-ui-config.json` file,
  321. * index.html, the compiled js bundles etc.
  322. */
  323. path: string;
  324. /**
  325. * @description
  326. * Specifies the url route to the Admin UI app.
  327. *
  328. * @default 'admin'
  329. */
  330. route?: string;
  331. /**
  332. * @description
  333. * The function which will be invoked to start the app compilation process.
  334. */
  335. compile?: () => Promise<void>;
  336. }
  337. /**
  338. * @description
  339. * Information about the Admin UI app dev server.
  340. *
  341. * @docsCategory common
  342. */
  343. export interface AdminUiAppDevModeConfig {
  344. /**
  345. * @description
  346. * The path to the uncompiled UI app source files. This path should contain the `vendure-ui-config.json` file.
  347. */
  348. sourcePath: string;
  349. /**
  350. * @description
  351. * The port on which the dev server is listening. Overrides the value set by `AdminUiOptions.port`.
  352. */
  353. port: number;
  354. /**
  355. * @description
  356. * Specifies the url route to the Admin UI app.
  357. *
  358. * @default 'admin'
  359. */
  360. route?: string;
  361. /**
  362. * @description
  363. * The function which will be invoked to start the app compilation process.
  364. */
  365. compile: () => Promise<void>;
  366. }