types.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import {
  2. Coordinate,
  3. CurrencyCode,
  4. LanguageCode,
  5. PriceRange,
  6. SearchInput,
  7. SearchResponse,
  8. SearchResult,
  9. } from '@vendure/common/lib/generated-types';
  10. import { ID, JsonCompatible } from '@vendure/common/lib/shared-types';
  11. import { Asset, SerializedRequestContext } from '@vendure/core';
  12. export type ElasticSearchResult = SearchResult & {
  13. inStock: boolean;
  14. };
  15. export type ElasticSearchInput = SearchInput & {
  16. priceRange?: PriceRange;
  17. priceRangeWithTax?: PriceRange;
  18. inStock?: boolean;
  19. [extendedInputField: string]: any;
  20. };
  21. export type ElasticSearchResponse = SearchResponse & {
  22. priceRange: SearchPriceData;
  23. items: ElasticSearchResult[];
  24. };
  25. export type SearchPriceData = {
  26. range: PriceRange;
  27. rangeWithTax: PriceRange;
  28. buckets: PriceRangeBucket[];
  29. bucketsWithTax: PriceRangeBucket[];
  30. };
  31. export type PriceRangeBucket = {
  32. to: number;
  33. count: number;
  34. };
  35. export enum ElasticSearchSortMode {
  36. /** Pick the lowest value */
  37. MIN = 'min',
  38. /** Pick the highest value */
  39. MAX = 'max',
  40. /** Use the sum of all values as sort value. Only applicable for number based array fields */
  41. SUM = 'sum',
  42. /** Use the average of all values as sort value. Only applicable for number based array fields */
  43. AVG = 'avg',
  44. /** Use the median of all values as sort value. Only applicable for number based array fields */
  45. MEDIAN = 'median',
  46. }
  47. export type ElasticSearchSortParameter = {
  48. missing?: '_last' | '_first' | string;
  49. mode?: ElasticSearchSortMode;
  50. order: 'asc' | 'desc';
  51. } & { [key: string]: any };
  52. export type ElasticSearchSortInput = Array<{ [key: string]: ElasticSearchSortParameter }>;
  53. export type IndexItemAssets = {
  54. productAssetId: ID | undefined;
  55. productPreview: string;
  56. productPreviewFocalPoint: Coordinate | undefined;
  57. productVariantAssetId: ID | undefined;
  58. productVariantPreview: string;
  59. productVariantPreviewFocalPoint: Coordinate | undefined;
  60. };
  61. export type VariantIndexItem = Omit<
  62. SearchResult,
  63. 'score' | 'price' | 'priceWithTax' | 'productAsset' | 'productVariantAsset'
  64. > &
  65. IndexItemAssets & {
  66. channelId: ID;
  67. languageCode: LanguageCode;
  68. price: number;
  69. priceWithTax: number;
  70. collectionSlugs: string[];
  71. productEnabled: boolean;
  72. productPriceMin: number;
  73. productPriceMax: number;
  74. productPriceWithTaxMin: number;
  75. productPriceWithTaxMax: number;
  76. productFacetIds: ID[];
  77. productFacetValueIds: ID[];
  78. productCollectionIds: ID[];
  79. productCollectionSlugs: string[];
  80. productChannelIds: ID[];
  81. [customMapping: string]: any;
  82. inStock: boolean;
  83. productInStock: boolean;
  84. };
  85. export type ProductIndexItem = IndexItemAssets & {
  86. sku: string;
  87. slug: string;
  88. productId: ID;
  89. channelId: ID;
  90. languageCode: LanguageCode;
  91. productName: string;
  92. productVariantId: ID;
  93. productVariantName: string;
  94. currencyCode: CurrencyCode;
  95. description: string;
  96. facetIds: ID[];
  97. facetValueIds: ID[];
  98. collectionIds: ID[];
  99. collectionSlugs: string[];
  100. channelIds: ID[];
  101. enabled: boolean;
  102. productEnabled: boolean;
  103. priceMin: number;
  104. priceMax: number;
  105. priceWithTaxMin: number;
  106. priceWithTaxMax: number;
  107. [customMapping: string]: any;
  108. };
  109. export type SearchHit<T> = {
  110. _id: string;
  111. _index: string;
  112. _score: number;
  113. _source: T;
  114. _type: string;
  115. fields?: any;
  116. };
  117. export type SearchRequestBody = {
  118. query?: any;
  119. sort?: any[];
  120. from?: number;
  121. size?: number;
  122. track_total_hits?: number | boolean;
  123. aggs?: any;
  124. collapse?: any;
  125. _source?: boolean;
  126. script_fields?: any;
  127. };
  128. export type SearchResponseBody<T = any> = {
  129. hits: {
  130. hits: Array<SearchHit<T>>;
  131. total: {
  132. relation: string;
  133. value: number;
  134. };
  135. max_score: number;
  136. };
  137. timed_out: boolean;
  138. took: number;
  139. _shards: {
  140. failed: number;
  141. skipped: number;
  142. successful: number;
  143. total: number;
  144. };
  145. aggregations?: {
  146. [key: string]: {
  147. doc_count_error_upper_bound: 0;
  148. sum_other_doc_count: 89;
  149. buckets: Array<{ key: string; doc_count: number; total: { value: number } }>;
  150. value: any;
  151. };
  152. };
  153. };
  154. export type BulkOperationType = 'index' | 'update' | 'delete';
  155. export type BulkOperation = { [operation in BulkOperationType]?: { _id: string } };
  156. export type BulkOperationDoc<T> = T | { doc: T; doc_as_upsert?: boolean };
  157. export type BulkResponseResult = {
  158. [operation in BulkOperationType]?: {
  159. _index: string;
  160. _type: string;
  161. _id: string;
  162. _version?: number;
  163. result?: string;
  164. _shards: {
  165. total: number;
  166. successful: number;
  167. failed: number;
  168. };
  169. status: number;
  170. _seq_no?: number;
  171. _primary_term?: number;
  172. error?: any;
  173. };
  174. };
  175. export type BulkResponseBody = {
  176. took: number;
  177. errors: boolean;
  178. items: BulkResponseResult[];
  179. };
  180. export interface ReindexMessageResponse {
  181. total: number;
  182. completed: number;
  183. duration: number;
  184. }
  185. export type ReindexMessageData = {
  186. ctx: SerializedRequestContext;
  187. };
  188. export type UpdateProductMessageData = {
  189. ctx: SerializedRequestContext;
  190. productId: ID;
  191. };
  192. export type UpdateVariantMessageData = {
  193. ctx: SerializedRequestContext;
  194. variantIds: ID[];
  195. };
  196. export interface UpdateVariantsByIdMessageData {
  197. ctx: SerializedRequestContext;
  198. ids: ID[];
  199. }
  200. export interface ProductChannelMessageData {
  201. ctx: SerializedRequestContext;
  202. productId: ID;
  203. channelId: ID;
  204. }
  205. export type VariantChannelMessageData = {
  206. ctx: SerializedRequestContext;
  207. productVariantId: ID;
  208. channelId: ID;
  209. };
  210. export interface UpdateAssetMessageData {
  211. ctx: SerializedRequestContext;
  212. asset: JsonCompatible<Required<Asset>>;
  213. }
  214. type Maybe<T> = T | undefined;
  215. type NamedJobData<Type extends string, MessageData> = { type: Type } & MessageData;
  216. export type ReindexJobData = NamedJobData<'reindex', ReindexMessageData>;
  217. type UpdateProductJobData = NamedJobData<'update-product', UpdateProductMessageData>;
  218. type UpdateVariantsJobData = NamedJobData<'update-variants', UpdateVariantMessageData>;
  219. type DeleteProductJobData = NamedJobData<'delete-product', UpdateProductMessageData>;
  220. type DeleteVariantJobData = NamedJobData<'delete-variant', UpdateVariantMessageData>;
  221. type UpdateVariantsByIdJobData = NamedJobData<'update-variants-by-id', UpdateVariantsByIdMessageData>;
  222. type UpdateAssetJobData = NamedJobData<'update-asset', UpdateAssetMessageData>;
  223. type DeleteAssetJobData = NamedJobData<'delete-asset', UpdateAssetMessageData>;
  224. type AssignProductToChannelJobData = NamedJobData<'assign-product-to-channel', ProductChannelMessageData>;
  225. type RemoveProductFromChannelJobData = NamedJobData<'remove-product-from-channel', ProductChannelMessageData>;
  226. type AssignVariantToChannelJobData = NamedJobData<'assign-variant-to-channel', VariantChannelMessageData>;
  227. type RemoveVariantFromChannelJobData = NamedJobData<'remove-variant-from-channel', VariantChannelMessageData>;
  228. export type UpdateIndexQueueJobData =
  229. | ReindexJobData
  230. | UpdateProductJobData
  231. | UpdateVariantsJobData
  232. | DeleteProductJobData
  233. | DeleteVariantJobData
  234. | UpdateVariantsByIdJobData
  235. | UpdateAssetJobData
  236. | DeleteAssetJobData
  237. | AssignProductToChannelJobData
  238. | RemoveProductFromChannelJobData
  239. | AssignVariantToChannelJobData
  240. | RemoveVariantFromChannelJobData;
  241. export type GraphQlPrimitive = 'ID' | 'String' | 'Int' | 'Float' | 'Boolean';
  242. export type PrimitiveTypeVariations<T extends GraphQlPrimitive> = T | `${T}!` | `[${T}!]` | `[${T}!]!`;
  243. type GraphQlPermittedReturnType = PrimitiveTypeVariations<GraphQlPrimitive>;
  244. type CustomMappingDefinition<Args extends any[], T extends GraphQlPermittedReturnType, R> = {
  245. graphQlType: T;
  246. public?: boolean;
  247. valueFn: (...args: Args) => Promise<R> | R;
  248. };
  249. type TypeVariationMap<GqlType extends GraphQlPrimitive, TsType> = {
  250. [Key in PrimitiveTypeVariations<GqlType>]: Key extends `[${string}!]!`
  251. ? TsType[]
  252. : Key extends `[${string}!]`
  253. ? Maybe<TsType[]>
  254. : Key extends `${string}!`
  255. ? TsType
  256. : Maybe<TsType>;
  257. };
  258. type GraphQlTypeMap = TypeVariationMap<'ID', ID> &
  259. TypeVariationMap<'String', string> &
  260. TypeVariationMap<'Int', number> &
  261. TypeVariationMap<'Float', number> &
  262. TypeVariationMap<'Boolean', boolean>;
  263. export type CustomMapping<Args extends any[]> = {
  264. [Type in GraphQlPermittedReturnType]: CustomMappingDefinition<Args, Type, GraphQlTypeMap[Type]>;
  265. }[GraphQlPermittedReturnType];
  266. export type CustomScriptContext = 'product' | 'variant' | 'both';
  267. type CustomScriptMappingDefinition<Args extends any[], T extends GraphQlPermittedReturnType> = {
  268. graphQlType: T;
  269. context: CustomScriptContext;
  270. scriptFn: (...args: Args) => { script: string };
  271. };
  272. export type CustomScriptMapping<Args extends any[]> = {
  273. [Type in GraphQlPermittedReturnType]: CustomScriptMappingDefinition<Args, Type>;
  274. }[GraphQlPermittedReturnType];