@labdigital/commercetools-mock 0.9.1 → 0.10.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.
- package/README.md +8 -0
- package/dist/index.d.ts +354 -188
- package/dist/index.global.js +2346 -2209
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +1968 -1829
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2171 -2032
- package/dist/index.mjs.map +1 -1
- package/package.json +30 -21
- package/src/constants.ts +4 -2
- package/src/ctMock.ts +27 -86
- package/src/helpers.ts +10 -11
- package/src/index.test.ts +1 -1
- package/src/lib/haversine.ts +2 -2
- package/src/lib/masking.ts +3 -1
- package/src/lib/predicateParser.ts +93 -92
- package/src/lib/projectionSearchFilter.test.ts +28 -36
- package/src/lib/projectionSearchFilter.ts +88 -103
- package/src/oauth/store.ts +3 -3
- package/src/priceSelector.test.ts +16 -35
- package/src/priceSelector.ts +6 -9
- package/src/product-projection-search.ts +49 -57
- package/src/projectAPI.test.ts +7 -0
- package/src/projectAPI.ts +17 -22
- package/src/repositories/abstract.ts +102 -51
- package/src/repositories/cart-discount.ts +4 -5
- package/src/repositories/cart.ts +56 -46
- package/src/repositories/category.ts +23 -26
- package/src/repositories/channel.ts +5 -6
- package/src/repositories/custom-object.ts +41 -32
- package/src/repositories/customer-group.ts +4 -5
- package/src/repositories/customer.ts +42 -5
- package/src/repositories/discount-code.ts +5 -6
- package/src/repositories/errors.ts +10 -14
- package/src/repositories/extension.ts +16 -15
- package/src/repositories/helpers.ts +10 -15
- package/src/repositories/index.ts +75 -0
- package/src/repositories/inventory-entry.ts +5 -6
- package/src/repositories/my-order.ts +2 -2
- package/src/repositories/order-edit.ts +39 -0
- package/src/repositories/order.test.ts +16 -11
- package/src/repositories/order.ts +21 -14
- package/src/repositories/payment.ts +9 -10
- package/src/repositories/product-discount.ts +5 -25
- package/src/repositories/product-projection.ts +12 -5
- package/src/repositories/product-selection.ts +40 -0
- package/src/repositories/product-type.ts +38 -60
- package/src/repositories/product.ts +128 -85
- package/src/repositories/project.ts +16 -33
- package/src/repositories/quote-request.ts +28 -0
- package/src/repositories/quote.ts +28 -0
- package/src/repositories/review.ts +34 -0
- package/src/repositories/shipping-method.ts +25 -28
- package/src/repositories/shopping-list.ts +6 -6
- package/src/repositories/staged-quote.ts +29 -0
- package/src/repositories/standalone-price.ts +36 -0
- package/src/repositories/state.ts +16 -17
- package/src/repositories/store.ts +13 -29
- package/src/repositories/subscription.ts +4 -5
- package/src/repositories/tax-category.ts +9 -26
- package/src/repositories/type.ts +24 -27
- package/src/repositories/zone.ts +9 -11
- package/src/server.ts +5 -0
- package/src/services/abstract.ts +43 -12
- package/src/services/cart-discount.ts +3 -4
- package/src/services/cart.test.ts +9 -11
- package/src/services/cart.ts +42 -38
- package/src/services/category.test.ts +1 -2
- package/src/services/category.ts +3 -4
- package/src/services/channel.ts +3 -4
- package/src/services/custom-object.test.ts +6 -6
- package/src/services/custom-object.ts +4 -5
- package/src/services/customer-group.ts +3 -4
- package/src/services/customer.test.ts +136 -0
- package/src/services/customer.ts +5 -6
- package/src/services/discount-code.ts +3 -4
- package/src/services/extension.ts +3 -4
- package/src/services/index.ts +74 -0
- package/src/services/inventory-entry.test.ts +9 -13
- package/src/services/inventory-entry.ts +3 -4
- package/src/services/my-cart.test.ts +2 -0
- package/src/services/my-cart.ts +4 -5
- package/src/services/my-customer.ts +3 -4
- package/src/services/my-order.ts +4 -5
- package/src/services/my-payment.ts +3 -4
- package/src/services/order.test.ts +28 -26
- package/src/services/order.ts +4 -5
- package/src/services/payment.ts +3 -4
- package/src/services/product-discount.ts +3 -20
- package/src/services/product-projection.test.ts +76 -8
- package/src/services/product-projection.ts +4 -5
- package/src/services/product-type.ts +3 -20
- package/src/services/product.test.ts +200 -90
- package/src/services/product.ts +3 -4
- package/src/services/project.ts +5 -6
- package/src/services/shipping-method.ts +3 -4
- package/src/services/shopping-list.ts +3 -4
- package/src/services/state.ts +3 -4
- package/src/services/store.test.ts +11 -2
- package/src/services/store.ts +4 -21
- package/src/services/subscription.ts +3 -4
- package/src/services/tax-category.ts +3 -20
- package/src/services/type.ts +3 -4
- package/src/services/zone.ts +3 -4
- package/src/storage/abstract.ts +82 -0
- package/src/{storage.ts → storage/in-memory.ts} +79 -147
- package/src/storage/index.ts +2 -0
- package/src/types.ts +52 -83
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseResource,
|
|
3
|
+
Project,
|
|
4
|
+
QueryParam,
|
|
5
|
+
ResourceIdentifier
|
|
6
|
+
} from '@commercetools/platform-sdk'
|
|
7
|
+
import { PagedQueryResponseMap, ResourceMap, ResourceType } from '../types'
|
|
8
|
+
|
|
9
|
+
export type GetParams = {
|
|
10
|
+
expand?: string[]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type QueryParams = {
|
|
14
|
+
expand?: string | string[]
|
|
15
|
+
sort?: string | string[]
|
|
16
|
+
limit?: number
|
|
17
|
+
offset?: number
|
|
18
|
+
withTotal?: boolean
|
|
19
|
+
where?: string | string[]
|
|
20
|
+
[key: string]: QueryParam
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export abstract class AbstractStorage {
|
|
24
|
+
abstract clear(): void
|
|
25
|
+
|
|
26
|
+
abstract all<RT extends ResourceType>(
|
|
27
|
+
projectKey: string,
|
|
28
|
+
typeId: RT
|
|
29
|
+
): Array<ResourceMap[RT]>
|
|
30
|
+
|
|
31
|
+
abstract add<RT extends ResourceType>(
|
|
32
|
+
projectKey: string,
|
|
33
|
+
typeId: RT,
|
|
34
|
+
obj: ResourceMap[RT]
|
|
35
|
+
): void
|
|
36
|
+
|
|
37
|
+
abstract get<RT extends ResourceType>(
|
|
38
|
+
projectKey: string,
|
|
39
|
+
typeId: RT,
|
|
40
|
+
id: string,
|
|
41
|
+
params?: GetParams
|
|
42
|
+
): ResourceMap[RT] | null
|
|
43
|
+
|
|
44
|
+
abstract getByKey<RT extends ResourceType>(
|
|
45
|
+
projectKey: string,
|
|
46
|
+
typeId: RT,
|
|
47
|
+
key: string,
|
|
48
|
+
params: GetParams
|
|
49
|
+
): ResourceMap[RT] | null
|
|
50
|
+
|
|
51
|
+
abstract addProject(projectKey: string): Project
|
|
52
|
+
abstract getProject(projectKey: string): Project
|
|
53
|
+
abstract saveProject(project: Project): Project
|
|
54
|
+
|
|
55
|
+
abstract delete<RT extends ResourceType>(
|
|
56
|
+
projectKey: string,
|
|
57
|
+
typeId: RT,
|
|
58
|
+
id: string,
|
|
59
|
+
params: GetParams
|
|
60
|
+
): ResourceMap[RT] | null
|
|
61
|
+
|
|
62
|
+
abstract query<RT extends ResourceType>(
|
|
63
|
+
projectKey: string,
|
|
64
|
+
typeId: RT,
|
|
65
|
+
params: QueryParams
|
|
66
|
+
): PagedQueryResponseMap[RT]
|
|
67
|
+
|
|
68
|
+
abstract getByResourceIdentifier<RT extends ResourceType>(
|
|
69
|
+
projectKey: string,
|
|
70
|
+
identifier: ResourceIdentifier
|
|
71
|
+
): ResourceMap[RT] | null
|
|
72
|
+
|
|
73
|
+
abstract expand<T>(
|
|
74
|
+
projectKey: string,
|
|
75
|
+
obj: T,
|
|
76
|
+
clause: undefined | string | string[]
|
|
77
|
+
): T
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type ProjectStorage = {
|
|
81
|
+
[index in ResourceType]: Map<string, BaseResource>
|
|
82
|
+
}
|
|
@@ -1,118 +1,54 @@
|
|
|
1
|
-
import assert from 'assert'
|
|
2
1
|
import {
|
|
3
|
-
BaseResource,
|
|
4
2
|
Cart,
|
|
3
|
+
CartDiscount,
|
|
5
4
|
Category,
|
|
5
|
+
Channel,
|
|
6
6
|
Customer,
|
|
7
|
+
CustomerGroup,
|
|
7
8
|
CustomObject,
|
|
9
|
+
DiscountCode,
|
|
10
|
+
Extension,
|
|
11
|
+
InvalidInputError,
|
|
8
12
|
InventoryEntry,
|
|
9
13
|
Order,
|
|
10
14
|
PagedQueryResponse,
|
|
11
|
-
QueryParam,
|
|
12
|
-
Reference,
|
|
13
|
-
ResourceIdentifier,
|
|
14
|
-
Product,
|
|
15
|
-
Store,
|
|
16
|
-
Type,
|
|
17
15
|
Payment,
|
|
16
|
+
Product,
|
|
17
|
+
ProductDiscount,
|
|
18
|
+
ProductProjection,
|
|
19
|
+
ProductType,
|
|
18
20
|
Project,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
Quote,
|
|
22
|
+
QuoteRequest,
|
|
23
|
+
Reference,
|
|
24
|
+
ResourceIdentifier,
|
|
21
25
|
ShippingMethod,
|
|
22
|
-
ProductType,
|
|
23
|
-
InvalidInputError,
|
|
24
|
-
ProductProjection,
|
|
25
26
|
ShoppingList,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
DiscountCode,
|
|
30
|
-
Zone,
|
|
31
|
-
Channel,
|
|
27
|
+
StagedQuote,
|
|
28
|
+
State,
|
|
29
|
+
Store,
|
|
32
30
|
Subscription,
|
|
33
|
-
|
|
31
|
+
TaxCategory,
|
|
32
|
+
Type,
|
|
33
|
+
Zone,
|
|
34
34
|
} from '@commercetools/platform-sdk'
|
|
35
|
-
import
|
|
36
|
-
import {
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export abstract class AbstractStorage {
|
|
55
|
-
abstract clear(): void
|
|
56
|
-
|
|
57
|
-
abstract assertStorage(typeId: RepositoryTypes): void
|
|
58
|
-
|
|
59
|
-
abstract all<RT extends RepositoryTypes>(
|
|
60
|
-
projectKey: string,
|
|
61
|
-
typeId: RT
|
|
62
|
-
): Array<ResourceMap[RT]>
|
|
63
|
-
|
|
64
|
-
abstract add<RT extends RepositoryTypes>(
|
|
65
|
-
projectKey: string,
|
|
66
|
-
typeId: RT,
|
|
67
|
-
obj: ResourceMap[RT]
|
|
68
|
-
): void
|
|
69
|
-
|
|
70
|
-
abstract get<RT extends RepositoryTypes>(
|
|
71
|
-
projectKey: string,
|
|
72
|
-
typeId: RT,
|
|
73
|
-
id: string,
|
|
74
|
-
params?: GetParams
|
|
75
|
-
): ResourceMap[RT] | null
|
|
76
|
-
|
|
77
|
-
abstract getByKey<RT extends RepositoryTypes>(
|
|
78
|
-
projectKey: string,
|
|
79
|
-
typeId: RT,
|
|
80
|
-
key: string,
|
|
81
|
-
params: GetParams
|
|
82
|
-
): ResourceMap[RT] | null
|
|
83
|
-
|
|
84
|
-
abstract addProject(projectKey: string): Project
|
|
85
|
-
abstract getProject(projectKey: string): Project
|
|
86
|
-
abstract saveProject(project: Project): Project
|
|
87
|
-
|
|
88
|
-
abstract delete<RT extends RepositoryTypes>(
|
|
89
|
-
projectKey: string,
|
|
90
|
-
typeId: RT,
|
|
91
|
-
id: string,
|
|
92
|
-
params: GetParams
|
|
93
|
-
): BaseResource | null
|
|
94
|
-
|
|
95
|
-
abstract query(
|
|
96
|
-
projectKey: string,
|
|
97
|
-
typeId: RepositoryTypes,
|
|
98
|
-
params: QueryParams
|
|
99
|
-
): PagedQueryResponse
|
|
100
|
-
|
|
101
|
-
abstract getByResourceIdentifier<RT extends RepositoryTypes>(
|
|
102
|
-
projectKey: string,
|
|
103
|
-
identifier: ResourceIdentifier
|
|
104
|
-
): ResourceMap[RT] | undefined
|
|
105
|
-
|
|
106
|
-
abstract expand<T>(
|
|
107
|
-
projectKey: string,
|
|
108
|
-
obj: T,
|
|
109
|
-
clause: undefined | string | string[]
|
|
110
|
-
): T
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
type ProjectStorage = {
|
|
114
|
-
[index in RepositoryTypes]: Map<string, BaseResource>
|
|
115
|
-
}
|
|
35
|
+
import assert from 'assert'
|
|
36
|
+
import { CommercetoolsError } from '../exceptions'
|
|
37
|
+
import { cloneObject } from '../helpers'
|
|
38
|
+
import { parseExpandClause } from '../lib/expandParser'
|
|
39
|
+
import { parseQueryExpression } from '../lib/predicateParser'
|
|
40
|
+
import {
|
|
41
|
+
PagedQueryResponseMap,
|
|
42
|
+
ResourceMap,
|
|
43
|
+
ResourceType,
|
|
44
|
+
Writable,
|
|
45
|
+
} from '../types'
|
|
46
|
+
import {
|
|
47
|
+
AbstractStorage,
|
|
48
|
+
GetParams,
|
|
49
|
+
ProjectStorage,
|
|
50
|
+
QueryParams,
|
|
51
|
+
} from './abstract'
|
|
116
52
|
|
|
117
53
|
export class InMemoryStorage extends AbstractStorage {
|
|
118
54
|
protected resources: {
|
|
@@ -143,13 +79,15 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
143
79
|
'order-edit': new Map<string, any>(),
|
|
144
80
|
payment: new Map<string, Payment>(),
|
|
145
81
|
product: new Map<string, Product>(),
|
|
82
|
+
quote: new Map<string, Quote>(),
|
|
83
|
+
'quote-request': new Map<string, QuoteRequest>(),
|
|
146
84
|
'product-discount': new Map<string, ProductDiscount>(),
|
|
147
|
-
'product-price': new Map<string, any>(),
|
|
148
85
|
'product-selection': new Map<string, any>(),
|
|
149
86
|
'product-type': new Map<string, ProductType>(),
|
|
150
87
|
'product-projection': new Map<string, ProductProjection>(),
|
|
151
88
|
review: new Map<string, any>(),
|
|
152
89
|
'shipping-method': new Map<string, ShippingMethod>(),
|
|
90
|
+
'staged-quote': new Map<string, StagedQuote>(),
|
|
153
91
|
state: new Map<string, State>(),
|
|
154
92
|
store: new Map<string, Store>(),
|
|
155
93
|
'shopping-list': new Map<string, ShoppingList>(),
|
|
@@ -171,20 +109,18 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
171
109
|
}
|
|
172
110
|
}
|
|
173
111
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
all<RT extends RepositoryTypes>(
|
|
112
|
+
all<RT extends ResourceType>(
|
|
177
113
|
projectKey: string,
|
|
178
114
|
typeId: RT
|
|
179
115
|
): ResourceMap[RT][] {
|
|
180
116
|
const store = this.forProjectKey(projectKey)[typeId]
|
|
181
117
|
if (store) {
|
|
182
|
-
return Array.from(store.values()) as ResourceMap[RT][]
|
|
118
|
+
return Array.from(store.values()).map(cloneObject) as ResourceMap[RT][]
|
|
183
119
|
}
|
|
184
120
|
return []
|
|
185
121
|
}
|
|
186
122
|
|
|
187
|
-
add<RT extends
|
|
123
|
+
add<RT extends ResourceType>(
|
|
188
124
|
projectKey: string,
|
|
189
125
|
typeId: RT,
|
|
190
126
|
obj: ResourceMap[RT],
|
|
@@ -195,10 +131,10 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
195
131
|
|
|
196
132
|
const resource = this.get(projectKey, typeId, obj.id, params)
|
|
197
133
|
assert(resource, `resource of type ${typeId} with id ${obj.id} not created`)
|
|
198
|
-
return resource
|
|
134
|
+
return cloneObject(resource)
|
|
199
135
|
}
|
|
200
136
|
|
|
201
|
-
get<RT extends
|
|
137
|
+
get<RT extends ResourceType>(
|
|
202
138
|
projectKey: string,
|
|
203
139
|
typeId: RT,
|
|
204
140
|
id: string,
|
|
@@ -206,12 +142,13 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
206
142
|
): ResourceMap[RT] | null {
|
|
207
143
|
const resource = this.forProjectKey(projectKey)[typeId]?.get(id)
|
|
208
144
|
if (resource) {
|
|
209
|
-
|
|
145
|
+
const clone = cloneObject(resource)
|
|
146
|
+
return this.expand(projectKey, clone, params.expand) as ResourceMap[RT]
|
|
210
147
|
}
|
|
211
148
|
return null
|
|
212
149
|
}
|
|
213
150
|
|
|
214
|
-
getByKey<RT extends
|
|
151
|
+
getByKey<RT extends ResourceType>(
|
|
215
152
|
projectKey: string,
|
|
216
153
|
typeId: RT,
|
|
217
154
|
key: string,
|
|
@@ -224,45 +161,46 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
224
161
|
}
|
|
225
162
|
|
|
226
163
|
const resources: any[] = Array.from(resourceStore.values())
|
|
227
|
-
const resource = resources.find(e => e.key === key)
|
|
164
|
+
const resource = resources.find((e) => e.key === key)
|
|
228
165
|
if (resource) {
|
|
229
|
-
|
|
166
|
+
const clone = cloneObject(resource)
|
|
167
|
+
return this.expand(projectKey, clone, params.expand) as ResourceMap[RT]
|
|
230
168
|
}
|
|
231
169
|
return null
|
|
232
170
|
}
|
|
233
171
|
|
|
234
|
-
delete<RT extends
|
|
172
|
+
delete<RT extends ResourceType>(
|
|
235
173
|
projectKey: string,
|
|
236
174
|
typeId: RT,
|
|
237
175
|
id: string,
|
|
238
176
|
params: GetParams = {}
|
|
239
|
-
):
|
|
177
|
+
): ResourceMap[RT] | null {
|
|
240
178
|
const resource = this.get(projectKey, typeId, id)
|
|
241
179
|
|
|
242
180
|
if (resource) {
|
|
243
181
|
this.forProjectKey(projectKey)[typeId]?.delete(id)
|
|
244
|
-
return this.expand(projectKey, resource, params.expand)
|
|
182
|
+
return this.expand(projectKey, resource, params.expand)
|
|
245
183
|
}
|
|
246
184
|
return resource
|
|
247
185
|
}
|
|
248
186
|
|
|
249
|
-
query(
|
|
187
|
+
query<RT extends ResourceType>(
|
|
250
188
|
projectKey: string,
|
|
251
|
-
typeId:
|
|
189
|
+
typeId: RT,
|
|
252
190
|
params: QueryParams
|
|
253
|
-
):
|
|
191
|
+
): PagedQueryResponseMap[RT] {
|
|
254
192
|
const store = this.forProjectKey(projectKey)[typeId]
|
|
255
193
|
if (!store) {
|
|
256
194
|
throw new Error('No type')
|
|
257
195
|
}
|
|
258
196
|
|
|
259
|
-
let resources =
|
|
197
|
+
let resources = this.all<RT>(projectKey, typeId)
|
|
260
198
|
|
|
261
199
|
// Apply predicates
|
|
262
200
|
if (params.where) {
|
|
263
201
|
try {
|
|
264
202
|
const filterFunc = parseQueryExpression(params.where)
|
|
265
|
-
resources = resources.filter(resource => filterFunc(resource, {}))
|
|
203
|
+
resources = resources.filter((resource) => filterFunc(resource, {}))
|
|
266
204
|
} catch (err) {
|
|
267
205
|
throw new CommercetoolsError<InvalidInputError>(
|
|
268
206
|
{
|
|
@@ -284,9 +222,9 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
284
222
|
|
|
285
223
|
// Expand the resources
|
|
286
224
|
if (params.expand !== undefined) {
|
|
287
|
-
resources = resources.map(resource =>
|
|
288
|
-
|
|
289
|
-
|
|
225
|
+
resources = resources.map((resource) =>
|
|
226
|
+
this.expand(projectKey, resource, params.expand)
|
|
227
|
+
)
|
|
290
228
|
}
|
|
291
229
|
|
|
292
230
|
return {
|
|
@@ -294,27 +232,22 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
294
232
|
total: resources.length,
|
|
295
233
|
offset: offset,
|
|
296
234
|
limit: limit,
|
|
297
|
-
results: resources,
|
|
298
|
-
}
|
|
235
|
+
results: resources.map(cloneObject),
|
|
236
|
+
} as PagedQueryResponseMap[RT]
|
|
299
237
|
}
|
|
300
238
|
|
|
301
239
|
search(
|
|
302
240
|
projectKey: string,
|
|
303
|
-
typeId:
|
|
241
|
+
typeId: ResourceType,
|
|
304
242
|
params: QueryParams
|
|
305
243
|
): PagedQueryResponse {
|
|
306
|
-
|
|
307
|
-
if (!store) {
|
|
308
|
-
throw new Error('No type')
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
let resources = Array.from(store.values())
|
|
244
|
+
let resources = this.all(projectKey, typeId)
|
|
312
245
|
|
|
313
246
|
// Apply predicates
|
|
314
247
|
if (params.where) {
|
|
315
248
|
try {
|
|
316
249
|
const filterFunc = parseQueryExpression(params.where)
|
|
317
|
-
resources = resources.filter(resource => filterFunc(resource, {}))
|
|
250
|
+
resources = resources.filter((resource) => filterFunc(resource, {}))
|
|
318
251
|
} catch (err) {
|
|
319
252
|
throw new CommercetoolsError<InvalidInputError>(
|
|
320
253
|
{
|
|
@@ -336,9 +269,9 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
336
269
|
|
|
337
270
|
// Expand the resources
|
|
338
271
|
if (params.expand !== undefined) {
|
|
339
|
-
resources = resources.map(resource =>
|
|
340
|
-
|
|
341
|
-
|
|
272
|
+
resources = resources.map((resource) =>
|
|
273
|
+
this.expand(projectKey, resource, params.expand)
|
|
274
|
+
)
|
|
342
275
|
}
|
|
343
276
|
|
|
344
277
|
return {
|
|
@@ -350,10 +283,10 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
350
283
|
}
|
|
351
284
|
}
|
|
352
285
|
|
|
353
|
-
getByResourceIdentifier<RT extends
|
|
286
|
+
getByResourceIdentifier<RT extends ResourceType>(
|
|
354
287
|
projectKey: string,
|
|
355
288
|
identifier: ResourceIdentifier
|
|
356
|
-
): ResourceMap[RT] |
|
|
289
|
+
): ResourceMap[RT] | null {
|
|
357
290
|
if (identifier.id) {
|
|
358
291
|
const resource = this.get(projectKey, identifier.typeId, identifier.id)
|
|
359
292
|
if (resource) {
|
|
@@ -362,7 +295,7 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
362
295
|
console.error(
|
|
363
296
|
`No resource found with typeId=${identifier.typeId}, id=${identifier.id}`
|
|
364
297
|
)
|
|
365
|
-
return
|
|
298
|
+
return null
|
|
366
299
|
}
|
|
367
300
|
|
|
368
301
|
if (identifier.key) {
|
|
@@ -373,7 +306,7 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
373
306
|
// have them all.
|
|
374
307
|
const resource = Array.from(store.values()).find(
|
|
375
308
|
// @ts-ignore
|
|
376
|
-
r => r.key === identifier.key
|
|
309
|
+
(r) => r.key === identifier.key
|
|
377
310
|
)
|
|
378
311
|
if (resource) {
|
|
379
312
|
return resource as ResourceMap[RT]
|
|
@@ -384,7 +317,7 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
384
317
|
)
|
|
385
318
|
}
|
|
386
319
|
}
|
|
387
|
-
return
|
|
320
|
+
return null
|
|
388
321
|
}
|
|
389
322
|
|
|
390
323
|
addProject = (projectKey: string): Project => {
|
|
@@ -423,19 +356,18 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
423
356
|
return project
|
|
424
357
|
}
|
|
425
358
|
|
|
426
|
-
getProject = (projectKey: string): Project =>
|
|
427
|
-
return this.addProject(projectKey)
|
|
428
|
-
}
|
|
359
|
+
getProject = (projectKey: string): Project => this.addProject(projectKey)
|
|
429
360
|
|
|
361
|
+
// Expand resolves a nested reference and injects the object in the given obj
|
|
430
362
|
public expand = <T>(
|
|
431
363
|
projectKey: string,
|
|
432
364
|
obj: T,
|
|
433
365
|
clause: undefined | string | string[]
|
|
434
366
|
): T => {
|
|
435
367
|
if (!clause) return obj
|
|
436
|
-
const newObj =
|
|
368
|
+
const newObj = cloneObject(obj)
|
|
437
369
|
if (Array.isArray(clause)) {
|
|
438
|
-
clause.forEach(c => {
|
|
370
|
+
clause.forEach((c) => {
|
|
439
371
|
this._resolveResource(projectKey, newObj, c)
|
|
440
372
|
})
|
|
441
373
|
} else {
|
package/src/types.ts
CHANGED
|
@@ -1,64 +1,53 @@
|
|
|
1
|
-
import { ProductProjectionRepository } from './repositories/product-projection'
|
|
2
|
-
import { ShoppingListRepository } from './repositories/shopping-list'
|
|
3
1
|
import * as ctp from '@commercetools/platform-sdk'
|
|
4
|
-
import {
|
|
2
|
+
import { RepositoryMap } from './repositories'
|
|
5
3
|
import AbstractService from './services/abstract'
|
|
6
|
-
import { CartRepository } from './repositories/cart'
|
|
7
|
-
import { CustomerRepository } from './repositories/customer'
|
|
8
|
-
import { CustomObjectRepository } from './repositories/custom-object'
|
|
9
|
-
import { InventoryEntryRepository } from './repositories/inventory-entry'
|
|
10
|
-
import { OrderRepository } from './repositories/order'
|
|
11
|
-
import { PaymentRepository } from './repositories/payment'
|
|
12
|
-
import { ProductRepository } from './repositories/product'
|
|
13
|
-
import { ProductTypeRepository } from './repositories/product-type'
|
|
14
|
-
import { ShippingMethodRepository } from './repositories/shipping-method'
|
|
15
|
-
import { StateRepository } from './repositories/state'
|
|
16
|
-
import { TaxCategoryRepository } from './repositories/tax-category'
|
|
17
|
-
import { ProductDiscountRepository } from 'repositories/product-discount'
|
|
18
4
|
|
|
19
5
|
export type Writable<T> = { -readonly [P in keyof T]: Writable<T[P]> }
|
|
6
|
+
export type ShallowWritable<T> = { -readonly [P in keyof T]: T[P] }
|
|
20
7
|
|
|
21
|
-
export type RepositoryTypes =
|
|
22
|
-
| ReferenceTypeId
|
|
23
|
-
| 'standalone-price'
|
|
24
|
-
| 'product-projection'
|
|
25
8
|
export type ServiceTypes =
|
|
26
|
-
|
|
|
9
|
+
| ctp.ReferenceTypeId
|
|
10
|
+
| 'product-projection'
|
|
27
11
|
| 'my-cart'
|
|
28
12
|
| 'my-order'
|
|
29
13
|
| 'my-payment'
|
|
30
14
|
| 'my-customer'
|
|
31
15
|
| 'product-projection'
|
|
32
16
|
|
|
33
|
-
export type Services = Partial<
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
17
|
+
export type Services = Partial<{
|
|
18
|
+
[index in ServiceTypes]: AbstractService
|
|
19
|
+
}>
|
|
20
|
+
|
|
21
|
+
export type ResourceType = keyof ResourceMap & keyof RepositoryMap
|
|
38
22
|
|
|
39
23
|
export type ResourceMap = {
|
|
40
|
-
cart: ctp.Cart
|
|
41
24
|
'cart-discount': ctp.CartDiscount
|
|
25
|
+
cart: ctp.Cart
|
|
42
26
|
category: ctp.Category
|
|
43
27
|
channel: ctp.Channel
|
|
44
|
-
customer:
|
|
28
|
+
'customer-email-token': never
|
|
45
29
|
'customer-group': ctp.CustomerGroup
|
|
30
|
+
'customer-password-token': never
|
|
31
|
+
customer: ctp.Customer
|
|
46
32
|
'discount-code': ctp.DiscountCode
|
|
47
33
|
extension: ctp.Extension
|
|
48
34
|
'inventory-entry': ctp.InventoryEntry
|
|
49
35
|
'key-value-document': ctp.CustomObject
|
|
50
|
-
order: ctp.Order
|
|
51
36
|
'order-edit': ctp.OrderEdit
|
|
37
|
+
order: ctp.Order
|
|
52
38
|
payment: ctp.Payment
|
|
53
|
-
product: ctp.Product
|
|
54
39
|
'product-discount': ctp.ProductDiscount
|
|
40
|
+
'product-price': ctp.StandalonePrice
|
|
55
41
|
'product-projection': ctp.ProductProjection
|
|
56
42
|
'product-selection': ctp.ProductSelection
|
|
57
|
-
'product-price': ctp.StandalonePrice
|
|
58
43
|
'product-type': ctp.ProductType
|
|
44
|
+
product: ctp.Product
|
|
45
|
+
'quote-request': ctp.QuoteRequest
|
|
46
|
+
quote: ctp.Quote
|
|
59
47
|
review: ctp.Review
|
|
60
48
|
'shipping-method': ctp.ShippingMethod
|
|
61
49
|
'shopping-list': ctp.ShoppingList
|
|
50
|
+
'staged-quote': ctp.StagedQuote
|
|
62
51
|
'standalone-price': ctp.StandalonePrice
|
|
63
52
|
state: ctp.State
|
|
64
53
|
store: ctp.Store
|
|
@@ -66,61 +55,41 @@ export type ResourceMap = {
|
|
|
66
55
|
'tax-category': ctp.TaxCategory
|
|
67
56
|
type: ctp.Type
|
|
68
57
|
zone: ctp.Zone
|
|
69
|
-
'customer-email-token': never
|
|
70
|
-
'customer-password-token': never
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export type ResourceIdentifierMap = {
|
|
74
|
-
cart: ctp.CartDiscountResourceIdentifier
|
|
75
|
-
// 'cart': ctp.CartResourceIdentifier,
|
|
76
|
-
// 'category': ctp.CategoryResourceIdentifier,
|
|
77
|
-
channel: ctp.ChannelResourceIdentifier
|
|
78
|
-
// 'foobar': ctp.CustomerResourceIdentifier,
|
|
79
|
-
// 'foobar': ctp.DiscountCodeResourceIdentifier,
|
|
80
|
-
// 'foobar': ctp.InventoryEntryResourceIdentifier,
|
|
81
|
-
// 'foobar': ctp.OrderEditResourceIdentifier,
|
|
82
|
-
// 'foobar': ctp.OrderResourceIdentifier,
|
|
83
|
-
// 'foobar': ctp.PaymentResourceIdentifier,
|
|
84
|
-
// 'foobar': ctp.ProductDiscountResourceIdentifier,
|
|
85
|
-
// 'foobar': ctp.ProductResourceIdentifier,
|
|
86
|
-
// 'foobar': ctp.ProductTypeResourceIdentifier,
|
|
87
|
-
// 'foobar': ctp.ReviewResourceIdentifier,
|
|
88
|
-
// 'foobar': ctp.ShippingMethodResourceIdentifier,
|
|
89
|
-
// 'foobar': ctp.ShoppingListResourceIdentifier,
|
|
90
|
-
// 'foobar': ctp.StateResourceIdentifier,
|
|
91
|
-
// 'foobar': ctp.StoreResourceIdentifier,
|
|
92
|
-
// 'foobar': ctp.TaxCategoryResourceIdentifier,
|
|
93
|
-
// 'foobar': ctp.TypeResourceIdentifier,
|
|
94
|
-
// 'foobar': ctp.ZoneResourceIdentifier,
|
|
95
58
|
}
|
|
96
59
|
|
|
97
|
-
export type
|
|
98
|
-
cart:
|
|
99
|
-
|
|
100
|
-
category:
|
|
101
|
-
channel:
|
|
102
|
-
customer: CustomerRepository
|
|
60
|
+
export type PagedQueryResponseMap = {
|
|
61
|
+
'cart-discount': ctp.CartDiscountPagedQueryResponse
|
|
62
|
+
cart: ctp.CartPagedQueryResponse
|
|
63
|
+
category: ctp.CategoryPagedQueryResponse
|
|
64
|
+
channel: ctp.ChannelPagedQueryResponse
|
|
103
65
|
'customer-email-token': never
|
|
104
|
-
'customer-group':
|
|
66
|
+
'customer-group': ctp.CustomerGroupPagedQueryResponse
|
|
105
67
|
'customer-password-token': never
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
'
|
|
110
|
-
|
|
111
|
-
'order-edit':
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
'product-
|
|
115
|
-
'product-
|
|
116
|
-
'product-
|
|
117
|
-
|
|
118
|
-
'
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
'
|
|
124
|
-
|
|
125
|
-
|
|
68
|
+
customer: ctp.CustomerPagedQueryResponse
|
|
69
|
+
'discount-code': ctp.DiscountCodePagedQueryResponse
|
|
70
|
+
extension: ctp.ExtensionPagedQueryResponse
|
|
71
|
+
'inventory-entry': ctp.InventoryPagedQueryResponse
|
|
72
|
+
'key-value-document': ctp.CustomObjectPagedQueryResponse
|
|
73
|
+
'order-edit': ctp.OrderEditPagedQueryResponse
|
|
74
|
+
order: ctp.OrderPagedQueryResponse
|
|
75
|
+
payment: ctp.PaymentPagedQueryResponse
|
|
76
|
+
'product-discount': ctp.ProductDiscountPagedQueryResponse
|
|
77
|
+
'product-price': ctp.StandalonePricePagedQueryResponse
|
|
78
|
+
'product-projection': ctp.ProductProjectionPagedQueryResponse
|
|
79
|
+
'product-selection': ctp.ProductSelectionPagedQueryResponse
|
|
80
|
+
'product-type': ctp.ProductTypePagedQueryResponse
|
|
81
|
+
product: ctp.ProductPagedQueryResponse
|
|
82
|
+
'quote-request': ctp.QuoteRequestPagedQueryResponse
|
|
83
|
+
quote: ctp.QuotePagedQueryResponse
|
|
84
|
+
review: ctp.ReviewPagedQueryResponse
|
|
85
|
+
'shipping-method': ctp.ShippingMethodPagedQueryResponse
|
|
86
|
+
'shopping-list': ctp.ShoppingListPagedQueryResponse
|
|
87
|
+
'staged-quote': ctp.StagedQuotePagedQueryResponse
|
|
88
|
+
'standalone-price': ctp.StandalonePricePagedQueryResponse
|
|
89
|
+
state: ctp.StatePagedQueryResponse
|
|
90
|
+
store: ctp.StorePagedQueryResponse
|
|
91
|
+
subscription: ctp.SubscriptionPagedQueryResponse
|
|
92
|
+
'tax-category': ctp.TaxCategoryPagedQueryResponse
|
|
93
|
+
type: ctp.TypePagedQueryResponse
|
|
94
|
+
zone: ctp.ZonePagedQueryResponse
|
|
126
95
|
}
|