@labdigital/commercetools-mock 1.4.0 → 1.6.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 (129) hide show
  1. package/README.md +5 -4
  2. package/dist/index.cjs +116 -18
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +30 -7
  5. package/dist/index.d.ts +30 -7
  6. package/dist/index.js +116 -18
  7. package/dist/index.js.map +1 -1
  8. package/package.json +4 -3
  9. package/src/constants.ts +2 -2
  10. package/src/ctMock.ts +176 -176
  11. package/src/exceptions.ts +10 -10
  12. package/src/helpers.ts +26 -26
  13. package/src/index.test.ts +173 -173
  14. package/src/index.ts +3 -3
  15. package/src/lib/expandParser.ts +19 -19
  16. package/src/lib/haversine.test.ts +13 -13
  17. package/src/lib/haversine.ts +14 -14
  18. package/src/lib/masking.ts +15 -15
  19. package/src/lib/parser.ts +2 -2
  20. package/src/lib/predicateParser.test.ts +204 -204
  21. package/src/lib/predicateParser.ts +398 -398
  22. package/src/lib/projectionSearchFilter.test.ts +168 -168
  23. package/src/lib/projectionSearchFilter.ts +272 -269
  24. package/src/lib/proxy.ts +8 -8
  25. package/src/oauth/errors.ts +4 -4
  26. package/src/oauth/helpers.ts +6 -6
  27. package/src/oauth/server.ts +103 -101
  28. package/src/oauth/store.ts +27 -27
  29. package/src/priceSelector.test.ts +68 -68
  30. package/src/priceSelector.ts +70 -70
  31. package/src/product-projection-search.ts +296 -296
  32. package/src/projectAPI.test.ts +3 -3
  33. package/src/projectAPI.ts +46 -46
  34. package/src/repositories/abstract.ts +190 -190
  35. package/src/repositories/associate-role.ts +10 -7
  36. package/src/repositories/attribute-group.ts +63 -8
  37. package/src/repositories/business-unit.ts +10 -7
  38. package/src/repositories/cart-discount.ts +134 -134
  39. package/src/repositories/cart.ts +517 -514
  40. package/src/repositories/category.ts +170 -167
  41. package/src/repositories/channel.ts +114 -111
  42. package/src/repositories/custom-object.ts +66 -63
  43. package/src/repositories/customer-group.ts +72 -69
  44. package/src/repositories/customer.ts +93 -79
  45. package/src/repositories/discount-code.ts +171 -168
  46. package/src/repositories/errors.ts +15 -15
  47. package/src/repositories/extension.ts +79 -76
  48. package/src/repositories/helpers.ts +180 -180
  49. package/src/repositories/index.ts +39 -39
  50. package/src/repositories/inventory-entry.ts +98 -95
  51. package/src/repositories/my-order.ts +11 -11
  52. package/src/repositories/order-edit.ts +29 -29
  53. package/src/repositories/order.test.ts +191 -191
  54. package/src/repositories/order.ts +393 -389
  55. package/src/repositories/payment.ts +155 -155
  56. package/src/repositories/product-discount.ts +149 -149
  57. package/src/repositories/product-projection.ts +116 -52
  58. package/src/repositories/product-selection.ts +31 -31
  59. package/src/repositories/product-type.ts +156 -156
  60. package/src/repositories/product.ts +600 -597
  61. package/src/repositories/project.ts +136 -135
  62. package/src/repositories/quote-request.ts +19 -19
  63. package/src/repositories/quote.ts +19 -19
  64. package/src/repositories/review.ts +24 -24
  65. package/src/repositories/shipping-method.ts +217 -217
  66. package/src/repositories/shopping-list.ts +49 -49
  67. package/src/repositories/staged-quote.ts +20 -20
  68. package/src/repositories/standalone-price.ts +72 -61
  69. package/src/repositories/state.ts +84 -84
  70. package/src/repositories/store.ts +114 -114
  71. package/src/repositories/subscription.ts +40 -40
  72. package/src/repositories/tax-category.ts +98 -98
  73. package/src/repositories/type.ts +157 -157
  74. package/src/repositories/zone.ts +71 -71
  75. package/src/server.ts +2 -2
  76. package/src/services/abstract.ts +173 -173
  77. package/src/services/attribute-group.ts +16 -0
  78. package/src/services/cart-discount.ts +8 -8
  79. package/src/services/cart.test.ts +409 -409
  80. package/src/services/cart.ts +50 -50
  81. package/src/services/category.test.ts +25 -25
  82. package/src/services/category.ts +8 -8
  83. package/src/services/channel.ts +8 -8
  84. package/src/services/custom-object.test.ts +184 -184
  85. package/src/services/custom-object.ts +48 -48
  86. package/src/services/customer-group.ts +8 -8
  87. package/src/services/customer.test.ts +151 -129
  88. package/src/services/customer.ts +27 -27
  89. package/src/services/discount-code.ts +8 -8
  90. package/src/services/extension.ts +8 -8
  91. package/src/services/index.ts +52 -44
  92. package/src/services/inventory-entry.test.ts +162 -162
  93. package/src/services/inventory-entry.ts +8 -8
  94. package/src/services/my-cart.test.ts +78 -78
  95. package/src/services/my-cart.ts +28 -28
  96. package/src/services/my-customer.test.ts +44 -44
  97. package/src/services/my-customer.ts +53 -53
  98. package/src/services/my-order.ts +20 -20
  99. package/src/services/my-payment.test.ts +65 -65
  100. package/src/services/my-payment.ts +8 -8
  101. package/src/services/order.test.ts +527 -527
  102. package/src/services/order.ts +31 -31
  103. package/src/services/payment.test.ts +65 -65
  104. package/src/services/payment.ts +8 -8
  105. package/src/services/product-discount.ts +8 -8
  106. package/src/services/product-projection.test.ts +492 -428
  107. package/src/services/product-projection.ts +32 -18
  108. package/src/services/product-type.test.ts +56 -56
  109. package/src/services/product-type.ts +8 -8
  110. package/src/services/product.test.ts +510 -510
  111. package/src/services/product.ts +8 -8
  112. package/src/services/project.ts +34 -34
  113. package/src/services/shipping-method.test.ts +81 -81
  114. package/src/services/shipping-method.ts +12 -12
  115. package/src/services/shopping-list.ts +8 -8
  116. package/src/services/standalone-price.test.ts +256 -256
  117. package/src/services/standalone-price.ts +8 -8
  118. package/src/services/state.test.ts +42 -42
  119. package/src/services/state.ts +8 -8
  120. package/src/services/store.test.ts +57 -57
  121. package/src/services/store.ts +8 -8
  122. package/src/services/subscription.ts +8 -8
  123. package/src/services/tax-category.test.ts +61 -61
  124. package/src/services/tax-category.ts +8 -8
  125. package/src/services/type.ts +8 -8
  126. package/src/services/zone.ts +8 -8
  127. package/src/storage/abstract.ts +58 -58
  128. package/src/storage/in-memory.ts +419 -419
  129. package/src/types.ts +82 -82
@@ -1,77 +1,80 @@
1
1
  import type {
2
- CustomObject,
3
- CustomObjectDraft,
4
- InvalidOperationError,
2
+ CustomObject,
3
+ CustomObjectDraft,
4
+ InvalidOperationError,
5
5
  } from '@commercetools/platform-sdk'
6
6
  import { CommercetoolsError } from '../exceptions.js'
7
7
  import { cloneObject, getBaseResourceProperties } from '../helpers.js'
8
8
  import type { Writable } from '../types.js'
9
- import { AbstractResourceRepository, type RepositoryContext } from './abstract.js'
9
+ import {
10
+ AbstractResourceRepository,
11
+ type RepositoryContext,
12
+ } from './abstract.js'
10
13
  import { checkConcurrentModification } from './errors.js'
11
14
 
12
15
  export class CustomObjectRepository extends AbstractResourceRepository<'key-value-document'> {
13
- getTypeId() {
14
- return 'key-value-document' as const
15
- }
16
+ getTypeId() {
17
+ return 'key-value-document' as const
18
+ }
16
19
 
17
- create(
18
- context: RepositoryContext,
19
- draft: Writable<CustomObjectDraft>
20
- ): CustomObject {
21
- const current = this.getWithContainerAndKey(
22
- context,
23
- draft.container,
24
- draft.key
25
- ) as Writable<CustomObject | undefined>
20
+ create(
21
+ context: RepositoryContext,
22
+ draft: Writable<CustomObjectDraft>
23
+ ): CustomObject {
24
+ const current = this.getWithContainerAndKey(
25
+ context,
26
+ draft.container,
27
+ draft.key
28
+ ) as Writable<CustomObject | undefined>
26
29
 
27
- if (current) {
28
- // Only check version if it is passed in the draft
29
- if (draft.version) {
30
- checkConcurrentModification(current.version, draft.version, current.id)
31
- } else {
32
- draft.version = current.version
33
- }
30
+ if (current) {
31
+ // Only check version if it is passed in the draft
32
+ if (draft.version) {
33
+ checkConcurrentModification(current.version, draft.version, current.id)
34
+ } else {
35
+ draft.version = current.version
36
+ }
34
37
 
35
- if (draft.value !== current.value) {
36
- const updated = cloneObject(current) as Writable<CustomObject>
37
- updated.value = draft.value
38
- updated.version += 1
39
- this.saveUpdate(context, draft.version, updated)
40
- return updated
41
- }
42
- return current
43
- } else {
44
- // If the resource is new the only valid version is 0
45
- if (draft.version) {
46
- throw new CommercetoolsError<InvalidOperationError>(
47
- {
48
- code: 'InvalidOperation',
49
- message: 'version on create must be 0',
50
- },
51
- 400
52
- )
53
- }
54
- const baseProperties = getBaseResourceProperties()
55
- const resource: CustomObject = {
56
- ...baseProperties,
57
- container: draft.container,
58
- key: draft.key,
59
- value: draft.value,
60
- }
38
+ if (draft.value !== current.value) {
39
+ const updated = cloneObject(current) as Writable<CustomObject>
40
+ updated.value = draft.value
41
+ updated.version += 1
42
+ this.saveUpdate(context, draft.version, updated)
43
+ return updated
44
+ }
45
+ return current
46
+ } else {
47
+ // If the resource is new the only valid version is 0
48
+ if (draft.version) {
49
+ throw new CommercetoolsError<InvalidOperationError>(
50
+ {
51
+ code: 'InvalidOperation',
52
+ message: 'version on create must be 0',
53
+ },
54
+ 400
55
+ )
56
+ }
57
+ const baseProperties = getBaseResourceProperties()
58
+ const resource: CustomObject = {
59
+ ...baseProperties,
60
+ container: draft.container,
61
+ key: draft.key,
62
+ value: draft.value,
63
+ }
61
64
 
62
- this.saveNew(context, resource)
63
- return resource
64
- }
65
- }
65
+ this.saveNew(context, resource)
66
+ return resource
67
+ }
68
+ }
66
69
 
67
- getWithContainerAndKey(
68
- context: RepositoryContext,
69
- container: string,
70
- key: string
71
- ) {
72
- const items = this._storage.all(context.projectKey, this.getTypeId())
73
- return items.find(
74
- (item) => item.container === container && item.key === key
75
- )
76
- }
70
+ getWithContainerAndKey(
71
+ context: RepositoryContext,
72
+ container: string,
73
+ key: string
74
+ ) {
75
+ const items = this._storage.all(context.projectKey, this.getTypeId())
76
+ return items.find(
77
+ (item) => item.container === container && item.key === key
78
+ )
79
+ }
77
80
  }
@@ -1,78 +1,81 @@
1
1
  import type {
2
- CustomerGroup,
3
- CustomerGroupChangeNameAction,
4
- CustomerGroupDraft,
5
- CustomerGroupSetCustomFieldAction,
6
- CustomerGroupSetCustomTypeAction,
7
- CustomerGroupSetKeyAction,
2
+ CustomerGroup,
3
+ CustomerGroupChangeNameAction,
4
+ CustomerGroupDraft,
5
+ CustomerGroupSetCustomFieldAction,
6
+ CustomerGroupSetCustomTypeAction,
7
+ CustomerGroupSetKeyAction,
8
8
  } from '@commercetools/platform-sdk'
9
9
  import { getBaseResourceProperties } from '../helpers.js'
10
10
  import type { Writable } from '../types.js'
11
- import { AbstractResourceRepository, type RepositoryContext } from './abstract.js'
11
+ import {
12
+ AbstractResourceRepository,
13
+ type RepositoryContext,
14
+ } from './abstract.js'
12
15
  import { createCustomFields } from './helpers.js'
13
16
 
14
17
  export class CustomerGroupRepository extends AbstractResourceRepository<'customer-group'> {
15
- getTypeId() {
16
- return 'customer-group' as const
17
- }
18
- create(context: RepositoryContext, draft: CustomerGroupDraft): CustomerGroup {
19
- const resource: CustomerGroup = {
20
- ...getBaseResourceProperties(),
21
- key: draft.key,
22
- name: draft.groupName,
23
- custom: createCustomFields(
24
- draft.custom,
25
- context.projectKey,
26
- this._storage
27
- ),
28
- }
29
- this.saveNew(context, resource)
30
- return resource
31
- }
18
+ getTypeId() {
19
+ return 'customer-group' as const
20
+ }
21
+ create(context: RepositoryContext, draft: CustomerGroupDraft): CustomerGroup {
22
+ const resource: CustomerGroup = {
23
+ ...getBaseResourceProperties(),
24
+ key: draft.key,
25
+ name: draft.groupName,
26
+ custom: createCustomFields(
27
+ draft.custom,
28
+ context.projectKey,
29
+ this._storage
30
+ ),
31
+ }
32
+ this.saveNew(context, resource)
33
+ return resource
34
+ }
32
35
 
33
- actions = {
34
- setKey: (
35
- context: RepositoryContext,
36
- resource: Writable<CustomerGroup>,
37
- { key }: CustomerGroupSetKeyAction
38
- ) => {
39
- resource.key = key
40
- },
41
- changeName: (
42
- context: RepositoryContext,
43
- resource: Writable<CustomerGroup>,
44
- { name }: CustomerGroupChangeNameAction
45
- ) => {
46
- resource.name = name
47
- },
48
- setCustomType: (
49
- context: RepositoryContext,
50
- resource: Writable<CustomerGroup>,
51
- { type, fields }: CustomerGroupSetCustomTypeAction
52
- ) => {
53
- if (type) {
54
- resource.custom = createCustomFields(
55
- { type, fields },
56
- context.projectKey,
57
- this._storage
58
- )
59
- } else {
60
- resource.custom = undefined
61
- }
62
- },
63
- setCustomField: (
64
- context: RepositoryContext,
65
- resource: Writable<CustomerGroup>,
66
- { name, value }: CustomerGroupSetCustomFieldAction
67
- ) => {
68
- if (!resource.custom) {
69
- return
70
- }
71
- if (value === null) {
72
- delete resource.custom.fields[name]
73
- } else {
74
- resource.custom.fields[name] = value
75
- }
76
- },
77
- }
36
+ actions = {
37
+ setKey: (
38
+ context: RepositoryContext,
39
+ resource: Writable<CustomerGroup>,
40
+ { key }: CustomerGroupSetKeyAction
41
+ ) => {
42
+ resource.key = key
43
+ },
44
+ changeName: (
45
+ context: RepositoryContext,
46
+ resource: Writable<CustomerGroup>,
47
+ { name }: CustomerGroupChangeNameAction
48
+ ) => {
49
+ resource.name = name
50
+ },
51
+ setCustomType: (
52
+ context: RepositoryContext,
53
+ resource: Writable<CustomerGroup>,
54
+ { type, fields }: CustomerGroupSetCustomTypeAction
55
+ ) => {
56
+ if (type) {
57
+ resource.custom = createCustomFields(
58
+ { type, fields },
59
+ context.projectKey,
60
+ this._storage
61
+ )
62
+ } else {
63
+ resource.custom = undefined
64
+ }
65
+ },
66
+ setCustomField: (
67
+ context: RepositoryContext,
68
+ resource: Writable<CustomerGroup>,
69
+ { name, value }: CustomerGroupSetCustomFieldAction
70
+ ) => {
71
+ if (!resource.custom) {
72
+ return
73
+ }
74
+ if (value === null) {
75
+ delete resource.custom.fields[name]
76
+ } else {
77
+ resource.custom.fields[name] = value
78
+ }
79
+ },
80
+ }
78
81
  }
@@ -1,90 +1,104 @@
1
1
  import type {
2
- Customer,
3
- CustomerChangeEmailAction,
4
- CustomerDraft,
5
- CustomerSetAuthenticationModeAction,
6
- InvalidInputError,
7
- InvalidJsonInputError,
2
+ Customer,
3
+ CustomerChangeEmailAction,
4
+ CustomerDraft,
5
+ CustomerSetAuthenticationModeAction,
6
+ CustomerSetCustomFieldAction,
7
+ InvalidInputError,
8
+ InvalidJsonInputError,
8
9
  } from '@commercetools/platform-sdk'
9
- import type { Writable } from '../types.js'
10
10
  import { CommercetoolsError } from '../exceptions.js'
11
11
  import { getBaseResourceProperties } from '../helpers.js'
12
- import { AbstractResourceRepository, type RepositoryContext } from './abstract.js'
12
+ import type { Writable } from '../types.js'
13
+ import {
14
+ AbstractResourceRepository,
15
+ type RepositoryContext,
16
+ } from './abstract.js'
13
17
 
14
18
  export class CustomerRepository extends AbstractResourceRepository<'customer'> {
15
- getTypeId() {
16
- return 'customer' as const
17
- }
19
+ getTypeId() {
20
+ return 'customer' as const
21
+ }
18
22
 
19
- create(context: RepositoryContext, draft: CustomerDraft): Customer {
20
- const resource: Customer = {
21
- ...getBaseResourceProperties(),
22
- authenticationMode: draft.authenticationMode || 'Password',
23
- email: draft.email,
24
- password: draft.password
25
- ? Buffer.from(draft.password).toString('base64')
26
- : undefined,
27
- isEmailVerified: draft.isEmailVerified || false,
28
- addresses: [],
29
- }
30
- this.saveNew(context, resource)
31
- return resource
32
- }
23
+ create(context: RepositoryContext, draft: CustomerDraft): Customer {
24
+ const resource: Customer = {
25
+ ...getBaseResourceProperties(),
26
+ authenticationMode: draft.authenticationMode || 'Password',
27
+ email: draft.email,
28
+ password: draft.password
29
+ ? Buffer.from(draft.password).toString('base64')
30
+ : undefined,
31
+ isEmailVerified: draft.isEmailVerified || false,
32
+ addresses: [],
33
+ }
34
+ this.saveNew(context, resource)
35
+ return resource
36
+ }
33
37
 
34
- getMe(context: RepositoryContext): Customer | undefined {
35
- const results = this._storage.query(
36
- context.projectKey,
37
- this.getTypeId(),
38
- {}
39
- ) // grab the first customer you can find
40
- if (results.count > 0) {
41
- return results.results[0] as Customer
42
- }
38
+ getMe(context: RepositoryContext): Customer | undefined {
39
+ const results = this._storage.query(
40
+ context.projectKey,
41
+ this.getTypeId(),
42
+ {}
43
+ ) // grab the first customer you can find
44
+ if (results.count > 0) {
45
+ return results.results[0] as Customer
46
+ }
43
47
 
44
- return
45
- }
48
+ return
49
+ }
46
50
 
47
- actions = {
48
- changeEmail: (
49
- _context: RepositoryContext,
50
- resource: Writable<Customer>,
51
- { email }: CustomerChangeEmailAction
52
- ) => {
53
- resource.email = email
54
- },
55
- setAuthenticationMode: (
56
- _context: RepositoryContext,
57
- resource: Writable<Customer>,
58
- { authMode, password }: CustomerSetAuthenticationModeAction
59
- ) => {
60
- if (resource.authenticationMode === authMode) {
61
- throw new CommercetoolsError<InvalidInputError>(
62
- {
63
- code: 'InvalidInput',
64
- message: `The customer is already using the '${resource.authenticationMode}' authentication mode.`,
65
- },
66
- 400
67
- )
68
- }
69
- resource.authenticationMode = authMode
70
- if (authMode === 'ExternalAuth') {
71
- delete resource.password
72
- return
73
- }
74
- if (authMode === 'Password') {
75
- resource.password = password
76
- ? Buffer.from(password).toString('base64')
77
- : undefined
78
- return
79
- }
80
- throw new CommercetoolsError<InvalidJsonInputError>(
81
- {
82
- code: 'InvalidJsonInput',
83
- message: 'Request body does not contain valid JSON.',
84
- detailedErrorMessage: `actions -> authMode: Invalid enum value: '${authMode}'. Expected one of: 'Password','ExternalAuth'`,
85
- },
86
- 400
87
- )
88
- },
89
- }
51
+ actions = {
52
+ changeEmail: (
53
+ _context: RepositoryContext,
54
+ resource: Writable<Customer>,
55
+ { email }: CustomerChangeEmailAction
56
+ ) => {
57
+ resource.email = email
58
+ },
59
+ setAuthenticationMode: (
60
+ _context: RepositoryContext,
61
+ resource: Writable<Customer>,
62
+ { authMode, password }: CustomerSetAuthenticationModeAction
63
+ ) => {
64
+ if (resource.authenticationMode === authMode) {
65
+ throw new CommercetoolsError<InvalidInputError>(
66
+ {
67
+ code: 'InvalidInput',
68
+ message: `The customer is already using the '${resource.authenticationMode}' authentication mode.`,
69
+ },
70
+ 400
71
+ )
72
+ }
73
+ resource.authenticationMode = authMode
74
+ if (authMode === 'ExternalAuth') {
75
+ delete resource.password
76
+ return
77
+ }
78
+ if (authMode === 'Password') {
79
+ resource.password = password
80
+ ? Buffer.from(password).toString('base64')
81
+ : undefined
82
+ return
83
+ }
84
+ throw new CommercetoolsError<InvalidJsonInputError>(
85
+ {
86
+ code: 'InvalidJsonInput',
87
+ message: 'Request body does not contain valid JSON.',
88
+ detailedErrorMessage: `actions -> authMode: Invalid enum value: '${authMode}'. Expected one of: 'Password','ExternalAuth'`,
89
+ },
90
+ 400
91
+ )
92
+ },
93
+ setCustomField: (
94
+ _context: RepositoryContext,
95
+ resource: Writable<Customer>,
96
+ { name, value }: CustomerSetCustomFieldAction
97
+ ) => {
98
+ if (!resource.custom) {
99
+ throw new Error('Resource has no custom field')
100
+ }
101
+ resource.custom.fields[name] = value
102
+ },
103
+ }
90
104
  }