@scalar/mock-server 0.3.23 → 0.3.25
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 +18 -0
- package/dist/createMockServer.d.ts +1 -1
- package/dist/createMockServer.d.ts.map +1 -1
- package/dist/createMockServer.js +36 -48
- package/dist/createMockServer.js.map +7 -0
- package/dist/createMockServer.test.js +598 -0
- package/dist/createMockServer.test.js.map +7 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +7 -0
- package/dist/routes/mockAnyResponse.d.ts +1 -1
- package/dist/routes/mockAnyResponse.d.ts.map +1 -1
- package/dist/routes/mockAnyResponse.js +56 -64
- package/dist/routes/mockAnyResponse.js.map +7 -0
- package/dist/routes/respondWithAuthorizePage.js +39 -31
- package/dist/routes/respondWithAuthorizePage.js.map +7 -0
- package/dist/routes/respondWithOpenApiDocument.js +35 -35
- package/dist/routes/respondWithOpenApiDocument.js.map +7 -0
- package/dist/routes/respondWithToken.js +44 -41
- package/dist/routes/respondWithToken.js.map +7 -0
- package/dist/types.js +5 -4
- package/dist/types.js.map +7 -0
- package/dist/utils/createOpenApiDefinition.js +11 -0
- package/dist/utils/createOpenApiDefinition.js.map +7 -0
- package/dist/utils/findPreferredResponseKey.js +8 -10
- package/dist/utils/findPreferredResponseKey.js.map +7 -0
- package/dist/utils/findPreferredResponseKey.test.js +20 -0
- package/dist/utils/findPreferredResponseKey.test.js.map +7 -0
- package/dist/utils/getOpenAuthTokenUrls.js +33 -45
- package/dist/utils/getOpenAuthTokenUrls.js.map +7 -0
- package/dist/utils/getOpenAuthTokenUrls.test.js +127 -0
- package/dist/utils/getOpenAuthTokenUrls.test.js.map +7 -0
- package/dist/utils/getOperations.d.ts +1 -1
- package/dist/utils/getOperations.d.ts.map +1 -1
- package/dist/utils/getOperations.js +11 -14
- package/dist/utils/getOperations.js.map +7 -0
- package/dist/utils/handleAuthentication.js +95 -101
- package/dist/utils/handleAuthentication.js.map +7 -0
- package/dist/utils/honoRouteFromPath.js +5 -7
- package/dist/utils/honoRouteFromPath.js.map +7 -0
- package/dist/utils/honoRouteFromPath.test.js +32 -0
- package/dist/utils/honoRouteFromPath.test.js.map +7 -0
- package/dist/utils/isAuthenticationRequired.js +14 -18
- package/dist/utils/isAuthenticationRequired.js.map +7 -0
- package/dist/utils/isAuthenticationRequired.test.js +23 -0
- package/dist/utils/isAuthenticationRequired.test.js.map +7 -0
- package/dist/utils/logAuthenticationInstructions.js +109 -107
- package/dist/utils/logAuthenticationInstructions.js.map +7 -0
- package/dist/utils/setupAuthenticationRoutes.js +76 -82
- package/dist/utils/setupAuthenticationRoutes.js.map +7 -0
- package/package.json +7 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @scalar/mock-server
|
|
2
2
|
|
|
3
|
+
## 0.3.25
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8c7bad8: chore: move build tooling to esbuild
|
|
8
|
+
- Updated dependencies [8c7bad8]
|
|
9
|
+
- @scalar/openapi-parser@0.10.17
|
|
10
|
+
- @scalar/openapi-types@0.2.2
|
|
11
|
+
- @scalar/oas-utils@0.2.141
|
|
12
|
+
|
|
13
|
+
## 0.3.24
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [e8457cb]
|
|
18
|
+
- @scalar/oas-utils@0.2.140
|
|
19
|
+
- @scalar/openapi-parser@0.10.16
|
|
20
|
+
|
|
3
21
|
## 0.3.23
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMockServer.d.ts","sourceRoot":"","sources":["../src/createMockServer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgB,IAAI,EAAE,MAAM,MAAM,CAAA;AAGzC,OAAO,KAAK,EAAc,iBAAiB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"createMockServer.d.ts","sourceRoot":"","sources":["../src/createMockServer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgB,IAAI,EAAE,MAAM,MAAM,CAAA;AAGzC,OAAO,KAAK,EAAc,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAW5D;;GAEG;AACH,wBAAsB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,uFA+ChE"}
|
package/dist/createMockServer.js
CHANGED
|
@@ -1,51 +1,39 @@
|
|
|
1
|
-
import { openapi } from
|
|
2
|
-
import { Hono } from
|
|
3
|
-
import { cors } from
|
|
4
|
-
import { getOperations } from
|
|
5
|
-
import { handleAuthentication } from
|
|
6
|
-
import { honoRouteFromPath } from
|
|
7
|
-
import { isAuthenticationRequired } from
|
|
8
|
-
import { logAuthenticationInstructions } from
|
|
9
|
-
import { setupAuthenticationRoutes } from
|
|
10
|
-
import { mockAnyResponse } from
|
|
11
|
-
import { respondWithOpenApiDocument } from
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Create a mock server instance
|
|
15
|
-
*/
|
|
1
|
+
import { openapi } from "@scalar/openapi-parser";
|
|
2
|
+
import { Hono } from "hono";
|
|
3
|
+
import { cors } from "hono/cors";
|
|
4
|
+
import { getOperations } from "./utils/getOperations.js";
|
|
5
|
+
import { handleAuthentication } from "./utils/handleAuthentication.js";
|
|
6
|
+
import { honoRouteFromPath } from "./utils/honoRouteFromPath.js";
|
|
7
|
+
import { isAuthenticationRequired } from "./utils/isAuthenticationRequired.js";
|
|
8
|
+
import { logAuthenticationInstructions } from "./utils/logAuthenticationInstructions.js";
|
|
9
|
+
import { setupAuthenticationRoutes } from "./utils/setupAuthenticationRoutes.js";
|
|
10
|
+
import { mockAnyResponse } from "./routes/mockAnyResponse.js";
|
|
11
|
+
import { respondWithOpenApiDocument } from "./routes/respondWithOpenApiDocument.js";
|
|
16
12
|
async function createMockServer(options) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const route = honoRouteFromPath(path);
|
|
35
|
-
const operation = schema?.paths?.[path]?.[method];
|
|
36
|
-
// Check if authentication is required for this operation
|
|
37
|
-
if (isAuthenticationRequired(operation.security)) {
|
|
38
|
-
app[method](route, handleAuthentication(schema, operation));
|
|
39
|
-
}
|
|
40
|
-
// Actual route
|
|
41
|
-
app[method](route, (c) => mockAnyResponse(c, operation, options));
|
|
42
|
-
});
|
|
13
|
+
const app = new Hono();
|
|
14
|
+
const { schema } = await openapi().load(options?.specification ?? {}).dereference().get();
|
|
15
|
+
app.use(cors());
|
|
16
|
+
setupAuthenticationRoutes(app, schema);
|
|
17
|
+
logAuthenticationInstructions(
|
|
18
|
+
schema?.components?.securitySchemes || {}
|
|
19
|
+
);
|
|
20
|
+
const paths = schema?.paths ?? {};
|
|
21
|
+
Object.keys(paths).forEach((path) => {
|
|
22
|
+
const methods = Object.keys(getOperations(paths[path]));
|
|
23
|
+
methods.forEach((method) => {
|
|
24
|
+
const route = honoRouteFromPath(path);
|
|
25
|
+
const operation = schema?.paths?.[path]?.[method];
|
|
26
|
+
if (isAuthenticationRequired(operation.security)) {
|
|
27
|
+
app[method](route, handleAuthentication(schema, operation));
|
|
28
|
+
}
|
|
29
|
+
app[method](route, (c) => mockAnyResponse(c, operation, options));
|
|
43
30
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return app;
|
|
31
|
+
});
|
|
32
|
+
app.get("/openapi.json", (c) => respondWithOpenApiDocument(c, options?.specification, "json"));
|
|
33
|
+
app.get("/openapi.yaml", (c) => respondWithOpenApiDocument(c, options?.specification, "yaml"));
|
|
34
|
+
return app;
|
|
49
35
|
}
|
|
50
|
-
|
|
51
|
-
|
|
36
|
+
export {
|
|
37
|
+
createMockServer
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=createMockServer.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/createMockServer.ts"],
|
|
4
|
+
"sourcesContent": ["import { openapi } from '@scalar/openapi-parser'\nimport type { OpenAPI, OpenAPIV3_1 } from '@scalar/openapi-types'\nimport { type Context, Hono } from 'hono'\nimport { cors } from 'hono/cors'\n\nimport type { HttpMethod, MockServerOptions } from '@/types'\nimport { getOperations } from '@/utils/getOperations'\nimport { handleAuthentication } from '@/utils/handleAuthentication'\nimport { honoRouteFromPath } from '@/utils/honoRouteFromPath'\nimport { isAuthenticationRequired } from '@/utils/isAuthenticationRequired'\nimport { logAuthenticationInstructions } from '@/utils/logAuthenticationInstructions'\nimport { setupAuthenticationRoutes } from '@/utils/setupAuthenticationRoutes'\n\nimport { mockAnyResponse } from './routes/mockAnyResponse'\nimport { respondWithOpenApiDocument } from './routes/respondWithOpenApiDocument'\n\n/**\n * Create a mock server instance\n */\nexport async function createMockServer(options: MockServerOptions) {\n const app = new Hono()\n\n /** Dereferenced OpenAPI document */\n const { schema } = await openapi()\n .load(options?.specification ?? {})\n .dereference()\n .get()\n\n // CORS headers\n app.use(cors())\n\n /** Authentication methods defined in the OpenAPI document */\n setupAuthenticationRoutes(app, schema)\n\n logAuthenticationInstructions(\n schema?.components?.securitySchemes || ({} as Record<string, OpenAPIV3_1.SecuritySchemeObject>),\n )\n\n /** Paths specified in the OpenAPI document */\n const paths = schema?.paths ?? {}\n\n Object.keys(paths).forEach((path) => {\n const methods = Object.keys(getOperations(paths[path])) as HttpMethod[]\n\n /** Keys for all operations of a specified path */\n methods.forEach((method) => {\n const route = honoRouteFromPath(path)\n const operation = schema?.paths?.[path]?.[method] as OpenAPI.Operation\n\n // Check if authentication is required for this operation\n if (isAuthenticationRequired(operation.security)) {\n app[method](route, handleAuthentication(schema, operation))\n }\n\n // Actual route\n app[method](route, (c: Context) => mockAnyResponse(c, operation, options))\n })\n })\n\n // OpenAPI JSON file\n app.get('/openapi.json', (c) => respondWithOpenApiDocument(c, options?.specification, 'json'))\n\n // OpenAPI YAML file\n app.get('/openapi.yaml', (c) => respondWithOpenApiDocument(c, options?.specification, 'yaml'))\n\n return app\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,eAAe;AAExB,SAAuB,YAAY;AACnC,SAAS,YAAY;AAGrB,SAAS,qBAAqB;AAC9B,SAAS,4BAA4B;AACrC,SAAS,yBAAyB;AAClC,SAAS,gCAAgC;AACzC,SAAS,qCAAqC;AAC9C,SAAS,iCAAiC;AAE1C,SAAS,uBAAuB;AAChC,SAAS,kCAAkC;AAK3C,eAAsB,iBAAiB,SAA4B;AACjE,QAAM,MAAM,IAAI,KAAK;AAGrB,QAAM,EAAE,OAAO,IAAI,MAAM,QAAQ,EAC9B,KAAK,SAAS,iBAAiB,CAAC,CAAC,EACjC,YAAY,EACZ,IAAI;AAGP,MAAI,IAAI,KAAK,CAAC;AAGd,4BAA0B,KAAK,MAAM;AAErC;AAAA,IACE,QAAQ,YAAY,mBAAoB,CAAC;AAAA,EAC3C;AAGA,QAAM,QAAQ,QAAQ,SAAS,CAAC;AAEhC,SAAO,KAAK,KAAK,EAAE,QAAQ,CAAC,SAAS;AACnC,UAAM,UAAU,OAAO,KAAK,cAAc,MAAM,IAAI,CAAC,CAAC;AAGtD,YAAQ,QAAQ,CAAC,WAAW;AAC1B,YAAM,QAAQ,kBAAkB,IAAI;AACpC,YAAM,YAAY,QAAQ,QAAQ,IAAI,IAAI,MAAM;AAGhD,UAAI,yBAAyB,UAAU,QAAQ,GAAG;AAChD,YAAI,MAAM,EAAE,OAAO,qBAAqB,QAAQ,SAAS,CAAC;AAAA,MAC5D;AAGA,UAAI,MAAM,EAAE,OAAO,CAAC,MAAe,gBAAgB,GAAG,WAAW,OAAO,CAAC;AAAA,IAC3E,CAAC;AAAA,EACH,CAAC;AAGD,MAAI,IAAI,iBAAiB,CAAC,MAAM,2BAA2B,GAAG,SAAS,eAAe,MAAM,CAAC;AAG7F,MAAI,IAAI,iBAAiB,CAAC,MAAM,2BAA2B,GAAG,SAAS,eAAe,MAAM,CAAC;AAE7F,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|