Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nextlove app directory support #112

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ yarn-error.log*

# turbo
.turbo

.yalc
11 changes: 9 additions & 2 deletions apps/example-todo-app/lib/middlewares/with-auth-token.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { UnauthorizedException, Middleware } from "nextlove"
import {
UnauthorizedException,
Middleware,
getLegacyCompatibleReqRes,
} from "nextlove"

export const withAuthToken: Middleware<{
auth: {
authorized_by: "auth_token"
}
}> = (next) => async (req, res) => {
if (req.headers.authorization?.split("Bearer ")?.[1] !== "auth_token") {
const { headers } = getLegacyCompatibleReqRes(req, res)

const authorization = headers.get("authorization") as string | undefined
if (authorization?.split("Bearer ")?.[1] !== "auth_token") {
throw new UnauthorizedException({
type: "unauthorized",
message: "Unauthorized",
Expand Down
25 changes: 21 additions & 4 deletions apps/example-todo-app/lib/middlewares/with-route-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,31 @@ const defaultRouteSpec = {
},
} as const

export const withRouteSpec = createWithRouteSpec(defaultRouteSpec)
export const {
withRouteSpecLegacy: withRouteSpec,
withRouteSpec: withRouteSpecEdge,
} = createWithRouteSpec(defaultRouteSpec)

export const withRouteSpecSupportedArrayFormats = (
supportedArrayFormats: QueryArrayFormats
) =>
createWithRouteSpec({
...defaultRouteSpec,
supportedArrayFormats,
})
}).withRouteSpecLegacy

export const withRouteSpecWithoutValidateGetRequestBody = createWithRouteSpec({
export const withRouteSpecEdgeSupportedArrayFormats = (
supportedArrayFormats: QueryArrayFormats
) =>
createWithRouteSpec({
...defaultRouteSpec,
supportedArrayFormats,
}).withRouteSpec

export const {
withRouteSpecLegacy: withRouteSpecWithoutValidateGetRequestBody,
withRouteSpec: withRouteSpecEdgeWithoutValidateGetRequestBody,
} = createWithRouteSpec({
authMiddlewareMap: { auth_token: withAuthToken },
globalMiddlewares: [],
apiName: "TODO API",
Expand All @@ -37,7 +51,10 @@ export const withRouteSpecWithoutValidateGetRequestBody = createWithRouteSpec({
},
} as const)

export const withRouteSpecWithoutValidateResponse = createWithRouteSpec({
export const {
withRouteSpecLegacy: withRouteSpecWithoutValidateResponse,
withRouteSpec: withRouteSpecEdgeWithoutValidateResponse,
} = createWithRouteSpec({
authMiddlewareMap: { auth_token: withAuthToken },
globalMiddlewares: [],
apiName: "TODO API",
Expand Down
14 changes: 7 additions & 7 deletions apps/example-todo-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"glob-promise": "4.2.2",
"micro": "9.3.4",
"mkdirp": "1.0.4",
"next": "12.2.0",
"next": "13.4.19",
"nextlove": "*",
"path-to-regexp": "6.2.1",
"raw-body": "2.3.2",
Expand All @@ -31,19 +31,19 @@
"zod": "^3.17.3"
},
"devDependencies": {
"@types/node": "18.0.1",
"@types/node": "20.5.3",
"@types/react": "18.2.21",
"@types/react-dom": "18.2.7",
"@types/qs": "^6.9.7",
"@types/react": "18.0.14",
"@types/react-dom": "18.0.6",
"ava": "^5.0.1",
"esbuild-register": "^3.3.3",
"eslint": "8.19.0",
"eslint-config-next": "12.2.0",
"eslint": "8.47.0",
"eslint-config-next": "13.4.19",
"get-port": "5",
"prettier": "^2.7.1",
"qs": "^6.11.2",
"rimraf": "^4.1.2",
"tsup": "^5.6.3",
"typescript": "^5.0.2"
"typescript": "5.1.6"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { withRouteSpecEdgeWithoutValidateResponse } from "lib/middlewares"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"

export const jsonBody = z.object({
id: z.string().uuid().optional().default(uuidv4()),
title: z.string(),
completed: z.boolean().optional().default(false),
})

export const route_spec = {
methods: ["POST"],
auth: "auth_token",
jsonBody,
jsonResponse: z.object({
ok: z.string(),
}),
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdgeWithoutValidateResponse(route_spec)(
async (req, res) => {
return res.status(200).json({
// @ts-ignore
ok: true,
})
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { withRouteSpecEdge } from "lib/middlewares"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"

export const jsonBody = z.object({
id: z.string().uuid().optional().default(uuidv4()),
title: z.string(),
completed: z.boolean().optional().default(false),
})

export const route_spec = {
methods: ["POST"],
auth: "auth_token",
jsonBody,
jsonResponse: z.object({
ok: z.string(),
}),
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdge(route_spec)(async (req, res) => {
return res.status(200).json({
// @ts-ignore
ok: true,
})
})
26 changes: 26 additions & 0 deletions apps/example-todo-app/pages/api/todo/add/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { withRouteSpecEdge } from "lib/middlewares"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"

export const jsonBody = z.object({
id: z.string().uuid().optional().default(uuidv4()),
title: z.string(),
completed: z.boolean().optional().default(false),
})

export const route_spec = {
methods: ["POST"],
auth: "auth_token",
jsonBody,
jsonResponse: z.object({
ok: z.boolean(),
}),
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdge(route_spec)(async (req, res) => {
return res.status(200).json({ ok: true })
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { withRouteSpecEdgeSupportedArrayFormats } from "lib/middlewares"
import { z } from "zod"

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

export const route_spec = {
methods: ["GET"],
auth: "none",
jsonResponse: z.object({
ok: z.boolean(),
ids: z.array(z.string()),
}),
queryParams,
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdgeSupportedArrayFormats(["brackets"])(route_spec)(
async (req, res) => {
return res.status(200).json({ ok: true, ids: req.queryParams.ids })
}
)
26 changes: 26 additions & 0 deletions apps/example-todo-app/pages/api/todo/array-query-comma/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { withRouteSpecEdgeSupportedArrayFormats } from "lib/middlewares"
import { z } from "zod"

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

export const route_spec = {
methods: ["GET"],
auth: "none",
jsonResponse: z.object({
ok: z.boolean(),
ids: z.array(z.string()),
}),
queryParams,
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdgeSupportedArrayFormats(["comma"])(route_spec)(
async (req, res) => {
return res.status(200).json({ ok: true, ids: req.queryParams.ids })
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { withRouteSpecEdge } from "lib/middlewares"
import { z } from "zod"

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

export const route_spec = {
methods: ["GET"],
auth: "none",
jsonResponse: z.object({
ok: z.boolean(),
ids: z.array(z.string()),
}),
queryParams,
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdge(route_spec)(async (req, res) => {
return res.status(200).json({ ok: true, ids: req.queryParams.ids })
})
26 changes: 26 additions & 0 deletions apps/example-todo-app/pages/api/todo/array-query-repeat/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { withRouteSpecEdgeSupportedArrayFormats } from "lib/middlewares"
import { z } from "zod"

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

export const route_spec = {
methods: ["GET"],
auth: "none",
jsonResponse: z.object({
ok: z.boolean(),
ids: z.array(z.string()),
}),
queryParams,
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdgeSupportedArrayFormats(["repeat"])(route_spec)(
async (req, res) => {
return res.status(200).json({ ok: true, ids: req.queryParams.ids })
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { withRouteSpecEdge } from "lib/middlewares"
import { NotFoundException } from "nextlove"
import { z } from "zod"

const TODO_ID = "7e100fdd-04a5-47f8-82da-ce93266b4cac"

export const commonParams = z.object({
id: z.string().uuid(),
})

export const route_spec = {
methods: ["DELETE"],
auth: "auth_token",
commonParams,
jsonResponse: z.object({
ok: z.boolean(),
}),
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdge(route_spec)(async (req, res) => {
const { id } = req.commonParams
if (id !== TODO_ID)
throw new NotFoundException({
type: "todo_not_found",
message: `Todo ${id} not found`,
data: { id },
})

return res.status(200).json({ ok: true })
})
34 changes: 34 additions & 0 deletions apps/example-todo-app/pages/api/todo/delete/edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { withRouteSpecEdge } from "lib/middlewares"
import { NotFoundException } from "nextlove"
import { z } from "zod"

const TODO_ID = "7e100fdd-04a5-47f8-82da-ce93266b4cac"

export const jsonBody = z.object({
id: z.string().uuid(),
})

export const route_spec = {
methods: ["DELETE"],
auth: "auth_token",
jsonBody,
jsonResponse: z.object({
ok: z.boolean(),
}),
} as const

export const config = {
runtime: "edge",
}

export default withRouteSpecEdge(route_spec)(async (req, res) => {
const { id } = req.jsonBody
if (id !== TODO_ID)
throw new NotFoundException({
type: "todo_not_found",
message: `Todo ${id} not found`,
data: { id },
})

return res.status(200).json({ ok: true })
})
Loading
Loading