Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions oxide-openapi-gen-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion oxide-openapi-gen-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oxide/openapi-gen-ts",
"version": "0.2.2",
"version": "0.3.0",
"description": "OpenAPI client generator used to generate Oxide TypeScript SDK",
"keywords": [
"oxide",
Expand Down
39 changes: 23 additions & 16 deletions oxide-openapi-gen-ts/src/client/msw-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {
? `body: Json<Api.${bodyType}>,`
: "";
const pathParams = conf.parameters?.filter(
(param) => "name" in param && param.schema && param.in === "path"
(param) => "name" in param && param.schema && param.in === "path",
);
const queryParams = conf.parameters?.filter(
(param) => "name" in param && param.schema && param.in === "query"
(param) => "name" in param && param.schema && param.in === "query",
);
const pathParamsType = pathParams?.length
? `path: Api.${snakeToPascal(opId)}PathParams,`
Expand Down Expand Up @@ -133,9 +133,10 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {

// if any of the errors come from path params, just 404 — the resource cannot
// exist if there's no valid name
const { issues } = result.error
const status = issues.some(e => e.path[0] === 'path') ? 404 : 400
return { paramsErr: json(issues, { status }) }
const status = result.error.issues.some((e) => e.path[0] === 'path') ? 404 : 400
const error_code = status === 404 ? 'NotFound' : 'InvalidRequest'
const message = 'Zod error for params: ' + JSON.stringify(result.error)
return { paramsErr: json({ error_code, message }, { status }) }
}

const handler = (handler: MSWHandlers[keyof MSWHandlers], paramSchema: ZodSchema | null, bodySchema: ZodSchema | null) =>
Expand All @@ -151,15 +152,18 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {
const { params, paramsErr } = paramSchema
? validateParams(paramSchema, req, pathParams)
: { params: {}, paramsErr: undefined };
if (paramsErr) return json(paramsErr, { status: 400 });
if (paramsErr) return paramsErr;

const { path, query } = params

let body = undefined
if (bodySchema) {
const rawBody = await req.json()
const result = bodySchema.transform(snakeify).safeParse(rawBody);
if (!result.success) return json(result.error.issues, { status: 400 })
if (!result.success) {
const message = 'Zod error for body: ' + JSON.stringify(result.error)
return json({ error_code: 'InvalidRequest', message }, { status: 400 })
}
body = result.data
}

Expand All @@ -172,9 +176,6 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {
if (typeof result === "number") {
return new HttpResponse(null, { status: result });
}
if (typeof result === "function") {
return result();
}
if (result instanceof Response) {
return result;
}
Expand All @@ -183,17 +184,23 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {
if (typeof thrown === 'number') {
return new HttpResponse(null, { status: thrown });
}
if (typeof thrown === "function") {
return thrown();
}
if (typeof thrown === "string") {
return json({ message: thrown }, { status: 400 });
}
if (thrown instanceof Response) {
return thrown;
}

// if it's not one of those, then we don't know what to do with it
console.error('Unexpected mock error', thrown)
return json({ message: "Unknown Server Error" }, { status: 500 });
if (typeof thrown === 'function') {
console.error(
"It looks like you've accidentally thrown an error constructor function from a mock handler without calling it!"
)
}
// rethrow so everything breaks because this isn't supposed to happen
throw thrown

}
}

Expand All @@ -215,8 +222,8 @@ export function generateMSWHandlers(spec: OpenAPIV3.Document, destDir: string) {

w(
`http.${method}('${formatPath(
path
)}', handler(handlers['${handler}'], ${paramSchema}, ${bodySchema})),`
path,
)}', handler(handlers['${handler}'], ${paramSchema}, ${bodySchema})),`,
);
}
w(`]}`);
Expand Down