shared-types.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. | 'html-editor-form-input'
  130. | 'number-form-input'
  131. | 'password-form-input'
  132. | 'product-selector-form-input'
  133. | 'relation-form-input'
  134. | 'rich-text-form-input'
  135. | 'select-form-input'
  136. | 'text-form-input'
  137. | 'textarea-form-input'
  138. | 'asset-form-input'
  139. | 'product-multi-form-input'
  140. | 'combination-mode-form-input';
  141. /**
  142. * @description
  143. * Used to define the expected arguments for a given default form input component.
  144. *
  145. * @docsCategory ConfigurableOperationDef
  146. */
  147. type DefaultFormConfigHash = {
  148. 'boolean-form-input': {};
  149. 'currency-form-input': {};
  150. 'customer-group-form-input': {};
  151. 'date-form-input': { min?: string; max?: string; yearRange?: number };
  152. 'facet-value-form-input': {};
  153. 'json-editor-form-input': { height?: string };
  154. 'html-editor-form-input': { height?: string };
  155. 'number-form-input': { min?: number; max?: number; step?: number; prefix?: string; suffix?: string };
  156. 'password-form-input': {};
  157. 'product-selector-form-input': {};
  158. 'relation-form-input': {};
  159. 'rich-text-form-input': {};
  160. 'select-form-input': {
  161. options?: Array<{ value: string; label?: Array<Omit<LocalizedString, '__typename'>> }>;
  162. };
  163. 'text-form-input': { prefix?: string; suffix?: string };
  164. 'textarea-form-input': {
  165. spellcheck?: boolean;
  166. };
  167. 'asset-form-input': {};
  168. 'product-multi-form-input': {
  169. selectionMode?: 'product' | 'variant';
  170. };
  171. 'combination-mode-form-input': {};
  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 AdminUiPlugin
  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
  237. * option.
  238. *
  239. * @default 'vendure-auth-token'
  240. */
  241. authTokenHeaderKey: string;
  242. /**
  243. * @description
  244. * The default language for the Admin UI. Must be one of the
  245. * items specified in the `availableLanguages` property.
  246. *
  247. * @default LanguageCode.en
  248. */
  249. defaultLanguage: LanguageCode;
  250. /**
  251. * @description
  252. * The default locale for the Admin UI. The locale affects the formatting of
  253. * currencies & dates.
  254. *
  255. * If not set, the browser default locale will be used.
  256. */
  257. defaultLocale?: string;
  258. /**
  259. * @description
  260. * An array of languages for which translations exist for the Admin UI.
  261. */
  262. availableLanguages: LanguageCode[];
  263. /**
  264. * @description
  265. * If you are using an external {@link AuthenticationStrategy} for the Admin API, you can configure
  266. * a custom URL for the login page with this option. On logging out or redirecting an unauthenticated
  267. * user, the Admin UI app will redirect the user to this URL rather than the default username/password
  268. * screen.
  269. */
  270. loginUrl?: string;
  271. /**
  272. * @description
  273. * The custom brand name.
  274. */
  275. brand?: string;
  276. /**
  277. * @description
  278. * Option to hide vendure branding.
  279. *
  280. * @default false
  281. */
  282. hideVendureBranding?: boolean;
  283. /**
  284. * @description
  285. * Option to hide version.
  286. *
  287. * @default false
  288. */
  289. hideVersion?: boolean;
  290. /**
  291. * @description
  292. * The custom login image
  293. *
  294. * @since 1.9.0
  295. */
  296. loginImage?: string;
  297. /**
  298. * @description
  299. * Allows you to provide default reasons for a refund or cancellation. This will be used in the
  300. * refund/cancel dialog. The values can be literal strings (e.g. "Not in stock") or translation
  301. * tokens (see [Adding Admin UI Translations](/docs/plugins/extending-the-admin-ui/adding-ui-translations/)).
  302. *
  303. * @since 1.5.0
  304. * @default ['order.cancel-reason-customer-request', 'order.cancel-reason-not-available']
  305. */
  306. cancellationReasons?: string[];
  307. }
  308. /**
  309. * @description
  310. * Configures the path to a custom-build of the Admin UI app.
  311. *
  312. * @docsCategory common
  313. */
  314. export interface AdminUiAppConfig {
  315. /**
  316. * @description
  317. * The path to the compiled admin ui app files. If not specified, an internal
  318. * default build is used. This path should contain the `vendure-ui-config.json` file,
  319. * index.html, the compiled js bundles etc.
  320. */
  321. path: string;
  322. /**
  323. * @description
  324. * Specifies the url route to the Admin UI app.
  325. *
  326. * @default 'admin'
  327. */
  328. route?: string;
  329. /**
  330. * @description
  331. * The function which will be invoked to start the app compilation process.
  332. */
  333. compile?: () => Promise<void>;
  334. }
  335. /**
  336. * @description
  337. * Information about the Admin UI app dev server.
  338. *
  339. * @docsCategory common
  340. */
  341. export interface AdminUiAppDevModeConfig {
  342. /**
  343. * @description
  344. * The path to the uncompiled ui app source files. This path should contain the `vendure-ui-config.json` file.
  345. */
  346. sourcePath: string;
  347. /**
  348. * @description
  349. * The port on which the dev server is listening. Overrides the value set by `AdminUiOptions.port`.
  350. */
  351. port: number;
  352. /**
  353. * @description
  354. * Specifies the url route to the Admin UI app.
  355. *
  356. * @default 'admin'
  357. */
  358. route?: string;
  359. /**
  360. * @description
  361. * The function which will be invoked to start the app compilation process.
  362. */
  363. compile: () => Promise<void>;
  364. }