types.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. [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. track_total_hits?: number|boolean,
  85. aggs?: any;
  86. };
  87. export type SearchResponseBody<T = any> = {
  88. hits: {
  89. hits: Array<SearchHit<T>>;
  90. total: {
  91. relation: string;
  92. value: number;
  93. };
  94. max_score: number;
  95. };
  96. timed_out: boolean;
  97. took: number;
  98. _shards: {
  99. failed: number;
  100. skipped: number;
  101. successful: number;
  102. total: number;
  103. };
  104. aggregations?: {
  105. [key: string]: {
  106. doc_count_error_upper_bound: 0;
  107. sum_other_doc_count: 89;
  108. buckets: Array<{ key: string; doc_count: number }>;
  109. value: any;
  110. };
  111. };
  112. };
  113. export type BulkOperationType = 'index' | 'update' | 'delete';
  114. export type BulkOperation = { [operation in BulkOperationType]?: { _id: string } };
  115. export type BulkOperationDoc<T> = T | { doc: T; doc_as_upsert?: boolean };
  116. export type BulkResponseResult = {
  117. [operation in BulkOperationType]?: {
  118. _index: string;
  119. _type: string;
  120. _id: string;
  121. _version?: number;
  122. result?: string;
  123. _shards: {
  124. total: number;
  125. successful: number;
  126. failed: number;
  127. };
  128. status: number;
  129. _seq_no?: number;
  130. _primary_term?: number;
  131. error?: any;
  132. };
  133. };
  134. export type BulkResponseBody = {
  135. took: number;
  136. errors: boolean;
  137. items: BulkResponseResult[];
  138. };
  139. export interface ReindexMessageResponse {
  140. total: number;
  141. completed: number;
  142. duration: number;
  143. }
  144. export type ReindexMessageData = {
  145. ctx: SerializedRequestContext;
  146. dropIndices: boolean;
  147. };
  148. export type UpdateProductMessageData = {
  149. ctx: SerializedRequestContext;
  150. productId: ID;
  151. };
  152. export type UpdateVariantMessageData = {
  153. ctx: SerializedRequestContext;
  154. variantIds: ID[];
  155. };
  156. export interface UpdateVariantsByIdMessageData {
  157. ctx: SerializedRequestContext;
  158. ids: ID[];
  159. }
  160. export interface ProductChannelMessageData {
  161. ctx: SerializedRequestContext;
  162. productId: ID;
  163. channelId: ID;
  164. }
  165. export type VariantChannelMessageData = {
  166. ctx: SerializedRequestContext;
  167. productVariantId: ID;
  168. channelId: ID;
  169. };
  170. export interface UpdateAssetMessageData {
  171. ctx: SerializedRequestContext;
  172. asset: JsonCompatible<Required<Asset>>;
  173. }
  174. type Maybe<T> = T | undefined;
  175. type CustomMappingDefinition<Args extends any[], T extends string, R> = {
  176. graphQlType: T;
  177. valueFn: (...args: Args) => R;
  178. };
  179. type NamedJobData<Type extends string, MessageData> = { type: Type } & MessageData;
  180. export type ReindexJobData = NamedJobData<'reindex', ReindexMessageData>;
  181. type UpdateProductJobData = NamedJobData<'update-product', UpdateProductMessageData>;
  182. type UpdateVariantsJobData = NamedJobData<'update-variants', UpdateVariantMessageData>;
  183. type DeleteProductJobData = NamedJobData<'delete-product', UpdateProductMessageData>;
  184. type DeleteVariantJobData = NamedJobData<'delete-variant', UpdateVariantMessageData>;
  185. type UpdateVariantsByIdJobData = NamedJobData<'update-variants-by-id', UpdateVariantsByIdMessageData>;
  186. type UpdateAssetJobData = NamedJobData<'update-asset', UpdateAssetMessageData>;
  187. type DeleteAssetJobData = NamedJobData<'delete-asset', UpdateAssetMessageData>;
  188. type AssignProductToChannelJobData = NamedJobData<'assign-product-to-channel', ProductChannelMessageData>;
  189. type RemoveProductFromChannelJobData = NamedJobData<'remove-product-from-channel', ProductChannelMessageData>;
  190. type AssignVariantToChannelJobData = NamedJobData<'assign-variant-to-channel', VariantChannelMessageData>;
  191. type RemoveVariantFromChannelJobData = NamedJobData<'remove-variant-from-channel', VariantChannelMessageData>;
  192. export type UpdateIndexQueueJobData =
  193. | ReindexJobData
  194. | UpdateProductJobData
  195. | UpdateVariantsJobData
  196. | DeleteProductJobData
  197. | DeleteVariantJobData
  198. | UpdateVariantsByIdJobData
  199. | UpdateAssetJobData
  200. | DeleteAssetJobData
  201. | AssignProductToChannelJobData
  202. | RemoveProductFromChannelJobData
  203. | AssignVariantToChannelJobData
  204. | RemoveVariantFromChannelJobData;
  205. type CustomStringMapping<Args extends any[]> = CustomMappingDefinition<Args, 'String!', string>;
  206. type CustomStringMappingNullable<Args extends any[]> = CustomMappingDefinition<Args, 'String', Maybe<string>>;
  207. type CustomIntMapping<Args extends any[]> = CustomMappingDefinition<Args, 'Int!', number>;
  208. type CustomIntMappingNullable<Args extends any[]> = CustomMappingDefinition<Args, 'Int', Maybe<number>>;
  209. type CustomFloatMapping<Args extends any[]> = CustomMappingDefinition<Args, 'Float!', number>;
  210. type CustomFloatMappingNullable<Args extends any[]> = CustomMappingDefinition<Args, 'Float', Maybe<number>>;
  211. type CustomBooleanMapping<Args extends any[]> = CustomMappingDefinition<Args, 'Boolean!', boolean>;
  212. type CustomBooleanMappingNullable<Args extends any[]> = CustomMappingDefinition<
  213. Args,
  214. 'Boolean',
  215. Maybe<boolean>
  216. >;
  217. export type CustomMapping<Args extends any[]> =
  218. | CustomStringMapping<Args>
  219. | CustomStringMappingNullable<Args>
  220. | CustomIntMapping<Args>
  221. | CustomIntMappingNullable<Args>
  222. | CustomFloatMapping<Args>
  223. | CustomFloatMappingNullable<Args>
  224. | CustomBooleanMapping<Args>
  225. | CustomBooleanMappingNullable<Args>;