Skip to content

Commit 024be9a

Browse files
committed
✨ support .swagger.yarml file on api folder
improve perf with tsup and vitest
1 parent ad4fe20 commit 024be9a

File tree

12 files changed

+1998
-5101
lines changed

12 files changed

+1998
-5101
lines changed

.eslintrc.cjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
env: {
3+
browser: false,
4+
es2021: true,
5+
node: true,
6+
},
7+
extends: ['airbnb'],
8+
parser: '@typescript-eslint/parser',
9+
parserOptions: {
10+
ecmaVersion: 'latest',
11+
sourceType: 'module',
12+
},
13+
plugins: ['@typescript-eslint'],
14+
rules: {
15+
'import/no-extraneous-dependencies': 'warn',
16+
'import/no-unresolved': 'warn',
17+
'import/extensions': 'warn',
18+
},
19+
};

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ node_modules
44
dist
55

66
.vercel
7-
docs
7+
docs
8+
coverage

.husky/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx lint-staged

.prettierrc.cjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
printWidth: 80,
3+
semi: true,
4+
singleQuote: true,
5+
trailingComma: 'es5',
6+
importOrder: ['^@core/(.*)$', '^@server/(.*)$', '^@ui/(.*)$', '^[./]'],
7+
importOrderSeparation: true,
8+
importOrderSortSpecifiers: true,
9+
};

package.json

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,68 @@
1515
"author": "Huynh Duc Dung",
1616
"type": "module",
1717
"exports": {
18-
"require": "./dist/index.js",
19-
"import": "./dist/next-swagger-doc.esm.js"
18+
"require": "./dist/index.cjs",
19+
"import": "./dist/index.js"
2020
},
21-
"main": "dist/index.js",
22-
"module": "dist/next-swagger-doc.esm.js",
21+
"main": "dist/index.cjs",
22+
"module": "dist/index.js",
2323
"typings": "dist/index.d.ts",
2424
"files": [
2525
"dist",
2626
"src"
2727
],
2828
"scripts": {
29-
"analyze": "size-limit --why",
30-
"build": "tsdx build",
31-
"lint": "tsdx lint",
32-
"prepare": "tsdx build",
33-
"size": "size-limit",
34-
"start": "tsdx watch",
35-
"test": "tsdx test",
29+
"build": "tsup",
30+
"coverage": "vitest run --coverage",
31+
"lint": "eslint src/**/*.ts",
32+
"prepare": "husky install && tsup",
33+
"start": "tsup --watch",
34+
"test": "vitest",
35+
"test:run": "vitest run",
36+
"test:ui": "vitest --ui",
3637
"vercel-build": "npx typedoc src/index.ts"
3738
},
38-
"husky": {
39-
"hooks": {
40-
"pre-commit": "tsdx lint"
41-
}
42-
},
43-
"prettier": {
44-
"printWidth": 80,
45-
"semi": true,
46-
"singleQuote": true,
47-
"trailingComma": "es5"
39+
"lint-staged": {
40+
"*.{js,jsx,ts,tsx}": "eslint --fix",
41+
"*.{js,css,md}": "prettier --write",
42+
"package.json": "sort-package-json"
4843
},
4944
"dependencies": {
50-
"isarray": "^2.0.5",
51-
"swagger-jsdoc": "^6.2.1"
45+
"isarray": "2.0.5",
46+
"swagger-jsdoc": "6.2.1"
5247
},
5348
"devDependencies": {
54-
"@size-limit/preset-small-lib": "7.0.8",
5549
"@skypack/package-check": "0.2.2",
50+
"@trivago/prettier-plugin-sort-imports": "3.2.0",
5651
"@types/swagger-jsdoc": "6.0.1",
52+
"@typescript-eslint/eslint-plugin": "5.20.0",
53+
"@typescript-eslint/parser": "5.20.0",
54+
"@vitest/ui": "^0.9.4",
5755
"all-contributors-cli": "6.20.0",
56+
"c8": "7.11.2",
57+
"eslint": "8.13.0",
58+
"eslint-config-airbnb": "19.0.4",
59+
"eslint-plugin-import": "2.26.0",
60+
"eslint-plugin-jsx-a11y": "6.5.1",
61+
"eslint-plugin-prettier": "4.0.0",
62+
"eslint-plugin-react": "7.29.4",
5863
"husky": "7.0.4",
64+
"lint-staged": "12.4.0",
5965
"next": "12.1.5",
66+
"prettier": "2.6.2",
6067
"size-limit": "7.0.8",
61-
"tsdx": "0.14.1",
62-
"tslib": "2.3.1",
68+
"sort-package-json": "1.55.0",
69+
"tslib": "2.4.0",
70+
"tsup": "5.12.6",
6371
"typedoc": "0.22.15",
64-
"typescript": "4.6.3"
72+
"typescript": "4.6.3",
73+
"vite": "2.9.5",
74+
"vitest": "0.9.4"
6575
},
6676
"peerDependencies": {
6777
"next": ">=9"
6878
},
6979
"engines": {
7080
"node": ">=12"
71-
},
72-
"size-limit": [
73-
{
74-
"path": "dist/next-swagger-doc.cjs.production.min.js",
75-
"limit": "10 KB"
76-
},
77-
{
78-
"path": "dist/next-swagger-doc.esm.js",
79-
"limit": "10 KB"
80-
}
81-
]
81+
}
8282
}

src/with-swagger.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { NextApiRequest, NextApiResponse } from 'next';
22
import { join } from 'path';
3-
43
import swaggerJsdoc, { Options } from 'swagger-jsdoc';
54

65
type SwaggerOptions = Options & {
@@ -37,14 +36,16 @@ export function createSwaggerSpec({
3736
const scanFolders = [apiFolder, ...schemaFolders];
3837

3938
const options: Options = {
40-
apis: scanFolders.flatMap(apiFolder => {
41-
const buildApiDirectory = join(process.cwd(), '.next/server', apiFolder);
42-
const apiDirectory = join(process.cwd(), apiFolder);
39+
apis: scanFolders.flatMap((folder) => {
40+
const buildApiDirectory = join(process.cwd(), '.next/server', folder);
41+
const apiDirectory = join(process.cwd(), folder);
42+
const fileTypes = ['ts', 'tsx', 'jsx', 'js', 'swagger.yaml'];
4343
return [
44-
`${apiDirectory}/**/*.js`,
45-
`${apiDirectory}/**/*.ts`,
46-
`${apiDirectory}/**/*.tsx`,
47-
`${buildApiDirectory}/**/*.js`,
44+
...fileTypes.map((fileType) => `${apiDirectory}/**/*.${fileType}`),
45+
// only scan build directory for *.swagger.yaml and *.js files
46+
...['js', 'swagger.yaml'].map(
47+
(fileType) => `${buildApiDirectory}/**/*.${fileType}`,
48+
),
4849
];
4950
}), // files containing annotations as above
5051
...swaggerOptions,
@@ -67,18 +68,16 @@ export function withSwagger({
6768
schemaFolders = [],
6869
...swaggerOptions
6970
}: SwaggerOptions = defaultOptions) {
70-
return () => {
71-
return (_req: NextApiRequest, res: NextApiResponse) => {
72-
try {
73-
const swaggerSpec = createSwaggerSpec({
74-
apiFolder,
75-
schemaFolders,
76-
...swaggerOptions,
77-
});
78-
res.status(200).send(swaggerSpec);
79-
} catch (error) {
80-
res.status(400).send(error);
81-
}
82-
};
71+
return () => (_req: NextApiRequest, res: NextApiResponse) => {
72+
try {
73+
const swaggerSpec = createSwaggerSpec({
74+
apiFolder,
75+
schemaFolders,
76+
...swaggerOptions,
77+
});
78+
res.status(200).send(swaggerSpec);
79+
} catch (error) {
80+
res.status(400).send(error);
81+
}
8382
};
8483
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1
22

3-
exports[`withSwagger should create default swagger json option 1`] = `
4-
Object {
5-
"components": Object {},
6-
"info": Object {
3+
exports[`withSwagger > should create default swagger json option 1`] = `
4+
{
5+
"components": {},
6+
"info": {
77
"title": "NextJS Swagger",
88
"version": "0.1.0",
99
},
1010
"openapi": "3.0.0",
11-
"paths": Object {},
12-
"tags": Array [],
11+
"paths": {},
12+
"tags": [],
1313
}
1414
`;

test/with-swagger.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, it } from 'vitest';
2+
13
import { createSwaggerSpec } from '../src/with-swagger';
24

35
describe('withSwagger', () => {
@@ -11,7 +13,7 @@ describe('withSwagger', () => {
1113
version: '0.1.0',
1214
},
1315
},
14-
})
16+
}),
1517
).toMatchSnapshot();
1618
});
1719
});

tsup.config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { defineConfig } from 'tsup';
2+
3+
export default defineConfig([
4+
{
5+
entry: ['src/index.ts'],
6+
splitting: false,
7+
sourcemap: true,
8+
clean: true,
9+
dts: true,
10+
format: ['cjs', 'esm'],
11+
},
12+
{
13+
name: 'minified',
14+
entry: ['src/index.ts'],
15+
splitting: false,
16+
sourcemap: true,
17+
clean: true,
18+
dts: true,
19+
format: ['cjs', 'esm'],
20+
minify: true,
21+
legacyOutput: true,
22+
outDir: 'dist/minified',
23+
},
24+
]);

0 commit comments

Comments
 (0)