Skip to content
Draft
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
17 changes: 5 additions & 12 deletions example/lib/middlewares/with-route-spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
createWithRouteSpec,
QueryArrayFormats,
UnauthorizedException,
} from "nextlove"
import { createWithRouteSpec, UnauthorizedException } from "nextlove"
import { withAuthToken } from "./with-auth-token"
import { withUserSession } from "./with-user-session"
import * as ZT from "lib/zod"
Expand Down Expand Up @@ -39,13 +35,10 @@ export const withRouteSpecWithGlobalMiddlewareAfterAuth = createWithRouteSpec({
...defaultRouteSpec,
} as const)

export const withRouteSpecSupportedArrayFormats = (
supportedArrayFormats: QueryArrayFormats
) =>
createWithRouteSpec({
...defaultRouteSpec,
supportedArrayFormats,
})
export const withRouteSpecNewQueryParamsParser = createWithRouteSpec({
...defaultRouteSpec,
useLegacyQueryParamsParser: false,
} as const)

export const withRouteSpecWithoutValidateGetRequestBody = createWithRouteSpec({
authMiddlewareMap: { auth_token: withAuthToken },
Expand Down
3 changes: 2 additions & 1 deletion example/package-lock.json

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

23 changes: 0 additions & 23 deletions example/pages/api/todo/array-query-comma.ts

This file was deleted.

23 changes: 0 additions & 23 deletions example/pages/api/todo/array-query-repeat.ts

This file was deleted.

27 changes: 27 additions & 0 deletions example/pages/api/todo/legacy-query-params-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"

export const queryParams = z.object({
ids: z.array(z.string()),
flag: z.boolean().optional(),
})

// Uses the default factory: the legacy query param parser is the
// default in v4, so no option is set.
export const route_spec = checkRouteSpec({
methods: ["GET"],
auth: "none",
jsonResponse: z.object({
ok: z.boolean(),
ids: z.array(z.string()),
flag: z.boolean().optional(),
}),
queryParams,
})

export default withRouteSpec(route_spec)(async (req, res) => {
return res
.status(200)
.json({ ok: true, ids: req.query.ids, flag: req.query.flag })
})
28 changes: 28 additions & 0 deletions example/pages/api/todo/new-query-params-parser-per-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"

export const queryParams = z.object({
ids: z.array(z.string()),
flag: z.boolean().optional(),
})

export const route_spec = checkRouteSpec({
methods: ["GET"],
auth: "none",
// The route-level option overrides the factory default (legacy in v4),
// so a shared createWithRouteSpec factory can migrate route by route.
useLegacyQueryParamsParser: false,
jsonResponse: z.object({
ok: z.boolean(),
ids: z.array(z.string()),
flag: z.boolean().optional(),
}),
queryParams,
})

export default withRouteSpec(route_spec)(async (req, res) => {
return res
.status(200)
.json({ ok: true, ids: req.query.ids, flag: req.query.flag })
})
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { withRouteSpecSupportedArrayFormats } from "lib/middlewares"
import { withRouteSpecNewQueryParamsParser } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"

export const queryParams = z.object({
ids: z.array(z.string()),
flag: z.boolean().optional(),
})

export const route_spec = checkRouteSpec({
Expand All @@ -12,12 +13,15 @@ export const route_spec = checkRouteSpec({
jsonResponse: z.object({
ok: z.boolean(),
ids: z.array(z.string()),
flag: z.boolean().optional(),
}),
queryParams,
})

export default withRouteSpecSupportedArrayFormats(["brackets"])(route_spec)(
export default withRouteSpecNewQueryParamsParser(route_spec)(
async (req, res) => {
return res.status(200).json({ ok: true, ids: req.query.ids })
return res
.status(200)
.json({ ok: true, ids: req.query.ids, flag: req.query.flag })
}
)
68 changes: 0 additions & 68 deletions example/tests/api/todo/array-query-brackets.test.ts

This file was deleted.

67 changes: 0 additions & 67 deletions example/tests/api/todo/array-query-comma.test.ts

This file was deleted.

65 changes: 0 additions & 65 deletions example/tests/api/todo/array-query-repeat.test.ts

This file was deleted.

Loading