Skip to content

Commit

Permalink
✨ support .swagger.yarml file on api folder
Browse files Browse the repository at this point in the history
improve perf with tsup and vitest
  • Loading branch information
jellydn committed Apr 22, 2022
1 parent ad4fe20 commit 024be9a
Show file tree
Hide file tree
Showing 12 changed files with 1,998 additions and 5,101 deletions.
19 changes: 19 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
env: {
browser: false,
es2021: true,
node: true,
},
extends: ['airbnb'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
rules: {
'import/no-extraneous-dependencies': 'warn',
'import/no-unresolved': 'warn',
'import/extensions': 'warn',
},
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
dist

.vercel
docs
docs
coverage
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
9 changes: 9 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
printWidth: 80,
semi: true,
singleQuote: true,
trailingComma: 'es5',
importOrder: ['^@core/(.*)$', '^@server/(.*)$', '^@ui/(.*)$', '^[./]'],
importOrderSeparation: true,
importOrderSortSpecifiers: true,
};
76 changes: 38 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,68 @@
"author": "Huynh Duc Dung",
"type": "module",
"exports": {
"require": "./dist/index.js",
"import": "./dist/next-swagger-doc.esm.js"
"require": "./dist/index.cjs",
"import": "./dist/index.js"
},
"main": "dist/index.js",
"module": "dist/next-swagger-doc.esm.js",
"main": "dist/index.cjs",
"module": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"scripts": {
"analyze": "size-limit --why",
"build": "tsdx build",
"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
"start": "tsdx watch",
"test": "tsdx test",
"build": "tsup",
"coverage": "vitest run --coverage",
"lint": "eslint src/**/*.ts",
"prepare": "husky install && tsup",
"start": "tsup --watch",
"test": "vitest",
"test:run": "vitest run",
"test:ui": "vitest --ui",
"vercel-build": "npx typedoc src/index.ts"
},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
"lint-staged": {
"*.{js,jsx,ts,tsx}": "eslint --fix",
"*.{js,css,md}": "prettier --write",
"package.json": "sort-package-json"
},
"dependencies": {
"isarray": "^2.0.5",
"swagger-jsdoc": "^6.2.1"
"isarray": "2.0.5",
"swagger-jsdoc": "6.2.1"
},
"devDependencies": {
"@size-limit/preset-small-lib": "7.0.8",
"@skypack/package-check": "0.2.2",
"@trivago/prettier-plugin-sort-imports": "3.2.0",
"@types/swagger-jsdoc": "6.0.1",
"@typescript-eslint/eslint-plugin": "5.20.0",
"@typescript-eslint/parser": "5.20.0",
"@vitest/ui": "^0.9.4",
"all-contributors-cli": "6.20.0",
"c8": "7.11.2",
"eslint": "8.13.0",
"eslint-config-airbnb": "19.0.4",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-jsx-a11y": "6.5.1",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-react": "7.29.4",
"husky": "7.0.4",
"lint-staged": "12.4.0",
"next": "12.1.5",
"prettier": "2.6.2",
"size-limit": "7.0.8",
"tsdx": "0.14.1",
"tslib": "2.3.1",
"sort-package-json": "1.55.0",
"tslib": "2.4.0",
"tsup": "5.12.6",
"typedoc": "0.22.15",
"typescript": "4.6.3"
"typescript": "4.6.3",
"vite": "2.9.5",
"vitest": "0.9.4"
},
"peerDependencies": {
"next": ">=9"
},
"engines": {
"node": ">=12"
},
"size-limit": [
{
"path": "dist/next-swagger-doc.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/next-swagger-doc.esm.js",
"limit": "10 KB"
}
]
}
}
41 changes: 20 additions & 21 deletions src/with-swagger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { join } from 'path';

import swaggerJsdoc, { Options } from 'swagger-jsdoc';

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

const options: Options = {
apis: scanFolders.flatMap(apiFolder => {
const buildApiDirectory = join(process.cwd(), '.next/server', apiFolder);
const apiDirectory = join(process.cwd(), apiFolder);
apis: scanFolders.flatMap((folder) => {
const buildApiDirectory = join(process.cwd(), '.next/server', folder);
const apiDirectory = join(process.cwd(), folder);
const fileTypes = ['ts', 'tsx', 'jsx', 'js', 'swagger.yaml'];
return [
`${apiDirectory}/**/*.js`,
`${apiDirectory}/**/*.ts`,
`${apiDirectory}/**/*.tsx`,
`${buildApiDirectory}/**/*.js`,
...fileTypes.map((fileType) => `${apiDirectory}/**/*.${fileType}`),
// only scan build directory for *.swagger.yaml and *.js files
...['js', 'swagger.yaml'].map(
(fileType) => `${buildApiDirectory}/**/*.${fileType}`,
),
];
}), // files containing annotations as above
...swaggerOptions,
Expand All @@ -67,18 +68,16 @@ export function withSwagger({
schemaFolders = [],
...swaggerOptions
}: SwaggerOptions = defaultOptions) {
return () => {
return (_req: NextApiRequest, res: NextApiResponse) => {
try {
const swaggerSpec = createSwaggerSpec({
apiFolder,
schemaFolders,
...swaggerOptions,
});
res.status(200).send(swaggerSpec);
} catch (error) {
res.status(400).send(error);
}
};
return () => (_req: NextApiRequest, res: NextApiResponse) => {
try {
const swaggerSpec = createSwaggerSpec({
apiFolder,
schemaFolders,
...swaggerOptions,
});
res.status(200).send(swaggerSpec);
} catch (error) {
res.status(400).send(error);
}
};
}
14 changes: 7 additions & 7 deletions test/__snapshots__/with-swagger.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1

exports[`withSwagger should create default swagger json option 1`] = `
Object {
"components": Object {},
"info": Object {
exports[`withSwagger > should create default swagger json option 1`] = `
{
"components": {},
"info": {
"title": "NextJS Swagger",
"version": "0.1.0",
},
"openapi": "3.0.0",
"paths": Object {},
"tags": Array [],
"paths": {},
"tags": [],
}
`;
4 changes: 3 additions & 1 deletion test/with-swagger.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import { createSwaggerSpec } from '../src/with-swagger';

describe('withSwagger', () => {
Expand All @@ -11,7 +13,7 @@ describe('withSwagger', () => {
version: '0.1.0',
},
},
})
}),
).toMatchSnapshot();
});
});
24 changes: 24 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { defineConfig } from 'tsup';

export default defineConfig([
{
entry: ['src/index.ts'],
splitting: false,
sourcemap: true,
clean: true,
dts: true,
format: ['cjs', 'esm'],
},
{
name: 'minified',
entry: ['src/index.ts'],
splitting: false,
sourcemap: true,
clean: true,
dts: true,
format: ['cjs', 'esm'],
minify: true,
legacyOutput: true,
outDir: 'dist/minified',
},
]);
5 changes: 5 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: { globals: true },
});
Loading

0 comments on commit 024be9a

Please sign in to comment.