Skip to content

Commit

Permalink
Merge c142c9d into 4cad8aa
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoneto committed Jun 15, 2024
2 parents 4cad8aa + c142c9d commit 91b7128
Show file tree
Hide file tree
Showing 17 changed files with 1,683 additions and 1,609 deletions.
8 changes: 8 additions & 0 deletions .changeset/sharp-eyes-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'guarapi': minor
'@guarapi/eslint-config-guarapi': patch
'@guarapi/create-guarapi-app': patch
'basic-api': patch
---

add multiple handlers support and upgrade packages
18 changes: 9 additions & 9 deletions examples/basic-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
"lint": "eslint ./src"
},
"dependencies": {
"guarapi": "^1.1.1"
"guarapi": "^1.2.0"
},
"devDependencies": {
"@guarapi/eslint-config-guarapi": "^0.1.2",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.80",
"@swc/cli": "^0.1.65",
"@swc/core": "^1.5.29",
"@swc/register": "^0.1.10",
"@types/node": "^20.5.6",
"eslint": "^8.48.0",
"nodemon": "^3.0.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"typescript": "^5.2.2"
"@types/node": "^20.14.2",
"eslint": "^8.57.0",
"nodemon": "^3.1.3",
"prettier": "^3.3.2",
"rimraf": "^5.0.7",
"typescript": "^5.4.5"
},
"prettier": "@guarapi/eslint-config-guarapi/prettierrc.js",
"nodemonConfig": {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"publish": "changeset publish"
},
"devDependencies": {
"turbo": "^1.10.15",
"@changesets/cli": "^2.26.2",
"@changesets/changelog-github": "^0.4.8"
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.27.5",
"turbo": "^1.13.4"
}
}
18 changes: 9 additions & 9 deletions packages/create-guarapi-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
},
"devDependencies": {
"@guarapi/eslint-config-guarapi": "^0.1.2",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.80",
"@swc/cli": "^0.1.65",
"@swc/core": "^1.5.29",
"@swc/register": "^0.1.10",
"@types/node": "^20.5.6",
"@types/node-forge": "^1.3.6",
"@types/supertest": "^2.0.13",
"eslint": "^8.51.0",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"typescript": "^5.2.2"
"@types/node": "^20.14.2",
"@types/node-forge": "^1.3.11",
"@types/supertest": "^2.0.16",
"eslint": "^8.57.0",
"prettier": "^3.3.2",
"rimraf": "^5.0.7",
"typescript": "^5.4.5"
}
}
24 changes: 12 additions & 12 deletions packages/eslint-config-guarapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
"homepage": "https://github.com/guarapi/guarapi#readme",
"repository": "github:guarapi/guarapi",
"dependencies": {
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"eslint": "^8.51.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"eslint": "^8.57.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "^3.0.3",
"prettier-plugin-tailwindcss": "^0.5.5",
"typescript": "^5.2.2"
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react": "^7.34.2",
"eslint-plugin-react-hooks": "^4.6.2",
"prettier": "^3.3.2",
"prettier-plugin-tailwindcss": "^0.5.14",
"typescript": "^5.4.5"
}
}
26 changes: 13 additions & 13 deletions packages/guarapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@
},
"devDependencies": {
"@guarapi/eslint-config-guarapi": "^0.1.2",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.80",
"@swc/jest": "^0.2.29",
"@swc/cli": "^0.1.65",
"@swc/core": "^1.5.29",
"@swc/jest": "^0.2.36",
"@swc/register": "^0.1.10",
"@types/jest": "^29.5.4",
"@types/node": "^20.5.6",
"@types/node-forge": "^1.3.6",
"@types/supertest": "^2.0.13",
"eslint": "^8.51.0",
"jest": "^29.6.4",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.2",
"@types/node-forge": "^1.3.11",
"@types/supertest": "^2.0.16",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"node-forge": "^1.3.1",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"supertest": "^6.3.3",
"typescript": "^5.2.2"
"prettier": "^3.3.2",
"rimraf": "^5.0.7",
"supertest": "^6.3.4",
"typescript": "^5.4.5"
}
}
5 changes: 3 additions & 2 deletions packages/guarapi/src/guarapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ function Guarapi(config?: GuarapiConfig): Guarapi {
}
});
} catch (err) {
console.error('Unhandled sync rejection detected');
nextPipeline(pluginsError, req, res, err);
nextPipeline(pluginsError, req, res, err, () => {
res.status(500).end('Internal Server Error');
});
}
};

Expand Down
12 changes: 8 additions & 4 deletions packages/guarapi/src/lib/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ function routerBuilder() {
const relativeUrl = req.url!.replace(req.middlewarePath!.replace(/\/?$/, '') || '/', '');
const handlers = matchRoutes(req.method as MethodsKeys, relativeUrl);

nextPipeline(handlers, req, res, null, (error) => {
res.statusCode = error ? 500 : 404;
next(error || new Error('Not Found'));
});
try {
nextPipeline(handlers, req, res, null, (error) => {
res.statusCode = error ? 500 : 404;
next(error || new Error('Not Found'));
});
} catch (error) {
next(error);
}
} as Router;

(Object.keys(Methods) as MethodsKeys[]).forEach((method) => {
Expand Down
10 changes: 7 additions & 3 deletions packages/guarapi/src/plugins/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Middleware, MiddlewareError, Plugin, Request } from '../types';
import nextMiddleware from '../lib/next-pipeline';
import nextPipeline from '../lib/next-pipeline';

declare module '../types' {
interface Guarapi {
Expand Down Expand Up @@ -57,10 +57,14 @@ const middlewarePlugin: Plugin = (app) => {
return {
name: 'middleware',
pre: (req, res, next) => {
nextMiddleware(matchMiddlewares(req.url!), req, res, null, next);
try {
nextPipeline(matchMiddlewares(req.url!), req, res, null, next);
} catch (error) {
next(error);
}
},
error: (error, req, res) => {
nextMiddleware(errorMiddlewares, req, res, error, () => {
nextPipeline(errorMiddlewares, req, res, error, () => {
res.end(JSON.stringify({ error }));
});
},
Expand Down
54 changes: 47 additions & 7 deletions packages/guarapi/test/guarapi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import http, { Server } from 'node:http';
import http2, { Http2Server } from 'node:http2';
import guarapi, { createServer, middlewarePlugin } from '../src/index';
import type { GuarapiConfig, Plugin, ServerOptions } from '../src/types';
import guarapi, { createServer, middlewarePlugin, nextPipeline } from '../src/index';
import { generateCertificates, request } from './utils';
import type { GuarapiConfig, Plugin, ServerOptions } from '../src/types';

describe('Guarapi', () => {
const env = process.env;
Expand Down Expand Up @@ -111,9 +111,9 @@ describe('Guarapi', () => {
});
await request(server).get('/');

expect(pluginThreePostHandler).toBeCalledTimes(1);
expect(pluginTwoPreHandler).toBeCalledTimes(1);
expect(pluginOne).toBeCalledTimes(1);
expect(pluginThreePostHandler).toHaveBeenCalledTimes(1);
expect(pluginTwoPreHandler).toHaveBeenCalledTimes(1);
expect(pluginOne).toHaveBeenCalledTimes(1);
});

it('should app throw err trying to use plugins not applyed', () => {
Expand All @@ -139,7 +139,7 @@ describe('Guarapi', () => {

await request(server).get('/').set('Host', 'localhost');

expect(httpVersion).toBeCalledWith('1.1');
expect(httpVersion).toHaveBeenCalledWith('1.1');
});

it('should app works with http2 ssl server', async () => {
Expand All @@ -158,7 +158,7 @@ describe('Guarapi', () => {

await request(server, { http2: true }).get('/').set('Host', 'localhost');

expect(httpVersion).toBeCalledWith('2.0');
expect(httpVersion).toHaveBeenCalledWith('2.0');
});

it('should respond with json', async () => {
Expand Down Expand Up @@ -186,4 +186,44 @@ describe('Guarapi', () => {

await request(server).get('/').expect(401, 'Unauthorized');
});

it('should handle plugin rejection', async () => {
const app = guarapi();
const server = createServer({}, app);

app.plugin(() => {
return {
name: 'unhandledSyncThrow',
pre: (_req, _res, _next) => {
throw new Error('Oh no');
},
error: (error, req, res) => {
nextPipeline([], req, res, error, (err) => {
res.status(500).end((err as Error).message);
});
},
};
});

await request(server).get('/').expect(500, 'Oh no');
});

it('should handle final rejection', async () => {
const app = guarapi();
const server = createServer({}, app);

app.plugin(() => {
return {
name: 'unhandledSyncThrow',
pre: (_req, _res, _next) => {
throw new Error('Oh no');
},
error: (error, req, res, next) => {
nextPipeline([], req, res, error, next);
},
};
});

await request(server).get('/').expect(500, 'Internal Server Error');
});
});
8 changes: 4 additions & 4 deletions packages/guarapi/test/lib/create-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Guarapi - lib/create-server', () => {

await request(server).get('/');

expect(httpVersion).toBeCalledWith('1.1');
expect(httpVersion).toHaveBeenCalledWith('1.1');
});

it('should create http2 server', async () => {
Expand All @@ -42,7 +42,7 @@ describe('Guarapi - lib/create-server', () => {

await request(server, { http2: true }).get('/');

expect(httpVersion).toBeCalledWith('2.0');
expect(httpVersion).toHaveBeenCalledWith('2.0');
});

it('should create ssl http1 server', async () => {
Expand All @@ -60,7 +60,7 @@ describe('Guarapi - lib/create-server', () => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
await request(server).get('/').set('Host', 'localhost');

expect(httpVersion).toBeCalledWith('1.1');
expect(httpVersion).toHaveBeenCalledWith('1.1');
});

it('should create ssl http2 server', async () => {
Expand All @@ -79,6 +79,6 @@ describe('Guarapi - lib/create-server', () => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
await request(server, { http2: true }).get('/').set('Host', 'localhost');

expect(httpVersion).toBeCalledWith('2.0');
expect(httpVersion).toHaveBeenCalledWith('2.0');
});
});
12 changes: 6 additions & 6 deletions packages/guarapi/test/lib/next-pipeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ describe('Guarapi - lib/next-pipeline', () => {

nextPipeline(pipeline, {} as Request, {} as Response);

expect(fnOne).toBeCalledTimes(1);
expect(fnTwo).toBeCalledTimes(1);
expect(fnOne).toHaveBeenCalledTimes(1);
expect(fnTwo).toHaveBeenCalledTimes(1);
});

it('should run async pipeline', (done) => {
Expand All @@ -43,8 +43,8 @@ describe('Guarapi - lib/next-pipeline', () => {
next();
},
async () => {
expect(fnOne).toBeCalledTimes(1);
expect(fnTwo).toBeCalledTimes(1);
expect(fnOne).toHaveBeenCalledTimes(1);
expect(fnTwo).toHaveBeenCalledTimes(1);
done();
},
];
Expand All @@ -69,7 +69,7 @@ describe('Guarapi - lib/next-pipeline', () => {
nextPipeline(pipeline, {} as Request, {} as Response, null, (err) => {
expect(err).toBeInstanceOf(Error);
expect((err as Error).message).toEqual('Something goes wrong');
expect(fnOne).toBeCalledTimes(1);
expect(fnOne).toHaveBeenCalledTimes(1);
expect(fnTwo).not.toBeCalled();
});
});
Expand All @@ -90,7 +90,7 @@ describe('Guarapi - lib/next-pipeline', () => {

expect(() => {
nextPipeline(pipeline, {} as Request, {} as Response, null, () => {
expect(fnOne).toBeCalledTimes(1);
expect(fnOne).toHaveBeenCalledTimes(1);
expect(fnTwo).not.toBeCalled();
});
}).toThrow('You should catch errors and pass in next function');
Expand Down
Loading

0 comments on commit 91b7128

Please sign in to comment.