-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new docs and specification middleware
- Loading branch information
1 parent
2559ca3
commit 22a85ad
Showing
8 changed files
with
249 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { RequestHandler } from "express"; | ||
import { AnyZodObject, z } from "zod"; | ||
import StatusCode from "status-code-enum"; | ||
import { Response, Request, NextFunction } from "express"; | ||
import { registerPathSpecification } from "../openapi"; | ||
import { RouteConfig } from "@asteasolutions/zod-to-openapi"; | ||
|
||
export type Method = RouteConfig["method"]; | ||
|
||
export enum Tag { | ||
ADMISSION = "Admission", | ||
AUTH = "Auth", | ||
EVENT = "Event", | ||
MAIL = "Mail", | ||
MENTOR = "Mentor", | ||
NEWSLETTER = "Newsletter", | ||
NOTIFICATION = "Notification", | ||
PROFILE = "Profile", | ||
PUZZLE = "Puzzle", | ||
REGISTRATION = "Registration", | ||
S3 = "S3", | ||
SHOP = "Shop", | ||
STAFF = "Staff", | ||
VERSION = "Version", | ||
} | ||
|
||
export interface ResponseObject { | ||
description: string; | ||
schema: AnyZodObject; | ||
} | ||
export interface ResponsesObject { | ||
[statusCode: string]: ResponseObject; | ||
} | ||
|
||
export interface Specification<Params = AnyZodObject, Responses = ResponsesObject, Body = AnyZodObject> { | ||
path: string; | ||
method: Method; | ||
tag: Tag; | ||
summary: string; | ||
parameters?: Params; | ||
body?: Body; | ||
responses: Responses; | ||
} | ||
|
||
// Utility types to convert Responses into a set of possible schemas | ||
type InferResponseBody<T> = T extends ResponseObject ? z.infer<T["schema"]> : never; | ||
type ResponseBody<T extends ResponsesObject> = InferResponseBody<T[keyof T]>; | ||
|
||
export default function specification<Params extends AnyZodObject, Responses extends ResponsesObject, Body extends AnyZodObject>( | ||
spec: Specification<Params, Responses, Body>, | ||
): RequestHandler<z.infer<Params>, ResponseBody<Responses>, z.infer<Body>> { | ||
registerPathSpecification(spec); | ||
|
||
return async (req: Request, res: Response, next: NextFunction) => { | ||
if (spec.parameters) { | ||
const result = await spec.parameters.safeParseAsync(req.params); | ||
if (!result.success) { | ||
res.status(StatusCode.ClientErrorBadRequest).json({ | ||
error: "BadRequest", | ||
message: "Bad request made - invalid parameters format", | ||
validationErrors: result.error.errors, | ||
}); | ||
} | ||
} | ||
if (spec.body) { | ||
const result = await spec.body.safeParseAsync(req.body); | ||
if (!result.success) { | ||
res.status(StatusCode.ClientErrorBadRequest).json({ | ||
error: "BadRequest", | ||
message: "Bad request made - invalid body format", | ||
validationErrors: result.error.errors, | ||
}); | ||
} | ||
} | ||
next(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { OpenApiGeneratorV31, OpenAPIRegistry, RouteConfig } from "@asteasolutions/zod-to-openapi"; | ||
import { AnyZodObject } from "zod"; | ||
import type { OpenAPIObject } from "openapi3-ts/oas31"; | ||
import Config from "./config"; | ||
import { ResponsesObject, Specification } from "./middleware/specification"; | ||
|
||
let openAPISpec: OpenAPIObject | undefined = undefined; | ||
export const Registry = new OpenAPIRegistry(); | ||
|
||
function generateOpenAPISpec(): OpenAPIObject { | ||
const generator = new OpenApiGeneratorV31(Registry.definitions); | ||
return generator.generateDocument({ | ||
info: { | ||
title: "adonix", | ||
version: "1.0.0", | ||
description: "HackIllinois' backend API", | ||
}, | ||
openapi: "3.1.0", | ||
servers: [ | ||
{ | ||
url: Config.ROOT_URL, | ||
description: Config.PROD ? "Production" : "Local", | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
export function getOpenAPISpec(): OpenAPIObject { | ||
if (!openAPISpec) { | ||
openAPISpec = generateOpenAPISpec(); | ||
} | ||
|
||
return openAPISpec; | ||
} | ||
|
||
export function registerPathSpecification< | ||
Params extends AnyZodObject, | ||
Responses extends ResponsesObject, | ||
Body extends AnyZodObject, | ||
>(specification: Specification<Params, Responses, Body>): void { | ||
const { method, path, summary, parameters: params, tag } = specification; | ||
|
||
const responses: RouteConfig["responses"] = {}; | ||
for (const [statusCode, response] of Object.entries(specification.responses)) { | ||
responses[statusCode] = { | ||
description: response.description, | ||
content: { | ||
"application/json": { | ||
schema: response.schema, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
const request: RouteConfig["request"] = { params }; | ||
if (specification.body) { | ||
request.body = { | ||
content: { | ||
"application/json": { | ||
schema: specification.body, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
Registry.registerPath({ | ||
method, | ||
path, | ||
responses, | ||
request, | ||
summary, | ||
tags: [tag], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi"; | ||
import { z } from "zod"; | ||
|
||
extendZodWithOpenApi(z); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters