@labdigital/commercetools-mock 1.11.0 → 2.0.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.d.cts CHANGED
@@ -630,7 +630,7 @@ declare class CommercetoolsMock {
630
630
  options: CommercetoolsMockOptions;
631
631
  private _storage;
632
632
  private _oauth2;
633
- private _nockScopes;
633
+ private _mswServer;
634
634
  private _services;
635
635
  private _repositories;
636
636
  private _projectService?;
@@ -641,8 +641,7 @@ declare class CommercetoolsMock {
641
641
  project(projectKey?: string): ProjectAPI;
642
642
  runServer(port?: number, options?: AppOptions): void;
643
643
  private createApp;
644
- private mockApiHost;
645
- private mockAuthHost;
644
+ private startServer;
646
645
  }
647
646
 
648
647
  declare const getBaseResourceProperties: () => {
package/dist/index.d.ts CHANGED
@@ -630,7 +630,7 @@ declare class CommercetoolsMock {
630
630
  options: CommercetoolsMockOptions;
631
631
  private _storage;
632
632
  private _oauth2;
633
- private _nockScopes;
633
+ private _mswServer;
634
634
  private _services;
635
635
  private _repositories;
636
636
  private _projectService?;
@@ -641,8 +641,7 @@ declare class CommercetoolsMock {
641
641
  project(projectKey?: string): ProjectAPI;
642
642
  runServer(port?: number, options?: AppOptions): void;
643
643
  private createApp;
644
- private mockApiHost;
645
- private mockAuthHost;
644
+ private startServer;
646
645
  }
647
646
 
648
647
  declare const getBaseResourceProperties: () => {
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  // src/ctMock.ts
2
- import nock from "nock";
3
2
  import express2 from "express";
4
3
  import supertest from "supertest";
5
4
  import morgan from "morgan";
5
+ import { setupServer } from "msw/node";
6
+ import { http, HttpResponse } from "msw";
6
7
 
7
8
  // src/storage/abstract.ts
8
9
  var AbstractStorage = class {
@@ -1378,19 +1379,19 @@ var ProjectAPI = class {
1378
1379
 
1379
1380
  // src/lib/proxy.ts
1380
1381
  var copyHeaders = (headers) => {
1381
- const validHeaders = ["accept", "host", "authorization"];
1382
+ const validHeaders = ["accept", "host", "authorization", "content-type"];
1382
1383
  const result = {};
1383
- Object.entries(headers).forEach(([key, value]) => {
1384
+ for (const [key, value] of headers.entries()) {
1384
1385
  if (validHeaders.includes(key.toLowerCase())) {
1385
1386
  result[key] = value;
1386
1387
  }
1387
- });
1388
+ }
1388
1389
  return result;
1389
1390
  };
1390
1391
 
1391
1392
  // src/constants.ts
1392
- var DEFAULT_API_HOSTNAME = /^https:\/\/api\..*?\.commercetools.com:443$/;
1393
- var DEFAULT_AUTH_HOSTNAME = /^https:\/\/auth\..*?\.commercetools.com:443$/;
1393
+ var DEFAULT_API_HOSTNAME = "https://api.*.commercetools.com";
1394
+ var DEFAULT_AUTH_HOSTNAME = "https://auth.*.commercetools.com";
1394
1395
 
1395
1396
  // src/repositories/helpers.ts
1396
1397
  import { v4 as uuidv42 } from "uuid";
@@ -6365,12 +6366,13 @@ var DEFAULT_OPTIONS = {
6365
6366
  authHost: DEFAULT_AUTH_HOSTNAME,
6366
6367
  silent: false
6367
6368
  };
6369
+ var _globalListeners = [];
6368
6370
  var CommercetoolsMock = class {
6369
6371
  app;
6370
6372
  options;
6371
6373
  _storage;
6372
6374
  _oauth2;
6373
- _nockScopes = { auth: void 0, api: void 0 };
6375
+ _mswServer = void 0;
6374
6376
  _services;
6375
6377
  _repositories;
6376
6378
  _projectService;
@@ -6387,16 +6389,15 @@ var CommercetoolsMock = class {
6387
6389
  this.app = this.createApp({ silent: this.options.silent });
6388
6390
  }
6389
6391
  start() {
6390
- this.mockAuthHost();
6391
- this.mockApiHost();
6392
+ this.clear();
6393
+ this.startServer();
6392
6394
  }
6393
6395
  stop() {
6394
- this._nockScopes.auth?.persist(false);
6395
- this._nockScopes.auth = void 0;
6396
- this._nockScopes.api?.persist(false);
6397
- this._nockScopes.api = void 0;
6396
+ this._mswServer?.close();
6397
+ this._mswServer = void 0;
6398
6398
  }
6399
6399
  clear() {
6400
+ this._mswServer?.resetHandlers();
6400
6401
  this._storage.clear();
6401
6402
  }
6402
6403
  project(projectKey) {
@@ -6459,25 +6460,65 @@ var CommercetoolsMock = class {
6459
6460
  });
6460
6461
  return app;
6461
6462
  }
6462
- mockApiHost() {
6463
- const app = this.app;
6464
- this._nockScopes.api = nock(this.options.apiHost).persist().get(/.*/).reply(async function(uri) {
6465
- const response = await supertest(app).get(uri).set(copyHeaders(this.req.headers));
6466
- return [response.status, response.body];
6467
- }).post(/.*/).reply(async function(uri, body) {
6468
- const response = await supertest(app).post(uri).set(copyHeaders(this.req.headers)).send(body);
6469
- return [response.status, response.body];
6470
- }).delete(/.*/).reply(async function(uri, body) {
6471
- const response = await supertest(app).delete(uri).set(copyHeaders(this.req.headers)).send(body);
6472
- return [response.status, response.body];
6473
- });
6474
- }
6475
- mockAuthHost() {
6463
+ startServer() {
6464
+ if (_globalListeners.length > 0) {
6465
+ if (this._mswServer !== void 0) {
6466
+ throw new Error("Server already started");
6467
+ } else {
6468
+ console.warn("Server wasn't stopped properly, clearing");
6469
+ _globalListeners.forEach((listener) => listener.close());
6470
+ }
6471
+ }
6476
6472
  const app = this.app;
6477
- this._nockScopes.auth = nock(this.options.authHost).persist().post(/^\/oauth\/.*/).reply(async function(uri, body) {
6478
- const response = await supertest(app).post(uri + "?" + body).set(copyHeaders(this.req.headers)).send();
6479
- return [response.status, response.body];
6473
+ this._mswServer = setupServer(
6474
+ http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
6475
+ const text = await request.text();
6476
+ const url = new URL(request.url);
6477
+ const res = await supertest(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(text);
6478
+ return new HttpResponse(res.text, {
6479
+ status: res.status,
6480
+ headers: res.headers
6481
+ });
6482
+ }),
6483
+ http.get(`${this.options.apiHost}/*`, async ({ request }) => {
6484
+ const body = await request.text();
6485
+ const url = new URL(request.url);
6486
+ const res = await supertest(app).get(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6487
+ return new HttpResponse(res.text, {
6488
+ status: res.status,
6489
+ headers: res.headers
6490
+ });
6491
+ }),
6492
+ http.post(`${this.options.apiHost}/*`, async ({ request }) => {
6493
+ const body = await request.text();
6494
+ const url = new URL(request.url);
6495
+ const res = await supertest(app).post(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6496
+ return new HttpResponse(res.text, {
6497
+ status: res.status,
6498
+ headers: res.headers
6499
+ });
6500
+ }),
6501
+ http.delete(`${this.options.apiHost}/*`, async ({ request }) => {
6502
+ const body = await request.text();
6503
+ const url = new URL(request.url);
6504
+ const res = await supertest(app).delete(url.pathname + "?" + url.searchParams.toString()).set(copyHeaders(request.headers)).send(body);
6505
+ return new HttpResponse(res.text, {
6506
+ status: res.status,
6507
+ headers: res.headers
6508
+ });
6509
+ })
6510
+ );
6511
+ this._mswServer.listen({
6512
+ // We need to allow requests done by supertest
6513
+ onUnhandledRequest: (request, print) => {
6514
+ const url = new URL(request.url);
6515
+ if (url.hostname === "127.0.0.1") {
6516
+ return;
6517
+ }
6518
+ print.error();
6519
+ }
6480
6520
  });
6521
+ _globalListeners.push(this._mswServer);
6481
6522
  }
6482
6523
  };
6483
6524
  export {