Skip to content

Commit

Permalink
Merge pull request #54 from codyjdalton/lit-49
Browse files Browse the repository at this point in the history
lit-49: Use enum for request types and add types throughout
  • Loading branch information
codyjdalton authored May 17, 2018
2 parents b3dee8f + 0154b2c commit 544e63c
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 205 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { GetMapping } from '@litstack/core/dist/http/mappings';
@LitComponent()
export class AppComponent {

private message = 'Hello world!';
private message = 'Hello World!';

@GetMapping({
path: '' // GET / is routed to onHello
Expand All @@ -56,6 +56,8 @@ export class AppComponent {
Test your components [using supertest methods](https://github.com/visionmedia/supertest) and the Litstack TestBed:

```typescript
import { TestBed, LitComponentTest } from '@litstack/core/dist/testing';

describe('AppComponent', () => {

let component: LitComponentTest;
Expand All @@ -72,15 +74,16 @@ describe('AppComponent', () => {

it('should return a welcome message', (done) => {

component.get('/')
.expect(200)
.expect((res) => {
expect(res.body.message).to.equal('Hello World!');
})
.end((err, res) => {
if (err) return done(err);
done();
});
component
.get('/')
.expect(200)
.expect((res) => {
expect(res.body.message).to.equal('Hello World!');
})
.end((err, res) => {
if (err) return done(err);
done();
});
});
});
```
Expand Down
26 changes: 14 additions & 12 deletions lib/compiler/classes/compiler.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import express = require('express');
import BodyParser = require('body-parser');

import { Application } from 'express';
import { Application, RequestHandler } from 'express';


import { CoreCompiler, ILitComponent, ILitModule } from '../utils/compiler.utils';
import { HttpServer } from '../../http/utils/http.utils';
import { RequestMethod } from '../../http/enums/request-method.enum';
import { HttpResponse } from '../../http/classes/response.class';
import { Injector } from './injector.class';

Expand Down Expand Up @@ -47,8 +49,6 @@ export class ServiceCompiler extends CoreCompiler {
*/
private listen(port: string | number): void {

port = process.env.PORT || port;

this.server = this.app.listen(port, this.greet(port));
}

Expand Down Expand Up @@ -118,19 +118,21 @@ export class ServiceCompiler extends CoreCompiler {
* @param {ILitComponent} aComponent
* @param {string} name
*/
private getHandler(aComponent: ILitComponent, name: string): Function {
return (req: any, res: any) => {
private getHandler(aComponent: ILitComponent, name: string): RequestHandler {
return (req: express.Request,
res: express.Response,
next: express.NextFunction): RequestHandler => {

// include metadata to send with response
const meta: Object = Injector.getAll(
aComponent,
name
);

aComponent[name](req, new HttpResponse(res, meta));
return aComponent[name](req, new HttpResponse(res, meta));
};
}

/**
* @function addRoute
* @param {string} method
Expand All @@ -145,7 +147,7 @@ export class ServiceCompiler extends CoreCompiler {
* POST /some/route
* adds handler aComponent.someMethod
*/
private addRoute(method: string, path: string, aComponent: ILitComponent, name: string): void {
private addRoute(method: RequestMethod, path: string, aComponent: ILitComponent, name: string): void {
this.app[method]('/' + path, this.getHandler(aComponent, name));
}

Expand All @@ -167,7 +169,7 @@ export class ServiceCompiler extends CoreCompiler {

// get a new instance of the component
const aComponent: ILitComponent = Injector.resolve(component);
const reqMethod: string = Injector.get(aComponent, 'method', null, method);
const reqMethod: RequestMethod = Injector.get(aComponent, 'method', null, method);

// check if method is elligible for route and add
if(reqMethod) {
Expand All @@ -193,9 +195,9 @@ export class ServiceCompiler extends CoreCompiler {
*/
private addExportedComponents(path: string, includes: ILitModule[]): void {
includes.forEach(
(Component: ILitComponent) => {
(Component: ILitComponent): void => {
this.getMethodList(Component).forEach(
(method: string) => this.addRouteFromMethod(Component, method, path)
(method: string): void => this.addRouteFromMethod(Component, method, path)
);
}
);
Expand All @@ -210,7 +212,7 @@ export class ServiceCompiler extends CoreCompiler {
* logs: Application running on port 1000
*/
private greet(port: string | number): Function {
return () => {
return (): void => {
this.console.log("Application running on port " + port);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/decorators/component.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Injector } from "./../classes/injector.class";
* @constructor
*/
export const LitComponent = () : GenericClassDecorator<Type<any>> => {
return (target: Type<any>) => {
return (target: Type<any>): void => {
Injector.set(target);
};
};
5 changes: 2 additions & 3 deletions lib/compiler/decorators/module.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import { LitModule } from '../..';
describe('Class: Injector', () => {

it('should allow passing exports to modules', () => {

@LitModule({
exports: [
'test'
]
exports: []
})
class TestModule {

Expand Down
18 changes: 11 additions & 7 deletions lib/compiler/decorators/module.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
/**
* module.decorator
*/
import { ILitComponent, ILitModule } from '../utils/compiler.utils';
import { GenericClassDecorator, Type } from "../../utils/core.util";
import { Injector } from "./../classes/injector.class";

/**
* @TODO move this into its own file!
*/
export interface LitModuleConfig {
path?: string;
imports?: any[];
exports?: any[];
path?: string;
imports?: ILitModule[];
exports?: ILitComponent[];
}

import { GenericClassDecorator, Type } from "../../utils/core.util";
import { Injector } from "./../classes/injector.class";

/**
* Classes decorated with the `@LitModule` decorator are stored within the injector and can be resolved by it.
* @returns {GenericClassDecorator<Type<any>>}
* @constructor
*/
export const LitModule = (config: LitModuleConfig = {}) : GenericClassDecorator<Type<any>> => {
return (target: Type<any>) => {
return (target: Type<any>): void => {

config.path = config.path || '';
config.imports = config.imports || [];
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/decorators/service.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Injector } from "./../classes/injector.class";
* @constructor
*/
export const LitService = () : GenericClassDecorator<Type<any>> => {
return (target: Type<any>) => {
return (target: Type<any>): void => {
Injector.set(target);
};
};
Loading

0 comments on commit 544e63c

Please sign in to comment.