types.ts 7.9 KB

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