@labdigital/commercetools-mock 2.8.0 → 2.9.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.cjs +235 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -2
- package/dist/index.d.ts +32 -2
- package/dist/index.js +235 -41
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/predicateParser.test.ts +96 -11
- package/src/lib/predicateParser.ts +43 -12
- package/src/oauth/server.ts +11 -1
- package/src/repositories/abstract.ts +5 -4
- package/src/repositories/cart.ts +77 -33
- package/src/repositories/custom-object.ts +18 -0
- package/src/repositories/shipping-method.ts +98 -22
- package/src/services/custom-object.ts +19 -0
- package/src/services/shipping-method.test.ts +120 -21
- package/src/services/shipping-method.ts +19 -2
- package/src/shippingCalculator.test.ts +280 -0
- package/src/shippingCalculator.ts +74 -0
- package/src/storage/abstract.ts +1 -1
- package/src/storage/in-memory.ts +82 -59
- package/src/types.ts +2 -0
package/src/storage/in-memory.ts
CHANGED
|
@@ -1,39 +1,41 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
import {
|
|
2
|
+
ReferencedResourceNotFoundError,
|
|
3
|
+
type AssociateRole,
|
|
4
|
+
type AttributeGroup,
|
|
5
|
+
type BusinessUnit,
|
|
6
|
+
type Cart,
|
|
7
|
+
type CartDiscount,
|
|
8
|
+
type Category,
|
|
9
|
+
type Channel,
|
|
10
|
+
type Customer,
|
|
11
|
+
type CustomerGroup,
|
|
12
|
+
type CustomObject,
|
|
13
|
+
type DiscountCode,
|
|
14
|
+
type Extension,
|
|
15
|
+
type InvalidInputError,
|
|
16
|
+
type InventoryEntry,
|
|
17
|
+
type Order,
|
|
18
|
+
type PagedQueryResponse,
|
|
19
|
+
type Payment,
|
|
20
|
+
type Product,
|
|
21
|
+
type ProductDiscount,
|
|
22
|
+
type ProductProjection,
|
|
23
|
+
type ProductType,
|
|
24
|
+
type Project,
|
|
25
|
+
type Quote,
|
|
26
|
+
type QuoteRequest,
|
|
27
|
+
type Reference,
|
|
28
|
+
type ResourceIdentifier,
|
|
29
|
+
type ShippingMethod,
|
|
30
|
+
type ShoppingList,
|
|
31
|
+
type StagedQuote,
|
|
32
|
+
type State,
|
|
33
|
+
type Store,
|
|
34
|
+
type Subscription,
|
|
35
|
+
type TaxCategory,
|
|
36
|
+
type Type,
|
|
37
|
+
type Zone,
|
|
38
|
+
InvalidJsonInputError,
|
|
37
39
|
} from '@commercetools/platform-sdk'
|
|
38
40
|
import assert from 'assert'
|
|
39
41
|
import { CommercetoolsError } from '../exceptions.js'
|
|
@@ -204,9 +206,17 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
204
206
|
|
|
205
207
|
// Apply predicates
|
|
206
208
|
if (params.where) {
|
|
209
|
+
// Get all key-value pairs starting with 'var.' to pass as variables, removing
|
|
210
|
+
// the 'var.' prefix.
|
|
211
|
+
const vars = Object.fromEntries(
|
|
212
|
+
Object.entries(params)
|
|
213
|
+
.filter(([key]) => key.startsWith('var.'))
|
|
214
|
+
.map(([key, value]) => [key.slice(4), value])
|
|
215
|
+
)
|
|
216
|
+
|
|
207
217
|
try {
|
|
208
218
|
const filterFunc = parseQueryExpression(params.where)
|
|
209
|
-
resources = resources.filter((resource) => filterFunc(resource,
|
|
219
|
+
resources = resources.filter((resource) => filterFunc(resource, vars))
|
|
210
220
|
} catch (err) {
|
|
211
221
|
throw new CommercetoolsError<InvalidInputError>(
|
|
212
222
|
{
|
|
@@ -292,38 +302,51 @@ export class InMemoryStorage extends AbstractStorage {
|
|
|
292
302
|
getByResourceIdentifier<RT extends ResourceType>(
|
|
293
303
|
projectKey: string,
|
|
294
304
|
identifier: ResourceIdentifier
|
|
295
|
-
): ResourceMap[RT]
|
|
305
|
+
): ResourceMap[RT] {
|
|
296
306
|
if (identifier.id) {
|
|
297
307
|
const resource = this.get(projectKey, identifier.typeId, identifier.id)
|
|
298
308
|
if (resource) {
|
|
299
309
|
return resource as ResourceMap[RT]
|
|
300
310
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
311
|
+
|
|
312
|
+
throw new CommercetoolsError<ReferencedResourceNotFoundError>({
|
|
313
|
+
code: 'ReferencedResourceNotFound',
|
|
314
|
+
message:
|
|
315
|
+
`The referenced object of type '${identifier.typeId}' with id ` +
|
|
316
|
+
`'${identifier.id}' was not found. It either doesn't exist, or it ` +
|
|
317
|
+
`can't be accessed from this endpoint (e.g., if the endpoint ` +
|
|
318
|
+
`filters by store or customer account).`,
|
|
319
|
+
typeId: identifier.typeId,
|
|
320
|
+
id: identifier.id,
|
|
321
|
+
})
|
|
305
322
|
}
|
|
306
323
|
|
|
307
324
|
if (identifier.key) {
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
(r) => r.key === identifier.key
|
|
316
|
-
)
|
|
317
|
-
if (resource) {
|
|
318
|
-
return resource as ResourceMap[RT]
|
|
319
|
-
}
|
|
320
|
-
} else {
|
|
321
|
-
throw new Error(
|
|
322
|
-
`No storage found for resource type: ${identifier.typeId}`
|
|
323
|
-
)
|
|
325
|
+
const resource = this.getByKey(
|
|
326
|
+
projectKey,
|
|
327
|
+
identifier.typeId,
|
|
328
|
+
identifier.key
|
|
329
|
+
)
|
|
330
|
+
if (resource) {
|
|
331
|
+
return resource as ResourceMap[RT]
|
|
324
332
|
}
|
|
333
|
+
|
|
334
|
+
throw new CommercetoolsError<ReferencedResourceNotFoundError>({
|
|
335
|
+
code: 'ReferencedResourceNotFound',
|
|
336
|
+
message:
|
|
337
|
+
`The referenced object of type '${identifier.typeId}' with key ` +
|
|
338
|
+
`'${identifier.key}' was not found. It either doesn't exist, or it ` +
|
|
339
|
+
`can't be accessed from this endpoint (e.g., if the endpoint ` +
|
|
340
|
+
`filters by store or customer account).`,
|
|
341
|
+
typeId: identifier.typeId,
|
|
342
|
+
key: identifier.key,
|
|
343
|
+
})
|
|
325
344
|
}
|
|
326
|
-
|
|
345
|
+
throw new CommercetoolsError<InvalidJsonInputError>({
|
|
346
|
+
code: 'InvalidJsonInput',
|
|
347
|
+
message: 'Request body does not contain valid JSON.',
|
|
348
|
+
detailedErrorMessage: "ResourceIdentifier requires an 'id' xor a 'key'",
|
|
349
|
+
})
|
|
327
350
|
}
|
|
328
351
|
|
|
329
352
|
addProject = (projectKey: string): Project => {
|
package/src/types.ts
CHANGED
|
@@ -2,6 +2,8 @@ import type * as ctp from '@commercetools/platform-sdk'
|
|
|
2
2
|
import { RepositoryMap } from './repositories/index.js'
|
|
3
3
|
import AbstractService from './services/abstract.js'
|
|
4
4
|
|
|
5
|
+
export const isType = <T>(x: T) => x
|
|
6
|
+
|
|
5
7
|
export type Writable<T> = { -readonly [P in keyof T]: Writable<T[P]> }
|
|
6
8
|
export type ShallowWritable<T> = { -readonly [P in keyof T]: T[P] }
|
|
7
9
|
|