@labdigital/commercetools-mock 0.9.0 → 0.10.0

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 (95) hide show
  1. package/README.md +8 -0
  2. package/dist/index.d.ts +18 -17
  3. package/dist/index.global.js +1751 -1664
  4. package/dist/index.global.js.map +1 -1
  5. package/dist/index.js +1773 -1684
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +1966 -1877
  8. package/dist/index.mjs.map +1 -1
  9. package/package.json +28 -20
  10. package/src/constants.ts +4 -2
  11. package/src/ctMock.ts +19 -84
  12. package/src/helpers.ts +9 -10
  13. package/src/index.test.ts +1 -1
  14. package/src/lib/haversine.ts +2 -2
  15. package/src/lib/masking.ts +3 -1
  16. package/src/lib/predicateParser.test.ts +16 -0
  17. package/src/lib/predicateParser.ts +94 -86
  18. package/src/lib/projectionSearchFilter.test.ts +28 -36
  19. package/src/lib/projectionSearchFilter.ts +86 -102
  20. package/src/oauth/store.ts +3 -3
  21. package/src/priceSelector.test.ts +18 -35
  22. package/src/priceSelector.ts +6 -9
  23. package/src/product-projection-search.ts +51 -57
  24. package/src/repositories/abstract.ts +85 -41
  25. package/src/repositories/cart-discount.ts +1 -1
  26. package/src/repositories/cart.ts +36 -31
  27. package/src/repositories/category.ts +17 -19
  28. package/src/repositories/channel.ts +1 -1
  29. package/src/repositories/custom-object.ts +35 -22
  30. package/src/repositories/customer-group.ts +1 -1
  31. package/src/repositories/customer.ts +39 -1
  32. package/src/repositories/discount-code.ts +1 -1
  33. package/src/repositories/errors.ts +9 -11
  34. package/src/repositories/extension.ts +13 -11
  35. package/src/repositories/helpers.ts +8 -13
  36. package/src/repositories/index.ts +59 -0
  37. package/src/repositories/inventory-entry.ts +1 -1
  38. package/src/repositories/order.ts +6 -6
  39. package/src/repositories/payment.ts +3 -3
  40. package/src/repositories/product-discount.ts +1 -1
  41. package/src/repositories/product-projection.ts +1 -0
  42. package/src/repositories/product-type.ts +29 -34
  43. package/src/repositories/product.ts +124 -80
  44. package/src/repositories/project.ts +10 -27
  45. package/src/repositories/shipping-method.ts +15 -17
  46. package/src/repositories/shopping-list.ts +2 -2
  47. package/src/repositories/state.ts +9 -9
  48. package/src/repositories/store.ts +2 -2
  49. package/src/repositories/subscription.ts +1 -1
  50. package/src/repositories/tax-category.ts +4 -4
  51. package/src/repositories/type.ts +12 -14
  52. package/src/repositories/zone.ts +5 -6
  53. package/src/server.ts +5 -0
  54. package/src/services/abstract.ts +44 -11
  55. package/src/services/cart-discount.ts +2 -3
  56. package/src/services/cart.test.ts +8 -10
  57. package/src/services/cart.ts +8 -11
  58. package/src/services/category.test.ts +1 -2
  59. package/src/services/category.ts +2 -3
  60. package/src/services/channel.ts +2 -3
  61. package/src/services/custom-object.test.ts +5 -5
  62. package/src/services/custom-object.ts +2 -3
  63. package/src/services/customer-group.ts +2 -3
  64. package/src/services/customer.test.ts +136 -0
  65. package/src/services/customer.ts +2 -3
  66. package/src/services/discount-code.ts +2 -3
  67. package/src/services/extension.ts +2 -3
  68. package/src/services/index.ts +74 -0
  69. package/src/services/inventory-entry.test.ts +8 -12
  70. package/src/services/inventory-entry.ts +2 -3
  71. package/src/services/my-cart.ts +3 -4
  72. package/src/services/my-customer.ts +2 -3
  73. package/src/services/my-order.ts +3 -4
  74. package/src/services/my-payment.ts +2 -3
  75. package/src/services/order.test.ts +4 -6
  76. package/src/services/order.ts +2 -3
  77. package/src/services/payment.ts +2 -3
  78. package/src/services/product-discount.ts +2 -3
  79. package/src/services/product-projection.test.ts +76 -8
  80. package/src/services/product-projection.ts +2 -3
  81. package/src/services/product-type.ts +2 -3
  82. package/src/services/product.test.ts +199 -89
  83. package/src/services/product.ts +2 -3
  84. package/src/services/project.ts +3 -3
  85. package/src/services/shipping-method.ts +2 -3
  86. package/src/services/shopping-list.ts +2 -3
  87. package/src/services/state.ts +2 -3
  88. package/src/services/store.test.ts +11 -2
  89. package/src/services/store.ts +2 -3
  90. package/src/services/subscription.ts +2 -3
  91. package/src/services/tax-category.ts +2 -3
  92. package/src/services/type.ts +2 -3
  93. package/src/services/zone.ts +2 -3
  94. package/src/storage.ts +23 -30
  95. package/src/types.ts +46 -6
package/src/storage.ts CHANGED
@@ -36,6 +36,7 @@ import { parseExpandClause } from './lib/expandParser'
36
36
  import { RepositoryTypes, ResourceMap, Writable } from './types'
37
37
  import { parseQueryExpression } from './lib/predicateParser'
38
38
  import { CommercetoolsError } from './exceptions'
39
+ import { cloneObject } from './helpers'
39
40
 
40
41
  type GetParams = {
41
42
  expand?: string[]
@@ -54,8 +55,6 @@ type QueryParams = {
54
55
  export abstract class AbstractStorage {
55
56
  abstract clear(): void
56
57
 
57
- abstract assertStorage(typeId: RepositoryTypes): void
58
-
59
58
  abstract all<RT extends RepositoryTypes>(
60
59
  projectKey: string,
61
60
  typeId: RT
@@ -171,15 +170,13 @@ export class InMemoryStorage extends AbstractStorage {
171
170
  }
172
171
  }
173
172
 
174
- assertStorage(typeId: RepositoryTypes) {}
175
-
176
173
  all<RT extends RepositoryTypes>(
177
174
  projectKey: string,
178
175
  typeId: RT
179
176
  ): ResourceMap[RT][] {
180
177
  const store = this.forProjectKey(projectKey)[typeId]
181
178
  if (store) {
182
- return Array.from(store.values()) as ResourceMap[RT][]
179
+ return Array.from(store.values()).map(cloneObject) as ResourceMap[RT][]
183
180
  }
184
181
  return []
185
182
  }
@@ -195,7 +192,7 @@ export class InMemoryStorage extends AbstractStorage {
195
192
 
196
193
  const resource = this.get(projectKey, typeId, obj.id, params)
197
194
  assert(resource, `resource of type ${typeId} with id ${obj.id} not created`)
198
- return resource
195
+ return cloneObject(resource)
199
196
  }
200
197
 
201
198
  get<RT extends RepositoryTypes>(
@@ -206,7 +203,8 @@ export class InMemoryStorage extends AbstractStorage {
206
203
  ): ResourceMap[RT] | null {
207
204
  const resource = this.forProjectKey(projectKey)[typeId]?.get(id)
208
205
  if (resource) {
209
- return this.expand(projectKey, resource, params.expand) as ResourceMap[RT]
206
+ const clone = cloneObject(resource)
207
+ return this.expand(projectKey, clone, params.expand) as ResourceMap[RT]
210
208
  }
211
209
  return null
212
210
  }
@@ -224,9 +222,10 @@ export class InMemoryStorage extends AbstractStorage {
224
222
  }
225
223
 
226
224
  const resources: any[] = Array.from(resourceStore.values())
227
- const resource = resources.find(e => e.key === key)
225
+ const resource = resources.find((e) => e.key === key)
228
226
  if (resource) {
229
- return this.expand(projectKey, resource, params.expand) as ResourceMap[RT]
227
+ const clone = cloneObject(resource)
228
+ return this.expand(projectKey, clone, params.expand) as ResourceMap[RT]
230
229
  }
231
230
  return null
232
231
  }
@@ -262,7 +261,7 @@ export class InMemoryStorage extends AbstractStorage {
262
261
  if (params.where) {
263
262
  try {
264
263
  const filterFunc = parseQueryExpression(params.where)
265
- resources = resources.filter(resource => filterFunc(resource, {}))
264
+ resources = resources.filter((resource) => filterFunc(resource, {}))
266
265
  } catch (err) {
267
266
  throw new CommercetoolsError<InvalidInputError>(
268
267
  {
@@ -284,9 +283,9 @@ export class InMemoryStorage extends AbstractStorage {
284
283
 
285
284
  // Expand the resources
286
285
  if (params.expand !== undefined) {
287
- resources = resources.map(resource => {
288
- return this.expand(projectKey, resource, params.expand)
289
- })
286
+ resources = resources.map((resource) =>
287
+ this.expand(projectKey, resource, params.expand)
288
+ )
290
289
  }
291
290
 
292
291
  return {
@@ -294,7 +293,7 @@ export class InMemoryStorage extends AbstractStorage {
294
293
  total: resources.length,
295
294
  offset: offset,
296
295
  limit: limit,
297
- results: resources,
296
+ results: resources.map(cloneObject),
298
297
  }
299
298
  }
300
299
 
@@ -303,18 +302,13 @@ export class InMemoryStorage extends AbstractStorage {
303
302
  typeId: RepositoryTypes,
304
303
  params: QueryParams
305
304
  ): PagedQueryResponse {
306
- const store = this.forProjectKey(projectKey)[typeId]
307
- if (!store) {
308
- throw new Error('No type')
309
- }
310
-
311
- let resources = Array.from(store.values())
305
+ let resources = this.all(projectKey, typeId)
312
306
 
313
307
  // Apply predicates
314
308
  if (params.where) {
315
309
  try {
316
310
  const filterFunc = parseQueryExpression(params.where)
317
- resources = resources.filter(resource => filterFunc(resource, {}))
311
+ resources = resources.filter((resource) => filterFunc(resource, {}))
318
312
  } catch (err) {
319
313
  throw new CommercetoolsError<InvalidInputError>(
320
314
  {
@@ -336,9 +330,9 @@ export class InMemoryStorage extends AbstractStorage {
336
330
 
337
331
  // Expand the resources
338
332
  if (params.expand !== undefined) {
339
- resources = resources.map(resource => {
340
- return this.expand(projectKey, resource, params.expand)
341
- })
333
+ resources = resources.map((resource) =>
334
+ this.expand(projectKey, resource, params.expand)
335
+ )
342
336
  }
343
337
 
344
338
  return {
@@ -373,7 +367,7 @@ export class InMemoryStorage extends AbstractStorage {
373
367
  // have them all.
374
368
  const resource = Array.from(store.values()).find(
375
369
  // @ts-ignore
376
- r => r.key === identifier.key
370
+ (r) => r.key === identifier.key
377
371
  )
378
372
  if (resource) {
379
373
  return resource as ResourceMap[RT]
@@ -423,19 +417,18 @@ export class InMemoryStorage extends AbstractStorage {
423
417
  return project
424
418
  }
425
419
 
426
- getProject = (projectKey: string): Project => {
427
- return this.addProject(projectKey)
428
- }
420
+ getProject = (projectKey: string): Project => this.addProject(projectKey)
429
421
 
422
+ // Expand resolves a nested reference and injects the object in the given obj
430
423
  public expand = <T>(
431
424
  projectKey: string,
432
425
  obj: T,
433
426
  clause: undefined | string | string[]
434
427
  ): T => {
435
428
  if (!clause) return obj
436
- const newObj = JSON.parse(JSON.stringify(obj))
429
+ const newObj = cloneObject(obj)
437
430
  if (Array.isArray(clause)) {
438
- clause.forEach(c => {
431
+ clause.forEach((c) => {
439
432
  this._resolveResource(projectKey, newObj, c)
440
433
  })
441
434
  } else {
package/src/types.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { ProductProjectionRepository } from './repositories/product-projection'
2
2
  import { ShoppingListRepository } from './repositories/shopping-list'
3
3
  import * as ctp from '@commercetools/platform-sdk'
4
- import { ReferenceTypeId } from '@commercetools/platform-sdk'
4
+ import {
5
+ ReferenceTypeId,
6
+ } from '@commercetools/platform-sdk'
5
7
  import AbstractService from './services/abstract'
6
8
  import { CartRepository } from './repositories/cart'
7
9
  import { CustomerRepository } from './repositories/customer'
@@ -15,6 +17,7 @@ import { ShippingMethodRepository } from './repositories/shipping-method'
15
17
  import { StateRepository } from './repositories/state'
16
18
  import { TaxCategoryRepository } from './repositories/tax-category'
17
19
  import { ProductDiscountRepository } from 'repositories/product-discount'
20
+ import { AbstractRepository } from 'repositories/abstract'
18
21
 
19
22
  export type Writable<T> = { -readonly [P in keyof T]: Writable<T[P]> }
20
23
 
@@ -22,6 +25,7 @@ export type RepositoryTypes =
22
25
  | ReferenceTypeId
23
26
  | 'standalone-price'
24
27
  | 'product-projection'
28
+
25
29
  export type ServiceTypes =
26
30
  | RepositoryTypes
27
31
  | 'my-cart'
@@ -30,11 +34,47 @@ export type ServiceTypes =
30
34
  | 'my-customer'
31
35
  | 'product-projection'
32
36
 
33
- export type Services = Partial<
34
- {
35
- [index in ServiceTypes]: AbstractService
36
- }
37
- >
37
+ export type Services = Partial<{
38
+ [index in ServiceTypes]: AbstractService
39
+ }>
40
+
41
+ export type Repositories = Partial<{
42
+ [index in RepositoryTypes | 'project']: AbstractRepository
43
+ }>
44
+
45
+
46
+ export type Resource =
47
+ | ctp.BaseResource
48
+ | ctp.Cart
49
+ | ctp.CartDiscount
50
+ | ctp.Category
51
+ | ctp.Channel
52
+ | ctp.Customer
53
+ | ctp.CustomerGroup
54
+ | ctp.DiscountCode
55
+ | ctp.Extension
56
+ | ctp.InventoryEntry
57
+ | ctp.CustomObject
58
+ | ctp.Order
59
+ | ctp.OrderEdit
60
+ | ctp.Payment
61
+ | ctp.Product
62
+ | ctp.Project
63
+ | ctp.ProductDiscount
64
+ | ctp.ProductProjection
65
+ | ctp.ProductSelection
66
+ | ctp.StandalonePrice
67
+ | ctp.ProductType
68
+ | ctp.Review
69
+ | ctp.ShippingMethod
70
+ | ctp.ShoppingList
71
+ | ctp.StandalonePrice
72
+ | ctp.State
73
+ | ctp.Store
74
+ | ctp.Subscription
75
+ | ctp.TaxCategory
76
+ | ctp.Type
77
+ | ctp.Zone
38
78
 
39
79
  export type ResourceMap = {
40
80
  cart: ctp.Cart