@schmock/cli 1.4.0 → 1.8.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/cli.d.ts.map +1 -1
- package/dist/cli.js +9 -51
- package/dist/index.d.ts +1 -1
- package/dist/index.js +65 -79
- package/package.json +3 -3
package/dist/cli.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAaxC,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,IAAI,IAAI,CAAC;CACf;AA0ED,wBAAsB,eAAe,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC,CA0E7E;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,UAAU,GAAG;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAqC3E;AAqBD,MAAM,WAAW,WAAW;IAC1B,KAAK,IAAI,IAAI,CAAC;CACf;AAED;;GAEG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,SAAS,EAClB,OAAO,EAAE,UAAU,GAClB,OAAO,CAAC,SAAS,CAAC,CAQpB;AAED;;GAEG;AACH,wBAAgB,UAAU,CACxB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,UAAU,EACnB,gBAAgB,EAAE,MAAM,SAAS,EACjC,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,IAAI,GACpC,WAAW,CA2Bb;AAED,wBAAsB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAiDvD"}
|
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { readFileSync, watch } from "node:fs";
|
|
3
3
|
import { createServer } from "node:http";
|
|
4
4
|
import { parseArgs } from "node:util";
|
|
5
|
-
import { schmock, toHttpMethod } from "@schmock/core";
|
|
5
|
+
import { collectBody, parseNodeHeaders, parseNodeQuery, schmock, toHttpMethod, writeSchmockResponse, } from "@schmock/core";
|
|
6
6
|
import { openapi } from "@schmock/openapi";
|
|
7
7
|
const CORS_HEADERS = {
|
|
8
8
|
"access-control-allow-origin": "*",
|
|
@@ -90,56 +90,14 @@ export async function createCliServer(options) {
|
|
|
90
90
|
return;
|
|
91
91
|
}
|
|
92
92
|
const method = toHttpMethod(req.method ?? "GET");
|
|
93
|
-
const headers =
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
query[key] = value;
|
|
102
|
-
});
|
|
103
|
-
const chunks = [];
|
|
104
|
-
req.on("data", (chunk) => chunks.push(chunk));
|
|
105
|
-
req.on("end", () => {
|
|
106
|
-
const raw = Buffer.concat(chunks).toString();
|
|
107
|
-
let body;
|
|
108
|
-
const contentType = headers["content-type"] ?? "";
|
|
109
|
-
if (raw && contentType.includes("json")) {
|
|
110
|
-
try {
|
|
111
|
-
body = JSON.parse(raw);
|
|
112
|
-
}
|
|
113
|
-
catch {
|
|
114
|
-
body = raw;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
else if (raw) {
|
|
118
|
-
body = raw;
|
|
119
|
-
}
|
|
120
|
-
void mock
|
|
121
|
-
.handle(method, path, { headers, body, query })
|
|
122
|
-
.then((schmockResponse) => {
|
|
123
|
-
const responseHeaders = {
|
|
124
|
-
...schmockResponse.headers,
|
|
125
|
-
};
|
|
126
|
-
if (cors) {
|
|
127
|
-
Object.assign(responseHeaders, CORS_HEADERS);
|
|
128
|
-
}
|
|
129
|
-
if (!responseHeaders["content-type"] &&
|
|
130
|
-
schmockResponse.body !== undefined &&
|
|
131
|
-
typeof schmockResponse.body !== "string") {
|
|
132
|
-
responseHeaders["content-type"] = "application/json";
|
|
133
|
-
}
|
|
134
|
-
const responseBody = schmockResponse.body === undefined
|
|
135
|
-
? undefined
|
|
136
|
-
: typeof schmockResponse.body === "string"
|
|
137
|
-
? schmockResponse.body
|
|
138
|
-
: JSON.stringify(schmockResponse.body);
|
|
139
|
-
res.writeHead(schmockResponse.status, responseHeaders);
|
|
140
|
-
res.end(responseBody);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
93
|
+
const headers = parseNodeHeaders(req);
|
|
94
|
+
const query = parseNodeQuery(url);
|
|
95
|
+
void collectBody(req, headers).then((body) => mock
|
|
96
|
+
.handle(method, path, { headers, body, query })
|
|
97
|
+
.then((schmockResponse) => {
|
|
98
|
+
const extra = cors ? CORS_HEADERS : undefined;
|
|
99
|
+
writeSchmockResponse(res, schmockResponse, extra);
|
|
100
|
+
}));
|
|
143
101
|
});
|
|
144
102
|
return new Promise((resolve, reject) => {
|
|
145
103
|
httpServer.on("error", reject);
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
2
|
-
// @bun
|
|
1
|
+
#!/usr/bin/env node
|
|
3
2
|
import { createRequire } from "node:module";
|
|
4
3
|
var __create = Object.create;
|
|
5
4
|
var __getProtoOf = Object.getPrototypeOf;
|
|
@@ -23256,6 +23255,59 @@ class ResourceLimitError extends SchmockError {
|
|
|
23256
23255
|
}
|
|
23257
23256
|
}
|
|
23258
23257
|
|
|
23258
|
+
// ../core/src/http-helpers.ts
|
|
23259
|
+
function parseNodeHeaders(req) {
|
|
23260
|
+
const headers = {};
|
|
23261
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
23262
|
+
if (typeof value === "string") {
|
|
23263
|
+
headers[key] = value;
|
|
23264
|
+
}
|
|
23265
|
+
}
|
|
23266
|
+
return headers;
|
|
23267
|
+
}
|
|
23268
|
+
function parseNodeQuery(url) {
|
|
23269
|
+
const query = {};
|
|
23270
|
+
url.searchParams.forEach((value, key) => {
|
|
23271
|
+
query[key] = value;
|
|
23272
|
+
});
|
|
23273
|
+
return query;
|
|
23274
|
+
}
|
|
23275
|
+
function collectBody(req, headers) {
|
|
23276
|
+
return new Promise((resolve) => {
|
|
23277
|
+
const chunks = [];
|
|
23278
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
23279
|
+
req.on("end", () => {
|
|
23280
|
+
const raw = Buffer.concat(chunks).toString();
|
|
23281
|
+
if (!raw) {
|
|
23282
|
+
resolve(undefined);
|
|
23283
|
+
return;
|
|
23284
|
+
}
|
|
23285
|
+
const contentType = headers["content-type"] ?? "";
|
|
23286
|
+
if (contentType.includes("json")) {
|
|
23287
|
+
try {
|
|
23288
|
+
resolve(JSON.parse(raw));
|
|
23289
|
+
} catch {
|
|
23290
|
+
resolve(raw);
|
|
23291
|
+
}
|
|
23292
|
+
} else {
|
|
23293
|
+
resolve(raw);
|
|
23294
|
+
}
|
|
23295
|
+
});
|
|
23296
|
+
});
|
|
23297
|
+
}
|
|
23298
|
+
function writeSchmockResponse(res, response, extraHeaders) {
|
|
23299
|
+
const responseHeaders = {
|
|
23300
|
+
...response.headers,
|
|
23301
|
+
...extraHeaders
|
|
23302
|
+
};
|
|
23303
|
+
if (!responseHeaders["content-type"] && response.body !== undefined && typeof response.body !== "string") {
|
|
23304
|
+
responseHeaders["content-type"] = "application/json";
|
|
23305
|
+
}
|
|
23306
|
+
const responseBody = response.body === undefined ? undefined : typeof response.body === "string" ? response.body : JSON.stringify(response.body);
|
|
23307
|
+
res.writeHead(response.status, responseHeaders);
|
|
23308
|
+
res.end(responseBody);
|
|
23309
|
+
}
|
|
23310
|
+
|
|
23259
23311
|
// ../core/src/parser.ts
|
|
23260
23312
|
function parseRouteKey(routeKey) {
|
|
23261
23313
|
const match = routeKey.match(/^(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS) (.+)$/);
|
|
@@ -23496,43 +23548,11 @@ class CallableMockInstance {
|
|
|
23496
23548
|
const url = new URL(req.url ?? "/", `http://${req.headers.host}`);
|
|
23497
23549
|
const method = toHttpMethod(req.method ?? "GET");
|
|
23498
23550
|
const path = url.pathname;
|
|
23499
|
-
const headers =
|
|
23500
|
-
|
|
23501
|
-
|
|
23502
|
-
|
|
23503
|
-
|
|
23504
|
-
}
|
|
23505
|
-
const query = {};
|
|
23506
|
-
url.searchParams.forEach((value, key) => {
|
|
23507
|
-
query[key] = value;
|
|
23508
|
-
});
|
|
23509
|
-
const chunks = [];
|
|
23510
|
-
req.on("data", (chunk) => chunks.push(chunk));
|
|
23511
|
-
req.on("end", () => {
|
|
23512
|
-
const raw = Buffer.concat(chunks).toString();
|
|
23513
|
-
let body;
|
|
23514
|
-
const contentType = headers["content-type"] ?? "";
|
|
23515
|
-
if (raw && contentType.includes("json")) {
|
|
23516
|
-
try {
|
|
23517
|
-
body = JSON.parse(raw);
|
|
23518
|
-
} catch {
|
|
23519
|
-
body = raw;
|
|
23520
|
-
}
|
|
23521
|
-
} else if (raw) {
|
|
23522
|
-
body = raw;
|
|
23523
|
-
}
|
|
23524
|
-
this.handle(method, path, { headers, body, query }).then((schmockResponse) => {
|
|
23525
|
-
const responseHeaders = {
|
|
23526
|
-
...schmockResponse.headers
|
|
23527
|
-
};
|
|
23528
|
-
if (!responseHeaders["content-type"] && schmockResponse.body !== undefined && typeof schmockResponse.body !== "string") {
|
|
23529
|
-
responseHeaders["content-type"] = "application/json";
|
|
23530
|
-
}
|
|
23531
|
-
const responseBody = schmockResponse.body === undefined ? undefined : typeof schmockResponse.body === "string" ? schmockResponse.body : JSON.stringify(schmockResponse.body);
|
|
23532
|
-
res.writeHead(schmockResponse.status, responseHeaders);
|
|
23533
|
-
res.end(responseBody);
|
|
23534
|
-
});
|
|
23535
|
-
});
|
|
23551
|
+
const headers = parseNodeHeaders(req);
|
|
23552
|
+
const query = parseNodeQuery(url);
|
|
23553
|
+
collectBody(req, headers).then((body) => this.handle(method, path, { headers, body, query }).then((schmockResponse) => {
|
|
23554
|
+
writeSchmockResponse(res, schmockResponse);
|
|
23555
|
+
}));
|
|
23536
23556
|
});
|
|
23537
23557
|
this.server = httpServer;
|
|
23538
23558
|
return new Promise((resolve, reject) => {
|
|
@@ -33967,46 +33987,12 @@ async function createCliServer(options) {
|
|
|
33967
33987
|
return;
|
|
33968
33988
|
}
|
|
33969
33989
|
const method = toHttpMethod(req.method ?? "GET");
|
|
33970
|
-
const headers =
|
|
33971
|
-
|
|
33972
|
-
|
|
33973
|
-
|
|
33974
|
-
|
|
33975
|
-
}
|
|
33976
|
-
const query = {};
|
|
33977
|
-
url.searchParams.forEach((value, key) => {
|
|
33978
|
-
query[key] = value;
|
|
33979
|
-
});
|
|
33980
|
-
const chunks = [];
|
|
33981
|
-
req.on("data", (chunk) => chunks.push(chunk));
|
|
33982
|
-
req.on("end", () => {
|
|
33983
|
-
const raw = Buffer.concat(chunks).toString();
|
|
33984
|
-
let body;
|
|
33985
|
-
const contentType = headers["content-type"] ?? "";
|
|
33986
|
-
if (raw && contentType.includes("json")) {
|
|
33987
|
-
try {
|
|
33988
|
-
body = JSON.parse(raw);
|
|
33989
|
-
} catch {
|
|
33990
|
-
body = raw;
|
|
33991
|
-
}
|
|
33992
|
-
} else if (raw) {
|
|
33993
|
-
body = raw;
|
|
33994
|
-
}
|
|
33995
|
-
mock.handle(method, path, { headers, body, query }).then((schmockResponse) => {
|
|
33996
|
-
const responseHeaders = {
|
|
33997
|
-
...schmockResponse.headers
|
|
33998
|
-
};
|
|
33999
|
-
if (cors) {
|
|
34000
|
-
Object.assign(responseHeaders, CORS_HEADERS);
|
|
34001
|
-
}
|
|
34002
|
-
if (!responseHeaders["content-type"] && schmockResponse.body !== undefined && typeof schmockResponse.body !== "string") {
|
|
34003
|
-
responseHeaders["content-type"] = "application/json";
|
|
34004
|
-
}
|
|
34005
|
-
const responseBody = schmockResponse.body === undefined ? undefined : typeof schmockResponse.body === "string" ? schmockResponse.body : JSON.stringify(schmockResponse.body);
|
|
34006
|
-
res.writeHead(schmockResponse.status, responseHeaders);
|
|
34007
|
-
res.end(responseBody);
|
|
34008
|
-
});
|
|
34009
|
-
});
|
|
33990
|
+
const headers = parseNodeHeaders(req);
|
|
33991
|
+
const query = parseNodeQuery(url);
|
|
33992
|
+
collectBody(req, headers).then((body) => mock.handle(method, path, { headers, body, query }).then((schmockResponse) => {
|
|
33993
|
+
const extra = cors ? CORS_HEADERS : undefined;
|
|
33994
|
+
writeSchmockResponse(res, schmockResponse, extra);
|
|
33995
|
+
}));
|
|
34010
33996
|
});
|
|
34011
33997
|
return new Promise((resolve2, reject) => {
|
|
34012
33998
|
httpServer.on("error", reject);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schmock/cli",
|
|
3
3
|
"description": "CLI for running Schmock mock servers from OpenAPI specs",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
},
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@schmock/core": "^1.
|
|
36
|
-
"@schmock/openapi": "^1.
|
|
35
|
+
"@schmock/core": "^1.8.0",
|
|
36
|
+
"@schmock/openapi": "^1.8.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@amiceli/vitest-cucumber": "^6.2.0",
|