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: Add route spec extraction command #134

Merged
merged 11 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ generateOpenAPI({
})
```

### Extracting route specs (Command Line)

Just run `nextlove extract-route-specs` in your project root! It will output a ESM file bundled by esbuild.

Caveats:

- All dependencies and dev dependencies in your `package.json` are automatically marked as external when bundling. This means that you may want to re-bundle the output file if you plan on publishing it as part of a library.
- By default, API route files aren't allowed to import anything besides dependencies declared in `package.json`. This is to avoid accidentally polluting the bundle. To allow specific imports, use the `--allowed-import-patterns` flag: `--allowed-import-patterns '**/lib/**' --allowed-import-patterns '**/models/**'`

### Wrap middlewares together using `wrappers`!

```ts
Expand Down
1 change: 0 additions & 1 deletion apps/example-todo-app/lib/middlewares/with-route-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from "nextlove"
import { withAuthToken } from "./with-auth-token"
import { withUserSession } from "./with-user-session"
export { checkRouteSpec } from "nextlove"
import * as ZT from "lib/zod"
import withGlobalMiddlewareAfterAuth from "./with-global-middeware-after-auth"

Expand Down
1 change: 1 addition & 0 deletions apps/example-todo-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "rimraf dist && nsm build && tsup ./index.ts --dts --sourcemap && cpy .next dist/.next",
"build:openapi": "nextlove generate-openapi --packageDir . --apiName 'Example TODO API' --outputFile ./openapi.json",
"build:type": "nextlove generate-route-types --packageDir .",
"build:routespecs": "nextlove extract-route-specs --packageDir . --allowed-import-patterns '**/lib/zod.ts' --allowed-import-patterns '**/pages/api/**/*.ts' --outputFile dist/extracted-route-specs.mjs",
"start": "next start",
"lint": "next lint",
"test": "ava"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
withRouteSpecWithoutValidateResponse,
checkRouteSpec,
} from "lib/middlewares"
import { withRouteSpecWithoutValidateResponse } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
checkRouteSpec,
withRouteSpecWithGlobalMiddlewareAfterAuth,
} from "lib/middlewares"
import { withRouteSpecWithGlobalMiddlewareAfterAuth } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"

Expand Down
3 changes: 2 additions & 1 deletion apps/example-todo-app/pages/api/todo/add.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { withRouteSpec, checkRouteSpec } from "lib/middlewares"
import { withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
withRouteSpecSupportedArrayFormats,
checkRouteSpec,
} from "lib/middlewares"
import { withRouteSpecSupportedArrayFormats } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"

export const queryParams = z.object({
Expand Down
6 changes: 2 additions & 4 deletions apps/example-todo-app/pages/api/todo/array-query-comma.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
withRouteSpecSupportedArrayFormats,
checkRouteSpec,
} from "lib/middlewares"
import { withRouteSpecSupportedArrayFormats } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"

export const queryParams = z.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkRouteSpec, withRouteSpec } from "lib/middlewares"
import { withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { z } from "zod"

export const queryParams = z.object({
Expand Down
6 changes: 2 additions & 4 deletions apps/example-todo-app/pages/api/todo/array-query-repeat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
withRouteSpecSupportedArrayFormats,
checkRouteSpec,
} from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpecSupportedArrayFormats } from "lib/middlewares"
import { z } from "zod"

export const queryParams = z.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkRouteSpec, withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { NotFoundException } from "nextlove"
import { TODO_ID } from "tests/fixtures"
import { z } from "zod"
Expand Down
3 changes: 2 additions & 1 deletion apps/example-todo-app/pages/api/todo/delete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkRouteSpec, withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { NotFoundException } from "nextlove"
import { TODO_ID } from "tests/fixtures"
import { z } from "zod"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { withRouteSpec, checkRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"

export const jsonBody = z.object({})
Expand Down
13 changes: 4 additions & 9 deletions apps/example-todo-app/pages/api/todo/form-add.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { withRouteSpec, checkRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"
import { HttpException } from "nextlove"

export const formData = z.object({
id: z.string().uuid().optional().default(uuidv4()),
title: z.string(),
completed: z.boolean().optional().default(false),
})
import { jsonBody } from "./add"

export const route_spec = checkRouteSpec({
methods: ["POST"],
auth: "auth_token",
formData,
formData: jsonBody,
jsonResponse: z.object({
ok: z.boolean(),
}),
Expand Down
3 changes: 2 additions & 1 deletion apps/example-todo-app/pages/api/todo/get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkRouteSpec, withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { NotFoundException } from "nextlove"
import { TODO_ID } from "tests/fixtures"
import { z } from "zod"
Expand Down
3 changes: 2 additions & 1 deletion apps/example-todo-app/pages/api/todo/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkRouteSpec, withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { NotFoundException } from "nextlove"
import { TODO_ID } from "tests/fixtures"
import { z } from "zod"
Expand Down
3 changes: 2 additions & 1 deletion apps/example-todo-app/pages/api/todo/list-optional-ids.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkRouteSpec, withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"
import * as ZT from "lib/zod"

Expand Down
3 changes: 2 additions & 1 deletion apps/example-todo-app/pages/api/todo/list-with-refine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkRouteSpec, withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"
import * as ZT from "lib/zod"

Expand Down
3 changes: 2 additions & 1 deletion apps/example-todo-app/pages/api/todo/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkRouteSpec, withRouteSpec } from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"
import * as ZT from "lib/zod"

Expand Down
6 changes: 2 additions & 4 deletions apps/example-todo-app/pages/api/todo/no-validate-body.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
checkRouteSpec,
withRouteSpecWithoutValidateGetRequestBody,
} from "lib/middlewares"
import { checkRouteSpec } from "nextlove"
import { withRouteSpecWithoutValidateGetRequestBody } from "lib/middlewares"
import { z } from "zod"

export const route_spec = checkRouteSpec({
Expand Down
2 changes: 1 addition & 1 deletion apps/example-todo-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2020",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down
Loading
Loading