@labdigital/commercetools-mock 2.17.1 → 2.18.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.
Files changed (180) hide show
  1. package/dist/index.cjs +4351 -4099
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +266 -413
  4. package/dist/index.d.ts +266 -413
  5. package/dist/index.js +4351 -4099
  6. package/dist/index.js.map +1 -1
  7. package/package.json +47 -47
  8. package/src/constants.ts +2 -2
  9. package/src/ctMock.test.ts +11 -11
  10. package/src/ctMock.ts +141 -127
  11. package/src/deprecation.ts +8 -0
  12. package/src/exceptions.ts +17 -15
  13. package/src/helpers.ts +32 -32
  14. package/src/index.test.ts +128 -128
  15. package/src/index.ts +3 -3
  16. package/src/lib/expandParser.ts +13 -13
  17. package/src/lib/haversine.test.ts +9 -9
  18. package/src/lib/haversine.ts +11 -11
  19. package/src/lib/masking.ts +11 -11
  20. package/src/lib/parser.ts +2 -2
  21. package/src/lib/password.ts +23 -3
  22. package/src/lib/predicateParser.test.ts +185 -183
  23. package/src/lib/predicateParser.ts +234 -234
  24. package/src/lib/projectionSearchFilter.test.ts +103 -101
  25. package/src/lib/projectionSearchFilter.ts +152 -150
  26. package/src/lib/proxy.ts +5 -5
  27. package/src/oauth/errors.ts +4 -4
  28. package/src/oauth/helpers.ts +6 -6
  29. package/src/oauth/server.test.ts +86 -86
  30. package/src/oauth/server.ts +158 -144
  31. package/src/oauth/store.ts +44 -43
  32. package/src/priceSelector.test.ts +35 -35
  33. package/src/priceSelector.ts +30 -30
  34. package/src/product-projection-search.ts +136 -134
  35. package/src/projectAPI.test.ts +7 -7
  36. package/src/projectAPI.ts +24 -22
  37. package/src/repositories/abstract.ts +168 -116
  38. package/src/repositories/associate-role.ts +90 -77
  39. package/src/repositories/attribute-group.ts +51 -40
  40. package/src/repositories/business-unit.ts +168 -148
  41. package/src/repositories/cart/actions.ts +489 -0
  42. package/src/repositories/cart/helpers.ts +30 -0
  43. package/src/repositories/cart/index.ts +180 -0
  44. package/src/repositories/cart-discount/actions.ts +148 -0
  45. package/src/repositories/cart-discount/index.ts +86 -0
  46. package/src/repositories/category/actions.ts +231 -0
  47. package/src/repositories/category/index.ts +52 -0
  48. package/src/repositories/channel.ts +88 -90
  49. package/src/repositories/custom-object.ts +46 -45
  50. package/src/repositories/customer/actions.ts +165 -0
  51. package/src/repositories/customer/index.ts +79 -0
  52. package/src/repositories/customer-group.ts +66 -55
  53. package/src/repositories/discount-code/actions.ts +149 -0
  54. package/src/repositories/discount-code/index.ts +50 -0
  55. package/src/repositories/errors.ts +10 -10
  56. package/src/repositories/extension.ts +64 -62
  57. package/src/repositories/helpers.ts +117 -118
  58. package/src/repositories/index.ts +80 -79
  59. package/src/repositories/inventory-entry/actions.ts +84 -0
  60. package/src/repositories/inventory-entry/index.ts +44 -0
  61. package/src/repositories/my-customer.ts +114 -0
  62. package/src/repositories/my-order.ts +8 -8
  63. package/src/repositories/order/actions.ts +281 -0
  64. package/src/repositories/{order.test.ts → order/index.test.ts} +77 -77
  65. package/src/repositories/order/index.ts +260 -0
  66. package/src/repositories/order-edit.ts +10 -23
  67. package/src/repositories/payment/actions.ts +305 -0
  68. package/src/repositories/payment/helpers.ts +17 -0
  69. package/src/repositories/payment/index.ts +56 -0
  70. package/src/repositories/product/actions.ts +943 -0
  71. package/src/repositories/product/helpers.ts +98 -0
  72. package/src/repositories/product/index.ts +130 -0
  73. package/src/repositories/product-discount.ts +127 -117
  74. package/src/repositories/product-projection.ts +56 -62
  75. package/src/repositories/product-selection.ts +31 -28
  76. package/src/repositories/product-type.ts +136 -134
  77. package/src/repositories/project.ts +133 -118
  78. package/src/repositories/quote-request.ts +7 -19
  79. package/src/repositories/quote.ts +7 -22
  80. package/src/repositories/review.ts +13 -26
  81. package/src/repositories/shipping-method/actions.ts +198 -0
  82. package/src/repositories/shipping-method/helpers.ts +10 -0
  83. package/src/repositories/shipping-method/index.ts +138 -0
  84. package/src/repositories/shopping-list/actions.ts +295 -0
  85. package/src/repositories/shopping-list/index.ts +122 -0
  86. package/src/repositories/staged-quote.ts +7 -20
  87. package/src/repositories/standalone-price.ts +57 -44
  88. package/src/repositories/state.ts +113 -68
  89. package/src/repositories/store.ts +106 -94
  90. package/src/repositories/subscription.ts +46 -22
  91. package/src/repositories/tax-category/actions.ts +94 -0
  92. package/src/repositories/tax-category/helpers.ts +8 -0
  93. package/src/repositories/tax-category/index.ts +25 -0
  94. package/src/repositories/type/actions.ts +162 -0
  95. package/src/repositories/type/index.ts +24 -0
  96. package/src/repositories/zone.ts +62 -58
  97. package/src/schemas/update-request.ts +12 -0
  98. package/src/server.ts +9 -9
  99. package/src/services/abstract.ts +85 -72
  100. package/src/services/associate-roles.test.ts +27 -27
  101. package/src/services/associate-roles.ts +7 -7
  102. package/src/services/attribute-group.ts +7 -7
  103. package/src/services/business-units.test.ts +28 -28
  104. package/src/services/business-units.ts +7 -7
  105. package/src/services/cart-discount.test.ts +199 -199
  106. package/src/services/cart-discount.ts +7 -7
  107. package/src/services/cart.test.ts +261 -261
  108. package/src/services/cart.ts +22 -21
  109. package/src/services/category.test.ts +121 -121
  110. package/src/services/category.ts +7 -7
  111. package/src/services/channel.ts +7 -7
  112. package/src/services/custom-object.test.ts +130 -130
  113. package/src/services/custom-object.ts +34 -31
  114. package/src/services/customer-group.ts +7 -7
  115. package/src/services/customer.test.ts +205 -205
  116. package/src/services/customer.ts +23 -36
  117. package/src/services/discount-code.ts +7 -7
  118. package/src/services/extension.ts +7 -7
  119. package/src/services/index.ts +85 -81
  120. package/src/services/inventory-entry.test.ts +106 -106
  121. package/src/services/inventory-entry.ts +7 -7
  122. package/src/services/my-cart.test.ts +56 -56
  123. package/src/services/my-cart.ts +20 -20
  124. package/src/services/my-customer.test.ts +155 -104
  125. package/src/services/my-customer.ts +66 -75
  126. package/src/services/my-order.ts +16 -16
  127. package/src/services/my-payment.test.ts +40 -40
  128. package/src/services/my-payment.ts +7 -7
  129. package/src/services/my-shopping-list.ts +7 -7
  130. package/src/services/order.test.ts +243 -243
  131. package/src/services/order.ts +23 -18
  132. package/src/services/payment.test.ts +40 -40
  133. package/src/services/payment.ts +7 -7
  134. package/src/services/product-discount.ts +7 -7
  135. package/src/services/product-projection.test.ts +190 -190
  136. package/src/services/product-projection.ts +34 -32
  137. package/src/services/product-selection.test.ts +19 -19
  138. package/src/services/product-selection.ts +7 -7
  139. package/src/services/product-type.test.ts +38 -38
  140. package/src/services/product-type.ts +7 -7
  141. package/src/services/product.test.ts +658 -656
  142. package/src/services/product.ts +7 -7
  143. package/src/services/project.test.ts +29 -24
  144. package/src/services/project.ts +22 -17
  145. package/src/services/reviews.ts +7 -7
  146. package/src/services/shipping-method.test.ts +78 -78
  147. package/src/services/shipping-method.ts +16 -16
  148. package/src/services/shopping-list.test.ts +170 -170
  149. package/src/services/shopping-list.ts +7 -7
  150. package/src/services/standalone-price.test.ts +112 -112
  151. package/src/services/standalone-price.ts +7 -7
  152. package/src/services/state.test.ts +30 -30
  153. package/src/services/state.ts +7 -7
  154. package/src/services/store.test.ts +40 -40
  155. package/src/services/store.ts +7 -7
  156. package/src/services/subscription.ts +7 -7
  157. package/src/services/tax-category.test.ts +43 -43
  158. package/src/services/tax-category.ts +7 -7
  159. package/src/services/type.ts +7 -7
  160. package/src/services/zone.ts +7 -7
  161. package/src/shippingCalculator.test.ts +43 -43
  162. package/src/shippingCalculator.ts +23 -23
  163. package/src/storage/abstract.ts +36 -34
  164. package/src/storage/in-memory.ts +237 -233
  165. package/src/storage/index.ts +2 -2
  166. package/src/types.ts +91 -91
  167. package/src/validate.ts +18 -0
  168. package/src/repositories/cart-discount.ts +0 -219
  169. package/src/repositories/cart.ts +0 -659
  170. package/src/repositories/category.ts +0 -256
  171. package/src/repositories/customer.ts +0 -228
  172. package/src/repositories/discount-code.ts +0 -181
  173. package/src/repositories/inventory-entry.ts +0 -109
  174. package/src/repositories/order.ts +0 -514
  175. package/src/repositories/payment.ts +0 -342
  176. package/src/repositories/product.ts +0 -1106
  177. package/src/repositories/shipping-method.ts +0 -312
  178. package/src/repositories/shopping-list.ts +0 -392
  179. package/src/repositories/tax-category.ts +0 -111
  180. package/src/repositories/type.ts +0 -172
@@ -1,66 +1,67 @@
1
- import auth from 'basic-auth'
2
- import bodyParser from 'body-parser'
1
+ import { InvalidTokenError } from "@commercetools/platform-sdk";
2
+ import auth from "basic-auth";
3
+ import bodyParser from "body-parser";
3
4
  import express, {
4
5
  type NextFunction,
5
6
  type Request,
6
7
  type Response,
7
- } from 'express'
8
- import { InvalidTokenError } from '@commercetools/platform-sdk'
8
+ } from "express";
9
9
  import {
10
10
  AuthError,
11
11
  CommercetoolsError,
12
12
  InvalidRequestError,
13
- } from '../exceptions.js'
14
- import { InvalidClientError, UnsupportedGrantType } from './errors.js'
15
- import { OAuth2Store } from './store.js'
16
- import { getBearerToken } from './helpers.js'
17
- import { CustomerRepository } from '../repositories/customer.js'
18
- import { hashPassword } from '../lib/password.js'
13
+ } from "~src/exceptions";
14
+ import { hashPassword } from "../lib/password";
15
+ import { CustomerRepository } from "../repositories/customer";
16
+ import { InvalidClientError, UnsupportedGrantType } from "./errors";
17
+ import { getBearerToken } from "./helpers";
18
+ import { OAuth2Store } from "./store";
19
19
 
20
20
  type AuthRequest = Request & {
21
- credentials: {
22
- clientId: string
23
- clientSecret: string
24
- }
25
- }
21
+ credentials?: {
22
+ clientId: string;
23
+ clientSecret: string;
24
+ };
25
+ };
26
26
  export type Token = {
27
- access_token: string
28
- token_type: 'Bearer'
29
- expires_in: number
30
- scope: string
31
- refresh_token?: string
32
- }
27
+ access_token: string;
28
+ token_type: "Bearer";
29
+ expires_in: number;
30
+ scope: string;
31
+ refresh_token?: string;
32
+ };
33
33
 
34
34
  export class OAuth2Server {
35
- store: OAuth2Store
36
- private customerRepository: CustomerRepository
35
+ store: OAuth2Store;
36
+
37
+ private customerRepository: CustomerRepository;
37
38
 
38
39
  constructor(private options: { enabled: boolean; validate: boolean }) {
39
- this.store = new OAuth2Store(options.validate)
40
+ this.store = new OAuth2Store(options.validate);
40
41
  }
41
42
 
42
43
  setCustomerRepository(repository: CustomerRepository) {
43
- this.customerRepository = repository
44
+ this.customerRepository = repository;
44
45
  }
45
46
 
46
47
  createRouter() {
47
- const router = express.Router()
48
- router.use(bodyParser.urlencoded({ extended: true }))
49
- router.use(this.validateClientCredentials.bind(this))
50
- router.post('/token', this.tokenHandler.bind(this))
48
+ const router = express.Router();
49
+ router.use(bodyParser.urlencoded({ extended: true }));
50
+ router.use(this.validateClientCredentials.bind(this));
51
+ router.post("/token", this.tokenHandler.bind(this));
51
52
  router.post(
52
- '/:projectKey/customers/token',
53
- this.customerTokenHandler.bind(this)
54
- )
53
+ "/:projectKey/customers/token",
54
+ this.customerTokenHandler.bind(this),
55
+ );
55
56
  router.post(
56
- '/:projectKey/in-store/key=:storeKey/customers/token',
57
- this.inStoreCustomerTokenHandler.bind(this)
58
- )
57
+ "/:projectKey/in-store/key=:storeKey/customers/token",
58
+ this.inStoreCustomerTokenHandler.bind(this),
59
+ );
59
60
  router.post(
60
- '/:projectKey/anonymous/token',
61
- this.anonymousTokenHandler.bind(this)
62
- )
63
- return router
61
+ "/:projectKey/anonymous/token",
62
+ this.anonymousTokenHandler.bind(this),
63
+ );
64
+ return router;
64
65
  }
65
66
 
66
67
  createMiddleware() {
@@ -68,253 +69,266 @@ export class OAuth2Server {
68
69
  return async (
69
70
  request: Request,
70
71
  response: Response,
71
- next: NextFunction
72
+ next: NextFunction,
72
73
  ) => {
73
- next()
74
- }
74
+ next();
75
+ };
75
76
  }
76
77
 
77
78
  return async (request: Request, response: Response, next: NextFunction) => {
78
- const token = getBearerToken(request)
79
+ const token = getBearerToken(request);
79
80
  if (!token) {
80
81
  next(
81
82
  new CommercetoolsError<InvalidTokenError>(
82
83
  {
83
- code: 'invalid_token',
84
+ code: "invalid_token",
84
85
  message:
85
- 'This endpoint requires an access token. You can get one from the authorization server.',
86
+ "This endpoint requires an access token. You can get one from the authorization server.",
86
87
  },
87
- 401
88
- )
89
- )
88
+ 401,
89
+ ),
90
+ );
90
91
  }
91
92
 
92
93
  if (!token || !this.store.validateToken(token)) {
93
94
  next(
94
95
  new CommercetoolsError<InvalidTokenError>(
95
96
  {
96
- code: 'invalid_token',
97
- message: 'invalid_token',
97
+ code: "invalid_token",
98
+ message: "invalid_token",
98
99
  },
99
- 401
100
- )
101
- )
100
+ 401,
101
+ ),
102
+ );
102
103
  }
103
104
 
104
- next()
105
- }
105
+ next();
106
+ };
106
107
  }
107
108
 
108
109
  async validateClientCredentials(
109
110
  request: AuthRequest,
110
111
  response: Response,
111
- next: NextFunction
112
+ next: NextFunction,
112
113
  ) {
113
- const authHeader = request.header('Authorization')
114
+ const authHeader = request.header("Authorization");
114
115
  if (!authHeader) {
115
116
  return next(
116
117
  new CommercetoolsError<InvalidClientError>(
117
118
  {
118
- code: 'invalid_client',
119
+ code: "invalid_client",
119
120
  message:
120
- 'Please provide valid client credentials using HTTP Basic Authentication.',
121
+ "Please provide valid client credentials using HTTP Basic Authentication.",
121
122
  },
122
- 401
123
- )
124
- )
123
+ 401,
124
+ ),
125
+ );
125
126
  }
126
- const credentials = auth.parse(authHeader)
127
+ const credentials = auth.parse(authHeader);
127
128
  if (!credentials) {
128
129
  return next(
129
130
  new CommercetoolsError<InvalidClientError>(
130
131
  {
131
- code: 'invalid_client',
132
+ code: "invalid_client",
132
133
  message:
133
- 'Please provide valid client credentials using HTTP Basic Authentication.',
134
+ "Please provide valid client credentials using HTTP Basic Authentication.",
134
135
  },
135
- 400
136
- )
137
- )
136
+ 400,
137
+ ),
138
+ );
138
139
  }
139
140
 
140
141
  request.credentials = {
141
142
  clientId: credentials.name,
142
143
  clientSecret: credentials.pass,
143
- }
144
+ };
144
145
 
145
- next()
146
+ next();
146
147
  }
147
148
 
148
149
  async tokenHandler(
149
150
  request: AuthRequest,
150
151
  response: Response,
151
- next: NextFunction
152
+ next: NextFunction,
152
153
  ) {
153
- const grantType = request.query.grant_type || request.body.grant_type
154
+ if (!request.credentials) {
155
+ return next(
156
+ new CommercetoolsError<InvalidClientError>(
157
+ {
158
+ code: "invalid_client",
159
+ message: "Client credentials are missing.",
160
+ },
161
+ 401,
162
+ ),
163
+ );
164
+ }
165
+
166
+ const grantType = request.query.grant_type || request.body.grant_type;
154
167
  if (!grantType) {
155
168
  return next(
156
169
  new CommercetoolsError<InvalidRequestError>(
157
170
  {
158
- code: 'invalid_request',
159
- message: 'Missing required parameter: grant_type.',
171
+ code: "invalid_request",
172
+ message: "Missing required parameter: grant_type.",
160
173
  },
161
- 400
162
- )
163
- )
174
+ 400,
175
+ ),
176
+ );
164
177
  }
165
178
 
166
- if (grantType === 'client_credentials') {
179
+ if (grantType === "client_credentials") {
167
180
  const token = this.store.getClientToken(
168
181
  request.credentials.clientId,
169
182
  request.credentials.clientSecret,
170
- request.query.scope?.toString()
171
- )
172
- return response.status(200).send(token)
173
- } else if (grantType === 'refresh_token') {
183
+ request.query.scope?.toString(),
184
+ );
185
+ return response.status(200).send(token);
186
+ } else if (grantType === "refresh_token") {
174
187
  const refreshToken =
175
- request.query.refresh_token?.toString() || request.body.refresh_token
188
+ request.query.refresh_token?.toString() || request.body.refresh_token;
176
189
  if (!refreshToken) {
177
190
  return next(
178
191
  new CommercetoolsError<InvalidRequestError>(
179
192
  {
180
- code: 'invalid_request',
181
- message: 'Missing required parameter: refresh_token.',
193
+ code: "invalid_request",
194
+ message: "Missing required parameter: refresh_token.",
182
195
  },
183
- 400
184
- )
185
- )
196
+ 400,
197
+ ),
198
+ );
186
199
  }
187
200
  const token = this.store.refreshToken(
188
201
  request.credentials.clientId,
189
202
  request.credentials.clientSecret,
190
- refreshToken
191
- )
203
+ refreshToken,
204
+ );
192
205
  if (!token) {
193
206
  return next(
194
207
  new CommercetoolsError<AuthError>(
195
208
  {
196
209
  statusCode: 400,
197
- message: 'The refresh token was not found. It may have expired.',
198
- error: 'invalid_grant',
210
+ message: "The refresh token was not found. It may have expired.",
211
+ error: "invalid_grant",
199
212
  error_description:
200
- 'The refresh token was not found. It may have expired.',
213
+ "The refresh token was not found. It may have expired.",
201
214
  },
202
- 400
203
- )
204
- )
215
+ 400,
216
+ ),
217
+ );
205
218
  }
206
- return response.status(200).send(token)
219
+ return response.status(200).send(token);
207
220
  } else {
208
221
  return next(
209
222
  new CommercetoolsError<UnsupportedGrantType>(
210
223
  {
211
- code: 'unsupported_grant_type',
224
+ code: "unsupported_grant_type",
212
225
  message: `Invalid parameter: grant_type: Invalid grant type: ${grantType}`,
213
226
  },
214
- 400
215
- )
216
- )
227
+ 400,
228
+ ),
229
+ );
217
230
  }
218
231
  }
232
+
219
233
  async customerTokenHandler(
220
234
  request: AuthRequest,
221
235
  response: Response,
222
- next: NextFunction
236
+ next: NextFunction,
223
237
  ) {
224
- const projectKey = request.params.projectKey
225
- const grantType = request.query.grant_type || request.body.grant_type
238
+ const projectKey = request.params.projectKey;
239
+ const grantType = request.query.grant_type || request.body.grant_type;
226
240
  if (!grantType) {
227
241
  return next(
228
242
  new CommercetoolsError<InvalidRequestError>(
229
243
  {
230
- code: 'invalid_request',
231
- message: 'Missing required parameter: grant_type.',
244
+ code: "invalid_request",
245
+ message: "Missing required parameter: grant_type.",
232
246
  },
233
- 400
234
- )
235
- )
247
+ 400,
248
+ ),
249
+ );
236
250
  }
237
251
 
238
- if (grantType === 'password') {
239
- const username = request.query.username || request.body.username
252
+ if (grantType === "password") {
253
+ const username = request.query.username || request.body.username;
240
254
  const password = hashPassword(
241
- request.query.password || request.body.password
242
- )
255
+ request.query.password || request.body.password,
256
+ );
243
257
  const scope =
244
- request.query.scope?.toString() || request.body.scope?.toString()
258
+ request.query.scope?.toString() || request.body.scope?.toString();
245
259
 
246
260
  const result = this.customerRepository.query(
247
261
  { projectKey: request.params.projectKey },
248
262
  {
249
263
  where: [`email = "${username}"`, `password = "${password}"`],
250
- }
251
- )
264
+ },
265
+ );
252
266
 
253
267
  if (result.count === 0) {
254
268
  return next(
255
269
  new CommercetoolsError<any>(
256
270
  {
257
- code: 'invalid_customer_account_credentials',
258
- message: 'Customer account with the given credentials not found.',
271
+ code: "invalid_customer_account_credentials",
272
+ message: "Customer account with the given credentials not found.",
259
273
  },
260
- 400
261
- )
262
- )
274
+ 400,
275
+ ),
276
+ );
263
277
  }
264
278
 
265
- const customer = result.results[0]
266
- const token = this.store.getCustomerToken(projectKey, customer.id, scope)
267
- return response.status(200).send(token)
279
+ const customer = result.results[0];
280
+ const token = this.store.getCustomerToken(projectKey, customer.id, scope);
281
+ return response.status(200).send(token);
268
282
  }
269
283
  }
270
284
 
271
285
  async inStoreCustomerTokenHandler(
272
286
  request: Request,
273
287
  response: Response,
274
- next: NextFunction
288
+ next: NextFunction,
275
289
  ) {
276
290
  return next(
277
291
  new CommercetoolsError<InvalidClientError>(
278
292
  {
279
- code: 'invalid_client',
280
- message: 'Not implemented yet in commercetools-mock',
293
+ code: "invalid_client",
294
+ message: "Not implemented yet in commercetools-mock",
281
295
  },
282
- 401
283
- )
284
- )
296
+ 401,
297
+ ),
298
+ );
285
299
  }
286
300
 
287
301
  async anonymousTokenHandler(
288
302
  request: Request,
289
303
  response: Response,
290
- next: NextFunction
304
+ next: NextFunction,
291
305
  ) {
292
- const projectKey = request.params.projectKey
293
- const grantType = request.query.grant_type || request.body.grant_type
306
+ const projectKey = request.params.projectKey;
307
+ const grantType = request.query.grant_type || request.body.grant_type;
294
308
  if (!grantType) {
295
309
  return next(
296
310
  new CommercetoolsError<InvalidRequestError>(
297
311
  {
298
- code: 'invalid_request',
299
- message: 'Missing required parameter: grant_type.',
312
+ code: "invalid_request",
313
+ message: "Missing required parameter: grant_type.",
300
314
  },
301
- 400
302
- )
303
- )
315
+ 400,
316
+ ),
317
+ );
304
318
  }
305
319
 
306
- if (grantType === 'client_credentials') {
320
+ if (grantType === "client_credentials") {
307
321
  const scope =
308
- request.query.scope?.toString() || request.body.scope?.toString()
322
+ request.query.scope?.toString() || request.body.scope?.toString();
309
323
 
310
- const anonymous_id = undefined
324
+ const anonymous_id = undefined;
311
325
 
312
326
  const token = this.store.getAnonymousToken(
313
327
  projectKey,
314
328
  anonymous_id,
315
- scope
316
- )
317
- return response.status(200).send(token)
329
+ scope,
330
+ );
331
+ return response.status(200).send(token);
318
332
  }
319
333
  }
320
334
  }
@@ -1,83 +1,84 @@
1
- import { randomBytes } from 'crypto'
2
- import { v4 as uuidv4 } from 'uuid'
1
+ import { randomBytes } from "crypto";
2
+ import { v4 as uuidv4 } from "uuid";
3
3
 
4
4
  type Token = {
5
- access_token: string
6
- token_type: 'Bearer'
7
- expires_in: number
8
- scope: string
9
- refresh_token?: string
10
- }
5
+ access_token: string;
6
+ token_type: "Bearer";
7
+ expires_in: number;
8
+ scope: string;
9
+ refresh_token?: string;
10
+ };
11
11
 
12
12
  export class OAuth2Store {
13
- tokens: Token[] = []
14
- validate = true
13
+ tokens: Token[] = [];
14
+
15
+ validate = true;
15
16
 
16
17
  constructor(validate = true) {
17
- this.validate = validate
18
+ this.validate = validate;
18
19
  }
19
20
 
20
21
  addToken(token: Token) {
21
- this.tokens.push(token)
22
+ this.tokens.push(token);
22
23
  }
23
24
 
24
25
  getClientToken(clientId: string, clientSecret: string, scope?: string) {
25
26
  const token: Token = {
26
- access_token: randomBytes(16).toString('base64'),
27
- token_type: 'Bearer',
27
+ access_token: randomBytes(16).toString("base64"),
28
+ token_type: "Bearer",
28
29
  expires_in: 172800,
29
- scope: scope || 'todo',
30
- refresh_token: `my-project-${randomBytes(16).toString('base64')}`,
31
- }
32
- this.addToken(token)
33
- return token
30
+ scope: scope || "todo",
31
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`,
32
+ };
33
+ this.addToken(token);
34
+ return token;
34
35
  }
35
36
 
36
37
  getAnonymousToken(
37
38
  projectKey: string,
38
39
  anonymousId: string | undefined,
39
- scope: string
40
+ scope: string,
40
41
  ) {
41
42
  if (!anonymousId) {
42
- anonymousId = uuidv4()
43
+ anonymousId = uuidv4();
43
44
  }
44
45
  const token: Token = {
45
- access_token: randomBytes(16).toString('base64'),
46
- token_type: 'Bearer',
46
+ access_token: randomBytes(16).toString("base64"),
47
+ token_type: "Bearer",
47
48
  expires_in: 172800,
48
49
  scope: scope
49
50
  ? `${scope} anonymous_id:${anonymousId}`
50
51
  : `anonymous_id:${anonymousId}`,
51
- refresh_token: `${projectKey}:${randomBytes(16).toString('base64')}`,
52
- }
53
- this.addToken(token)
54
- return token
52
+ refresh_token: `${projectKey}:${randomBytes(16).toString("base64")}`,
53
+ };
54
+ this.addToken(token);
55
+ return token;
55
56
  }
56
57
 
57
58
  getCustomerToken(projectKey: string, customerId: string, scope: string) {
58
59
  const token: Token = {
59
- access_token: randomBytes(16).toString('base64'),
60
- token_type: 'Bearer',
60
+ access_token: randomBytes(16).toString("base64"),
61
+ token_type: "Bearer",
61
62
  expires_in: 172800,
62
63
  scope: scope
63
64
  ? `${scope} customer_id:${customerId}`
64
65
  : `customer_id:${customerId}`,
65
- refresh_token: `${projectKey}:${randomBytes(16).toString('base64')}`,
66
- }
67
- this.addToken(token)
68
- return token
66
+ refresh_token: `${projectKey}:${randomBytes(16).toString("base64")}`,
67
+ };
68
+ this.addToken(token);
69
+ return token;
69
70
  }
70
71
 
71
72
  refreshToken(clientId: string, clientSecret: string, refreshToken: string) {
72
- const existing = this.tokens.find((t) => t.refresh_token === refreshToken)
73
+ const existing = this.tokens.find((t) => t.refresh_token === refreshToken);
73
74
  if (!existing) {
74
- return undefined
75
+ return undefined;
75
76
  }
76
77
  const token: Token = {
77
78
  ...existing,
78
- access_token: randomBytes(16).toString('base64'),
79
- }
80
- this.addToken(token)
79
+ access_token: randomBytes(16).toString("base64"),
80
+ };
81
+ this.addToken(token);
81
82
 
82
83
  // We don't want to return the refresh_token again
83
84
  return {
@@ -85,16 +86,16 @@ export class OAuth2Store {
85
86
  token_type: token.token_type,
86
87
  expires_in: token.expires_in,
87
88
  scope: token.scope,
88
- }
89
+ };
89
90
  }
90
91
 
91
92
  validateToken(token: string) {
92
- if (!this.validate) return true
93
+ if (!this.validate) return true;
93
94
 
94
- const foundToken = this.tokens.find((t) => t.access_token === token)
95
+ const foundToken = this.tokens.find((t) => t.access_token === token);
95
96
  if (foundToken) {
96
- return true
97
+ return true;
97
98
  }
98
- return false
99
+ return false;
99
100
  }
100
101
  }