Skip to content

Commit

Permalink
Merge pull request #76 from codyjdalton/lit-20
Browse files Browse the repository at this point in the history
LIT-20: Add ability to include middleware at the module level
  • Loading branch information
codyjdalton committed Sep 10, 2018
2 parents f77fbf5 + 97ab05b commit b68a4f1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 38 deletions.
50 changes: 49 additions & 1 deletion lib/compiler/classes/compiler.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from 'chai';

import { LitComponent, LitModule, LitService } from '../..';
import { HttpNext, HttpRequest, HttpResponse } from '../../http';
import { DeleteMapping, GetMapping, PostMapping, PutMapping } from '../../http/mappings';
import { DeleteMapping, GetMapping, PostMapping, PutMapping, RequestMapping } from '../../http/mappings';
import { LitComponentTest, TestBed } from '../../testing';

describe('Class: Compiler', () => {
Expand Down Expand Up @@ -455,4 +455,52 @@ describe('Class: Compiler', () => {
done();
});
});

it('should allow using middlewares', (done) => {

let testValue: string = '';

@LitComponent()
class PreMiddleware {

@RequestMapping()
public test(req, res, next) {
testValue = 'newValue';
next();
}
}

@LitComponent()
class SomeComponent {

@GetMapping({
path: ':id'
})
public getOne(req, res) {
res.success({
message: testValue
});
}
}

@LitModule({
exports: [
PreMiddleware,
SomeComponent
]
})
class TestModule {}

const aModule: LitComponentTest = TestBed.startModule(TestModule);

aModule.get('/some-val')
.expect(200)
.expect((res) => {
expect(res.body).deep.equal({ message: 'newValue' });
})
.end((err, res) => {
if (err) { return done(err); }
done();
});
});
});
31 changes: 1 addition & 30 deletions lib/http/classes/response.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import { HttpRequest } from '..';
import { LitComponent } from '../..';
import { LitComponentTest, TestBed } from '../../testing';
import { GetMapping, RequestMapping } from '../mappings';
import { GetMapping } from '../mappings';
import { HttpResponse } from './response.class';

describe('Class: HttpResponse', () => {
Expand Down Expand Up @@ -101,33 +101,4 @@ describe('Class: HttpResponse', () => {
done();
});
});

it('should support a custom mapping', (done) => {

@LitComponent()
class TestComponent {

@RequestMapping({
method: 'post',
path: 'someurl'
})
public customSuccess(req: HttpRequest, res: HttpResponse) {
res.success({
success: true
}, 200);
}
}

component = TestBed.start(TestComponent);
component
.post('/someurl')
.expect(200)
.expect((res: HttpRequest) => {
expect(res.body.toString()).to.equal({ success: true }.toString());
})
.end((err, res) => {
if (err) { return done(err); }
done();
});
});
});
6 changes: 2 additions & 4 deletions lib/http/decorators/mapping.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ import { RequestConfig } from '../models/request-config.model';
/**
* Create generic request mapping method
*/
const GenericMapping = (type: RequestMethod = RequestMethod.ANY) => {
const GenericMapping = (type: RequestMethod = RequestMethod.USE) => {
return (config: RequestConfig = {}): any => {
return (target: Type<any>, propertyKey: string, descriptor: PropertyDescriptor): void => {

if (type !== RequestMethod.ANY) {
config.method = type;
}
config.method = type;

Injector.set(target, config, propertyKey);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/http/enums/request-method.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* request-method.enum
*/
export enum RequestMethod {
ANY = 'any',
USE = 'use',
GET = 'get',
PUT = 'put',
POST = 'post',
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@litstack/core",
"version": "0.7.0",
"version": "0.8.0",
"description": "Typescript REST Framework",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit b68a4f1

Please sign in to comment.