@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
@@ -37,173 +37,271 @@ import {
37
37
  type TaxCategory,
38
38
  type Type,
39
39
  type Zone,
40
- } from '@commercetools/platform-sdk'
41
- import assert from 'assert'
42
- import { CommercetoolsError } from '../exceptions.js'
43
- import { cloneObject } from '../helpers.js'
44
- import { parseExpandClause } from '../lib/expandParser.js'
45
- import { parseQueryExpression } from '../lib/predicateParser.js'
40
+ } from "@commercetools/platform-sdk";
41
+ import assert from "assert";
42
+ import { CommercetoolsError } from "~src/exceptions";
43
+ import { cloneObject } from "../helpers";
44
+ import { parseExpandClause } from "../lib/expandParser";
45
+ import { parseQueryExpression } from "../lib/predicateParser";
46
46
  import {
47
47
  PagedQueryResponseMap,
48
48
  ResourceMap,
49
49
  ResourceType,
50
50
  Writable,
51
- } from '../types.js'
51
+ } from "../types";
52
52
  import {
53
53
  AbstractStorage,
54
54
  GetParams,
55
55
  ProjectStorage,
56
56
  QueryParams,
57
- } from './abstract.js'
57
+ } from "./abstract";
58
58
 
59
59
  export class InMemoryStorage extends AbstractStorage {
60
60
  protected resources: {
61
- [projectKey: string]: ProjectStorage
62
- } = {}
61
+ [projectKey: string]: ProjectStorage;
62
+ } = {};
63
63
 
64
64
  protected projects: {
65
- [projectKey: string]: Project
66
- } = {}
65
+ [projectKey: string]: Project;
66
+ } = {};
67
+
68
+ addProject = (projectKey: string): Project => {
69
+ if (!this.projects[projectKey]) {
70
+ this.projects[projectKey] = {
71
+ key: projectKey,
72
+ name: "",
73
+ countries: [],
74
+ currencies: [],
75
+ languages: [],
76
+ createdAt: "2018-10-04T11:32:12.603Z",
77
+ trialUntil: "2018-12",
78
+ carts: {
79
+ countryTaxRateFallbackEnabled: false,
80
+ deleteDaysAfterLastModification: 90,
81
+ },
82
+ messages: { enabled: false, deleteDaysAfterCreation: 15 },
83
+ shippingRateInputType: undefined,
84
+ externalOAuth: undefined,
85
+ searchIndexing: {
86
+ products: {
87
+ status: "Deactivated",
88
+ },
89
+ orders: {
90
+ status: "Deactivated",
91
+ },
92
+ },
93
+ version: 1,
94
+ };
95
+ }
96
+ return this.projects[projectKey];
97
+ };
98
+
99
+ saveProject = (project: Project): Project => {
100
+ this.projects[project.key] = project;
101
+ return project;
102
+ };
103
+
104
+ getProject = (projectKey: string): Project => this.addProject(projectKey);
105
+
106
+ // Expand resolves a nested reference and injects the object in the given obj
107
+ public expand = <T>(
108
+ projectKey: string,
109
+ obj: T,
110
+ clause: undefined | string | string[],
111
+ ): T => {
112
+ if (!clause) return obj;
113
+ const newObj = cloneObject(obj);
114
+ if (Array.isArray(clause)) {
115
+ for (const c of clause) {
116
+ this._resolveResource(projectKey, newObj, c);
117
+ }
118
+ } else {
119
+ this._resolveResource(projectKey, newObj, clause);
120
+ }
121
+ return newObj;
122
+ };
123
+
124
+ private _resolveResource = (projectKey: string, obj: any, expand: string) => {
125
+ const params = parseExpandClause(expand);
126
+
127
+ // 'lineItems[*].variant' on ShoppingList is an exception, these variants are not references
128
+ if (params.index === "*") {
129
+ const reference = obj[params.element];
130
+ if (
131
+ params.element === "lineItems" &&
132
+ params.rest?.startsWith("variant") &&
133
+ reference.every(
134
+ (item: any) =>
135
+ item.variant === undefined && item.variantId !== undefined,
136
+ )
137
+ ) {
138
+ for (const item of reference as ShoppingListLineItem[]) {
139
+ this._resolveShoppingListLineItemVariant(projectKey, item);
140
+ }
141
+ }
142
+ }
143
+
144
+ if (!params.index) {
145
+ const reference = obj[params.element];
146
+ if (reference === undefined) {
147
+ return;
148
+ }
149
+ this._resolveReference(projectKey, reference, params.rest);
150
+ } else if (params.index === "*") {
151
+ const reference = obj[params.element];
152
+ if (reference === undefined || !Array.isArray(reference)) return;
153
+ for (const itemRef of reference as Writable<Reference>[]) {
154
+ this._resolveReference(projectKey, itemRef, params.rest);
155
+ }
156
+ } else {
157
+ const reference = obj[params.element][params.index];
158
+ if (reference === undefined) return;
159
+ this._resolveReference(projectKey, reference, params.rest);
160
+ }
161
+ };
67
162
 
68
163
  private forProjectKey(projectKey: string): ProjectStorage {
69
- this.addProject(projectKey)
164
+ this.addProject(projectKey);
70
165
 
71
- let projectStorage = this.resources[projectKey]
166
+ let projectStorage = this.resources[projectKey];
72
167
  if (!projectStorage) {
73
168
  projectStorage = this.resources[projectKey] = {
74
- 'associate-role': new Map<string, AssociateRole>(),
75
- 'attribute-group': new Map<string, AttributeGroup>(),
76
- 'business-unit': new Map<string, BusinessUnit>(),
77
- cart: new Map<string, Cart>(),
78
- 'cart-discount': new Map<string, CartDiscount>(),
79
- category: new Map<string, Category>(),
80
- channel: new Map<string, Channel>(),
81
- customer: new Map<string, Customer>(),
82
- 'customer-group': new Map<string, CustomerGroup>(),
83
- 'discount-code': new Map<string, DiscountCode>(),
84
- extension: new Map<string, Extension>(),
85
- 'inventory-entry': new Map<string, InventoryEntry>(),
86
- 'key-value-document': new Map<string, CustomObject>(),
87
- order: new Map<string, Order>(),
88
- 'order-edit': new Map<string, any>(),
89
- payment: new Map<string, Payment>(),
90
- product: new Map<string, Product>(),
91
- quote: new Map<string, Quote>(),
92
- 'quote-request': new Map<string, QuoteRequest>(),
93
- 'product-discount': new Map<string, ProductDiscount>(),
94
- 'product-selection': new Map<string, any>(),
95
- 'product-type': new Map<string, ProductType>(),
96
- 'product-projection': new Map<string, ProductProjection>(),
97
- review: new Map<string, any>(),
98
- 'shipping-method': new Map<string, ShippingMethod>(),
99
- 'staged-quote': new Map<string, StagedQuote>(),
100
- state: new Map<string, State>(),
101
- store: new Map<string, Store>(),
102
- 'shopping-list': new Map<string, ShoppingList>(),
103
- 'standalone-price': new Map<string, any>(),
104
- subscription: new Map<string, Subscription>(),
105
- 'tax-category': new Map<string, TaxCategory>(),
106
- type: new Map<string, Type>(),
107
- zone: new Map<string, Zone>(),
108
- }
169
+ "associate-role": new Map<string, AssociateRole>(),
170
+ "attribute-group": new Map<string, AttributeGroup>(),
171
+ "business-unit": new Map<string, BusinessUnit>(),
172
+ "cart": new Map<string, Cart>(),
173
+ "cart-discount": new Map<string, CartDiscount>(),
174
+ "category": new Map<string, Category>(),
175
+ "channel": new Map<string, Channel>(),
176
+ "customer": new Map<string, Customer>(),
177
+ "customer-group": new Map<string, CustomerGroup>(),
178
+ "discount-code": new Map<string, DiscountCode>(),
179
+ "extension": new Map<string, Extension>(),
180
+ "inventory-entry": new Map<string, InventoryEntry>(),
181
+ "key-value-document": new Map<string, CustomObject>(),
182
+ "order": new Map<string, Order>(),
183
+ "order-edit": new Map<string, any>(),
184
+ "payment": new Map<string, Payment>(),
185
+ "product": new Map<string, Product>(),
186
+ "quote": new Map<string, Quote>(),
187
+ "quote-request": new Map<string, QuoteRequest>(),
188
+ "product-discount": new Map<string, ProductDiscount>(),
189
+ "product-selection": new Map<string, any>(),
190
+ "product-type": new Map<string, ProductType>(),
191
+ "product-projection": new Map<string, ProductProjection>(),
192
+ "review": new Map<string, any>(),
193
+ "shipping-method": new Map<string, ShippingMethod>(),
194
+ "staged-quote": new Map<string, StagedQuote>(),
195
+ "state": new Map<string, State>(),
196
+ "store": new Map<string, Store>(),
197
+ "shopping-list": new Map<string, ShoppingList>(),
198
+ "standalone-price": new Map<string, any>(),
199
+ "subscription": new Map<string, Subscription>(),
200
+ "tax-category": new Map<string, TaxCategory>(),
201
+ "type": new Map<string, Type>(),
202
+ "zone": new Map<string, Zone>(),
203
+ };
109
204
  }
110
- return projectStorage
205
+ return projectStorage;
111
206
  }
112
207
 
113
208
  clear() {
114
209
  for (const [, projectStorage] of Object.entries(this.resources)) {
115
210
  for (const [, value] of Object.entries(projectStorage)) {
116
- value?.clear()
211
+ value?.clear();
117
212
  }
118
213
  }
119
214
  }
120
215
 
121
216
  all<RT extends ResourceType>(
122
217
  projectKey: string,
123
- typeId: RT
218
+ typeId: RT,
124
219
  ): ResourceMap[RT][] {
125
- const store = this.forProjectKey(projectKey)[typeId]
220
+ const store = this.forProjectKey(projectKey)[typeId];
126
221
  if (store) {
127
- return Array.from(store.values()).map(cloneObject) as ResourceMap[RT][]
222
+ return Array.from(store.values()).map(cloneObject) as ResourceMap[RT][];
128
223
  }
129
- return []
224
+ return [];
130
225
  }
131
226
 
132
227
  add<RT extends ResourceType>(
133
228
  projectKey: string,
134
229
  typeId: RT,
135
230
  obj: ResourceMap[RT],
136
- params: GetParams = {}
231
+ params: GetParams = {},
137
232
  ): ResourceMap[RT] {
138
- const store = this.forProjectKey(projectKey)
139
- store[typeId]?.set(obj.id, obj)
140
-
141
- const resource = this.get(projectKey, typeId, obj.id, params)
142
- assert(resource, `resource of type ${typeId} with id ${obj.id} not created`)
143
- return cloneObject(resource)
233
+ const store = this.forProjectKey(projectKey);
234
+ store[typeId]?.set(obj.id, obj);
235
+
236
+ const resource = this.get(projectKey, typeId, obj.id, params);
237
+ assert(
238
+ resource,
239
+ `resource of type ${typeId} with id ${obj.id} not created`,
240
+ );
241
+ return cloneObject(resource);
144
242
  }
145
243
 
146
244
  get<RT extends ResourceType>(
147
245
  projectKey: string,
148
246
  typeId: RT,
149
247
  id: string,
150
- params: GetParams = {}
248
+ params: GetParams = {},
151
249
  ): ResourceMap[RT] | null {
152
- const resource = this.forProjectKey(projectKey)[typeId]?.get(id)
250
+ const resource = this.forProjectKey(projectKey)[typeId]?.get(id);
153
251
  if (resource) {
154
- const clone = cloneObject(resource)
155
- return this.expand(projectKey, clone, params.expand) as ResourceMap[RT]
252
+ const clone = cloneObject(resource);
253
+ return this.expand(projectKey, clone, params.expand) as ResourceMap[RT];
156
254
  }
157
- return null
255
+ return null;
158
256
  }
159
257
 
160
258
  getByKey<RT extends ResourceType>(
161
259
  projectKey: string,
162
260
  typeId: RT,
163
261
  key: string,
164
- params: GetParams = {}
262
+ params: GetParams = {},
165
263
  ): ResourceMap[RT] | null {
166
- const store = this.forProjectKey(projectKey)
167
- const resourceStore = store[typeId]
264
+ const store = this.forProjectKey(projectKey);
265
+ const resourceStore = store[typeId];
168
266
  if (!store) {
169
- throw new Error('No type')
267
+ throw new Error("No type");
170
268
  }
171
269
 
172
- const resources: any[] = Array.from(resourceStore.values())
173
- const resource = resources.find((e) => e.key === key)
270
+ const resources: any[] = Array.from(resourceStore.values());
271
+ const resource = resources.find((e) => e.key === key);
174
272
  if (resource) {
175
- const clone = cloneObject(resource)
176
- return this.expand(projectKey, clone, params.expand) as ResourceMap[RT]
273
+ const clone = cloneObject(resource);
274
+ return this.expand(projectKey, clone, params.expand) as ResourceMap[RT];
177
275
  }
178
- return null
276
+ return null;
179
277
  }
180
278
 
181
279
  delete<RT extends ResourceType>(
182
280
  projectKey: string,
183
281
  typeId: RT,
184
282
  id: string,
185
- params: GetParams = {}
283
+ params: GetParams = {},
186
284
  ): ResourceMap[RT] | null {
187
- const resource = this.get(projectKey, typeId, id)
285
+ const resource = this.get(projectKey, typeId, id);
188
286
 
189
287
  if (resource) {
190
- this.forProjectKey(projectKey)[typeId]?.delete(id)
191
- return this.expand(projectKey, resource, params.expand)
288
+ this.forProjectKey(projectKey)[typeId]?.delete(id);
289
+ return this.expand(projectKey, resource, params.expand);
192
290
  }
193
- return resource
291
+ return resource;
194
292
  }
195
293
 
196
294
  query<RT extends ResourceType>(
197
295
  projectKey: string,
198
296
  typeId: RT,
199
- params: QueryParams
297
+ params: QueryParams,
200
298
  ): PagedQueryResponseMap[RT] {
201
- const store = this.forProjectKey(projectKey)[typeId]
299
+ const store = this.forProjectKey(projectKey)[typeId];
202
300
  if (!store) {
203
- throw new Error('No type')
301
+ throw new Error("No type");
204
302
  }
205
303
 
206
- let resources = this.all<RT>(projectKey, typeId)
304
+ let resources = this.all<RT>(projectKey, typeId);
207
305
 
208
306
  // Apply predicates
209
307
  if (params.where) {
@@ -211,37 +309,37 @@ export class InMemoryStorage extends AbstractStorage {
211
309
  // the 'var.' prefix.
212
310
  const vars = Object.fromEntries(
213
311
  Object.entries(params)
214
- .filter(([key]) => key.startsWith('var.'))
215
- .map(([key, value]) => [key.slice(4), value])
216
- )
312
+ .filter(([key]) => key.startsWith("var."))
313
+ .map(([key, value]) => [key.slice(4), value]),
314
+ );
217
315
 
218
316
  try {
219
- const filterFunc = parseQueryExpression(params.where)
220
- resources = resources.filter((resource) => filterFunc(resource, vars))
317
+ const filterFunc = parseQueryExpression(params.where);
318
+ resources = resources.filter((resource) => filterFunc(resource, vars));
221
319
  } catch (err) {
222
320
  throw new CommercetoolsError<InvalidInputError>(
223
321
  {
224
- code: 'InvalidInput',
322
+ code: "InvalidInput",
225
323
  message: (err as any).message,
226
324
  },
227
- 400
228
- )
325
+ 400,
326
+ );
229
327
  }
230
328
  }
231
329
 
232
330
  // Get the total before slicing the array
233
- const totalResources = resources.length
331
+ const totalResources = resources.length;
234
332
 
235
333
  // Apply offset, limit
236
- const offset = params.offset || 0
237
- const limit = params.limit || 20
238
- resources = resources.slice(offset, offset + limit)
334
+ const offset = params.offset || 0;
335
+ const limit = params.limit || 20;
336
+ resources = resources.slice(offset, offset + limit);
239
337
 
240
338
  // Expand the resources
241
339
  if (params.expand !== undefined) {
242
340
  resources = resources.map((resource) =>
243
- this.expand(projectKey, resource, params.expand)
244
- )
341
+ this.expand(projectKey, resource, params.expand),
342
+ );
245
343
  }
246
344
 
247
345
  return {
@@ -250,45 +348,45 @@ export class InMemoryStorage extends AbstractStorage {
250
348
  offset: offset,
251
349
  limit: limit,
252
350
  results: resources.map(cloneObject),
253
- } as PagedQueryResponseMap[RT]
351
+ } as PagedQueryResponseMap[RT];
254
352
  }
255
353
 
256
354
  search(
257
355
  projectKey: string,
258
356
  typeId: ResourceType,
259
- params: QueryParams
357
+ params: QueryParams,
260
358
  ): PagedQueryResponse {
261
- let resources = this.all(projectKey, typeId)
359
+ let resources = this.all(projectKey, typeId);
262
360
 
263
361
  // Apply predicates
264
362
  if (params.where) {
265
363
  try {
266
- const filterFunc = parseQueryExpression(params.where)
267
- resources = resources.filter((resource) => filterFunc(resource, {}))
364
+ const filterFunc = parseQueryExpression(params.where);
365
+ resources = resources.filter((resource) => filterFunc(resource, {}));
268
366
  } catch (err) {
269
367
  throw new CommercetoolsError<InvalidInputError>(
270
368
  {
271
- code: 'InvalidInput',
369
+ code: "InvalidInput",
272
370
  message: (err as any).message,
273
371
  },
274
- 400
275
- )
372
+ 400,
373
+ );
276
374
  }
277
375
  }
278
376
 
279
377
  // Get the total before slicing the array
280
- const totalResources = resources.length
378
+ const totalResources = resources.length;
281
379
 
282
380
  // Apply offset, limit
283
- const offset = params.offset || 0
284
- const limit = params.limit || 20
285
- resources = resources.slice(offset, offset + limit)
381
+ const offset = params.offset || 0;
382
+ const limit = params.limit || 20;
383
+ resources = resources.slice(offset, offset + limit);
286
384
 
287
385
  // Expand the resources
288
386
  if (params.expand !== undefined) {
289
387
  resources = resources.map((resource) =>
290
- this.expand(projectKey, resource, params.expand)
291
- )
388
+ this.expand(projectKey, resource, params.expand),
389
+ );
292
390
  }
293
391
 
294
392
  return {
@@ -297,21 +395,21 @@ export class InMemoryStorage extends AbstractStorage {
297
395
  offset: offset,
298
396
  limit: limit,
299
397
  results: resources,
300
- }
398
+ };
301
399
  }
302
400
 
303
401
  getByResourceIdentifier<RT extends ResourceType>(
304
402
  projectKey: string,
305
- identifier: ResourceIdentifier
403
+ identifier: ResourceIdentifier,
306
404
  ): ResourceMap[RT] {
307
405
  if (identifier.id) {
308
- const resource = this.get(projectKey, identifier.typeId, identifier.id)
406
+ const resource = this.get(projectKey, identifier.typeId, identifier.id);
309
407
  if (resource) {
310
- return resource as ResourceMap[RT]
408
+ return resource as ResourceMap[RT];
311
409
  }
312
410
 
313
411
  throw new CommercetoolsError<ReferencedResourceNotFoundError>({
314
- code: 'ReferencedResourceNotFound',
412
+ code: "ReferencedResourceNotFound",
315
413
  message:
316
414
  `The referenced object of type '${identifier.typeId}' with id ` +
317
415
  `'${identifier.id}' was not found. It either doesn't exist, or it ` +
@@ -319,21 +417,21 @@ export class InMemoryStorage extends AbstractStorage {
319
417
  `filters by store or customer account).`,
320
418
  typeId: identifier.typeId,
321
419
  id: identifier.id,
322
- })
420
+ });
323
421
  }
324
422
 
325
423
  if (identifier.key) {
326
424
  const resource = this.getByKey(
327
425
  projectKey,
328
426
  identifier.typeId,
329
- identifier.key
330
- )
427
+ identifier.key,
428
+ );
331
429
  if (resource) {
332
- return resource as ResourceMap[RT]
430
+ return resource as ResourceMap[RT];
333
431
  }
334
432
 
335
433
  throw new CommercetoolsError<ReferencedResourceNotFoundError>({
336
- code: 'ReferencedResourceNotFound',
434
+ code: "ReferencedResourceNotFound",
337
435
  message:
338
436
  `The referenced object of type '${identifier.typeId}' with key ` +
339
437
  `'${identifier.key}' was not found. It either doesn't exist, or it ` +
@@ -341,116 +439,21 @@ export class InMemoryStorage extends AbstractStorage {
341
439
  `filters by store or customer account).`,
342
440
  typeId: identifier.typeId,
343
441
  key: identifier.key,
344
- })
442
+ });
345
443
  }
346
444
  throw new CommercetoolsError<InvalidJsonInputError>({
347
- code: 'InvalidJsonInput',
348
- message: 'Request body does not contain valid JSON.',
445
+ code: "InvalidJsonInput",
446
+ message: "Request body does not contain valid JSON.",
349
447
  detailedErrorMessage: "ResourceIdentifier requires an 'id' xor a 'key'",
350
- })
351
- }
352
-
353
- addProject = (projectKey: string): Project => {
354
- if (!this.projects[projectKey]) {
355
- this.projects[projectKey] = {
356
- key: projectKey,
357
- name: '',
358
- countries: [],
359
- currencies: [],
360
- languages: [],
361
- createdAt: '2018-10-04T11:32:12.603Z',
362
- trialUntil: '2018-12',
363
- carts: {
364
- countryTaxRateFallbackEnabled: false,
365
- deleteDaysAfterLastModification: 90,
366
- },
367
- messages: { enabled: false, deleteDaysAfterCreation: 15 },
368
- shippingRateInputType: undefined,
369
- externalOAuth: undefined,
370
- searchIndexing: {
371
- products: {
372
- status: 'Deactivated',
373
- },
374
- orders: {
375
- status: 'Deactivated',
376
- },
377
- },
378
- version: 1,
379
- }
380
- }
381
- return this.projects[projectKey]
382
- }
383
-
384
- saveProject = (project: Project): Project => {
385
- this.projects[project.key] = project
386
- return project
387
- }
388
-
389
- getProject = (projectKey: string): Project => this.addProject(projectKey)
390
-
391
- // Expand resolves a nested reference and injects the object in the given obj
392
- public expand = <T>(
393
- projectKey: string,
394
- obj: T,
395
- clause: undefined | string | string[]
396
- ): T => {
397
- if (!clause) return obj
398
- const newObj = cloneObject(obj)
399
- if (Array.isArray(clause)) {
400
- clause.forEach((c) => {
401
- this._resolveResource(projectKey, newObj, c)
402
- })
403
- } else {
404
- this._resolveResource(projectKey, newObj, clause)
405
- }
406
- return newObj
407
- }
408
-
409
- private _resolveResource = (projectKey: string, obj: any, expand: string) => {
410
- const params = parseExpandClause(expand)
411
-
412
- // 'lineItems[*].variant' on ShoppingList is an exception, these variants are not references
413
- if (params.index === '*') {
414
- const reference = obj[params.element]
415
- if (
416
- params.element === 'lineItems' &&
417
- params.rest?.startsWith('variant') &&
418
- reference.every(
419
- (item: any) =>
420
- item.variant === undefined && item.variantId !== undefined
421
- )
422
- ) {
423
- reference.forEach((item: ShoppingListLineItem) => {
424
- this._resolveShoppingListLineItemVariant(projectKey, item)
425
- })
426
- }
427
- }
428
-
429
- if (!params.index) {
430
- const reference = obj[params.element]
431
- if (reference === undefined) {
432
- return
433
- }
434
- this._resolveReference(projectKey, reference, params.rest)
435
- } else if (params.index === '*') {
436
- const reference = obj[params.element]
437
- if (reference === undefined || !Array.isArray(reference)) return
438
- reference.forEach((itemRef: Writable<Reference>) => {
439
- this._resolveReference(projectKey, itemRef, params.rest)
440
- })
441
- } else {
442
- const reference = obj[params.element][params.index]
443
- if (reference === undefined) return
444
- this._resolveReference(projectKey, reference, params.rest)
445
- }
448
+ });
446
449
  }
447
450
 
448
451
  private _resolveReference(
449
452
  projectKey: string,
450
453
  reference: any,
451
- expand: string | undefined
454
+ expand: string | undefined,
452
455
  ) {
453
- if (reference === undefined) return
456
+ if (reference === undefined) return;
454
457
 
455
458
  if (
456
459
  reference.typeId !== undefined &&
@@ -463,35 +466,36 @@ export class InMemoryStorage extends AbstractStorage {
463
466
  typeId: reference.typeId,
464
467
  id: reference.id,
465
468
  key: reference.key,
466
- } as ResourceIdentifier)
469
+ } as ResourceIdentifier);
467
470
  }
468
471
  if (expand) {
469
- this._resolveResource(projectKey, reference.obj, expand)
472
+ this._resolveResource(projectKey, reference.obj, expand);
470
473
  }
471
474
  } else {
472
475
  if (expand) {
473
- this._resolveResource(projectKey, reference, expand)
476
+ this._resolveResource(projectKey, reference, expand);
474
477
  }
475
478
  }
476
479
  }
480
+
477
481
  private _resolveShoppingListLineItemVariant(
478
482
  projectKey: string,
479
- lineItem: ShoppingListLineItem
483
+ lineItem: ShoppingListLineItem,
480
484
  ) {
481
485
  const product = this.getByResourceIdentifier(projectKey, {
482
- typeId: 'product',
486
+ typeId: "product",
483
487
  id: lineItem.productId,
484
- }) as Product | undefined
488
+ }) as Product | undefined;
485
489
 
486
490
  if (!product) {
487
- return
491
+ return;
488
492
  }
489
493
 
490
494
  const variant = [
491
495
  product.masterData.current.masterVariant,
492
496
  ...product.masterData.current.variants,
493
- ].find((e) => e.id === lineItem.variantId)
497
+ ].find((e) => e.id === lineItem.variantId);
494
498
  // @ts-ignore
495
- lineItem.variant = variant
499
+ lineItem.variant = variant;
496
500
  }
497
501
  }