types.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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, WorkerMessage } from '@vendure/core';
  12. export type ElasticSearchInput = SearchInput & {
  13. priceRange?: PriceRange;
  14. priceRangeWithTax?: PriceRange;
  15. };
  16. export type ElasticSearchResponse = SearchResponse & {
  17. priceRange: SearchPriceData;
  18. };
  19. export type SearchPriceData = {
  20. range: PriceRange;
  21. rangeWithTax: PriceRange;
  22. buckets: PriceRangeBucket[];
  23. bucketsWithTax: PriceRangeBucket[];
  24. };
  25. export type PriceRangeBucket = {
  26. to: number;
  27. count: number;
  28. };
  29. export type IndexItemAssets = {
  30. productAssetId: ID | undefined;
  31. productPreview: string;
  32. productPreviewFocalPoint: Coordinate | undefined;
  33. productVariantAssetId: ID | undefined;
  34. productVariantPreview: string;
  35. productVariantPreviewFocalPoint: Coordinate | undefined;
  36. };
  37. export type VariantIndexItem = Omit<
  38. SearchResult,
  39. 'score' | 'price' | 'priceWithTax' | 'productAsset' | 'productVariantAsset'
  40. > &
  41. IndexItemAssets & {
  42. channelId: ID;
  43. languageCode: LanguageCode;
  44. price: number;
  45. priceWithTax: number;
  46. collectionSlugs: string[];
  47. [customMapping: string]: any;
  48. };
  49. export type ProductIndexItem = IndexItemAssets & {
  50. sku: string;
  51. slug: string;
  52. productId: ID;
  53. channelId: ID;
  54. languageCode: LanguageCode;
  55. productName: string;
  56. productVariantId: ID;
  57. productVariantName: string;
  58. currencyCode: CurrencyCode;
  59. description: string;
  60. facetIds: ID[];
  61. facetValueIds: ID[];
  62. collectionIds: ID[];
  63. collectionSlugs: string[];
  64. channelIds: ID[];
  65. enabled: boolean;
  66. priceMin: number;
  67. priceMax: number;
  68. priceWithTaxMin: number;
  69. priceWithTaxMax: number;
  70. [customMapping: string]: any;
  71. };
  72. export type SearchHit<T> = {
  73. _id: string;
  74. _index: string;
  75. _score: number;
  76. _source: T;
  77. _type: string;
  78. };
  79. export type SearchRequestBody = {
  80. query?: any;
  81. sort?: any[];
  82. from?: number;
  83. size?: number;
  84. aggs?: any;
  85. };
  86. export type SearchResponseBody<T = any> = {
  87. hits: {
  88. hits: Array<SearchHit<T>>;
  89. total: {
  90. relation: string;
  91. value: number;
  92. };
  93. max_score: number;
  94. };
  95. timed_out: boolean;
  96. took: number;
  97. _shards: {
  98. failed: number;
  99. skipped: number;
  100. successful: number;
  101. total: number;
  102. };
  103. aggregations?: {
  104. [key: string]: {
  105. doc_count_error_upper_bound: 0;
  106. sum_other_doc_count: 89;
  107. buckets: Array<{ key: string; doc_count: number }>;
  108. value: any;
  109. };
  110. };
  111. };
  112. export type BulkOperationType = 'index' | 'update' | 'delete';
  113. export type BulkOperation = { [operation in BulkOperationType]?: { _id: string } };
  114. export type BulkOperationDoc<T> = T | { doc: T; doc_as_upsert?: boolean };
  115. export type BulkResponseResult = {
  116. [operation in BulkOperationType]?: {
  117. _index: string;
  118. _type: string;
  119. _id: string;
  120. _version?: number;
  121. result?: string;
  122. _shards: {
  123. total: number;
  124. successful: number;
  125. failed: number;
  126. };
  127. status: number;
  128. _seq_no?: number;
  129. _primary_term?: number;
  130. error?: any;
  131. };
  132. };
  133. export type BulkResponseBody = {
  134. took: number;
  135. errors: boolean;
  136. items: BulkResponseResult[];
  137. };
  138. export interface ReindexMessageResponse {
  139. total: number;
  140. completed: number;
  141. duration: number;
  142. }
  143. export type ReindexMessageData = {
  144. ctx: SerializedRequestContext;
  145. dropIndices: boolean;
  146. };
  147. export type UpdateProductMessageData = {
  148. ctx: SerializedRequestContext;
  149. productId: ID;
  150. };
  151. export type UpdateVariantMessageData = {
  152. ctx: SerializedRequestContext;
  153. variantIds: ID[];
  154. };
  155. export interface UpdateVariantsByIdMessageData {
  156. ctx: SerializedRequestContext;
  157. ids: ID[];
  158. }
  159. export interface ProductChannelMessageData {
  160. ctx: SerializedRequestContext;
  161. productId: ID;
  162. channelId: ID;
  163. }
  164. export interface UpdateAssetMessageData {
  165. ctx: SerializedRequestContext;
  166. asset: JsonCompatible<Required<Asset>>;
  167. }
  168. export class ReindexMessage extends WorkerMessage<ReindexMessageData, ReindexMessageResponse> {
  169. static readonly pattern = 'Reindex';
  170. }
  171. export class UpdateVariantMessage extends WorkerMessage<UpdateVariantMessageData, boolean> {
  172. static readonly pattern = 'UpdateProduct';
  173. }
  174. export class UpdateProductMessage extends WorkerMessage<UpdateProductMessageData, boolean> {
  175. static readonly pattern = 'UpdateVariant';
  176. }
  177. export class DeleteVariantMessage extends WorkerMessage<UpdateVariantMessageData, boolean> {
  178. static readonly pattern = 'DeleteProduct';
  179. }
  180. export class DeleteProductMessage extends WorkerMessage<UpdateProductMessageData, boolean> {
  181. static readonly pattern = 'DeleteVariant';
  182. }
  183. export class UpdateVariantsByIdMessage extends WorkerMessage<
  184. UpdateVariantsByIdMessageData,
  185. ReindexMessageResponse
  186. > {
  187. static readonly pattern = 'UpdateVariantsById';
  188. }
  189. export class AssignProductToChannelMessage extends WorkerMessage<ProductChannelMessageData, boolean> {
  190. static readonly pattern = 'AssignProductToChannel';
  191. }
  192. export class RemoveProductFromChannelMessage extends WorkerMessage<ProductChannelMessageData, boolean> {
  193. static readonly pattern = 'RemoveProductFromChannel';
  194. }
  195. export class UpdateAssetMessage extends WorkerMessage<UpdateAssetMessageData, boolean> {
  196. static readonly pattern = 'UpdateAsset';
  197. }
  198. export class DeleteAssetMessage extends WorkerMessage<UpdateAssetMessageData, boolean> {
  199. static readonly pattern = 'DeleteAsset';
  200. }
  201. type Maybe<T> = T | undefined;
  202. type CustomMappingDefinition<Args extends any[], T extends string, R> = {
  203. graphQlType: T;
  204. valueFn: (...args: Args) => R;
  205. };
  206. type NamedJobData<Type extends string, MessageData> = { type: Type } & MessageData;
  207. export type ReindexJobData = NamedJobData<'reindex', ReindexMessageData>;
  208. type UpdateProductJobData = NamedJobData<'update-product', UpdateProductMessageData>;
  209. type UpdateVariantsJobData = NamedJobData<'update-variants', UpdateVariantMessageData>;
  210. type DeleteProductJobData = NamedJobData<'delete-product', UpdateProductMessageData>;
  211. type DeleteVariantJobData = NamedJobData<'delete-variant', UpdateVariantMessageData>;
  212. type UpdateVariantsByIdJobData = NamedJobData<'update-variants-by-id', UpdateVariantsByIdMessageData>;
  213. type UpdateAssetJobData = NamedJobData<'update-asset', UpdateAssetMessageData>;
  214. type DeleteAssetJobData = NamedJobData<'delete-asset', UpdateAssetMessageData>;
  215. type AssignProductToChannelJobData = NamedJobData<'assign-product-to-channel', ProductChannelMessageData>;
  216. type RemoveProductFromChannelJobData = NamedJobData<'remove-product-from-channel', ProductChannelMessageData>;
  217. export type UpdateIndexQueueJobData =
  218. | ReindexJobData
  219. | UpdateProductJobData
  220. | UpdateVariantsJobData
  221. | DeleteProductJobData
  222. | DeleteVariantJobData
  223. | UpdateVariantsByIdJobData
  224. | UpdateAssetJobData
  225. | DeleteAssetJobData
  226. | AssignProductToChannelJobData
  227. | RemoveProductFromChannelJobData;
  228. type CustomStringMapping<Args extends any[]> = CustomMappingDefinition<Args, 'String!', string>;
  229. type CustomStringMappingNullable<Args extends any[]> = CustomMappingDefinition<Args, 'String', Maybe<string>>;
  230. type CustomIntMapping<Args extends any[]> = CustomMappingDefinition<Args, 'Int!', number>;
  231. type CustomIntMappingNullable<Args extends any[]> = CustomMappingDefinition<Args, 'Int', Maybe<number>>;
  232. type CustomFloatMapping<Args extends any[]> = CustomMappingDefinition<Args, 'Float!', number>;
  233. type CustomFloatMappingNullable<Args extends any[]> = CustomMappingDefinition<Args, 'Float', Maybe<number>>;
  234. type CustomBooleanMapping<Args extends any[]> = CustomMappingDefinition<Args, 'Boolean!', boolean>;
  235. type CustomBooleanMappingNullable<Args extends any[]> = CustomMappingDefinition<
  236. Args,
  237. 'Boolean',
  238. Maybe<boolean>
  239. >;
  240. export type CustomMapping<Args extends any[]> =
  241. | CustomStringMapping<Args>
  242. | CustomStringMappingNullable<Args>
  243. | CustomIntMapping<Args>
  244. | CustomIntMappingNullable<Args>
  245. | CustomFloatMapping<Args>
  246. | CustomFloatMappingNullable<Args>
  247. | CustomBooleanMapping<Args>
  248. | CustomBooleanMappingNullable<Args>;