Skip to content

Commit d7d90c0

Browse files
committed
feat(cookie-session): add cookie-session middleware
1 parent 8602a17 commit d7d90c0

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/cookie-session/index.spec.ts

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

src/cookie-session/index.ts

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

src/cookie-session/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/middleware-name",
3+
"version": "1.0.0",
4+
"description": "NestJS middleware for Middleware",
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+
"peerDependencies": {
26+
"@nestjs/common": "^4.0.0"
27+
},
28+
"dependencies": {
29+
"cookie-session": "^2.0.0-beta.3"
30+
}
31+
}

0 commit comments

Comments
 (0)