@scalar/mock-server
Advanced tools
Comparing version 0.1.64 to 0.1.65
# @scalar/mock-server | ||
## 0.1.65 | ||
### Patch Changes | ||
- 742ef52: feat: add wildcard CORS headers | ||
- 46a3a94: feat: return responses other than 200 | ||
## 0.1.64 | ||
@@ -4,0 +11,0 @@ |
import * as hono_types from 'hono/types'; | ||
import * as hono from 'hono'; | ||
import { Context, Hono } from 'hono'; | ||
import { Operation } from '@scalar/oas-utils'; | ||
import { ResolvedOpenAPI } from '@scalar/openapi-parser'; | ||
@@ -13,3 +13,3 @@ /** | ||
context: Context; | ||
operation: Operation; | ||
operation: ResolvedOpenAPI.Operation; | ||
}) => void; | ||
@@ -19,5 +19,5 @@ }): Promise<Hono<hono.Env, hono_types.BlankSchema, "/">>; | ||
/** | ||
* Normalize OpenAPI JSON string, YAML string … to object | ||
* Find the preferred response key: default, 200, 201 … | ||
*/ | ||
declare function normalize(openapi: string | Record<string, any>): string | Record<string, any>; | ||
declare function findPreferredResponseKey(responses?: string[]): string | undefined; | ||
@@ -30,2 +30,2 @@ /** | ||
export { createMockServer, normalize, routeFromPath }; | ||
export { createMockServer, findPreferredResponseKey, routeFromPath }; |
import { getExampleFromSchema } from '@scalar/oas-utils'; | ||
import { openapi } from '@scalar/openapi-parser'; | ||
import { Hono } from 'hono'; | ||
import { cors } from 'hono/cors'; | ||
function normalize(openapi) { | ||
if (typeof openapi === "string") { | ||
openapi = JSON.parse(openapi); | ||
} | ||
return openapi; | ||
function findPreferredResponseKey(responses) { | ||
return ["default", "200", "201", "204", "404", "500"].find( | ||
(key) => responses?.includes(key) ?? false | ||
); | ||
} | ||
@@ -19,2 +19,3 @@ | ||
const result = await openapi().load(options?.specification ?? {}).resolve(); | ||
app.use(cors()); | ||
app.get("/openapi.json", (c) => { | ||
@@ -44,7 +45,14 @@ if (!options?.specification) { | ||
const operation = result.schema?.paths?.[path]?.[method]; | ||
const jsonResponseConfiguration = operation.responses?.["200"]?.content["application/json"]; | ||
const response = jsonResponseConfiguration?.example ? jsonResponseConfiguration.example : jsonResponseConfiguration?.schema ? getExampleFromSchema(jsonResponseConfiguration.schema, { | ||
const preferredResponseKey = findPreferredResponseKey( | ||
Object.keys(operation.responses ?? {}) | ||
); | ||
const jsonResponse = preferredResponseKey ? operation.responses?.[preferredResponseKey]?.content["application/json"] : null; | ||
const response = jsonResponse?.example ? jsonResponse.example : jsonResponse?.schema ? getExampleFromSchema(jsonResponse.schema, { | ||
emptyString: "\u2026" | ||
}) : null; | ||
return c.json(response); | ||
const statusCode = parseInt( | ||
preferredResponseKey === "default" ? "200" : preferredResponseKey ?? "200", | ||
10 | ||
); | ||
return c.json(response, statusCode); | ||
}); | ||
@@ -56,3 +64,3 @@ }); | ||
export { createMockServer, normalize, routeFromPath }; | ||
export { createMockServer, findPreferredResponseKey, routeFromPath }; | ||
//# sourceMappingURL=index.js.map |
@@ -19,3 +19,3 @@ { | ||
], | ||
"version": "0.1.64", | ||
"version": "0.1.65", | ||
"engines": { | ||
@@ -22,0 +22,0 @@ "node": ">=18" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28611
145