@scalar/mock-server 0.2.17 → 0.2.19
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 +12 -0
- package/dist/createMockServer.d.ts.map +1 -1
- package/dist/createMockServer.js +18 -19
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -3
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +11 -0
- package/dist/utils/findPreferredResponseKey.test.d.ts +2 -0
- package/dist/utils/findPreferredResponseKey.test.d.ts.map +1 -0
- package/dist/utils/getOperations.d.ts +8 -0
- package/dist/utils/getOperations.d.ts.map +1 -0
- package/dist/utils/getOperations.js +17 -0
- package/dist/utils/honoRouteFromPath.d.ts +6 -0
- package/dist/utils/honoRouteFromPath.d.ts.map +1 -0
- package/dist/utils/{routeFromPath.js → honoRouteFromPath.js} +2 -2
- package/dist/utils/honoRouteFromPath.test.d.ts +2 -0
- package/dist/utils/honoRouteFromPath.test.d.ts.map +1 -0
- package/dist/utils/index.d.ts +9 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +4 -5
- package/dist/utils/routeFromPath.d.ts +0 -6
- package/dist/utils/routeFromPath.d.ts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMockServer.d.ts","sourceRoot":"","sources":["../src/createMockServer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,OAAO,EAAW,MAAM,wBAAwB,CAAA;AAC9D,OAAO,EAAE,KAAK,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"createMockServer.d.ts","sourceRoot":"","sources":["../src/createMockServer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,OAAO,EAAW,MAAM,wBAAwB,CAAA;AAC9D,OAAO,EAAE,KAAK,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAiBzC;;GAEG;AACH,wBAAsB,gBAAgB,CAAC,OAAO,CAAC,EAAE;IAC/C,aAAa,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC3C,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC,SAAS,CAAA;KAAE,KAAK,IAAI,CAAA;CAC/E,uFA8IA"}
|
package/dist/createMockServer.js
CHANGED
|
@@ -2,13 +2,14 @@ import { getExampleFromSchema } from '@scalar/oas-utils/spec-getters';
|
|
|
2
2
|
import { openapi } from '@scalar/openapi-parser';
|
|
3
3
|
import { Hono } from 'hono';
|
|
4
4
|
import { cors } from 'hono/cors';
|
|
5
|
-
import { anyBasicAuthentication } from './utils/anyBasicAuthentication.js';
|
|
6
|
-
import { anyOpenAuthPasswordGrantAuthentication } from './utils/anyOpenAuthPasswordGrantAuthentication.js';
|
|
7
5
|
import { getOpenAuthTokenUrl } from './utils/getOpenAuthTokenUrl.js';
|
|
6
|
+
import { getOperations } from './utils/getOperations.js';
|
|
7
|
+
import { honoRouteFromPath } from './utils/honoRouteFromPath.js';
|
|
8
|
+
import { isAuthenticationRequired } from './utils/isAuthenticationRequired.js';
|
|
8
9
|
import { isBasicAuthenticationRequired } from './utils/isBasicAuthenticationRequired.js';
|
|
10
|
+
import { anyBasicAuthentication } from './utils/anyBasicAuthentication.js';
|
|
9
11
|
import { isOpenAuthPasswordGrantRequired } from './utils/isOpenAuthPasswordGrantRequired.js';
|
|
10
|
-
import {
|
|
11
|
-
import { isAuthenticationRequired } from './utils/isAuthenticationRequired.js';
|
|
12
|
+
import { anyOpenAuthPasswordGrantAuthentication } from './utils/anyOpenAuthPasswordGrantAuthentication.js';
|
|
12
13
|
import { findPreferredResponseKey } from './utils/findPreferredResponseKey.js';
|
|
13
14
|
|
|
14
15
|
/**
|
|
@@ -16,8 +17,8 @@ import { findPreferredResponseKey } from './utils/findPreferredResponseKey.js';
|
|
|
16
17
|
*/
|
|
17
18
|
async function createMockServer(options) {
|
|
18
19
|
const app = new Hono();
|
|
19
|
-
|
|
20
|
-
const
|
|
20
|
+
/** Dereferenced OpenAPI document */
|
|
21
|
+
const { schema } = await openapi()
|
|
21
22
|
.load(options?.specification ?? {})
|
|
22
23
|
.dereference()
|
|
23
24
|
.get();
|
|
@@ -40,7 +41,7 @@ async function createMockServer(options) {
|
|
|
40
41
|
return c.text(specification);
|
|
41
42
|
});
|
|
42
43
|
// OpenAuth2 token endpoint
|
|
43
|
-
const tokenUrl = getOpenAuthTokenUrl(
|
|
44
|
+
const tokenUrl = getOpenAuthTokenUrl(schema);
|
|
44
45
|
if (typeof tokenUrl === 'string') {
|
|
45
46
|
app.post(tokenUrl, async (c) => {
|
|
46
47
|
return c.json({
|
|
@@ -59,31 +60,29 @@ async function createMockServer(options) {
|
|
|
59
60
|
});
|
|
60
61
|
});
|
|
61
62
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
Object.keys(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
63
|
+
/** Paths specified in the OpenAPI document */
|
|
64
|
+
const paths = schema?.paths ?? {};
|
|
65
|
+
Object.keys(paths).forEach((path) => {
|
|
66
|
+
const methods = Object.keys(getOperations(paths[path]));
|
|
67
|
+
/** Keys for all operations of a specified path */
|
|
68
|
+
methods.forEach((method) => {
|
|
69
|
+
const route = honoRouteFromPath(path);
|
|
70
|
+
const operation = schema?.paths?.[path]?.[method];
|
|
69
71
|
// Check if authentication is required
|
|
70
72
|
const requiresAuthentication = isAuthenticationRequired(operation.security);
|
|
71
73
|
// Check whether we need basic authentication
|
|
72
|
-
const requiresBasicAuthentication = isBasicAuthenticationRequired(operation,
|
|
74
|
+
const requiresBasicAuthentication = isBasicAuthenticationRequired(operation, schema);
|
|
73
75
|
// Add HTTP basic authentication
|
|
74
76
|
if (requiresAuthentication && requiresBasicAuthentication) {
|
|
75
|
-
// @ts-expect-error Needs a proper type
|
|
76
77
|
app[method](route, anyBasicAuthentication());
|
|
77
78
|
}
|
|
78
79
|
// Check whether we need OpenAuth password grant authentication
|
|
79
|
-
const requiresOpenAuthPasswordGrant = isOpenAuthPasswordGrantRequired(operation,
|
|
80
|
+
const requiresOpenAuthPasswordGrant = isOpenAuthPasswordGrantRequired(operation, schema);
|
|
80
81
|
// Add HTTP basic authentication
|
|
81
82
|
if (requiresAuthentication && requiresOpenAuthPasswordGrant) {
|
|
82
|
-
// @ts-expect-error Needs a proper type
|
|
83
83
|
app[method](route, anyOpenAuthPasswordGrantAuthentication());
|
|
84
84
|
}
|
|
85
85
|
// Route
|
|
86
|
-
// @ts-expect-error Needs a proper type
|
|
87
86
|
app[method](route, (c) => {
|
|
88
87
|
// Call onRequest callback
|
|
89
88
|
if (options?.onRequest) {
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1 @@
|
|
|
1
1
|
export { createMockServer } from './createMockServer.js';
|
|
2
|
-
export { findPreferredResponseKey } from './utils/findPreferredResponseKey.js';
|
|
3
|
-
export { routeFromPath } from './utils/routeFromPath.js';
|
|
4
|
-
export { isAuthenticationRequired } from './utils/isAuthenticationRequired.js';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,eAAO,MAAM,WAAW,+DAOd,CAAA;AAEV,wBAAwB;AACxB,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAA"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findPreferredResponseKey.test.d.ts","sourceRoot":"","sources":["../../src/utils/findPreferredResponseKey.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from '@scalar/openapi-parser';
|
|
2
|
+
import { type HttpMethod } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Takes a dereferenced OpenAPI document and returns all operations.
|
|
5
|
+
* Ignores other attributes, like summary, parameters, etc.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getOperations(path?: OpenAPIV2.PathItemObject | OpenAPIV3.PathItemObject | OpenAPIV3_1.PathItemObject): Record<HttpMethod, OpenAPI.Operation>;
|
|
8
|
+
//# sourceMappingURL=getOperations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getOperations.d.ts","sourceRoot":"","sources":["../../src/utils/getOperations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,SAAS,EACT,SAAS,EACT,WAAW,EACZ,MAAM,wBAAwB,CAAA;AAE/B,OAAO,EAAE,KAAK,UAAU,EAAe,MAAM,UAAU,CAAA;AAEvD;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,IAAI,CAAC,EACD,SAAS,CAAC,cAAc,GACxB,SAAS,CAAC,cAAc,GACxB,WAAW,CAAC,cAAc,GAC7B,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC,SAAS,CAAC,CAUvC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { httpMethods } from '../types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Takes a dereferenced OpenAPI document and returns all operations.
|
|
5
|
+
* Ignores other attributes, like summary, parameters, etc.
|
|
6
|
+
*/
|
|
7
|
+
function getOperations(path) {
|
|
8
|
+
const operations = {};
|
|
9
|
+
for (const method of httpMethods) {
|
|
10
|
+
if (path?.[method]) {
|
|
11
|
+
operations[method] = path?.[method];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return operations;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { getOperations };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"honoRouteFromPath.d.ts","sourceRoot":"","sources":["../../src/utils/honoRouteFromPath.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,UAE7C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"honoRouteFromPath.test.d.ts","sourceRoot":"","sources":["../../src/utils/honoRouteFromPath.test.ts"],"names":[],"mappings":""}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
+
export * from './anyBasicAuthentication.js';
|
|
2
|
+
export * from './anyOpenAuthPasswordGrantAuthentication.js';
|
|
3
|
+
export * from './findPreferredResponseKey.test';
|
|
1
4
|
export * from './findPreferredResponseKey.js';
|
|
2
|
-
export * from './
|
|
5
|
+
export * from './getOpenAuthTokenUrl.js';
|
|
6
|
+
export * from './getOperations.js';
|
|
7
|
+
export * from './honoRouteFromPath.test';
|
|
8
|
+
export * from './honoRouteFromPath.js';
|
|
3
9
|
export * from './isAuthenticationRequired.js';
|
|
10
|
+
export * from './isBasicAuthenticationRequired.js';
|
|
11
|
+
export * from './isOpenAuthPasswordGrantRequired.js';
|
|
4
12
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAA;AAC1C,cAAc,iBAAiB,CAAA;AAC/B,cAAc,4BAA4B,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAA;AACxC,cAAc,0CAA0C,CAAA;AACxD,cAAc,iCAAiC,CAAA;AAC/C,cAAc,4BAA4B,CAAA;AAC1C,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,0BAA0B,CAAA;AACxC,cAAc,qBAAqB,CAAA;AACnC,cAAc,4BAA4B,CAAA;AAC1C,cAAc,iCAAiC,CAAA;AAC/C,cAAc,mCAAmC,CAAA"}
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"swagger",
|
|
17
17
|
"cli"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.2.
|
|
19
|
+
"version": "0.2.19",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=18"
|
|
22
22
|
},
|
|
@@ -37,14 +37,13 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@scalar/openapi-parser": "^0.7.2",
|
|
39
39
|
"hono": "^4.2.7",
|
|
40
|
-
"@scalar/oas-utils": "0.2.
|
|
40
|
+
"@scalar/oas-utils": "0.2.17"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@hono/node-server": "^1.11.0",
|
|
44
44
|
"@types/node": "^20.14.10",
|
|
45
|
-
"
|
|
46
|
-
"@scalar/build-tooling": "0.1.10"
|
|
47
|
-
"@scalar/galaxy": "0.2.4"
|
|
45
|
+
"@scalar/galaxy": "0.2.5",
|
|
46
|
+
"@scalar/build-tooling": "0.1.10"
|
|
48
47
|
},
|
|
49
48
|
"scripts": {
|
|
50
49
|
"build": "scalar-build-rollup",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"routeFromPath.d.ts","sourceRoot":"","sources":["../../src/utils/routeFromPath.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,UAEzC"}
|