@scalar/mock-server 0.2.0 → 0.2.2

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,22 @@
1
1
  # @scalar/mock-server
2
2
 
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [a2cb3c3]
8
+ - @scalar/oas-utils@0.2.2
9
+
10
+ ## 0.2.1
11
+
12
+ ### Patch Changes
13
+
14
+ - e0fc110: chore: patch bump all package
15
+ - f0acc89: chore: upgrade to new @scalar/openapi-parser version
16
+ - Updated dependencies [e0fc110]
17
+ - Updated dependencies [f0acc89]
18
+ - @scalar/oas-utils@0.2.1
19
+
3
20
  ## 0.2.0
4
21
 
5
22
  ### Minor Changes
package/dist/index.cjs CHANGED
@@ -62,19 +62,21 @@ function isBasicAuthenticationRequired(operation, schema) {
62
62
 
63
63
  async function createMockServer(options) {
64
64
  const app = new hono.Hono();
65
- const result = await openapiParser.openapi().load(options?.specification ?? {}).resolve();
65
+ const result = await openapiParser.openapi().load(options?.specification ?? {}).dereference().get();
66
66
  app.use(cors.cors());
67
- app.get("/openapi.json", (c) => {
67
+ app.get("/openapi.json", async (c) => {
68
68
  if (!options?.specification) {
69
69
  return c.text("Not found", 404);
70
70
  }
71
- return c.json(openapiParser.openapi().load(options.specification).get());
71
+ const { specification } = await openapiParser.openapi().load(options.specification).get();
72
+ return c.json(specification);
72
73
  });
73
- app.get("/openapi.yaml", (c) => {
74
+ app.get("/openapi.yaml", async (c) => {
74
75
  if (!options?.specification) {
75
76
  return c.text("Not found", 404);
76
77
  }
77
- return c.text(openapiParser.openapi().load(options.specification).toYaml());
78
+ const specification = await openapiParser.openapi().load(options.specification).toYaml();
79
+ return c.text(specification);
78
80
  });
79
81
  Object.keys(result.schema?.paths ?? {}).forEach((path) => {
80
82
  Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {
@@ -1 +1 @@
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/spec-getters'\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","cors","getExampleFromSchema"],"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,EAAM,GAAG,CAAE,CAAA,OAAA,CAAQ,MAAM,EAAE,CAAA,CAAA;AACjD;;ACDO,SAAS,yBACd,QACS,EAAA;AAET,EAAA,IAAI,CAAC,QAAU,EAAA;AACb,IAAO,OAAA,KAAA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AA,GACZE,gCAAqB,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 { OpenAPI, OpenAPIV3_1 } from '@scalar/openapi-parser'\n\nexport function isBasicAuthenticationRequired(\n operation: OpenAPI.Operation,\n schema?: OpenAPI.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/spec-getters'\nimport { type OpenAPI, 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: { context: Context; operation: OpenAPI.Operation }) => void\n}) {\n const app = new Hono()\n\n // Resolve references\n const result = await openapi()\n .load(options?.specification ?? {})\n .dereference()\n .get()\n\n // CORS headers\n app.use(cors())\n\n // OpenAPI JSON file\n app.get('/openapi.json', async (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n const { specification } = await openapi().load(options.specification).get()\n\n return c.json(specification)\n })\n\n // OpenAPI YAML file\n app.get('/openapi.yaml', async (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n const specification = await openapi().load(options.specification).toYaml()\n\n return c.text(specification)\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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APIV3 } from '@scalar/openapi-parser';
3
3
  import { Context, Hono } from 'hono';
4
- import { ResolvedOpenAPI, OpenAPIV3 } from '@scalar/openapi-parser';
5
4
 
6
5
  /**
7
6
  * Create a mock server instance
@@ -10,9 +9,9 @@ declare function createMockServer(options?: {
10
9
  specification: string | Record<string, any>;
11
10
  onRequest?: (data: {
12
11
  context: Context;
13
- operation: ResolvedOpenAPI.Operation;
12
+ operation: OpenAPI.Operation;
14
13
  }) => void;
15
- }): Promise<Hono<hono.Env, hono_types.BlankSchema, "/">>;
14
+ }): Promise<Hono<hono_types.BlankEnv, hono_types.BlankSchema, "/">>;
16
15
 
17
16
  /**
18
17
  * Find the preferred response key: default, 200, 201 …
package/dist/index.js CHANGED
@@ -60,19 +60,21 @@ function isBasicAuthenticationRequired(operation, schema) {
60
60
 
61
61
  async function createMockServer(options) {
62
62
  const app = new Hono();
63
- const result = await openapi().load(options?.specification ?? {}).resolve();
63
+ const result = await openapi().load(options?.specification ?? {}).dereference().get();
64
64
  app.use(cors());
65
- app.get("/openapi.json", (c) => {
65
+ app.get("/openapi.json", async (c) => {
66
66
  if (!options?.specification) {
67
67
  return c.text("Not found", 404);
68
68
  }
69
- return c.json(openapi().load(options.specification).get());
69
+ const { specification } = await openapi().load(options.specification).get();
70
+ return c.json(specification);
70
71
  });
71
- app.get("/openapi.yaml", (c) => {
72
+ app.get("/openapi.yaml", async (c) => {
72
73
  if (!options?.specification) {
73
74
  return c.text("Not found", 404);
74
75
  }
75
- return c.text(openapi().load(options.specification).toYaml());
76
+ const specification = await openapi().load(options.specification).toYaml();
77
+ return c.text(specification);
76
78
  });
77
79
  Object.keys(result.schema?.paths ?? {}).forEach((path) => {
78
80
  Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
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/spec-getters'\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,EAAM,GAAG,CAAE,CAAA,OAAA,CAAQ,MAAM,EAAE,CAAA,CAAA;AACjD;;ACDO,SAAS,yBACd,QACS,EAAA;AAET,EAAA,IAAI,CAAC,QAAU,EAAA;AACb,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAGA,EAAA,IAAI,MAAM,OAAQ,CAAA,QAAQ,CAAK,IAAA,CAAC,SAAS,MAAQ,EAAA;AAC/C,IAAO,OAAA,KAAA,CAAA;AAAA,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,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.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 { OpenAPI, OpenAPIV3_1 } from '@scalar/openapi-parser'\n\nexport function isBasicAuthenticationRequired(\n operation: OpenAPI.Operation,\n schema?: OpenAPI.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/spec-getters'\nimport { type OpenAPI, 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: { context: Context; operation: OpenAPI.Operation }) => void\n}) {\n const app = new Hono()\n\n // Resolve references\n const result = await openapi()\n .load(options?.specification ?? {})\n .dereference()\n .get()\n\n // CORS headers\n app.use(cors())\n\n // OpenAPI JSON file\n app.get('/openapi.json', async (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n const { specification } = await openapi().load(options.specification).get()\n\n return c.json(specification)\n })\n\n // OpenAPI YAML file\n app.get('/openapi.yaml', async (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n const specification = await openapi().load(options.specification).toYaml()\n\n return c.text(specification)\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,GAA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U,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,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.2.0",
19
+ "version": "0.2.2",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
@@ -33,9 +33,9 @@
33
33
  "module": "dist/index.js",
34
34
  "dependencies": {
35
35
  "@hono/node-server": "^1.11.0",
36
- "@scalar/openapi-parser": "^0.3.2",
36
+ "@scalar/openapi-parser": "^0.7.1",
37
37
  "hono": "^4.2.7",
38
- "@scalar/oas-utils": "0.2.0"
38
+ "@scalar/oas-utils": "0.2.2"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^20.8.4",
@@ -47,8 +47,8 @@
47
47
  "tslib": "^2.6.2",
48
48
  "typescript": "^5.4.3",
49
49
  "vite-node": "^1.3.1",
50
- "@scalar/galaxy": "0.2.0",
51
- "@scalar/build-tooling": "0.1.5"
50
+ "@scalar/build-tooling": "0.1.6",
51
+ "@scalar/galaxy": "0.2.1"
52
52
  },
53
53
  "scripts": {
54
54
  "build": "rm -Rf dist/ && rollup -c",