@labdigital/commercetools-mock 2.17.1 → 2.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. package/dist/index.cjs +4351 -4099
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +266 -413
  4. package/dist/index.d.ts +266 -413
  5. package/dist/index.js +4351 -4099
  6. package/dist/index.js.map +1 -1
  7. package/package.json +47 -47
  8. package/src/constants.ts +2 -2
  9. package/src/ctMock.test.ts +11 -11
  10. package/src/ctMock.ts +141 -127
  11. package/src/deprecation.ts +8 -0
  12. package/src/exceptions.ts +17 -15
  13. package/src/helpers.ts +32 -32
  14. package/src/index.test.ts +128 -128
  15. package/src/index.ts +3 -3
  16. package/src/lib/expandParser.ts +13 -13
  17. package/src/lib/haversine.test.ts +9 -9
  18. package/src/lib/haversine.ts +11 -11
  19. package/src/lib/masking.ts +11 -11
  20. package/src/lib/parser.ts +2 -2
  21. package/src/lib/password.ts +23 -3
  22. package/src/lib/predicateParser.test.ts +185 -183
  23. package/src/lib/predicateParser.ts +234 -234
  24. package/src/lib/projectionSearchFilter.test.ts +103 -101
  25. package/src/lib/projectionSearchFilter.ts +152 -150
  26. package/src/lib/proxy.ts +5 -5
  27. package/src/oauth/errors.ts +4 -4
  28. package/src/oauth/helpers.ts +6 -6
  29. package/src/oauth/server.test.ts +86 -86
  30. package/src/oauth/server.ts +158 -144
  31. package/src/oauth/store.ts +44 -43
  32. package/src/priceSelector.test.ts +35 -35
  33. package/src/priceSelector.ts +30 -30
  34. package/src/product-projection-search.ts +136 -134
  35. package/src/projectAPI.test.ts +7 -7
  36. package/src/projectAPI.ts +24 -22
  37. package/src/repositories/abstract.ts +168 -116
  38. package/src/repositories/associate-role.ts +90 -77
  39. package/src/repositories/attribute-group.ts +51 -40
  40. package/src/repositories/business-unit.ts +168 -148
  41. package/src/repositories/cart/actions.ts +489 -0
  42. package/src/repositories/cart/helpers.ts +30 -0
  43. package/src/repositories/cart/index.ts +180 -0
  44. package/src/repositories/cart-discount/actions.ts +148 -0
  45. package/src/repositories/cart-discount/index.ts +86 -0
  46. package/src/repositories/category/actions.ts +231 -0
  47. package/src/repositories/category/index.ts +52 -0
  48. package/src/repositories/channel.ts +88 -90
  49. package/src/repositories/custom-object.ts +46 -45
  50. package/src/repositories/customer/actions.ts +165 -0
  51. package/src/repositories/customer/index.ts +79 -0
  52. package/src/repositories/customer-group.ts +66 -55
  53. package/src/repositories/discount-code/actions.ts +149 -0
  54. package/src/repositories/discount-code/index.ts +50 -0
  55. package/src/repositories/errors.ts +10 -10
  56. package/src/repositories/extension.ts +64 -62
  57. package/src/repositories/helpers.ts +117 -118
  58. package/src/repositories/index.ts +80 -79
  59. package/src/repositories/inventory-entry/actions.ts +84 -0
  60. package/src/repositories/inventory-entry/index.ts +44 -0
  61. package/src/repositories/my-customer.ts +114 -0
  62. package/src/repositories/my-order.ts +8 -8
  63. package/src/repositories/order/actions.ts +281 -0
  64. package/src/repositories/{order.test.ts → order/index.test.ts} +77 -77
  65. package/src/repositories/order/index.ts +260 -0
  66. package/src/repositories/order-edit.ts +10 -23
  67. package/src/repositories/payment/actions.ts +305 -0
  68. package/src/repositories/payment/helpers.ts +17 -0
  69. package/src/repositories/payment/index.ts +56 -0
  70. package/src/repositories/product/actions.ts +943 -0
  71. package/src/repositories/product/helpers.ts +98 -0
  72. package/src/repositories/product/index.ts +130 -0
  73. package/src/repositories/product-discount.ts +127 -117
  74. package/src/repositories/product-projection.ts +56 -62
  75. package/src/repositories/product-selection.ts +31 -28
  76. package/src/repositories/product-type.ts +136 -134
  77. package/src/repositories/project.ts +133 -118
  78. package/src/repositories/quote-request.ts +7 -19
  79. package/src/repositories/quote.ts +7 -22
  80. package/src/repositories/review.ts +13 -26
  81. package/src/repositories/shipping-method/actions.ts +198 -0
  82. package/src/repositories/shipping-method/helpers.ts +10 -0
  83. package/src/repositories/shipping-method/index.ts +138 -0
  84. package/src/repositories/shopping-list/actions.ts +295 -0
  85. package/src/repositories/shopping-list/index.ts +122 -0
  86. package/src/repositories/staged-quote.ts +7 -20
  87. package/src/repositories/standalone-price.ts +57 -44
  88. package/src/repositories/state.ts +113 -68
  89. package/src/repositories/store.ts +106 -94
  90. package/src/repositories/subscription.ts +46 -22
  91. package/src/repositories/tax-category/actions.ts +94 -0
  92. package/src/repositories/tax-category/helpers.ts +8 -0
  93. package/src/repositories/tax-category/index.ts +25 -0
  94. package/src/repositories/type/actions.ts +162 -0
  95. package/src/repositories/type/index.ts +24 -0
  96. package/src/repositories/zone.ts +62 -58
  97. package/src/schemas/update-request.ts +12 -0
  98. package/src/server.ts +9 -9
  99. package/src/services/abstract.ts +85 -72
  100. package/src/services/associate-roles.test.ts +27 -27
  101. package/src/services/associate-roles.ts +7 -7
  102. package/src/services/attribute-group.ts +7 -7
  103. package/src/services/business-units.test.ts +28 -28
  104. package/src/services/business-units.ts +7 -7
  105. package/src/services/cart-discount.test.ts +199 -199
  106. package/src/services/cart-discount.ts +7 -7
  107. package/src/services/cart.test.ts +261 -261
  108. package/src/services/cart.ts +22 -21
  109. package/src/services/category.test.ts +121 -121
  110. package/src/services/category.ts +7 -7
  111. package/src/services/channel.ts +7 -7
  112. package/src/services/custom-object.test.ts +130 -130
  113. package/src/services/custom-object.ts +34 -31
  114. package/src/services/customer-group.ts +7 -7
  115. package/src/services/customer.test.ts +205 -205
  116. package/src/services/customer.ts +23 -36
  117. package/src/services/discount-code.ts +7 -7
  118. package/src/services/extension.ts +7 -7
  119. package/src/services/index.ts +85 -81
  120. package/src/services/inventory-entry.test.ts +106 -106
  121. package/src/services/inventory-entry.ts +7 -7
  122. package/src/services/my-cart.test.ts +56 -56
  123. package/src/services/my-cart.ts +20 -20
  124. package/src/services/my-customer.test.ts +155 -104
  125. package/src/services/my-customer.ts +66 -75
  126. package/src/services/my-order.ts +16 -16
  127. package/src/services/my-payment.test.ts +40 -40
  128. package/src/services/my-payment.ts +7 -7
  129. package/src/services/my-shopping-list.ts +7 -7
  130. package/src/services/order.test.ts +243 -243
  131. package/src/services/order.ts +23 -18
  132. package/src/services/payment.test.ts +40 -40
  133. package/src/services/payment.ts +7 -7
  134. package/src/services/product-discount.ts +7 -7
  135. package/src/services/product-projection.test.ts +190 -190
  136. package/src/services/product-projection.ts +34 -32
  137. package/src/services/product-selection.test.ts +19 -19
  138. package/src/services/product-selection.ts +7 -7
  139. package/src/services/product-type.test.ts +38 -38
  140. package/src/services/product-type.ts +7 -7
  141. package/src/services/product.test.ts +658 -656
  142. package/src/services/product.ts +7 -7
  143. package/src/services/project.test.ts +29 -24
  144. package/src/services/project.ts +22 -17
  145. package/src/services/reviews.ts +7 -7
  146. package/src/services/shipping-method.test.ts +78 -78
  147. package/src/services/shipping-method.ts +16 -16
  148. package/src/services/shopping-list.test.ts +170 -170
  149. package/src/services/shopping-list.ts +7 -7
  150. package/src/services/standalone-price.test.ts +112 -112
  151. package/src/services/standalone-price.ts +7 -7
  152. package/src/services/state.test.ts +30 -30
  153. package/src/services/state.ts +7 -7
  154. package/src/services/store.test.ts +40 -40
  155. package/src/services/store.ts +7 -7
  156. package/src/services/subscription.ts +7 -7
  157. package/src/services/tax-category.test.ts +43 -43
  158. package/src/services/tax-category.ts +7 -7
  159. package/src/services/type.ts +7 -7
  160. package/src/services/zone.ts +7 -7
  161. package/src/shippingCalculator.test.ts +43 -43
  162. package/src/shippingCalculator.ts +23 -23
  163. package/src/storage/abstract.ts +36 -34
  164. package/src/storage/in-memory.ts +237 -233
  165. package/src/storage/index.ts +2 -2
  166. package/src/types.ts +91 -91
  167. package/src/validate.ts +18 -0
  168. package/src/repositories/cart-discount.ts +0 -219
  169. package/src/repositories/cart.ts +0 -659
  170. package/src/repositories/category.ts +0 -256
  171. package/src/repositories/customer.ts +0 -228
  172. package/src/repositories/discount-code.ts +0 -181
  173. package/src/repositories/inventory-entry.ts +0 -109
  174. package/src/repositories/order.ts +0 -514
  175. package/src/repositories/payment.ts +0 -342
  176. package/src/repositories/product.ts +0 -1106
  177. package/src/repositories/shipping-method.ts +0 -312
  178. package/src/repositories/shopping-list.ts +0 -392
  179. package/src/repositories/tax-category.ts +0 -111
  180. package/src/repositories/type.ts +0 -172
@@ -8,9 +8,9 @@ import type {
8
8
  QueryParam,
9
9
  RangeFacetResult,
10
10
  TermFacetResult,
11
- } from '@commercetools/platform-sdk'
12
- import { CommercetoolsError } from './exceptions.js'
13
- import { nestedLookup } from './helpers.js'
11
+ } from "@commercetools/platform-sdk";
12
+ import { CommercetoolsError } from "./exceptions";
13
+ import { nestedLookup } from "./helpers";
14
14
  import {
15
15
  FilterExpression,
16
16
  RangeExpression,
@@ -18,56 +18,56 @@ import {
18
18
  getVariants,
19
19
  parseFilterExpression,
20
20
  resolveVariantValue,
21
- } from './lib/projectionSearchFilter.js'
22
- import { applyPriceSelector } from './priceSelector.js'
23
- import { AbstractStorage } from './storage/index.js'
24
- import type { Writable } from './types.js'
21
+ } from "./lib/projectionSearchFilter";
22
+ import { applyPriceSelector } from "./priceSelector";
23
+ import { AbstractStorage } from "./storage";
24
+ import type { Writable } from "./types";
25
25
 
26
26
  export type ProductProjectionSearchParams = {
27
- fuzzy?: boolean
28
- fuzzyLevel?: number
29
- markMatchingVariants?: boolean
30
- staged?: boolean
31
- filter?: string[]
32
- 'filter.facets'?: string[]
33
- 'filter.query'?: string[]
34
- facet?: string | string[]
35
- sort?: string | string[]
36
- limit?: number
37
- offset?: number
38
- withTotal?: boolean
39
- priceCurrency?: string
40
- priceCountry?: string
41
- priceCustomerGroup?: string
42
- priceChannel?: string
43
- localeProjection?: string
44
- storeProjection?: string
45
- expand?: string | string[]
46
- [key: string]: QueryParam
47
- }
27
+ "fuzzy"?: boolean;
28
+ "fuzzyLevel"?: number;
29
+ "markMatchingVariants"?: boolean;
30
+ "staged"?: boolean;
31
+ "filter"?: string[];
32
+ "filter.facets"?: string[];
33
+ "filter.query"?: string[];
34
+ "facet"?: string | string[];
35
+ "sort"?: string | string[];
36
+ "limit"?: number;
37
+ "offset"?: number;
38
+ "withTotal"?: boolean;
39
+ "priceCurrency"?: string;
40
+ "priceCountry"?: string;
41
+ "priceCustomerGroup"?: string;
42
+ "priceChannel"?: string;
43
+ "localeProjection"?: string;
44
+ "storeProjection"?: string;
45
+ "expand"?: string | string[];
46
+ [key: string]: QueryParam;
47
+ };
48
48
 
49
49
  export class ProductProjectionSearch {
50
- protected _storage: AbstractStorage
50
+ protected _storage: AbstractStorage;
51
51
 
52
52
  constructor(storage: AbstractStorage) {
53
- this._storage = storage
53
+ this._storage = storage;
54
54
  }
55
55
 
56
56
  search(
57
57
  projectKey: string,
58
- params: ProductProjectionSearchParams
58
+ params: ProductProjectionSearchParams,
59
59
  ): ProductProjectionPagedSearchResponse {
60
60
  let resources = this._storage
61
- .all(projectKey, 'product')
61
+ .all(projectKey, "product")
62
62
  .map((r) => this.transform(r, params.staged ?? false))
63
63
  .filter((p) => {
64
64
  if (!params.staged ?? false) {
65
- return p.published
65
+ return p.published;
66
66
  }
67
- return true
68
- })
67
+ return true;
68
+ });
69
69
 
70
- const markMatchingVariant = params.markMatchingVariants ?? false
70
+ const markMatchingVariant = params.markMatchingVariants ?? false;
71
71
 
72
72
  // Apply the priceSelector
73
73
  applyPriceSelector(resources, {
@@ -75,64 +75,64 @@ export class ProductProjectionSearch {
75
75
  channel: params.priceChannel,
76
76
  customerGroup: params.priceCustomerGroup,
77
77
  currency: params.priceCurrency,
78
- })
78
+ });
79
79
 
80
80
  // Apply filters pre facetting
81
81
  if (params.filter) {
82
82
  try {
83
- const filters = params.filter.map(parseFilterExpression)
83
+ const filters = params.filter.map(parseFilterExpression);
84
84
 
85
85
  // Filters can modify the output. So clone the resources first.
86
86
  resources = resources.filter((resource) =>
87
- filters.every((f) => f(resource, markMatchingVariant))
88
- )
87
+ filters.every((f) => f(resource, markMatchingVariant)),
88
+ );
89
89
  } catch (err) {
90
- console.error(err)
90
+ console.error(err);
91
91
  throw new CommercetoolsError<InvalidInputError>(
92
92
  {
93
- code: 'InvalidInput',
93
+ code: "InvalidInput",
94
94
  message: (err as any).message,
95
95
  },
96
- 400
97
- )
96
+ 400,
97
+ );
98
98
  }
99
99
  }
100
100
 
101
101
  // TODO: Calculate facets
102
- const facets = this.getFacets(params, resources)
102
+ const facets = this.getFacets(params, resources);
103
103
 
104
104
  // Apply filters post facetting
105
- if (params['filter.query']) {
105
+ if (params["filter.query"]) {
106
106
  try {
107
- const filters = params['filter.query'].map(parseFilterExpression)
107
+ const filters = params["filter.query"].map(parseFilterExpression);
108
108
  resources = resources.filter((resource) =>
109
- filters.every((f) => f(resource, markMatchingVariant))
110
- )
109
+ filters.every((f) => f(resource, markMatchingVariant)),
110
+ );
111
111
  } catch (err) {
112
112
  throw new CommercetoolsError<InvalidInputError>(
113
113
  {
114
- code: 'InvalidInput',
114
+ code: "InvalidInput",
115
115
  message: (err as any).message,
116
116
  },
117
- 400
118
- )
117
+ 400,
118
+ );
119
119
  }
120
120
  }
121
121
 
122
122
  // Expand the resources
123
123
  if (params.expand !== undefined) {
124
124
  resources = resources.map((resource) =>
125
- this._storage.expand(projectKey, resource, params.expand)
126
- )
125
+ this._storage.expand(projectKey, resource, params.expand),
126
+ );
127
127
  }
128
128
 
129
129
  // Create a slice for the pagination. If we were working with large datasets
130
130
  // then we should have done this before transforming. But that isn't the
131
131
  // goal of this library. So lets keep it simple.
132
- const totalResults = resources.length
133
- const offset = params.offset || 0
134
- const limit = params.limit || 20
135
- const results = resources.slice(offset, offset + limit)
132
+ const totalResults = resources.length;
133
+ const offset = params.offset || 0;
134
+ const limit = params.limit || 20;
135
+ const results = resources.slice(offset, offset + limit);
136
136
 
137
137
  return {
138
138
  count: totalResults,
@@ -141,11 +141,13 @@ export class ProductProjectionSearch {
141
141
  limit: limit,
142
142
  results: results,
143
143
  facets: facets,
144
- }
144
+ };
145
145
  }
146
146
 
147
147
  transform(product: Product, staged: boolean): ProductProjection {
148
- const obj = !staged ? product.masterData.current : product.masterData.staged
148
+ const obj = !staged
149
+ ? product.masterData.current
150
+ : product.masterData.staged;
149
151
 
150
152
  return {
151
153
  id: product.id,
@@ -163,57 +165,57 @@ export class ProductProjectionSearch {
163
165
  productType: product.productType,
164
166
  hasStagedChanges: product.masterData.hasStagedChanges,
165
167
  published: product.masterData.published,
166
- }
168
+ };
167
169
  }
168
170
 
169
171
  getFacets(
170
172
  params: ProductProjectionSearchParams,
171
- products: ProductProjection[]
173
+ products: ProductProjection[],
172
174
  ): FacetResults {
173
- if (!params.facet) return {}
174
- const result: FacetResults = {}
175
+ if (!params.facet) return {};
176
+ const result: FacetResults = {};
175
177
 
176
- const regexp = new RegExp(/ counting products$/)
178
+ const regexp = new RegExp(/ counting products$/);
177
179
  for (let facet of params.facet) {
178
- let countProducts = false
179
- if (facet.endsWith(' counting products')) {
180
- facet = facet.replace(regexp, '')
181
- countProducts = true
180
+ let countProducts = false;
181
+ if (facet.endsWith(" counting products")) {
182
+ facet = facet.replace(regexp, "");
183
+ countProducts = true;
182
184
  }
183
185
 
184
- const expression = generateFacetFunc(facet)
186
+ const expression = generateFacetFunc(facet);
185
187
 
186
188
  // Term Facet
187
- if (expression.type === 'TermExpression') {
189
+ if (expression.type === "TermExpression") {
188
190
  result[facet] = this.termFacet(
189
191
  expression.source,
190
192
  products,
191
- countProducts
192
- )
193
+ countProducts,
194
+ );
193
195
  }
194
196
 
195
197
  // Range Facet
196
- if (expression.type === 'RangeExpression') {
198
+ if (expression.type === "RangeExpression") {
197
199
  result[expression.source] = this.rangeFacet(
198
200
  expression.source,
199
201
  expression.children,
200
202
  products,
201
- countProducts
202
- )
203
+ countProducts,
204
+ );
203
205
  }
204
206
 
205
207
  // FilteredFacet
206
- if (expression.type === 'FilterExpression') {
208
+ if (expression.type === "FilterExpression") {
207
209
  result[expression.source] = this.filterFacet(
208
210
  expression.source,
209
211
  expression.children,
210
212
  products,
211
- countProducts
212
- )
213
+ countProducts,
214
+ );
213
215
  }
214
216
  }
215
217
 
216
- return result
218
+ return result;
217
219
  }
218
220
 
219
221
  /**
@@ -224,137 +226,137 @@ export class ProductProjectionSearch {
224
226
  termFacet(
225
227
  facet: string,
226
228
  products: ProductProjection[],
227
- countProducts: boolean
229
+ countProducts: boolean,
228
230
  ): TermFacetResult {
229
231
  const result: Writable<TermFacetResult> = {
230
- type: 'terms',
231
- dataType: 'text',
232
+ type: "terms",
233
+ dataType: "text",
232
234
  missing: 0,
233
235
  total: 0,
234
236
  other: 0,
235
237
  terms: [],
236
- }
237
- const terms: Record<any, number> = {}
238
+ };
239
+ const terms: Record<any, number> = {};
238
240
 
239
- if (facet.startsWith('variants.')) {
240
- products.forEach((p) => {
241
- const variants = getVariants(p)
242
- variants.forEach((v) => {
243
- result.total++
241
+ if (facet.startsWith("variants.")) {
242
+ for (const p of products) {
243
+ const variants = getVariants(p);
244
+ for (const v of variants) {
245
+ result.total++;
244
246
 
245
- let value = resolveVariantValue(v, facet)
247
+ let value = resolveVariantValue(v, facet);
246
248
  if (value === undefined) {
247
- result.missing++
249
+ result.missing++;
248
250
  } else {
249
- if (typeof value === 'number') {
250
- value = Number(value).toFixed(1)
251
+ if (typeof value === "number") {
252
+ value = Number(value).toFixed(1);
251
253
  }
252
- terms[value] = value in terms ? terms[value] + 1 : 1
254
+ terms[value] = value in terms ? terms[value] + 1 : 1;
253
255
  }
254
- })
255
- })
256
+ }
257
+ }
256
258
  } else {
257
- products.forEach((p) => {
258
- const value = nestedLookup(p, facet)
259
- result.total++
259
+ for (const p of products) {
260
+ const value = nestedLookup(p, facet);
261
+ result.total++;
260
262
  if (value === undefined) {
261
- result.missing++
263
+ result.missing++;
262
264
  } else {
263
- terms[value] = value in terms ? terms[value] + 1 : 1
265
+ terms[value] = value in terms ? terms[value] + 1 : 1;
264
266
  }
265
- })
267
+ }
266
268
  }
267
269
  for (const term in terms) {
268
270
  result.terms.push({
269
271
  term: term as any,
270
272
  count: terms[term],
271
- })
273
+ });
272
274
  }
273
275
 
274
- return result
276
+ return result;
275
277
  }
276
278
 
277
279
  filterFacet(
278
280
  source: string,
279
281
  filters: FilterExpression[] | undefined,
280
282
  products: ProductProjection[],
281
- countProducts: boolean
283
+ countProducts: boolean,
282
284
  ): FilteredFacetResult {
283
- let count = 0
284
- if (source.startsWith('variants.')) {
285
+ let count = 0;
286
+ if (source.startsWith("variants.")) {
285
287
  for (const p of products) {
286
288
  for (const v of getVariants(p)) {
287
- const val = resolveVariantValue(v, source)
289
+ const val = resolveVariantValue(v, source);
288
290
  if (filters?.some((f) => f.match(val))) {
289
- count++
291
+ count++;
290
292
  }
291
293
  }
292
294
  }
293
295
  } else {
294
- throw new Error('not supported')
296
+ throw new Error("not supported");
295
297
  }
296
298
 
297
299
  return {
298
- type: 'filter',
300
+ type: "filter",
299
301
  count: count,
300
- }
302
+ };
301
303
  }
302
304
 
303
305
  rangeFacet(
304
306
  source: string,
305
307
  ranges: RangeExpression[] | undefined,
306
308
  products: ProductProjection[],
307
- countProducts: boolean
309
+ countProducts: boolean,
308
310
  ): RangeFacetResult {
309
311
  const counts =
310
312
  ranges?.map((range) => {
311
- if (source.startsWith('variants.')) {
312
- const values = []
313
+ if (source.startsWith("variants.")) {
314
+ const values: number[] = [];
313
315
  for (const p of products) {
314
316
  for (const v of getVariants(p)) {
315
- const val = resolveVariantValue(v, source)
317
+ const val = resolveVariantValue(v, source);
316
318
  if (val === undefined) {
317
- continue
319
+ continue;
318
320
  }
319
321
 
320
322
  if (range.match(val)) {
321
- values.push(val)
323
+ values.push(val);
322
324
  }
323
325
  }
324
326
  }
325
327
 
326
- const numValues = values.length
328
+ const numValues = values.length;
327
329
  return {
328
- type: 'double',
330
+ type: "double",
329
331
  from: range.start || 0,
330
- fromStr: range.start !== null ? Number(range.start).toFixed(1) : '',
332
+ fromStr: range.start !== null ? Number(range.start).toFixed(1) : "",
331
333
  to: range.stop || 0,
332
- toStr: range.stop !== null ? Number(range.stop).toFixed(1) : '',
334
+ toStr: range.stop !== null ? Number(range.stop).toFixed(1) : "",
333
335
  count: numValues,
334
336
  // totalCount: 0,
335
337
  total: values.reduce((a, b) => a + b, 0),
336
338
  min: numValues > 0 ? Math.min(...values) : 0,
337
339
  max: numValues > 0 ? Math.max(...values) : 0,
338
340
  mean: numValues > 0 ? mean(values) : 0,
339
- }
341
+ };
340
342
  } else {
341
- throw new Error('not supported')
343
+ throw new Error("not supported");
342
344
  }
343
- }) || []
345
+ }) || [];
344
346
  const data: RangeFacetResult = {
345
- type: 'range',
347
+ type: "range",
346
348
  // @ts-ignore
347
- dataType: 'number',
349
+ dataType: "number",
348
350
  ranges: counts,
349
- }
350
- return data
351
+ };
352
+ return data;
351
353
  }
352
354
  }
353
355
 
354
356
  const mean = (arr: number[]) => {
355
- let total = 0
357
+ let total = 0;
356
358
  for (let i = 0; i < arr.length; i++) {
357
- total += arr[i]
359
+ total += arr[i];
358
360
  }
359
- return total / arr.length
360
- }
361
+ return total / arr.length;
362
+ };
@@ -1,8 +1,8 @@
1
- import { CommercetoolsMock } from './index.js'
2
- import { test } from 'vitest'
1
+ import { test } from "vitest";
2
+ import { CommercetoolsMock } from "./index";
3
3
 
4
- test('getRepository', async () => {
5
- const ctMock = new CommercetoolsMock()
6
- const repo = ctMock.project('my-project-key').getRepository('order')
7
- repo.get({ projectKey: 'unittest' }, '1234')
8
- })
4
+ test("getRepository", async () => {
5
+ const ctMock = new CommercetoolsMock();
6
+ const repo = ctMock.project("my-project-key").getRepository("order");
7
+ repo.get({ projectKey: "unittest" }, "1234");
8
+ });
package/src/projectAPI.ts CHANGED
@@ -1,58 +1,60 @@
1
- import { getBaseResourceProperties } from './helpers.js'
2
- import { GetParams } from './repositories/abstract.js'
3
- import { RepositoryMap } from './repositories/index.js'
4
- import { AbstractStorage } from './storage/index.js'
5
- import { ResourceMap, ResourceType } from './types.js'
1
+ import { getBaseResourceProperties } from "./helpers";
2
+ import { RepositoryMap } from "./repositories";
3
+ import { GetParams } from "./repositories/abstract";
4
+ import { AbstractStorage } from "./storage";
5
+ import { ResourceMap, ResourceType } from "./types";
6
6
 
7
7
  export class ProjectAPI {
8
- private projectKey: string
9
- private _storage: AbstractStorage
10
- private _repositories: RepositoryMap
8
+ private projectKey: string;
9
+
10
+ private _storage: AbstractStorage;
11
+
12
+ private _repositories: RepositoryMap;
11
13
 
12
14
  constructor(
13
15
  projectKey: string,
14
16
  repositories: RepositoryMap,
15
- storage: AbstractStorage
17
+ storage: AbstractStorage,
16
18
  ) {
17
- this.projectKey = projectKey
18
- this._storage = storage
19
- this._repositories = repositories
19
+ this.projectKey = projectKey;
20
+ this._storage = storage;
21
+ this._repositories = repositories;
20
22
  }
21
23
 
22
24
  add<T extends keyof RepositoryMap & keyof ResourceMap>(
23
25
  typeId: T,
24
- resource: ResourceMap[T]
26
+ resource: ResourceMap[T],
25
27
  ) {
26
- const repository = this._repositories[typeId]
28
+ const repository = this._repositories[typeId];
27
29
  if (repository) {
28
30
  this._storage.add(this.projectKey, typeId, {
29
31
  ...getBaseResourceProperties(),
30
32
  ...resource,
31
- })
33
+ });
32
34
  } else {
33
- throw new Error(`Service for ${typeId} not implemented yet`)
35
+ throw new Error(`Service for ${typeId} not implemented yet`);
34
36
  }
35
37
  }
36
38
 
37
39
  get<RT extends ResourceType>(
38
40
  typeId: RT,
39
41
  id: string,
40
- params?: GetParams
42
+ params?: GetParams,
41
43
  ): ResourceMap[RT] {
42
44
  return this._storage.get(
43
45
  this.projectKey,
44
46
  typeId,
45
47
  id,
46
- params
47
- ) as ResourceMap[RT]
48
+ params,
49
+ ) as ResourceMap[RT];
48
50
  }
49
51
 
50
52
  // TODO: Not sure if we want to expose this...
51
53
  getRepository<RT extends keyof RepositoryMap>(typeId: RT): RepositoryMap[RT] {
52
- const repository = this._repositories[typeId]
54
+ const repository = this._repositories[typeId];
53
55
  if (repository !== undefined) {
54
- return repository as RepositoryMap[RT]
56
+ return repository as RepositoryMap[RT];
55
57
  }
56
- throw new Error('No such repository')
58
+ throw new Error("No such repository");
57
59
  }
58
60
  }