types.ts 7.7 KB

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