Skip to content

Commit

Permalink
Merge pull request #2 from codyjs/master
Browse files Browse the repository at this point in the history
Integration with Inversify standards + Unit tests
  • Loading branch information
remojansen committed May 26, 2016
2 parents 2e8666a + d20975f commit f0cb09e
Show file tree
Hide file tree
Showing 23 changed files with 881 additions and 871 deletions.
31 changes: 30 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,33 @@ node_modules
# Optional REPL history
.node_repl_history

typings
build
node_modules
bower_components
docs
bundled

typings
.typingsrc
dist
lib
es

type_definitions/inversify/*.js

src/*.js
src/**/*.js

src/*.js.map
src/**/*.js.map

src/*.d.ts
src/decorator/*.d.ts
src/factory/*.d.ts
src/syntax/*.d.ts

test/*.js
test/**/*.js

type_definitions/**/*.js
type_definitions/*.js
22 changes: 22 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
src
test
typings
bundled
build
coverage
docs
wiki
gulpfile.js
bower.json
karma.conf.js
tsconfig.json
typings.json
CONTRIBUTING.md
ISSUE_TEMPLATE.md
PULL_REQUEST_TEMPLATE.md
tslint.json
wallaby.js
.travis.yml
.gitignore
.vscode
type_definitions
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
node_js:
- stable
- 5.4.1
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.1
- '0.12'
before_install:
- npm install -g typings
- typings install
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"src/**/*.js": true,
"test/**/*.js": true,
"**/es": true,
"**/lib": true,
"**/coverage": true,
"**/dist": true,
"**/docs": true,
"type_definitions/**/*.js": true
}
}
95 changes: 63 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
# inversify-express-utils
Some utilities for the development of express applications with Inversify
Some utilities for the development of express applications with Inversify.

## Decorators

* `@Controller(path, [middleware, ...])`

Registers the decorated class as a controller with a root path, and optionally registers any global middleware for this controller.

* `@Method(method, path, [middleware, ...])`
## Installation
Coming soon to npm!

Registers the decorated method as a request handler for a particular path and method, where the method is a valid method on the express.Router class.
## The Basics

* `@SHORTCUT(path, [middleware, ...])`
### Step 1: Decorate your controllers
To use a class as a "controller" for your express app, simply add the `@Controller` decorator to the class. Similarly, decorate methods of the class to serve as request handlers.
The following example will declare a controller that responds to `GET /foo'.

Shortcut decorators which are simply wrappers for `@Method`. Right now these include `@Get`, `@Post`, `@Put`, `@Patch`, `@Head`, `@Delete`, and `@All`. For anything more obscure, use `@Method` (Or make a PR :smile:).

### Example

#### FooController:
```Typescript
```ts
import * as express from 'express';
import { Controller, Get } from '../framework/decorators';
import { FooService } from '../services/foo-service';
import { Controller, Get } from 'inversify-express-utils';
import { injectable, inject } from 'inversify';

@Controller('/foo')
Expand All @@ -35,11 +26,6 @@ export class FooController {
return this.fooService.get(req.query.id);
}
}
```

#### FooService:
```Typescript
import { injectable } from 'inversify';

@injectable()
export class FooService {
Expand All @@ -55,25 +41,28 @@ export class FooService {
}
```

#### app.ts (composition root)
```Typescript
/// <reference path="../node_modules/inversify/type_definitions/inversify/inversify.d.ts" />
/// <reference path="../node_modules/reflect-metadata/reflect-metadata.d.ts" />
### Step 2: Configure kernel and server
Configure the inversify kernel in your composition root as usual.

Then, pass the kernel to the InversifyExpressServer constructor. This will allow it to register all controllers and their dependencies from your kernel and attach them to the express app.
Then just call server.build() to prepare your app.

import "reflect-metadata";
```ts
import 'reflect-metadata';
import * as express from 'express';
import { Kernel } from 'inversify';
import { Server } from './framework/server';
import { InversifyExpressServer } from 'inversify-express-utils';

import { FooController } from './controllers/foo-controller';
import { FooService } from './services/foo-service';

// set up kernel
var kernel = new Kernel();
let kernel = new Kernel();
kernel.bind<FooService>('FooService').to(FooService);
kernel.bind<FooController>('FooController').to(FooController);

// create server
var server = new Server(kernel);
let server = new InversifyExpressServer(kernel);

server
.build()
Expand All @@ -82,4 +71,46 @@ server
function callback() {
console.log('listening on http://localhost:3000');
}
```
```

## InversifyExpressServer
A wrapper for an express Application.

### `.setConfig(configFn)`
Optional - exposes the express application object for convenient loading of server-level middleware.

```ts
import * as morgan from 'morgan';
// ...
let server = new InversifyExpressServer(kernel);
server.setConfig((app) => {
var logger = morgan('combined')
app.use(logger);
});
```

### `.build()`
Attaches all registered controllers and middleware to the express application. Returns the application instance.

```ts
// ...
let server = new InversifyExpressServer(kernel);
server
.setConfig(configFn)
.build()
.listen(3000, 'localhost', callback);
```

## Decorators

### `@Controller(path, [middleware, ...])`

Registers the decorated class as a controller with a root path, and optionally registers any global middleware for this controller.

### `@Method(method, path, [middleware, ...])`

Registers the decorated controller method as a request handler for a particular path and method, where the method name is a valid express routing method.

### `@SHORTCUT(path, [middleware, ...])`

Shortcut decorators which are simply wrappers for `@Method`. Right now these include `@Get`, `@Post`, `@Put`, `@Patch`, `@Head`, `@Delete`, and `@All`. For anything more obscure, use `@Method` (Or make a PR :smile:).
Loading

0 comments on commit f0cb09e

Please sign in to comment.