@scalar/mock-server 0.2.17 → 0.2.18

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @scalar/mock-server
2
2
 
3
+ ## 0.2.18
4
+
5
+ ### Patch Changes
6
+
7
+ - de9e775: fix: tries to register non-http methods as routes
8
+
3
9
  ## 0.2.17
4
10
 
5
11
  ### Patch Changes
@@ -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;AAezC;;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,uFAiJA"}
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"}
@@ -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 { routeFromPath } from './utils/routeFromPath.js';
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
- // Resolve references
20
- const result = await openapi()
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(result?.schema);
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
- // Paths
63
- Object.keys(result.schema?.paths ?? {}).forEach((path) => {
64
- // Operations
65
- Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {
66
- const route = routeFromPath(path);
67
- // @ts-expect-error Needs a proper type
68
- const operation = result.schema?.paths?.[path]?.[method];
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, result?.schema);
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, result?.schema);
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
@@ -1,3 +1,2 @@
1
1
  export * from './createMockServer.js';
2
- export * from './utils';
3
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,SAAS,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';
@@ -0,0 +1,5 @@
1
+ /** Available HTTP methods for Hono routes */
2
+ export declare const httpMethods: readonly ["get", "put", "post", "delete", "options", "patch"];
3
+ /** Valid HTTP method */
4
+ export type HttpMethod = (typeof httpMethods)[number];
5
+ //# sourceMappingURL=types.d.ts.map
@@ -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,11 @@
1
+ /** Available HTTP methods for Hono routes */
2
+ const httpMethods = [
3
+ 'get',
4
+ 'put',
5
+ 'post',
6
+ 'delete',
7
+ 'options',
8
+ 'patch',
9
+ ];
10
+
11
+ export { httpMethods };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=findPreferredResponseKey.test.d.ts.map
@@ -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,6 @@
1
+ /**
2
+ * Convert path to route
3
+ * Example: /posts/{id} -> /posts/:id
4
+ */
5
+ export declare function honoRouteFromPath(path: string): string;
6
+ //# sourceMappingURL=honoRouteFromPath.d.ts.map
@@ -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"}
@@ -2,8 +2,8 @@
2
2
  * Convert path to route
3
3
  * Example: /posts/{id} -> /posts/:id
4
4
  */
5
- function routeFromPath(path) {
5
+ function honoRouteFromPath(path) {
6
6
  return path.replace(/{/g, ':').replace(/}/g, '');
7
7
  }
8
8
 
9
- export { routeFromPath };
9
+ export { honoRouteFromPath };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=honoRouteFromPath.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"honoRouteFromPath.test.d.ts","sourceRoot":"","sources":["../../src/utils/honoRouteFromPath.test.ts"],"names":[],"mappings":""}
@@ -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 './routeFromPath.js';
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.17",
19
+ "version": "0.2.18",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
@@ -42,9 +42,8 @@
42
42
  "devDependencies": {
43
43
  "@hono/node-server": "^1.11.0",
44
44
  "@types/node": "^20.14.10",
45
- "vite-node": "^1.3.1",
46
- "@scalar/build-tooling": "0.1.10",
47
- "@scalar/galaxy": "0.2.4"
45
+ "@scalar/galaxy": "0.2.4",
46
+ "@scalar/build-tooling": "0.1.10"
48
47
  },
49
48
  "scripts": {
50
49
  "build": "scalar-build-rollup",
@@ -1,6 +0,0 @@
1
- /**
2
- * Convert path to route
3
- * Example: /posts/{id} -> /posts/:id
4
- */
5
- export declare function routeFromPath(path: string): string;
6
- //# sourceMappingURL=routeFromPath.d.ts.map
@@ -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"}