Skip to content

Commit 884635b

Browse files
committed
feat(cors): add cors middleware
1 parent f88e29d commit 884635b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/cors/index.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { CorsMiddleware } from './index';
2+
import { expect } from 'chai';
3+
4+
describe('CorsMiddleware', () => {
5+
let middleware: CorsMiddleware;
6+
7+
beforeEach(() => {
8+
middleware = new CorsMiddleware();
9+
});
10+
11+
it('should be defined', () => {
12+
expect(middleware).to.not.be.undefined;
13+
});
14+
15+
it('should have a function called resolve', () => {
16+
expect(middleware.resolve).to.be.instanceof(Function);
17+
});
18+
19+
it('should should return a middleware from calling resolve', () => {
20+
expect(middleware.resolve()).to.be.an.instanceof(Function);
21+
});
22+
});

src/cors/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as cors from 'cors';
2+
3+
import { Middleware, NestMiddleware } from '@nestjs/common';
4+
5+
import { RequestHandler } from 'express';
6+
7+
@Middleware()
8+
export class CorsMiddleware implements NestMiddleware {
9+
10+
public static configure(opts: cors.CorsOptions) {
11+
this.options = opts;
12+
}
13+
14+
private static options: cors.CorsOptions;
15+
16+
17+
public resolve(...args: any[]) {
18+
if (CorsMiddleware.options) {
19+
return cors(CorsMiddleware.options);
20+
} else {
21+
return cors();
22+
}
23+
}
24+
}

src/cors/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@nest-middlewares/cors",
3+
"version": "1.0.0",
4+
"description": "NestJS middleware for Helmet",
5+
"main": "index",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/wbhob/nest-middlewares.git"
12+
},
13+
"keywords": [
14+
"nest",
15+
"middlewares",
16+
"express",
17+
"node"
18+
],
19+
"author": "Wilson Hobbs <wilsonhobbs1@gmail.com>",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/wbhob/nest-middlewares/issues"
23+
},
24+
"homepage": "https://github.com/wbhob/nest-middlewares#readme",
25+
"dependencies": {
26+
"cors": "^2.8.4"
27+
},
28+
"peerDependencies": {
29+
"@nestjs/common": "^4.0.0"
30+
}
31+
}

0 commit comments

Comments
 (0)