@labdigital/commercetools-mock 0.6.5 → 0.8.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.
- package/dist/index.d.ts +409 -3
- package/dist/index.global.js +49983 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +4835 -6
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +4803 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -17
- package/src/ctMock.ts +5 -0
- package/src/helpers.ts +39 -0
- package/src/lib/projectionSearchFilter.test.ts +183 -0
- package/src/lib/projectionSearchFilter.ts +347 -0
- package/src/priceSelector.test.ts +96 -0
- package/src/priceSelector.ts +109 -0
- package/src/product-projection-search.ts +345 -0
- package/src/projectAPI.ts +19 -20
- package/src/repositories/category.ts +36 -0
- package/src/repositories/channel.ts +104 -0
- package/src/repositories/customer-group.ts +37 -0
- package/src/repositories/discount-code.ts +37 -0
- package/src/repositories/helpers.ts +46 -4
- package/src/repositories/product-discount.ts +181 -0
- package/src/repositories/product-projection.ts +30 -59
- package/src/repositories/product-type.ts +88 -6
- package/src/repositories/product.ts +49 -9
- package/src/repositories/shipping-method.ts +31 -0
- package/src/repositories/store.ts +43 -3
- package/src/repositories/type.ts +19 -0
- package/src/services/custom-object.test.ts +2 -2
- package/src/services/product-discount.ts +33 -0
- package/src/services/product-projection.test.ts +329 -107
- package/src/services/product.test.ts +12 -0
- package/src/storage.ts +116 -58
- package/src/types.ts +9 -2
- package/dist/commercetools-mock.cjs.development.js +0 -4382
- package/dist/commercetools-mock.cjs.development.js.map +0 -1
- package/dist/commercetools-mock.cjs.production.min.js +0 -2
- package/dist/commercetools-mock.cjs.production.min.js.map +0 -1
- package/dist/commercetools-mock.esm.js +0 -4374
- package/dist/commercetools-mock.esm.js.map +0 -1
- package/dist/constants.d.ts +0 -2
- package/dist/ctMock.d.ts +0 -32
- package/dist/exceptions.d.ts +0 -12
- package/dist/helpers.d.ts +0 -6
- package/dist/lib/expandParser.d.ts +0 -15
- package/dist/lib/filterParser.d.ts +0 -1
- package/dist/lib/haversine.d.ts +0 -8
- package/dist/lib/masking.d.ts +0 -1
- package/dist/lib/predicateParser.d.ts +0 -11
- package/dist/lib/proxy.d.ts +0 -1
- package/dist/oauth/errors.d.ts +0 -8
- package/dist/oauth/helpers.d.ts +0 -2
- package/dist/oauth/server.d.ts +0 -12
- package/dist/oauth/store.d.ts +0 -14
- package/dist/projectAPI.d.ts +0 -12
- package/dist/repositories/abstract.d.ts +0 -33
- package/dist/repositories/cart-discount.d.ts +0 -9
- package/dist/repositories/cart.d.ts +0 -21
- package/dist/repositories/category.d.ts +0 -18
- package/dist/repositories/channel.d.ts +0 -6
- package/dist/repositories/custom-object.d.ts +0 -8
- package/dist/repositories/customer-group.d.ts +0 -11
- package/dist/repositories/customer.d.ts +0 -11
- package/dist/repositories/discount-code.d.ts +0 -8
- package/dist/repositories/errors.d.ts +0 -2
- package/dist/repositories/extension.d.ts +0 -8
- package/dist/repositories/helpers.d.ts +0 -10
- package/dist/repositories/inventory-entry.d.ts +0 -14
- package/dist/repositories/my-order.d.ts +0 -6
- package/dist/repositories/order.d.ts +0 -26
- package/dist/repositories/payment.d.ts +0 -23
- package/dist/repositories/product-projection.d.ts +0 -10
- package/dist/repositories/product-type.d.ts +0 -10
- package/dist/repositories/product.d.ts +0 -11
- package/dist/repositories/project.d.ts +0 -8
- package/dist/repositories/shipping-method.d.ts +0 -10
- package/dist/repositories/shopping-list.d.ts +0 -6
- package/dist/repositories/state.d.ts +0 -8
- package/dist/repositories/store.d.ts +0 -10
- package/dist/repositories/subscription.d.ts +0 -6
- package/dist/repositories/tax-category.d.ts +0 -10
- package/dist/repositories/type.d.ts +0 -8
- package/dist/repositories/zone.d.ts +0 -8
- package/dist/server.d.ts +0 -1
- package/dist/services/abstract.d.ts +0 -20
- package/dist/services/cart-discount.d.ts +0 -9
- package/dist/services/cart.d.ts +0 -12
- package/dist/services/category.d.ts +0 -9
- package/dist/services/channel.d.ts +0 -9
- package/dist/services/custom-object.d.ts +0 -13
- package/dist/services/customer-group.d.ts +0 -9
- package/dist/services/customer.d.ts +0 -10
- package/dist/services/discount-code.d.ts +0 -9
- package/dist/services/extension.d.ts +0 -9
- package/dist/services/inventory-entry.d.ts +0 -9
- package/dist/services/my-cart.d.ts +0 -11
- package/dist/services/my-customer.d.ts +0 -13
- package/dist/services/my-order.d.ts +0 -10
- package/dist/services/my-payment.d.ts +0 -9
- package/dist/services/order.d.ts +0 -12
- package/dist/services/payment.d.ts +0 -9
- package/dist/services/product-projection.d.ts +0 -11
- package/dist/services/product-type.d.ts +0 -11
- package/dist/services/product.d.ts +0 -9
- package/dist/services/project.d.ts +0 -11
- package/dist/services/shipping-method.d.ts +0 -10
- package/dist/services/shopping-list.d.ts +0 -9
- package/dist/services/state.d.ts +0 -9
- package/dist/services/store.d.ts +0 -11
- package/dist/services/subscription.d.ts +0 -9
- package/dist/services/tax-category.d.ts +0 -11
- package/dist/services/type.d.ts +0 -9
- package/dist/services/zone.d.ts +0 -9
- package/dist/storage.d.ts +0 -56
- package/dist/types.d.ts +0 -89
- package/dist/validate.d.ts +0 -7482
- package/src/lib/filterParser.test.ts +0 -15
- 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
|
|
59
|
+
abstract all<RT extends RepositoryTypes>(
|
|
60
|
+
projectKey: string,
|
|
61
|
+
typeId: RT
|
|
62
|
+
): Array<ResourceMap[RT]>
|
|
59
63
|
|
|
60
|
-
abstract add<
|
|
64
|
+
abstract add<RT extends RepositoryTypes>(
|
|
61
65
|
projectKey: string,
|
|
62
|
-
typeId:
|
|
63
|
-
obj: ResourceMap[
|
|
66
|
+
typeId: RT,
|
|
67
|
+
obj: ResourceMap[RT]
|
|
64
68
|
): void
|
|
65
69
|
|
|
66
|
-
abstract get<
|
|
70
|
+
abstract get<RT extends RepositoryTypes>(
|
|
67
71
|
projectKey: string,
|
|
68
|
-
typeId:
|
|
72
|
+
typeId: RT,
|
|
69
73
|
id: string,
|
|
70
74
|
params?: GetParams
|
|
71
|
-
): ResourceMap[
|
|
75
|
+
): ResourceMap[RT] | null
|
|
72
76
|
|
|
73
|
-
abstract getByKey<
|
|
77
|
+
abstract getByKey<RT extends RepositoryTypes>(
|
|
74
78
|
projectKey: string,
|
|
75
|
-
typeId:
|
|
79
|
+
typeId: RT,
|
|
76
80
|
key: string,
|
|
77
81
|
params: GetParams
|
|
78
|
-
): ResourceMap[
|
|
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:
|
|
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
|
-
):
|
|
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 =
|
|
104
|
-
|
|
105
|
-
|
|
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
|
|
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<
|
|
187
|
+
add<RT extends RepositoryTypes>(
|
|
172
188
|
projectKey: string,
|
|
173
|
-
typeId:
|
|
174
|
-
obj: ResourceMap[
|
|
189
|
+
typeId: RT,
|
|
190
|
+
obj: ResourceMap[RT],
|
|
175
191
|
params: GetParams = {}
|
|
176
|
-
): ResourceMap[
|
|
177
|
-
this.forProjectKey(projectKey)
|
|
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<
|
|
201
|
+
get<RT extends RepositoryTypes>(
|
|
185
202
|
projectKey: string,
|
|
186
|
-
typeId:
|
|
203
|
+
typeId: RT,
|
|
187
204
|
id: string,
|
|
188
205
|
params: GetParams = {}
|
|
189
|
-
): ResourceMap[
|
|
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<
|
|
214
|
+
getByKey<RT extends RepositoryTypes>(
|
|
202
215
|
projectKey: string,
|
|
203
|
-
typeId:
|
|
216
|
+
typeId: RT,
|
|
204
217
|
key: string,
|
|
205
218
|
params: GetParams = {}
|
|
206
|
-
): ResourceMap[
|
|
207
|
-
const store = this.forProjectKey(projectKey)
|
|
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(
|
|
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:
|
|
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
|
-
|
|
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[
|
|
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[
|
|
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[
|
|
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
|
-
|
|
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 =
|
|
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':
|
|
115
|
+
'product-discount': ProductDiscountRepository
|
|
109
116
|
'product-type': ProductTypeRepository
|
|
110
117
|
review: never
|
|
111
118
|
'shipping-method': ShippingMethodRepository
|