Skip to content

Commit

Permalink
add openApiMetadata to openapi builder
Browse files Browse the repository at this point in the history
  • Loading branch information
kainpets committed Oct 11, 2023
1 parent 979f9be commit 53daecb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 49 deletions.
4 changes: 4 additions & 0 deletions packages/nextlove/src/generators/generate-openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ export async function generateOpenAPI(opts: GenerateOpenAPIOpts) {
)
// TODO: support multipart/form-data

if (routeSpec.openApiMetada) {
// handle openApiMetadata
}

// handle body
let body_to_generate_schema
if (isPostOrPutOrPatch) {
Expand Down
57 changes: 29 additions & 28 deletions packages/nextlove/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface RouteSpec<
JsonResponse extends ParamDef = z.ZodTypeAny,
FormData extends ParamDef = z.ZodTypeAny
> {
openApiMetada?: any
methods: Methods
auth: Auth
jsonBody?: JsonBody
Expand All @@ -47,11 +48,11 @@ export type MiddlewareChainOutput<
? {}
: MWChain extends readonly [infer First, ...infer Rest]
? First extends Middleware<infer T, any>
? T &
(Rest extends readonly Middleware<any, any>[]
? MiddlewareChainOutput<Rest>
: never)
: never
? T &
(Rest extends readonly Middleware<any, any>[]
? MiddlewareChainOutput<Rest>
: never)
: never
: never

export type AuthMiddlewares = {
Expand Down Expand Up @@ -116,34 +117,34 @@ export type RouteFunction<
> = (
req: (SP["authMiddlewareMap"] &
typeof defaultMiddlewareMap)[RS["auth"]] extends Middleware<
infer AuthMWOut,
any
>
infer AuthMWOut,
any
>
? Omit<NextApiRequest, "query" | "body"> &
AuthMWOut &
MiddlewareChainOutput<
RS["middlewares"] extends readonly Middleware<any, any>[]
? [...SP["globalMiddlewares"], ...RS["middlewares"]]
: SP["globalMiddlewares"]
> & {
body: RS["formData"] extends z.ZodTypeAny
? z.infer<RS["formData"]>
: RS["jsonBody"] extends z.ZodTypeAny
? z.infer<RS["jsonBody"]>
: {}
query: RS["queryParams"] extends z.ZodTypeAny
? z.infer<RS["queryParams"]>
: {}
commonParams: RS["commonParams"] extends z.ZodTypeAny
? z.infer<RS["commonParams"]>
: {}
}
AuthMWOut &
MiddlewareChainOutput<
RS["middlewares"] extends readonly Middleware<any, any>[]
? [...SP["globalMiddlewares"], ...RS["middlewares"]]
: SP["globalMiddlewares"]
> & {
body: RS["formData"] extends z.ZodTypeAny
? z.infer<RS["formData"]>
: RS["jsonBody"] extends z.ZodTypeAny
? z.infer<RS["jsonBody"]>
: {}
query: RS["queryParams"] extends z.ZodTypeAny
? z.infer<RS["queryParams"]>
: {}
commonParams: RS["commonParams"] extends z.ZodTypeAny
? z.infer<RS["commonParams"]>
: {}
}
: `unknown auth type: ${RS["auth"]}. You should configure this auth type in your auth_middlewares w/ createWithRouteSpec, or maybe you need to add "as const" to your route spec definition.`,
res: NextApiResponseWithoutJsonAndStatusMethods &
SuccessfulNextApiResponseMethods<
RS["jsonResponse"] extends z.ZodTypeAny
? z.infer<RS["jsonResponse"]>
: any
? z.infer<RS["jsonResponse"]>
: any
> &
ErrorNextApiResponseMethods
) => Promise<void>
Expand Down
9 changes: 2 additions & 7 deletions packages/nextlove/src/with-route-spec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import withMethods, { HTTPMethods } from "./middlewares/with-methods"
import withValidation from "./middlewares/with-validation"
import { z } from "zod"
import withMetadata, { MetaData } from "./middlewares/with-metadata"

type ParamDef = z.ZodTypeAny | z.ZodEffects<z.ZodTypeAny>

Expand All @@ -24,25 +23,22 @@ export const checkRouteSpec = <
any
>[],
FormData extends ParamDef = z.ZodTypeAny,
MetaData extends ParamDef = z.ZodTypeAny,
Spec extends RouteSpec<
AuthType,
Methods,
JsonBody,
QueryParams,
CommonParams,
Middlewares,
FormData,
MetaData
FormData
> = RouteSpec<
AuthType,
Methods,
JsonBody,
QueryParams,
CommonParams,
Middlewares,
FormData,
MetaData
FormData
>
>(
spec: Spec
Expand Down Expand Up @@ -113,7 +109,6 @@ export const createWithRouteSpec: CreateWithRouteSpecFunction = ((
shouldValidateGetRequestBody,
supportedArrayFormats,
}),
withMetadata(spec.metaData),
userDefinedRouteFn
)(req as any, res)
}
Expand Down
14 changes: 0 additions & 14 deletions packages/nextlove/src/with-route-spec/middlewares/with-metadata.ts

This file was deleted.

0 comments on commit 53daecb

Please sign in to comment.