@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.
@@ -1,39 +1,41 @@
1
- import type {
2
- AssociateRole,
3
- AttributeGroup,
4
- BusinessUnit,
5
- Cart,
6
- CartDiscount,
7
- Category,
8
- Channel,
9
- Customer,
10
- CustomerGroup,
11
- CustomObject,
12
- DiscountCode,
13
- Extension,
14
- InvalidInputError,
15
- InventoryEntry,
16
- Order,
17
- PagedQueryResponse,
18
- Payment,
19
- Product,
20
- ProductDiscount,
21
- ProductProjection,
22
- ProductType,
23
- Project,
24
- Quote,
25
- QuoteRequest,
26
- Reference,
27
- ResourceIdentifier,
28
- ShippingMethod,
29
- ShoppingList,
30
- StagedQuote,
31
- State,
32
- Store,
33
- Subscription,
34
- TaxCategory,
35
- Type,
36
- Zone,
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] | null {
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
- console.error(
302
- `No resource found with typeId=${identifier.typeId}, id=${identifier.id}`
303
- )
304
- return null
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 store = this.forProjectKey(projectKey)[identifier.typeId]
309
-
310
- if (store) {
311
- // TODO: BaseResource has no key attribute, but the subclasses should
312
- // have them all.
313
- const resource = Array.from(store.values()).find(
314
- // @ts-ignore
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
- return null
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