Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/loopback/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {Binding, Context, Constructor} from '@loopback/context';
import * as http from 'http';
import {SwaggerRouter} from './router/SwaggerRouter';
import {SwaggerRouter} from '@loopback/router';
import {getApiSpec} from './router/metadata';

const debug = require('debug')('loopback:Application');
Expand Down
2 changes: 1 addition & 1 deletion packages/loopback/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import http = require('http');
import bluebird = require('bluebird');
import {Context} from '@loopback/context';
import {Application} from '../lib/application';
import {SwaggerRouter} from './router/SwaggerRouter';
import {SwaggerRouter} from '@loopback/router';

const debug = require('debug')('loopback:Server');

Expand Down
3 changes: 1 addition & 2 deletions packages/loopback/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
"dependencies": {
"@loopback/context": "^4.0.0-alpha.1",
"@loopback/openapi-spec": "^4.0.0-alpha.1",
"@loopback/router": "^4.0.0-alpha.1",
"@types/bluebird": "^3.5.2",
"bluebird": "^3.5.0",
"body": "^5.1.0",
"debug": "^2.6.0",
"path-to-regexp": "^1.7.0",
"reflect-metadata": "^0.1.10"
},
"devDependencies": {
Expand Down
25 changes: 25 additions & 0 deletions packages/router/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2013,2017. All Rights Reserved.
Node module: @loopback/openapi-spec
This project is licensed under the MIT License, full text below.

--------

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
111 changes: 111 additions & 0 deletions packages/router/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# @loopback/router

HTTP(s) server router using OpenAPI Spec/Swagger to define HTTP endpoints and Controller pattern to implement them.

## Overview

TBD

## Installation

```
$ npm install --save @loopback/router
```

## Basic use

### 1. Create a router instance

```ts
import {SwaggerRouter} from '@loopback/router';

const router = new SwaggerRouter();
```

### 2. Define your REST API

Create OpenAPI Spec/Swagger document describing a part of your API
that will be implemented by a single controller
(or load it from a YAML file):

```ts
const spec = {
basePath: '/',
paths: {
'/echo': {
get: {
'x-operation-name': 'echo',
parameters: [
{
name: 'msg',
in: 'query',
type: 'string',
},
]
responses: {
'200': {
type: 'string'
}
}
}
}
}
};
```

Notice the custom extension `x-operation-name`, which provides a name of the
controller method implementing this endpoint.

### 3. Implement the controller

Controllers are regular classes where each public method implements
a single REST endpoint. The router creates a new controller instance
for each incoming request.

```ts
class HelloController {
public async echo(msg : string): Promise<string> {
return `Echo ${msg}`;
}
}
```

### 4. Register your Controller with the router

```ts
router.controller((req, res) => new Controller(), spec);
```

### 5. Start the app

`router.handler` provides a handler function that can be passed directly
to `http.createServer`:

```ts
import * as http from 'http';

http.createServer(router.handler).listen(3000);
```

The handler can be mounted as an Express middleware too:

```ts
import * as express from 'express';

const app = express();
app.use(router.handle);
app.listen();
```

## Related resources

See https://www.openapis.org/ and [version 2.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md)
of OpenAPI Specification.

## Contributions

IBM/StrongLoop is an active supporter of open source and welcomes contributions to our projects as well as those of the Node.js community in general. For more information on how to contribute please refer to the [Contribution Guide](https://loopback.io/doc/en/contrib/index.html).

## License

MIT
6 changes: 6 additions & 0 deletions packages/router/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: @loopback/router
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './src/SwaggerRouter';
31 changes: 31 additions & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@loopback/router",
"version": "4.0.0-alpha.1",
"description": "HTTP(s) server router using OpenAPI Spec/Swagger to define HTTP endpoints and Controller pattern to implement them.",
"author": "IBM",
"license": "MIT",
"keywords": [
"LoopBack",
"Router",
"Controller",
"Swagger",
"OpenAPI Spec"
],
"scripts": {
"integration": "mocha --opts ../../test/mocha.opts 'test/integration/**/*.ts'",
"test": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts' 'test/integration/**/*.ts'",
"unit": "mocha --opts ../../test/mocha.opts 'test/unit/**/*.ts'"
},
"dependencies": {
"@loopback/openapi-spec": "^4.0.0-alpha.1",
"body": "^5.1.0",
"bluebird": "^3.5.0",
"debug": "^2.6.0",
"path-to-regexp": "^1.7.0"
},
"devDependencies": {
"@loopback/openapi-spec-builder": "^4.0.0-alpha.1",
"@loopback/testlab": "^4.0.0-alpha.1",
"mocha": "^3.2.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: loopback
// Node module: @loopback/router
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

Expand Down Expand Up @@ -72,7 +72,7 @@ export class SwaggerRouter {
*/
public controller(factory: ControllerFactory, spec: OpenApiSpec): void {
assert(typeof factory === 'function', 'Controller factory must be a function.');
assert(typeof spec === 'object' && !!spec, 'API speciification must be a non-null object');
assert(typeof spec === 'object' && !!spec, 'API specification must be a non-null object');
if (!spec.paths || !Object.keys(spec.paths).length) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {SwaggerRouter} from '../../../lib/router/SwaggerRouter';
import {SwaggerRouter} from '../..';
import * as http from 'http';
import * as request from 'request-promise';
import { FullRequestResponse } from './../../support/FullRequestResponse';
import { FullRequestResponse } from '../../../loopback/test/support/FullRequestResponse'; // FIXME(bajtos)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing this (and L12 below) properly requires a bit more of work, let's leave it up for later (as part of #135).

import * as bluebird from 'bluebird';
import {expect} from 'testlab';
import {listen} from '../../support/util';
import {listen} from '../../../loopback/test/support/util'; // FIXME(bajtos)
import {OpenApiSpec, ParameterObject} from '@loopback/openapi-spec';
import {givenOpenApiSpec} from '@loopback/openapi-spec-builder';

Expand Down