@labdigital/commercetools-mock 2.5.0 → 2.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
3
  "author": "Michael van Tellingen",
4
- "version": "2.5.0",
4
+ "version": "2.6.0",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",
package/src/ctMock.ts CHANGED
@@ -165,25 +165,18 @@ export class CommercetoolsMock {
165
165
  return app
166
166
  }
167
167
 
168
- private startServer() {
169
- // Check if there are any other servers running
170
- if (_globalListeners.length > 0) {
171
- if (this._mswServer !== undefined) {
172
- throw new Error('Server already started')
173
- } else {
174
- console.warn("Server wasn't stopped properly, clearing")
175
- _globalListeners.forEach((listener) => listener.close())
176
- }
177
- }
178
-
179
- const server = this.app
180
- this._mswServer = setupServer(
168
+ // registerHandlers is an alternative way to work with commercetools-mock, it
169
+ // allows you to manage msw server yourself and register the handlers needed
170
+ // for commercetools-mock to work.
171
+ public registerHandlers(server: SetupServer) {
172
+ const app = this.app
173
+ server.use(
181
174
  http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
182
175
  const body = await request.text()
183
176
  const url = new URL(request.url)
184
177
  const headers = copyHeaders(request.headers)
185
178
 
186
- const res = await inject(server)
179
+ const res = await inject(app)
187
180
  .post(url.pathname + '?' + url.searchParams.toString())
188
181
  .body(body)
189
182
  .headers(headers)
@@ -198,7 +191,7 @@ export class CommercetoolsMock {
198
191
  const url = new URL(request.url)
199
192
  const headers = copyHeaders(request.headers)
200
193
 
201
- const res = await inject(server)
194
+ const res = await inject(app)
202
195
  .get(url.pathname + '?' + url.searchParams.toString())
203
196
  .body(body)
204
197
  .headers(headers)
@@ -229,7 +222,7 @@ export class CommercetoolsMock {
229
222
  const url = new URL(request.url)
230
223
  const headers = copyHeaders(request.headers)
231
224
 
232
- const res = await inject(server)
225
+ const res = await inject(app)
233
226
  .get(url.pathname + '?' + url.searchParams.toString())
234
227
  .body(body)
235
228
  .headers(headers)
@@ -244,7 +237,7 @@ export class CommercetoolsMock {
244
237
  const url = new URL(request.url)
245
238
  const headers = copyHeaders(request.headers)
246
239
 
247
- const res = await inject(server)
240
+ const res = await inject(app)
248
241
  .post(url.pathname + '?' + url.searchParams.toString())
249
242
  .body(body)
250
243
  .headers(headers)
@@ -259,7 +252,7 @@ export class CommercetoolsMock {
259
252
  const url = new URL(request.url)
260
253
  const headers = copyHeaders(request.headers)
261
254
 
262
- const res = await inject(server)
255
+ const res = await inject(app)
263
256
  .delete(url.pathname + '?' + url.searchParams.toString())
264
257
  .body(body)
265
258
  .headers(headers)
@@ -270,7 +263,22 @@ export class CommercetoolsMock {
270
263
  })
271
264
  })
272
265
  )
273
- this._mswServer.listen({
266
+ }
267
+
268
+ private startServer() {
269
+ // Check if there are any other servers running
270
+ if (_globalListeners.length > 0) {
271
+ if (this._mswServer !== undefined) {
272
+ throw new Error('Server already started')
273
+ } else {
274
+ console.warn("Server wasn't stopped properly, clearing")
275
+ _globalListeners.forEach((listener) => listener.close())
276
+ }
277
+ }
278
+
279
+ const server = setupServer()
280
+ this.registerHandlers(server)
281
+ server.listen({
274
282
  // We need to allow requests done by supertest
275
283
  onUnhandledRequest: (request, print) => {
276
284
  const url = new URL(request.url)
@@ -280,7 +288,7 @@ export class CommercetoolsMock {
280
288
  print.error()
281
289
  },
282
290
  })
283
-
284
- _globalListeners.push(this._mswServer)
291
+ _globalListeners.push(server)
292
+ this._mswServer = server
285
293
  }
286
294
  }