types.ts 8.2 KB

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