@scalar/mock-server 0.1.69 → 0.1.71
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/CHANGELOG.md +13 -0
- package/dist/index.cjs +58 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.js +58 -4
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @scalar/mock-server
|
|
2
2
|
|
|
3
|
+
## 0.1.71
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [cc5402c]
|
|
8
|
+
- @scalar/oas-utils@0.1.16
|
|
9
|
+
|
|
10
|
+
## 0.1.70
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 32b08ad: feat: http basic auth middleware
|
|
15
|
+
|
|
3
16
|
## 0.1.69
|
|
4
17
|
|
|
5
18
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,7 @@ var oasUtils = require('@scalar/oas-utils');
|
|
|
4
4
|
var openapiParser = require('@scalar/openapi-parser');
|
|
5
5
|
var hono = require('hono');
|
|
6
6
|
var cors = require('hono/cors');
|
|
7
|
+
var httpException = require('hono/http-exception');
|
|
7
8
|
|
|
8
9
|
function findPreferredResponseKey(responses) {
|
|
9
10
|
return ["default", "200", "201", "204", "404", "500"].find(
|
|
@@ -15,6 +16,50 @@ function routeFromPath(path) {
|
|
|
15
16
|
return path.replace(/{/g, ":").replace(/}/g, "");
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
function isAuthenticationRequired(security) {
|
|
20
|
+
if (!security) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
if (Array.isArray(security) && !security.length) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if ((security ?? []).some(
|
|
27
|
+
(securityRequirement) => !Object.keys(securityRequirement).length
|
|
28
|
+
)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function anyBasicAuthentication() {
|
|
35
|
+
return async function(ctx, next) {
|
|
36
|
+
if (ctx.req.header("Authorization")?.startsWith("Basic ")) {
|
|
37
|
+
return await next();
|
|
38
|
+
}
|
|
39
|
+
throw new httpException.HTTPException(401, {
|
|
40
|
+
res: new Response("Unauthorized", {
|
|
41
|
+
status: 401,
|
|
42
|
+
headers: {
|
|
43
|
+
"WWW-Authenticate": 'Basic realm="Authentication Required"'
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isBasicAuthenticationRequired(operation, schema) {
|
|
51
|
+
const allowedSecuritySchemes = operation.security?.map(
|
|
52
|
+
(securityScheme) => {
|
|
53
|
+
return Object.keys(securityScheme)[0];
|
|
54
|
+
}
|
|
55
|
+
);
|
|
56
|
+
const httpBasicAuthIsRequired = allowedSecuritySchemes?.findIndex((securitySchemeKey) => {
|
|
57
|
+
const securityScheme = schema?.components?.securitySchemes?.[securitySchemeKey];
|
|
58
|
+
return securityScheme?.type === "http" && securityScheme?.scheme === "basic";
|
|
59
|
+
}) !== void 0;
|
|
60
|
+
return httpBasicAuthIsRequired;
|
|
61
|
+
}
|
|
62
|
+
|
|
18
63
|
async function createMockServer(options) {
|
|
19
64
|
const app = new hono.Hono();
|
|
20
65
|
const result = await openapiParser.openapi().load(options?.specification ?? {}).resolve();
|
|
@@ -34,15 +79,24 @@ async function createMockServer(options) {
|
|
|
34
79
|
Object.keys(result.schema?.paths ?? {}).forEach((path) => {
|
|
35
80
|
Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {
|
|
36
81
|
const route = routeFromPath(path);
|
|
82
|
+
const operation = result.schema?.paths?.[path]?.[method];
|
|
83
|
+
const requiresAuthentication = isAuthenticationRequired(
|
|
84
|
+
operation.security
|
|
85
|
+
);
|
|
86
|
+
const requiresBasicAuthentication = isBasicAuthenticationRequired(
|
|
87
|
+
operation,
|
|
88
|
+
result?.schema
|
|
89
|
+
);
|
|
90
|
+
if (requiresAuthentication && requiresBasicAuthentication) {
|
|
91
|
+
app[method](route, anyBasicAuthentication());
|
|
92
|
+
}
|
|
37
93
|
app[method](route, (c) => {
|
|
38
94
|
if (options?.onRequest) {
|
|
39
95
|
options.onRequest({
|
|
40
96
|
context: c,
|
|
41
|
-
|
|
42
|
-
operation: result.schema.paths[path][method]
|
|
97
|
+
operation
|
|
43
98
|
});
|
|
44
99
|
}
|
|
45
|
-
const operation = result.schema?.paths?.[path]?.[method];
|
|
46
100
|
const preferredResponseKey = findPreferredResponseKey(
|
|
47
101
|
Object.keys(operation.responses ?? {})
|
|
48
102
|
);
|
|
@@ -64,5 +118,6 @@ async function createMockServer(options) {
|
|
|
64
118
|
|
|
65
119
|
exports.createMockServer = createMockServer;
|
|
66
120
|
exports.findPreferredResponseKey = findPreferredResponseKey;
|
|
121
|
+
exports.isAuthenticationRequired = isAuthenticationRequired;
|
|
67
122
|
exports.routeFromPath = routeFromPath;
|
|
68
123
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/utils/findPreferredResponseKey.ts","../src/utils/routeFromPath.ts","../src/createMockServer.ts"],"sourcesContent":["/**\n * Find the preferred response key: default, 200, 201 …\n */\nexport function findPreferredResponseKey(responses?: string[]) {\n return ['default', '200', '201', '204', '404', '500'].find(\n (key) => responses?.includes(key) ?? false,\n )\n}\n","/**\n * Convert path to route\n * Example: /posts/{id} -> /posts/:id\n */\nexport function routeFromPath(path: string) {\n return path.replace(/{/g, ':').replace(/}/g, '')\n}\n","import { getExampleFromSchema } from '@scalar/oas-utils'\nimport { type ResolvedOpenAPI, openapi } from '@scalar/openapi-parser'\nimport { type Context, Hono } from 'hono'\nimport { cors } from 'hono/cors'\nimport type { StatusCode } from 'hono/utils/http-status'\n\nimport { findPreferredResponseKey, routeFromPath } from './utils'\n\n/**\n * Create a mock server instance\n */\nexport async function createMockServer(options?: {\n specification: string | Record<string, any>\n onRequest?: (data: {\n context: Context\n operation: ResolvedOpenAPI.Operation\n }) => void\n}) {\n const app = new Hono()\n\n // Resolve references\n const result = await openapi()\n .load(options?.specification ?? {})\n .resolve()\n\n // CORS headers\n app.use(cors())\n\n // OpenAPI JSON file\n app.get('/openapi.json', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.json(openapi().load(options.specification).get())\n })\n\n // OpenAPI YAML file\n app.get('/openapi.yaml', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.text(openapi().load(options.specification).toYaml())\n })\n\n // Paths\n Object.keys(result.schema?.paths ?? {}).forEach((path) => {\n // Request methods\n Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {\n const route = routeFromPath(path)\n\n // Route\n // @ts-expect-error Needs a proper type\n app[method](route, (c: Context) => {\n // Call onRequest callback\n if (options?.onRequest) {\n options.onRequest({\n context: c,\n // @ts-expect-error Needs a proper type\n operation: result.schema.paths[path][method],\n })\n }\n\n // Response\n // @ts-expect-error Needs a proper type\n const operation = result.schema?.paths?.[path]?.[method]\n\n // default, 200, 201 …\n const preferredResponseKey = findPreferredResponseKey(\n Object.keys(operation.responses ?? {}),\n )\n\n // Focus on JSON for now\n const jsonResponse = preferredResponseKey\n ? operation.responses?.[preferredResponseKey]?.content?.[\n 'application/json'\n ]\n : null\n\n // Get or generate JSON\n const response = jsonResponse?.example\n ? jsonResponse.example\n : jsonResponse?.schema\n ? getExampleFromSchema(jsonResponse.schema, {\n emptyString: '…',\n variables: c.req.param(),\n })\n : null\n\n // Status code\n const statusCode = parseInt(\n preferredResponseKey === 'default'\n ? '200'\n : preferredResponseKey ?? '200',\n 10,\n ) as StatusCode\n\n return c.json(response,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E,6BAAqB,CAAA,YAAA,CAAa,MAAQ,EAAA;AAAA,UACxC,WAAa,EAAA,QAAA;AAAA,UACb,SAAA,EAAW,CAAE,CAAA,GAAA,CAAI,KAAM,EAAA;AAAA,SACxB,CACD,GAAA,IAAA,CAAA;AAGN,QAAA,MAAM,UAAa,GAAA,QAAA;AAAA,UACjB,oBAAA,KAAyB,SACrB,GAAA,KAAA,GACA,oBAAwB,IAAA,KAAA;AAAA,UAC5B,EAAA;AAAA,SACF,CAAA;AAEA,QAAO,OAAA,CAAA,CAAE,IAAK,CAAA,QAAA,EAAU,UAAU,CAAA,CAAA;AAAA,OACnC,CAAA,CAAA;AAAA,KACF,CAAA,CAAA;AAAA,GACF,CAAA,CAAA;AAED,EAAO,OAAA,GAAA,CAAA;AACT;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/utils/findPreferredResponseKey.ts","../src/utils/routeFromPath.ts","../src/utils/isAuthenticationRequired.ts","../src/utils/anyBasicAuthentication.ts","../src/utils/isBasicAuthenticationRequired.ts","../src/createMockServer.ts"],"sourcesContent":["/**\n * Find the preferred response key: default, 200, 201 …\n */\nexport function findPreferredResponseKey(responses?: string[]) {\n return ['default', '200', '201', '204', '404', '500'].find(\n (key) => responses?.includes(key) ?? false,\n )\n}\n","/**\n * Convert path to route\n * Example: /posts/{id} -> /posts/:id\n */\nexport function routeFromPath(path: string) {\n return path.replace(/{/g, ':').replace(/}/g, '')\n}\n","import type { OpenAPIV3 } from '@scalar/openapi-parser'\n\n/**\n * Check whether the given security scheme key is in the `security` configuration for this operation.\n **/\nexport function isAuthenticationRequired(\n security?: OpenAPIV3.SecurityRequirementObject[],\n): boolean {\n // If security is not defined, auth is not required.\n if (!security) {\n return false\n }\n\n // Don’t require auth if security is just an empty array []\n if (Array.isArray(security) && !security.length) {\n return false\n }\n\n // Includes empty object = auth is not required\n if (\n (security ?? []).some(\n (securityRequirement) => !Object.keys(securityRequirement).length,\n )\n ) {\n return false\n }\n\n return true\n}\n","import type { Context } from 'hono'\nimport { HTTPException } from 'hono/http-exception'\n\n/**\n * Middleware to check for any basic authentication header\n */\nexport function anyBasicAuthentication() {\n return async function (ctx: Context, next: () => Promise<void>) {\n // Check if the request has an Authorization header\n // Note: We don’t care *what* credentials are sent, though.\n if (ctx.req.header('Authorization')?.startsWith('Basic ')) {\n return await next()\n }\n\n // Unauthorized\n throw new HTTPException(401, {\n res: new Response('Unauthorized', {\n status: 401,\n headers: {\n 'WWW-Authenticate': 'Basic realm=\"Authentication Required\"',\n },\n }),\n })\n }\n}\n","import type {\n OpenAPI,\n OpenAPIV3_1,\n ResolvedOpenAPI,\n} from '@scalar/openapi-parser'\n\nexport function isBasicAuthenticationRequired(\n operation: OpenAPI.Operation,\n schema?: ResolvedOpenAPI.Document,\n) {\n const allowedSecuritySchemes = operation.security?.map(\n (securityScheme: OpenAPIV3_1.SecurityRequirementObject) => {\n return Object.keys(securityScheme)[0]\n },\n )\n // Check if one of them is HTTP Basic Auth\n const httpBasicAuthIsRequired =\n allowedSecuritySchemes?.findIndex((securitySchemeKey: string) => {\n const securityScheme =\n schema?.components?.securitySchemes?.[securitySchemeKey]\n\n return (\n securityScheme?.type === 'http' && securityScheme?.scheme === 'basic'\n )\n }) !== undefined\n return httpBasicAuthIsRequired\n}\n","import { getExampleFromSchema } from '@scalar/oas-utils'\nimport { type ResolvedOpenAPI, openapi } from '@scalar/openapi-parser'\nimport { type Context, Hono } from 'hono'\nimport { cors } from 'hono/cors'\nimport type { StatusCode } from 'hono/utils/http-status'\n\nimport {\n findPreferredResponseKey,\n isAuthenticationRequired,\n routeFromPath,\n} from './utils'\nimport { anyBasicAuthentication } from './utils/anyBasicAuthentication'\nimport { isBasicAuthenticationRequired } from './utils/isBasicAuthenticationRequired'\n\n/**\n * Create a mock server instance\n */\nexport async function createMockServer(options?: {\n specification: string | Record<string, any>\n onRequest?: (data: {\n context: Context\n operation: ResolvedOpenAPI.Operation\n }) => void\n}) {\n const app = new Hono()\n\n // Resolve references\n const result = await openapi()\n .load(options?.specification ?? {})\n .resolve()\n\n // CORS headers\n app.use(cors())\n\n // OpenAPI JSON file\n app.get('/openapi.json', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.json(openapi().load(options.specification).get())\n })\n\n // OpenAPI YAML file\n app.get('/openapi.yaml', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.text(openapi().load(options.specification).toYaml())\n })\n\n // Paths\n Object.keys(result.schema?.paths ?? {}).forEach((path) => {\n // Operations\n Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {\n const route = routeFromPath(path)\n\n // @ts-expect-error Needs a proper type\n const operation = result.schema?.paths?.[path]?.[method]\n\n // Check if authentication is required\n const requiresAuthentication = isAuthenticationRequired(\n operation.security,\n )\n // Get all available authentication methods\n const requiresBasicAuthentication = isBasicAuthenticationRequired(\n operation,\n result?.schema,\n )\n\n // Add HTTP basic authentication\n if (requiresAuthentication && requiresBasicAuthentication) {\n // @ts-expect-error Needs a proper type\n app[method](route, anyBasicAuthentication())\n }\n\n // Route\n // @ts-expect-error Needs a proper type\n app[method](route, (c: Context) => {\n // Call onRequest callback\n if (options?.onRequest) {\n options.onRequest({\n context: c,\n operation,\n })\n }\n\n // Response\n // default, 200, 201 …\n const preferredResponseKey = findPreferredResponseKey(\n Object.keys(operation.responses ?? {}),\n )\n\n // Focus on JSON for now\n const jsonResponse = preferredResponseKey\n ? operation.responses?.[preferredResponseKey]?.content?.[\n 'application/json'\n ]\n : null\n\n // Get or generate JSON\n const response = jsonResponse?.example\n ? jsonResponse.example\n : jsonResponse?.schema\n ? getExampleFromSchema(jsonResponse.schema, {\n emptyString: '…',\n variables: c.req.param(),\n })\n : null\n\n // Status code\n const statusCode = parseInt(\n preferredResponseKey === 'default'\n ? '200'\n : preferredResponseKey ?? '200',\n 10,\n ) as StatusCode\n\n return c.json(response, statusCode)\n })\n })\n })\n\n return app\n}\n"],"names":["HTTPException","Hono","openapi","c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e,KAAA;AAEjC,QAAA,IAAI,SAAS,SAAW,EAAA;AACtB,UAAA,OAAA,CAAQ,SAAU,CAAA;AAAA,YAChB,OAAS,EAAA,CAAA;AAAA,YACT,SAAA;AAAA,WACD,CAAA,CAAA;AAAA,SACH;AAIA,QAAA,MAAM,oBAAuB,GAAA,wBAAA;AAAA,UAC3B,MAAO,CAAA,IAAA,CAAK,SAAU,CAAA,SAAA,IAAa,EAAE,CAAA;AAAA,SACvC,CAAA;AAGA,QAAM,MAAA,YAAA,GAAe,uBACjB,SAAU,CAAA,SAAA,GAAY,oBAAoB,CAAG,EAAA,OAAA,GAC3C,kBACF,CACA,GAAA,IAAA,CAAA;AAGJ,QAAM,MAAA,QAAA,GAAW,cAAc,OAC3B,GAAA,YAAA,CAAa,UACb,YAAc,EAAA,MAAA,GACZE,6BAAqB,CAAA,YAAA,CAAa,MAAQ,EAAA;AAAA,UACxC,WAAa,EAAA,QAAA;AAAA,UACb,SAAA,EAAW,CAAE,CAAA,GAAA,CAAI,KAAM,EAAA;AAAA,SACxB,CACD,GAAA,IAAA,CAAA;AAGN,QAAA,MAAM,UAAa,GAAA,QAAA;AAAA,UACjB,oBAAA,KAAyB,SACrB,GAAA,KAAA,GACA,oBAAwB,IAAA,KAAA;AAAA,UAC5B,EAAA;AAAA,SACF,CAAA;AAEA,QAAO,OAAA,CAAA,CAAE,IAAK,CAAA,QAAA,EAAU,UAAU,CAAA,CAAA;AAAA,OACnC,CAAA,CAAA;AAAA,KACF,CAAA,CAAA;AAAA,GACF,CAAA,CAAA;AAED,EAAO,OAAA,GAAA,CAAA;AACT;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as hono_types from 'hono/types';
|
|
2
2
|
import * as hono from 'hono';
|
|
3
3
|
import { Context, Hono } from 'hono';
|
|
4
|
-
import { ResolvedOpenAPI } from '@scalar/openapi-parser';
|
|
4
|
+
import { ResolvedOpenAPI, OpenAPIV3 } from '@scalar/openapi-parser';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Create a mock server instance
|
|
@@ -25,4 +25,9 @@ declare function findPreferredResponseKey(responses?: string[]): string | undefi
|
|
|
25
25
|
*/
|
|
26
26
|
declare function routeFromPath(path: string): string;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Check whether the given security scheme key is in the `security` configuration for this operation.
|
|
30
|
+
**/
|
|
31
|
+
declare function isAuthenticationRequired(security?: OpenAPIV3.SecurityRequirementObject[]): boolean;
|
|
32
|
+
|
|
33
|
+
export { createMockServer, findPreferredResponseKey, isAuthenticationRequired, routeFromPath };
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { getExampleFromSchema } from '@scalar/oas-utils';
|
|
|
2
2
|
import { openapi } from '@scalar/openapi-parser';
|
|
3
3
|
import { Hono } from 'hono';
|
|
4
4
|
import { cors } from 'hono/cors';
|
|
5
|
+
import { HTTPException } from 'hono/http-exception';
|
|
5
6
|
|
|
6
7
|
function findPreferredResponseKey(responses) {
|
|
7
8
|
return ["default", "200", "201", "204", "404", "500"].find(
|
|
@@ -13,6 +14,50 @@ function routeFromPath(path) {
|
|
|
13
14
|
return path.replace(/{/g, ":").replace(/}/g, "");
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
function isAuthenticationRequired(security) {
|
|
18
|
+
if (!security) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(security) && !security.length) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
if ((security ?? []).some(
|
|
25
|
+
(securityRequirement) => !Object.keys(securityRequirement).length
|
|
26
|
+
)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function anyBasicAuthentication() {
|
|
33
|
+
return async function(ctx, next) {
|
|
34
|
+
if (ctx.req.header("Authorization")?.startsWith("Basic ")) {
|
|
35
|
+
return await next();
|
|
36
|
+
}
|
|
37
|
+
throw new HTTPException(401, {
|
|
38
|
+
res: new Response("Unauthorized", {
|
|
39
|
+
status: 401,
|
|
40
|
+
headers: {
|
|
41
|
+
"WWW-Authenticate": 'Basic realm="Authentication Required"'
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isBasicAuthenticationRequired(operation, schema) {
|
|
49
|
+
const allowedSecuritySchemes = operation.security?.map(
|
|
50
|
+
(securityScheme) => {
|
|
51
|
+
return Object.keys(securityScheme)[0];
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
const httpBasicAuthIsRequired = allowedSecuritySchemes?.findIndex((securitySchemeKey) => {
|
|
55
|
+
const securityScheme = schema?.components?.securitySchemes?.[securitySchemeKey];
|
|
56
|
+
return securityScheme?.type === "http" && securityScheme?.scheme === "basic";
|
|
57
|
+
}) !== void 0;
|
|
58
|
+
return httpBasicAuthIsRequired;
|
|
59
|
+
}
|
|
60
|
+
|
|
16
61
|
async function createMockServer(options) {
|
|
17
62
|
const app = new Hono();
|
|
18
63
|
const result = await openapi().load(options?.specification ?? {}).resolve();
|
|
@@ -32,15 +77,24 @@ async function createMockServer(options) {
|
|
|
32
77
|
Object.keys(result.schema?.paths ?? {}).forEach((path) => {
|
|
33
78
|
Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {
|
|
34
79
|
const route = routeFromPath(path);
|
|
80
|
+
const operation = result.schema?.paths?.[path]?.[method];
|
|
81
|
+
const requiresAuthentication = isAuthenticationRequired(
|
|
82
|
+
operation.security
|
|
83
|
+
);
|
|
84
|
+
const requiresBasicAuthentication = isBasicAuthenticationRequired(
|
|
85
|
+
operation,
|
|
86
|
+
result?.schema
|
|
87
|
+
);
|
|
88
|
+
if (requiresAuthentication && requiresBasicAuthentication) {
|
|
89
|
+
app[method](route, anyBasicAuthentication());
|
|
90
|
+
}
|
|
35
91
|
app[method](route, (c) => {
|
|
36
92
|
if (options?.onRequest) {
|
|
37
93
|
options.onRequest({
|
|
38
94
|
context: c,
|
|
39
|
-
|
|
40
|
-
operation: result.schema.paths[path][method]
|
|
95
|
+
operation
|
|
41
96
|
});
|
|
42
97
|
}
|
|
43
|
-
const operation = result.schema?.paths?.[path]?.[method];
|
|
44
98
|
const preferredResponseKey = findPreferredResponseKey(
|
|
45
99
|
Object.keys(operation.responses ?? {})
|
|
46
100
|
);
|
|
@@ -60,5 +114,5 @@ async function createMockServer(options) {
|
|
|
60
114
|
return app;
|
|
61
115
|
}
|
|
62
116
|
|
|
63
|
-
export { createMockServer, findPreferredResponseKey, routeFromPath };
|
|
117
|
+
export { createMockServer, findPreferredResponseKey, isAuthenticationRequired, routeFromPath };
|
|
64
118
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/utils/findPreferredResponseKey.ts","../src/utils/routeFromPath.ts","../src/createMockServer.ts"],"sourcesContent":["/**\n * Find the preferred response key: default, 200, 201 …\n */\nexport function findPreferredResponseKey(responses?: string[]) {\n return ['default', '200', '201', '204', '404', '500'].find(\n (key) => responses?.includes(key) ?? false,\n )\n}\n","/**\n * Convert path to route\n * Example: /posts/{id} -> /posts/:id\n */\nexport function routeFromPath(path: string) {\n return path.replace(/{/g, ':').replace(/}/g, '')\n}\n","import { getExampleFromSchema } from '@scalar/oas-utils'\nimport { type ResolvedOpenAPI, openapi } from '@scalar/openapi-parser'\nimport { type Context, Hono } from 'hono'\nimport { cors } from 'hono/cors'\nimport type { StatusCode } from 'hono/utils/http-status'\n\nimport { findPreferredResponseKey, routeFromPath } from './utils'\n\n/**\n * Create a mock server instance\n */\nexport async function createMockServer(options?: {\n specification: string | Record<string, any>\n onRequest?: (data: {\n context: Context\n operation: ResolvedOpenAPI.Operation\n }) => void\n}) {\n const app = new Hono()\n\n // Resolve references\n const result = await openapi()\n .load(options?.specification ?? {})\n .resolve()\n\n // CORS headers\n app.use(cors())\n\n // OpenAPI JSON file\n app.get('/openapi.json', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.json(openapi().load(options.specification).get())\n })\n\n // OpenAPI YAML file\n app.get('/openapi.yaml', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.text(openapi().load(options.specification).toYaml())\n })\n\n // Paths\n Object.keys(result.schema?.paths ?? {}).forEach((path) => {\n // Request methods\n Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {\n const route = routeFromPath(path)\n\n // Route\n // @ts-expect-error Needs a proper type\n app[method](route, (c: Context) => {\n // Call onRequest callback\n if (options?.onRequest) {\n options.onRequest({\n context: c,\n // @ts-expect-error Needs a proper type\n operation: result.schema.paths[path][method],\n })\n }\n\n // Response\n // @ts-expect-error Needs a proper type\n const operation = result.schema?.paths?.[path]?.[method]\n\n // default, 200, 201 …\n const preferredResponseKey = findPreferredResponseKey(\n Object.keys(operation.responses ?? {}),\n )\n\n // Focus on JSON for now\n const jsonResponse = preferredResponseKey\n ? operation.responses?.[preferredResponseKey]?.content?.[\n 'application/json'\n ]\n : null\n\n // Get or generate JSON\n const response = jsonResponse?.example\n ? jsonResponse.example\n : jsonResponse?.schema\n ? getExampleFromSchema(jsonResponse.schema, {\n emptyString: '…',\n variables: c.req.param(),\n })\n : null\n\n // Status code\n const statusCode = parseInt(\n preferredResponseKey === 'default'\n ? '200'\n : preferredResponseKey ?? '200',\n 10,\n ) as StatusCode\n\n return c.json(response, statusCode)\n })\n })\n })\n\n return app\n}\n"],"names":[],"mappings":";;;;;AAGO,SAAS,yBAAyB,SAAsB,EAAA;AAC7D,EAAA,OAAO,CAAC,SAAW,EAAA,KAAA,EAAO,OAAO,KAAO,EAAA,KAAA,EAAO,KAAK,CAAE,CAAA,IAAA;AAAA,IACpD,CAAC,GAAA,KAAQ,SAAW,EAAA,QAAA,CAAS,GAAG,CAAK,IAAA,KAAA;AA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;AAEA,QAAO,OAAA,CAAA,CAAE,IAAK,CAAA,QAAA,EAAU,UAAU,CAAA,CAAA;AAAA,OACnC,CAAA,CAAA;AAAA,KACF,CAAA,CAAA;AAAA,GACF,CAAA,CAAA;AAED,EAAO,OAAA,GAAA,CAAA;AACT;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/utils/findPreferredResponseKey.ts","../src/utils/routeFromPath.ts","../src/utils/isAuthenticationRequired.ts","../src/utils/anyBasicAuthentication.ts","../src/utils/isBasicAuthenticationRequired.ts","../src/createMockServer.ts"],"sourcesContent":["/**\n * Find the preferred response key: default, 200, 201 …\n */\nexport function findPreferredResponseKey(responses?: string[]) {\n return ['default', '200', '201', '204', '404', '500'].find(\n (key) => responses?.includes(key) ?? false,\n )\n}\n","/**\n * Convert path to route\n * Example: /posts/{id} -> /posts/:id\n */\nexport function routeFromPath(path: string) {\n return path.replace(/{/g, ':').replace(/}/g, '')\n}\n","import type { OpenAPIV3 } from '@scalar/openapi-parser'\n\n/**\n * Check whether the given security scheme key is in the `security` configuration for this operation.\n **/\nexport function isAuthenticationRequired(\n security?: OpenAPIV3.SecurityRequirementObject[],\n): boolean {\n // If security is not defined, auth is not required.\n if (!security) {\n return false\n }\n\n // Don’t require auth if security is just an empty array []\n if (Array.isArray(security) && !security.length) {\n return false\n }\n\n // Includes empty object = auth is not required\n if (\n (security ?? []).some(\n (securityRequirement) => !Object.keys(securityRequirement).length,\n )\n ) {\n return false\n }\n\n return true\n}\n","import type { Context } from 'hono'\nimport { HTTPException } from 'hono/http-exception'\n\n/**\n * Middleware to check for any basic authentication header\n */\nexport function anyBasicAuthentication() {\n return async function (ctx: Context, next: () => Promise<void>) {\n // Check if the request has an Authorization header\n // Note: We don’t care *what* credentials are sent, though.\n if (ctx.req.header('Authorization')?.startsWith('Basic ')) {\n return await next()\n }\n\n // Unauthorized\n throw new HTTPException(401, {\n res: new Response('Unauthorized', {\n status: 401,\n headers: {\n 'WWW-Authenticate': 'Basic realm=\"Authentication Required\"',\n },\n }),\n })\n }\n}\n","import type {\n OpenAPI,\n OpenAPIV3_1,\n ResolvedOpenAPI,\n} from '@scalar/openapi-parser'\n\nexport function isBasicAuthenticationRequired(\n operation: OpenAPI.Operation,\n schema?: ResolvedOpenAPI.Document,\n) {\n const allowedSecuritySchemes = operation.security?.map(\n (securityScheme: OpenAPIV3_1.SecurityRequirementObject) => {\n return Object.keys(securityScheme)[0]\n },\n )\n // Check if one of them is HTTP Basic Auth\n const httpBasicAuthIsRequired =\n allowedSecuritySchemes?.findIndex((securitySchemeKey: string) => {\n const securityScheme =\n schema?.components?.securitySchemes?.[securitySchemeKey]\n\n return (\n securityScheme?.type === 'http' && securityScheme?.scheme === 'basic'\n )\n }) !== undefined\n return httpBasicAuthIsRequired\n}\n","import { getExampleFromSchema } from '@scalar/oas-utils'\nimport { type ResolvedOpenAPI, openapi } from '@scalar/openapi-parser'\nimport { type Context, Hono } from 'hono'\nimport { cors } from 'hono/cors'\nimport type { StatusCode } from 'hono/utils/http-status'\n\nimport {\n findPreferredResponseKey,\n isAuthenticationRequired,\n routeFromPath,\n} from './utils'\nimport { anyBasicAuthentication } from './utils/anyBasicAuthentication'\nimport { isBasicAuthenticationRequired } from './utils/isBasicAuthenticationRequired'\n\n/**\n * Create a mock server instance\n */\nexport async function createMockServer(options?: {\n specification: string | Record<string, any>\n onRequest?: (data: {\n context: Context\n operation: ResolvedOpenAPI.Operation\n }) => void\n}) {\n const app = new Hono()\n\n // Resolve references\n const result = await openapi()\n .load(options?.specification ?? {})\n .resolve()\n\n // CORS headers\n app.use(cors())\n\n // OpenAPI JSON file\n app.get('/openapi.json', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.json(openapi().load(options.specification).get())\n })\n\n // OpenAPI YAML file\n app.get('/openapi.yaml', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.text(openapi().load(options.specification).toYaml())\n })\n\n // Paths\n Object.keys(result.schema?.paths ?? {}).forEach((path) => {\n // Operations\n Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {\n const route = routeFromPath(path)\n\n // @ts-expect-error Needs a proper type\n const operation = result.schema?.paths?.[path]?.[method]\n\n // Check if authentication is required\n const requiresAuthentication = isAuthenticationRequired(\n operation.security,\n )\n // Get all available authentication methods\n const requiresBasicAuthentication = isBasicAuthenticationRequired(\n operation,\n result?.schema,\n )\n\n // Add HTTP basic authentication\n if (requiresAuthentication && requiresBasicAuthentication) {\n // @ts-expect-error Needs a proper type\n app[method](route, anyBasicAuthentication())\n }\n\n // Route\n // @ts-expect-error Needs a proper type\n app[method](route, (c: Context) => {\n // Call onRequest callback\n if (options?.onRequest) {\n options.onRequest({\n context: c,\n operation,\n })\n }\n\n // Response\n // default, 200, 201 …\n const preferredResponseKey = findPreferredResponseKey(\n Object.keys(operation.responses ?? {}),\n )\n\n // Focus on JSON for now\n const jsonResponse = preferredResponseKey\n ? operation.responses?.[preferredResponseKey]?.content?.[\n 'application/json'\n ]\n : null\n\n // Get or generate JSON\n const response = jsonResponse?.example\n ? jsonResponse.example\n : jsonResponse?.schema\n ? getExampleFromSchema(jsonResponse.schema, {\n emptyString: '…',\n variables: c.req.param(),\n })\n : null\n\n // Status code\n const statusCode = parseInt(\n preferredResponseKey === 'default'\n ? '200'\n : preferredResponseKey ?? '200',\n 10,\n ) as StatusCode\n\n return c.json(response, statusCode)\n })\n })\n })\n\n return app\n}\n"],"names":[],"mappings":";;;;;;AAGO,SAAS,yBAAyB,SAAsB,EAAA;AAC7D,EAAA,OAAO,CAAC,SAAW,EAAA,KAAA,EAAO,OAAO,KAAO,EAAA,KAAA,EAAO,KAAK,CAAE,CAAA,IAAA;AAAA,IACpD,CAAC,GAAA,KAAQ,SAAW,EAAA,QAAA,CAAS,GAAG,CAAK,IAAA,KAAA;AAAA,GACvC,CAAA;AACF;;ACHO,SAAS,cAAc,IAAc,EAAA;AAC1C,EAAA,OAAO,KAAK,OAAQ,CAAA,IAAA,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AAoB,CAAG,EAAA,OAAA,GAC3C,kBACF,CACA,GAAA,IAAA,CAAA;AAGJ,QAAM,MAAA,QAAA,GAAW,cAAc,OAC3B,GAAA,YAAA,CAAa,UACb,YAAc,EAAA,MAAA,GACZ,oBAAqB,CAAA,YAAA,CAAa,MAAQ,EAAA;AAAA,UACxC,WAAa,EAAA,QAAA;AAAA,UACb,SAAA,EAAW,CAAE,CAAA,GAAA,CAAI,KAAM,EAAA;AAAA,SACxB,CACD,GAAA,IAAA,CAAA;AAGN,QAAA,MAAM,UAAa,GAAA,QAAA;AAAA,UACjB,oBAAA,KAAyB,SACrB,GAAA,KAAA,GACA,oBAAwB,IAAA,KAAA;AAAA,UAC5B,EAAA;AAAA,SACF,CAAA;AAEA,QAAO,OAAA,CAAA,CAAE,IAAK,CAAA,QAAA,EAAU,UAAU,CAAA,CAAA;AAAA,OACnC,CAAA,CAAA;AAAA,KACF,CAAA,CAAA;AAAA,GACF,CAAA,CAAA;AAED,EAAO,OAAA,GAAA,CAAA;AACT;;;;"}
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"swagger",
|
|
17
17
|
"cli"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.1.
|
|
19
|
+
"version": "0.1.71",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=18"
|
|
22
22
|
},
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@hono/node-server": "^1.11.0",
|
|
36
36
|
"@scalar/openapi-parser": "^0.3.2",
|
|
37
37
|
"hono": "^4.2.7",
|
|
38
|
-
"@scalar/oas-utils": "0.1.
|
|
38
|
+
"@scalar/oas-utils": "0.1.16"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "^20.8.4",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"tslib": "^2.6.2",
|
|
48
48
|
"typescript": "^5.4.3",
|
|
49
49
|
"vite-node": "^1.3.1",
|
|
50
|
-
"@scalar/galaxy": "0.1.
|
|
50
|
+
"@scalar/galaxy": "0.1.4"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "rm -Rf dist/ && rollup -c",
|