Skip to content

Commit

Permalink
add with-metada middleware to routeSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
kainpets committed Oct 11, 2023
1 parent 8beece2 commit 744fa8e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ export const parseRoutesInPackage = async (opts: {
filepaths.map(async (p) => {
const { default: routeFn } = await require(path.resolve(p))

if (routeFn) {
if (routeFn._routeSpec?.dontIncludeInOpenApi) {
return
}
}

if (routeFn) {
if (!routeFn._setupParams) {
console.warn(
Expand Down
11 changes: 9 additions & 2 deletions packages/nextlove/src/with-route-spec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 @@ -23,22 +24,25 @@ export const checkRouteSpec = <
any
>[],
FormData extends ParamDef = z.ZodTypeAny,
MetaData extends ParamDef = z.ZodTypeAny,
Spec extends RouteSpec<
AuthType,
Methods,
JsonBody,
QueryParams,
CommonParams,
Middlewares,
FormData
FormData,
MetaData
> = RouteSpec<
AuthType,
Methods,
JsonBody,
QueryParams,
CommonParams,
Middlewares,
FormData
FormData,
MetaData
>
>(
spec: Spec
Expand Down Expand Up @@ -109,6 +113,9 @@ export const createWithRouteSpec: CreateWithRouteSpecFunction = ((
shouldValidateGetRequestBody,
supportedArrayFormats,
}),
withMetadata({
legacySdkTakesDeviceIdStringParameter: true,
}),
userDefinedRouteFn
)(req as any, res)
}
Expand Down
13 changes: 13 additions & 0 deletions packages/nextlove/src/with-route-spec/middlewares/with-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { z } from "zod"

const MetaDataSchema = z.object({
legacySdkTakesDeviceIdStringParameter: z.boolean().optional(),
})

export type MetaData = z.infer<typeof MetaDataSchema>

export const withMetadata = (metaData: MetaData) => (next) => (req, res) => {
return next(req, res)
}

export default withMetadata

0 comments on commit 744fa8e

Please sign in to comment.