types.ts 8.2 KB

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