@labdigital/commercetools-mock 0.6.5 → 0.7.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 (113) hide show
  1. package/package.json +26 -13
  2. package/src/ctMock.ts +5 -0
  3. package/src/helpers.ts +15 -0
  4. package/src/lib/projectionSearchFilter.test.ts +177 -0
  5. package/src/lib/projectionSearchFilter.ts +248 -0
  6. package/src/priceSelector.test.ts +96 -0
  7. package/src/priceSelector.ts +109 -0
  8. package/src/product-projection-search.ts +149 -0
  9. package/src/projectAPI.ts +19 -20
  10. package/src/repositories/category.ts +36 -0
  11. package/src/repositories/channel.ts +104 -0
  12. package/src/repositories/customer-group.ts +37 -0
  13. package/src/repositories/discount-code.ts +37 -0
  14. package/src/repositories/helpers.ts +46 -4
  15. package/src/repositories/product-discount.ts +181 -0
  16. package/src/repositories/product-projection.ts +29 -59
  17. package/src/repositories/product-type.ts +88 -6
  18. package/src/repositories/product.ts +49 -9
  19. package/src/repositories/shipping-method.ts +31 -0
  20. package/src/repositories/store.ts +43 -3
  21. package/src/repositories/type.ts +19 -0
  22. package/src/services/custom-object.test.ts +2 -2
  23. package/src/services/product-discount.ts +33 -0
  24. package/src/services/product-projection.test.ts +171 -116
  25. package/src/services/product.test.ts +12 -0
  26. package/src/storage.ts +116 -58
  27. package/src/types.ts +9 -2
  28. package/dist/commercetools-mock.cjs.development.js +0 -4382
  29. package/dist/commercetools-mock.cjs.development.js.map +0 -1
  30. package/dist/commercetools-mock.cjs.production.min.js +0 -2
  31. package/dist/commercetools-mock.cjs.production.min.js.map +0 -1
  32. package/dist/commercetools-mock.esm.js +0 -4374
  33. package/dist/commercetools-mock.esm.js.map +0 -1
  34. package/dist/constants.d.ts +0 -2
  35. package/dist/ctMock.d.ts +0 -32
  36. package/dist/exceptions.d.ts +0 -12
  37. package/dist/helpers.d.ts +0 -6
  38. package/dist/index.d.ts +0 -3
  39. package/dist/index.js +0 -8
  40. package/dist/lib/expandParser.d.ts +0 -15
  41. package/dist/lib/filterParser.d.ts +0 -1
  42. package/dist/lib/haversine.d.ts +0 -8
  43. package/dist/lib/masking.d.ts +0 -1
  44. package/dist/lib/predicateParser.d.ts +0 -11
  45. package/dist/lib/proxy.d.ts +0 -1
  46. package/dist/oauth/errors.d.ts +0 -8
  47. package/dist/oauth/helpers.d.ts +0 -2
  48. package/dist/oauth/server.d.ts +0 -12
  49. package/dist/oauth/store.d.ts +0 -14
  50. package/dist/projectAPI.d.ts +0 -12
  51. package/dist/repositories/abstract.d.ts +0 -33
  52. package/dist/repositories/cart-discount.d.ts +0 -9
  53. package/dist/repositories/cart.d.ts +0 -21
  54. package/dist/repositories/category.d.ts +0 -18
  55. package/dist/repositories/channel.d.ts +0 -6
  56. package/dist/repositories/custom-object.d.ts +0 -8
  57. package/dist/repositories/customer-group.d.ts +0 -11
  58. package/dist/repositories/customer.d.ts +0 -11
  59. package/dist/repositories/discount-code.d.ts +0 -8
  60. package/dist/repositories/errors.d.ts +0 -2
  61. package/dist/repositories/extension.d.ts +0 -8
  62. package/dist/repositories/helpers.d.ts +0 -10
  63. package/dist/repositories/inventory-entry.d.ts +0 -14
  64. package/dist/repositories/my-order.d.ts +0 -6
  65. package/dist/repositories/order.d.ts +0 -26
  66. package/dist/repositories/payment.d.ts +0 -23
  67. package/dist/repositories/product-projection.d.ts +0 -10
  68. package/dist/repositories/product-type.d.ts +0 -10
  69. package/dist/repositories/product.d.ts +0 -11
  70. package/dist/repositories/project.d.ts +0 -8
  71. package/dist/repositories/shipping-method.d.ts +0 -10
  72. package/dist/repositories/shopping-list.d.ts +0 -6
  73. package/dist/repositories/state.d.ts +0 -8
  74. package/dist/repositories/store.d.ts +0 -10
  75. package/dist/repositories/subscription.d.ts +0 -6
  76. package/dist/repositories/tax-category.d.ts +0 -10
  77. package/dist/repositories/type.d.ts +0 -8
  78. package/dist/repositories/zone.d.ts +0 -8
  79. package/dist/server.d.ts +0 -1
  80. package/dist/services/abstract.d.ts +0 -20
  81. package/dist/services/cart-discount.d.ts +0 -9
  82. package/dist/services/cart.d.ts +0 -12
  83. package/dist/services/category.d.ts +0 -9
  84. package/dist/services/channel.d.ts +0 -9
  85. package/dist/services/custom-object.d.ts +0 -13
  86. package/dist/services/customer-group.d.ts +0 -9
  87. package/dist/services/customer.d.ts +0 -10
  88. package/dist/services/discount-code.d.ts +0 -9
  89. package/dist/services/extension.d.ts +0 -9
  90. package/dist/services/inventory-entry.d.ts +0 -9
  91. package/dist/services/my-cart.d.ts +0 -11
  92. package/dist/services/my-customer.d.ts +0 -13
  93. package/dist/services/my-order.d.ts +0 -10
  94. package/dist/services/my-payment.d.ts +0 -9
  95. package/dist/services/order.d.ts +0 -12
  96. package/dist/services/payment.d.ts +0 -9
  97. package/dist/services/product-projection.d.ts +0 -11
  98. package/dist/services/product-type.d.ts +0 -11
  99. package/dist/services/product.d.ts +0 -9
  100. package/dist/services/project.d.ts +0 -11
  101. package/dist/services/shipping-method.d.ts +0 -10
  102. package/dist/services/shopping-list.d.ts +0 -9
  103. package/dist/services/state.d.ts +0 -9
  104. package/dist/services/store.d.ts +0 -11
  105. package/dist/services/subscription.d.ts +0 -9
  106. package/dist/services/tax-category.d.ts +0 -11
  107. package/dist/services/type.d.ts +0 -9
  108. package/dist/services/zone.d.ts +0 -9
  109. package/dist/storage.d.ts +0 -56
  110. package/dist/types.d.ts +0 -89
  111. package/dist/validate.d.ts +0 -7482
  112. package/src/lib/filterParser.test.ts +0 -15
  113. package/src/lib/filterParser.ts +0 -17
package/src/storage.ts CHANGED
@@ -30,6 +30,7 @@ import {
30
30
  Zone,
31
31
  Channel,
32
32
  Subscription,
33
+ ProductDiscount,
33
34
  } from '@commercetools/platform-sdk'
34
35
  import { parseExpandClause } from './lib/expandParser'
35
36
  import { RepositoryTypes, ResourceMap, Writable } from './types'
@@ -55,35 +56,38 @@ export abstract class AbstractStorage {
55
56
 
56
57
  abstract assertStorage(typeId: RepositoryTypes): void
57
58
 
58
- abstract all(projectKey: string, typeId: RepositoryTypes): Array<BaseResource>
59
+ abstract all<RT extends RepositoryTypes>(
60
+ projectKey: string,
61
+ typeId: RT
62
+ ): Array<ResourceMap[RT]>
59
63
 
60
- abstract add<RepositoryTypes extends keyof ResourceMap>(
64
+ abstract add<RT extends RepositoryTypes>(
61
65
  projectKey: string,
62
- typeId: RepositoryTypes,
63
- obj: ResourceMap[RepositoryTypes]
66
+ typeId: RT,
67
+ obj: ResourceMap[RT]
64
68
  ): void
65
69
 
66
- abstract get<RepositoryTypes extends keyof ResourceMap>(
70
+ abstract get<RT extends RepositoryTypes>(
67
71
  projectKey: string,
68
- typeId: RepositoryTypes,
72
+ typeId: RT,
69
73
  id: string,
70
74
  params?: GetParams
71
- ): ResourceMap[RepositoryTypes] | null
75
+ ): ResourceMap[RT] | null
72
76
 
73
- abstract getByKey<RepositoryTypes extends keyof ResourceMap>(
77
+ abstract getByKey<RT extends RepositoryTypes>(
74
78
  projectKey: string,
75
- typeId: RepositoryTypes,
79
+ typeId: RT,
76
80
  key: string,
77
81
  params: GetParams
78
- ): ResourceMap[RepositoryTypes] | null
82
+ ): ResourceMap[RT] | null
79
83
 
80
84
  abstract addProject(projectKey: string): Project
81
85
  abstract getProject(projectKey: string): Project
82
86
  abstract saveProject(project: Project): Project
83
87
 
84
- abstract delete(
88
+ abstract delete<RT extends RepositoryTypes>(
85
89
  projectKey: string,
86
- typeId: RepositoryTypes,
90
+ typeId: RT,
87
91
  id: string,
88
92
  params: GetParams
89
93
  ): BaseResource | null
@@ -94,17 +98,21 @@ export abstract class AbstractStorage {
94
98
  params: QueryParams
95
99
  ): PagedQueryResponse
96
100
 
97
- abstract getByResourceIdentifier(
101
+ abstract getByResourceIdentifier<RT extends RepositoryTypes>(
98
102
  projectKey: string,
99
103
  identifier: ResourceIdentifier
100
- ): BaseResource | undefined
104
+ ): ResourceMap[RT] | undefined
105
+
106
+ abstract expand<T>(
107
+ projectKey: string,
108
+ obj: T,
109
+ clause: undefined | string | string[]
110
+ ): T
101
111
  }
102
112
 
103
- type ProjectStorage = Partial<
104
- {
105
- [index in RepositoryTypes]: Map<string, BaseResource>
106
- }
107
- >
113
+ type ProjectStorage = {
114
+ [index in RepositoryTypes]: Map<string, BaseResource>
115
+ }
108
116
 
109
117
  export class InMemoryStorage extends AbstractStorage {
110
118
  protected resources: {
@@ -115,7 +123,7 @@ export class InMemoryStorage extends AbstractStorage {
115
123
  [projectKey: string]: Project
116
124
  } = {}
117
125
 
118
- private forProjectKey(projectKey: string) {
126
+ private forProjectKey(projectKey: string): ProjectStorage {
119
127
  this.addProject(projectKey)
120
128
 
121
129
  let projectStorage = this.resources[projectKey]
@@ -132,15 +140,20 @@ export class InMemoryStorage extends AbstractStorage {
132
140
  'inventory-entry': new Map<string, InventoryEntry>(),
133
141
  'key-value-document': new Map<string, CustomObject>(),
134
142
  order: new Map<string, Order>(),
143
+ 'order-edit': new Map<string, any>(),
135
144
  payment: new Map<string, Payment>(),
136
- 'product-type': new Map<string, ProductType>(),
137
145
  product: new Map<string, Product>(),
146
+ 'product-discount': new Map<string, ProductDiscount>(),
147
+ 'product-price': new Map<string, any>(),
138
148
  'product-selection': new Map<string, any>(),
149
+ 'product-type': new Map<string, ProductType>(),
139
150
  'product-projection': new Map<string, ProductProjection>(),
151
+ review: new Map<string, any>(),
140
152
  'shipping-method': new Map<string, ShippingMethod>(),
141
153
  state: new Map<string, State>(),
142
154
  store: new Map<string, Store>(),
143
155
  'shopping-list': new Map<string, ShoppingList>(),
156
+ 'standalone-price': new Map<string, any>(),
144
157
  subscription: new Map<string, Subscription>(),
145
158
  'tax-category': new Map<string, TaxCategory>(),
146
159
  type: new Map<string, Type>(),
@@ -160,70 +173,67 @@ export class InMemoryStorage extends AbstractStorage {
160
173
 
161
174
  assertStorage(typeId: RepositoryTypes) {}
162
175
 
163
- all(projectKey: string, typeId: RepositoryTypes) {
176
+ all<RT extends RepositoryTypes>(
177
+ projectKey: string,
178
+ typeId: RT
179
+ ): ResourceMap[RT][] {
164
180
  const store = this.forProjectKey(projectKey)[typeId]
165
181
  if (store) {
166
- return Array.from(store.values())
182
+ return Array.from(store.values()) as ResourceMap[RT][]
167
183
  }
168
184
  return []
169
185
  }
170
186
 
171
- add<RepositoryTypes extends keyof ResourceMap>(
187
+ add<RT extends RepositoryTypes>(
172
188
  projectKey: string,
173
- typeId: RepositoryTypes,
174
- obj: ResourceMap[RepositoryTypes],
189
+ typeId: RT,
190
+ obj: ResourceMap[RT],
175
191
  params: GetParams = {}
176
- ): ResourceMap[RepositoryTypes] {
177
- this.forProjectKey(projectKey)[typeId]?.set(obj.id, obj)
192
+ ): ResourceMap[RT] {
193
+ const store = this.forProjectKey(projectKey)
194
+ store[typeId]?.set(obj.id, obj)
178
195
 
179
196
  const resource = this.get(projectKey, typeId, obj.id, params)
180
197
  assert(resource, `resource of type ${typeId} with id ${obj.id} not created`)
181
198
  return resource
182
199
  }
183
200
 
184
- get<RepositoryTypes extends keyof ResourceMap>(
201
+ get<RT extends RepositoryTypes>(
185
202
  projectKey: string,
186
- typeId: RepositoryTypes,
203
+ typeId: RT,
187
204
  id: string,
188
205
  params: GetParams = {}
189
- ): ResourceMap[RepositoryTypes] | null {
206
+ ): ResourceMap[RT] | null {
190
207
  const resource = this.forProjectKey(projectKey)[typeId]?.get(id)
191
208
  if (resource) {
192
- return this.expand(
193
- projectKey,
194
- resource,
195
- params.expand
196
- ) as ResourceMap[RepositoryTypes]
209
+ return this.expand(projectKey, resource, params.expand) as ResourceMap[RT]
197
210
  }
198
211
  return null
199
212
  }
200
213
 
201
- getByKey<RepositoryTypes extends keyof ResourceMap>(
214
+ getByKey<RT extends RepositoryTypes>(
202
215
  projectKey: string,
203
- typeId: RepositoryTypes,
216
+ typeId: RT,
204
217
  key: string,
205
218
  params: GetParams = {}
206
- ): ResourceMap[RepositoryTypes] | null {
207
- const store = this.forProjectKey(projectKey)[typeId]
219
+ ): ResourceMap[RT] | null {
220
+ const store = this.forProjectKey(projectKey)
221
+ const resourceStore = store[typeId]
208
222
  if (!store) {
209
223
  throw new Error('No type')
210
224
  }
211
225
 
212
- const resources: any[] = Array.from(store.values())
226
+ const resources: any[] = Array.from(resourceStore.values())
213
227
  const resource = resources.find(e => e.key === key)
214
228
  if (resource) {
215
- return this.expand(
216
- projectKey,
217
- resource,
218
- params.expand
219
- ) as ResourceMap[RepositoryTypes]
229
+ return this.expand(projectKey, resource, params.expand) as ResourceMap[RT]
220
230
  }
221
231
  return null
222
232
  }
223
233
 
224
- delete(
234
+ delete<RT extends RepositoryTypes>(
225
235
  projectKey: string,
226
- typeId: RepositoryTypes,
236
+ typeId: RT,
227
237
  id: string,
228
238
  params: GetParams = {}
229
239
  ): BaseResource | null {
@@ -231,11 +241,7 @@ export class InMemoryStorage extends AbstractStorage {
231
241
 
232
242
  if (resource) {
233
243
  this.forProjectKey(projectKey)[typeId]?.delete(id)
234
- return this.expand(
235
- projectKey,
236
- resource,
237
- params.expand
238
- ) as ResourceMap[RepositoryTypes]
244
+ return this.expand(projectKey, resource, params.expand) as ResourceMap[RT]
239
245
  }
240
246
  return resource
241
247
  }
@@ -292,14 +298,66 @@ export class InMemoryStorage extends AbstractStorage {
292
298
  }
293
299
  }
294
300
 
295
- getByResourceIdentifier<RepositoryTypes extends keyof ResourceMap>(
301
+ search(
302
+ projectKey: string,
303
+ typeId: RepositoryTypes,
304
+ params: QueryParams
305
+ ): 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())
312
+
313
+ // Apply predicates
314
+ if (params.where) {
315
+ try {
316
+ const filterFunc = parseQueryExpression(params.where)
317
+ resources = resources.filter(resource => filterFunc(resource, {}))
318
+ } catch (err) {
319
+ throw new CommercetoolsError<InvalidInputError>(
320
+ {
321
+ code: 'InvalidInput',
322
+ message: (err as any).message,
323
+ },
324
+ 400
325
+ )
326
+ }
327
+ }
328
+
329
+ // Get the total before slicing the array
330
+ const totalResources = resources.length
331
+
332
+ // Apply offset, limit
333
+ const offset = params.offset || 0
334
+ const limit = params.limit || 20
335
+ resources = resources.slice(offset, offset + limit)
336
+
337
+ // Expand the resources
338
+ if (params.expand !== undefined) {
339
+ resources = resources.map(resource => {
340
+ return this.expand(projectKey, resource, params.expand)
341
+ })
342
+ }
343
+
344
+ return {
345
+ count: totalResources,
346
+ total: resources.length,
347
+ offset: offset,
348
+ limit: limit,
349
+ results: resources,
350
+ }
351
+ }
352
+
353
+ getByResourceIdentifier<RT extends RepositoryTypes>(
296
354
  projectKey: string,
297
355
  identifier: ResourceIdentifier
298
- ): ResourceMap[RepositoryTypes] | undefined {
356
+ ): ResourceMap[RT] | undefined {
299
357
  if (identifier.id) {
300
358
  const resource = this.get(projectKey, identifier.typeId, identifier.id)
301
359
  if (resource) {
302
- return resource as ResourceMap[RepositoryTypes]
360
+ return resource as ResourceMap[RT]
303
361
  }
304
362
  console.error(
305
363
  `No resource found with typeId=${identifier.typeId}, id=${identifier.id}`
@@ -318,7 +376,7 @@ export class InMemoryStorage extends AbstractStorage {
318
376
  r => r.key === identifier.key
319
377
  )
320
378
  if (resource) {
321
- return resource as ResourceMap[RepositoryTypes]
379
+ return resource as ResourceMap[RT]
322
380
  }
323
381
  } else {
324
382
  throw new Error(
@@ -369,7 +427,7 @@ export class InMemoryStorage extends AbstractStorage {
369
427
  return this.addProject(projectKey)
370
428
  }
371
429
 
372
- private expand = <T>(
430
+ public expand = <T>(
373
431
  projectKey: string,
374
432
  obj: T,
375
433
  clause: undefined | string | string[]
package/src/types.ts CHANGED
@@ -14,16 +14,21 @@ import { ProductTypeRepository } from './repositories/product-type'
14
14
  import { ShippingMethodRepository } from './repositories/shipping-method'
15
15
  import { StateRepository } from './repositories/state'
16
16
  import { TaxCategoryRepository } from './repositories/tax-category'
17
+ import { ProductDiscountRepository } from 'repositories/product-discount'
17
18
 
18
19
  export type Writable<T> = { -readonly [P in keyof T]: Writable<T[P]> }
19
20
 
20
- export type RepositoryTypes = ReferenceTypeId | 'product-projection'
21
+ export type RepositoryTypes =
22
+ | ReferenceTypeId
23
+ | 'standalone-price'
24
+ | 'product-projection'
21
25
  export type ServiceTypes =
22
26
  | RepositoryTypes
23
27
  | 'my-cart'
24
28
  | 'my-order'
25
29
  | 'my-payment'
26
30
  | 'my-customer'
31
+ | 'product-projection'
27
32
 
28
33
  export type Services = Partial<
29
34
  {
@@ -49,10 +54,12 @@ export type ResourceMap = {
49
54
  'product-discount': ctp.ProductDiscount
50
55
  'product-projection': ctp.ProductProjection
51
56
  'product-selection': ctp.ProductSelection
57
+ 'product-price': ctp.StandalonePrice
52
58
  'product-type': ctp.ProductType
53
59
  review: ctp.Review
54
60
  'shipping-method': ctp.ShippingMethod
55
61
  'shopping-list': ctp.ShoppingList
62
+ 'standalone-price': ctp.StandalonePrice
56
63
  state: ctp.State
57
64
  store: ctp.Store
58
65
  subscription: ctp.Subscription
@@ -105,7 +112,7 @@ export type RepositoryMap = {
105
112
  payment: PaymentRepository
106
113
  product: ProductRepository
107
114
  'product-projection': ProductProjectionRepository
108
- 'product-discount': never
115
+ 'product-discount': ProductDiscountRepository
109
116
  'product-type': ProductTypeRepository
110
117
  review: never
111
118
  'shipping-method': ShippingMethodRepository