Skip to content

Commit

Permalink
Add JS route with external dependency to example app
Browse files Browse the repository at this point in the history
This allows testing that API routes written with JS are also
supported and that third-party dependencies like JSDom
don't cause issues in the OpenAPI generation.

This commit also adds an example on writing custom scripts
for generating/validating the OpenAPI spec.

The deep object comparison as part of the OpenAPI
generation/validation is also replaced with a faster
stringified comparison between the generated and
existing OpenAPI specs.

Co-authored-by: Austin Kelleher <[email protected]>
  • Loading branch information
blomqma and austinkelleher committed Apr 16, 2024
1 parent 2f09414 commit 5e3e980
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 4 deletions.
3 changes: 3 additions & 0 deletions apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
"start": "next start",
"generate": "pnpm prebuild && NODE_OPTIONS='--import=tsx' next-rest-framework generate",
"validate": "pnpm prebuild && NODE_OPTIONS='--import=tsx' next-rest-framework validate",
"custom-generate-openapi": "pnpm prebuild && tsx ./src/scripts/custom-generate-openapi.ts",
"custom-validate-openapi": "pnpm prebuild && tsx ./src/scripts/custom-validate-openapi.ts",
"lint": "tsc && next lint"
},
"dependencies": {
"jsdom": "24.0.0",
"next-rest-framework": "workspace:*",
"tsx": "4.7.2",
"zod-form-data": "2.0.2"
Expand Down
2 changes: 2 additions & 0 deletions apps/example/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
}
}
},
"/api/v1/js-endpoint": { "get": { "operationId": "jsEndpoint" } },
"/api/v1/route-with-params/{slug}": {
"get": {
"operationId": "getParams",
Expand Down Expand Up @@ -526,6 +527,7 @@
}
}
},
"/api/v2/js-endpoint": { "get": { "operationId": "jsEndpoint" } },
"/api/v2/route-with-params/{slug}": {
"get": {
"operationId": "getPathParams",
Expand Down
13 changes: 13 additions & 0 deletions apps/example/src/app/api/v2/js-endpoint/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { route, routeOperation } from 'next-rest-framework';
import { JSDOM } from 'jsdom';

export const runtime = 'edge';

export const { GET } = route({
jsEndpoint: routeOperation({
method: 'GET'
}).handler((_req, res) => {
const dom = new JSDOM('<!DOCTYPE html><p>Hello world</p>');
res.json(dom);
})
});
11 changes: 11 additions & 0 deletions apps/example/src/pages/api/v1/js-endpoint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { apiRoute, apiRouteOperation } from 'next-rest-framework';
import { JSDOM } from 'jsdom';

export default apiRoute({
jsEndpoint: apiRouteOperation({
method: 'GET'
}).handler((_req, res) => {
const dom = new JSDOM('<!DOCTYPE html><p>Hello world</p>');
res.json(dom);
})
});
7 changes: 7 additions & 0 deletions apps/example/src/scripts/custom-generate-openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { generate } from 'next-rest-framework/dist/cli/generate';

generate({ configPath: '/api/v2' })
.then(() => {
console.log('Completed building OpenAPI schema from custom script.');
})
.catch(console.error);
7 changes: 7 additions & 0 deletions apps/example/src/scripts/custom-validate-openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { validate } from 'next-rest-framework/dist/cli/validate';

validate({ configPath: '/api/v2' })
.then(() => {
console.log('Completed validating OpenAPI schema from custom script.');
})
.catch(console.error);
3 changes: 1 addition & 2 deletions packages/next-rest-framework/src/cli/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import * as prettier from 'prettier';
import { findConfig, generateOpenApiSpec } from './utils';
import { isEqualWith } from 'lodash';

const writeOpenApiSpec = async ({
path,
Expand Down Expand Up @@ -49,7 +48,7 @@ export const generate = async ({ configPath }: { configPath?: string }) => {
const data = readFileSync(path);
const openApiSpec = JSON.parse(data.toString());

if (!isEqualWith(openApiSpec, spec)) {
if (!(JSON.stringify(openApiSpec) === JSON.stringify(spec))) {
console.info(
chalk.yellowBright(
'OpenAPI spec changed, regenerating `openapi.json`...'
Expand Down
3 changes: 1 addition & 2 deletions packages/next-rest-framework/src/cli/validate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { join } from 'path';
import { findConfig, generateOpenApiSpec } from './utils';
import { readFileSync } from 'fs';
import { isEqualWith } from 'lodash';
import chalk from 'chalk';

// Check if the OpenAPI spec is up-to-date.
Expand All @@ -19,7 +18,7 @@ export const validate = async ({ configPath }: { configPath?: string }) => {
const data = readFileSync(path);
const openApiSpec = JSON.parse(data.toString());

if (!isEqualWith(openApiSpec, spec)) {
if (!(JSON.stringify(openApiSpec) === JSON.stringify(spec))) {
console.error(
chalk.red(
'API spec changed is not up-to-date. Run `next-rest-framework generate` to update it.'
Expand Down
Loading

0 comments on commit 5e3e980

Please sign in to comment.