shared-types.ts 11 KB

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