Skip to content

Commit

Permalink
feat(connect-rid): add connect-rid middleware and respective types
Browse files Browse the repository at this point in the history
  • Loading branch information
wbhob committed Nov 23, 2017
1 parent b0d4426 commit 78f93aa
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/compression/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nest-middlewares/compression",
"version": "1.0.0",
"description": "NestJS middleware for Helmet",
"description": "NestJS middleware for Compression",
"main": "index",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
34 changes: 34 additions & 0 deletions src/connect-rid/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ConnectRidMiddleware } from './index';
import { expect } from 'chai';

describe('ConnectRidMiddleware', () => {
let middleware: ConnectRidMiddleware;
describe('properly configured', () => {
beforeEach(() => {
ConnectRidMiddleware.configure({});
middleware = new ConnectRidMiddleware();
});

it('should be defined', () => {
expect(middleware).to.not.be.undefined;
});

it('should have a function called resolve', () => {
expect(middleware.resolve).to.be.instanceof(Function);
});

it('should should return a middleware from calling resolve', () => {
expect(middleware.resolve()).to.be.an.instanceof(Function);
});
afterEach(() => {
ConnectRidMiddleware.configure(undefined);
});
});

describe('not configured', () => {
middleware = new ConnectRidMiddleware();
it('should should return a middleware from calling resolve', () => {
expect(middleware.resolve()).to.be.an.instanceof(Function);
});
});
});
24 changes: 24 additions & 0 deletions src/connect-rid/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as rid from 'connect-rid';

import { Middleware, NestMiddleware } from '@nestjs/common';

import { RequestHandler } from 'express';

@Middleware()
export class ConnectRidMiddleware implements NestMiddleware {

// DELETE THESE LINES IF MIDDLEWARE DOES NOT TAKE OPTIONS
public static configure(opts: rid.ConnectRidOptions) {
this.options = opts;
}

private static options: rid.ConnectRidOptions;

public resolve(...args: any[]) {
if (ConnectRidMiddleware.options) {
return rid(ConnectRidMiddleware.options);
} else {
return rid();
}
}
}
31 changes: 31 additions & 0 deletions src/connect-rid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@nest-middlewares/connect-rid",
"version": "1.0.0",
"description": "NestJS middleware for Connect RID",
"main": "index",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/wbhob/nest-middlewares.git"
},
"keywords": [
"nest",
"middlewares",
"express",
"node"
],
"author": "Wilson Hobbs <wilsonhobbs1@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/wbhob/nest-middlewares/issues"
},
"homepage": "https://github.com/wbhob/nest-middlewares#readme",
"peerDependencies": {
"@nestjs/common": "^4.0.0"
},
"dependencies": {
"connect-rid": "0.0.1"
}
}
2 changes: 1 addition & 1 deletion src/cors/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nest-middlewares/cors",
"version": "1.0.0",
"description": "NestJS middleware for Helmet",
"description": "NestJS middleware for CORS",
"main": "index",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
4 changes: 2 additions & 2 deletions template/USING_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ Thanks for contributing to Nest Middlewares! This document outlines the best pra

1. Duplicate the `tpl` folder and move it into `src`.
1. Rename `tpl` to the **exact** name of the middleware as it is on NPM. For example, for Helmet, use `helmet`, or for `body-parser`, use `body-parser`.
1. In `package.json`, rename the `name` property to `@nest-middlewares/MIDDLEWARE_NAME`, where MIDDLEWARE_NAME is the same name as the middleware on NPM.
1. In `package.json`, rename the `name` property to `@nest-middlewares/MIDDLEWARE_NAME`, where MIDDLEWARE_NAME is the same name as the middleware on NPM. Also, be sure to change the name of `Middleware` in the description field of `package.json`.
1. `npm install` the types for your middleware in the root folder (for example, `npm install --save-dev @types/helmet`). Make sure they are saved to `devDependencies` of the root package.json.
1. `cd` into your newly created middleware folder, and `npm install` the package containing the middleware and save it to `package.json` (for example, `npm install --save helmet`).
1. In `index.ts`, change all the instances of "middleware" to what they should logically be named, maintaining capitalization. Check out [CONTRIBUTING.md](../CONTRIBUTING.md) for clarification.
1. Write tests as necessary for your middleware.
1. Run `sh ./scripts/build.sh` and make sure that no errors arise.
1. Run `lerna bootstrap` and make sure no errors arise.
1. Run `lerna clean && lerna bootstrap` and make sure no errors arise.
1. 🎉 Congratulations! You've made a middleware! File a pull request, and sit back and look at your great work.
2 changes: 1 addition & 1 deletion template/tpl/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nest-middlewares/middleware-name",
"version": "1.0.0",
"description": "NEST MIDDLEWARE TEMPLATE - DO NOT INSTALL",
"description": "NestJS middleware for Middleware",
"main": "index",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"sourceMap": true,
"target": "es6",
"typeRoots": [
"node_modules/@types"
"node_modules/@types",
"types"
],
"lib": [
"es7"
Expand Down
10 changes: 10 additions & 0 deletions types/connect-rid/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare function rid(): void;
declare function rid(opts: rid.ConnectRidOptions): void;

declare namespace rid {
export interface ConnectRidOptions {
headerName?: string;
}
}

export = rid;

0 comments on commit 78f93aa

Please sign in to comment.