diff --git a/packages/loopback/lib/application.ts b/packages/loopback/lib/application.ts index 05aa32500add..098860605ac9 100644 --- a/packages/loopback/lib/application.ts +++ b/packages/loopback/lib/application.ts @@ -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'); diff --git a/packages/loopback/lib/server.ts b/packages/loopback/lib/server.ts index 0f62b1c45d8c..1a4406fd4f28 100644 --- a/packages/loopback/lib/server.ts +++ b/packages/loopback/lib/server.ts @@ -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'); diff --git a/packages/loopback/package.json b/packages/loopback/package.json index c90c2e8b3ab5..60d2e86890fc 100644 --- a/packages/loopback/package.json +++ b/packages/loopback/package.json @@ -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": { diff --git a/packages/router/LICENSE b/packages/router/LICENSE new file mode 100644 index 000000000000..f2821c5cbe2c --- /dev/null +++ b/packages/router/LICENSE @@ -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. diff --git a/packages/router/README.md b/packages/router/README.md new file mode 100644 index 000000000000..f14c603bc1f4 --- /dev/null +++ b/packages/router/README.md @@ -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 { + 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 diff --git a/packages/router/index.ts b/packages/router/index.ts new file mode 100644 index 000000000000..71ab3adb9abb --- /dev/null +++ b/packages/router/index.ts @@ -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'; diff --git a/packages/router/package.json b/packages/router/package.json new file mode 100644 index 000000000000..c9688851cc64 --- /dev/null +++ b/packages/router/package.json @@ -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" + } +} diff --git a/packages/loopback/lib/router/SwaggerRouter.ts b/packages/router/src/SwaggerRouter.ts similarity index 98% rename from packages/loopback/lib/router/SwaggerRouter.ts rename to packages/router/src/SwaggerRouter.ts index 4de78211032c..d37b2afaae12 100644 --- a/packages/loopback/lib/router/SwaggerRouter.ts +++ b/packages/router/src/SwaggerRouter.ts @@ -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 @@ -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; } diff --git a/packages/loopback/test/integration/router/SwaggerRouter.integration.ts b/packages/router/test/integration/SwaggerRouter.integration.ts similarity index 98% rename from packages/loopback/test/integration/router/SwaggerRouter.integration.ts rename to packages/router/test/integration/SwaggerRouter.integration.ts index 8abdc9db1346..e852f8d26169 100644 --- a/packages/loopback/test/integration/router/SwaggerRouter.integration.ts +++ b/packages/router/test/integration/SwaggerRouter.integration.ts @@ -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) 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';