Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Unify req and res under a single object #1313

Closed
wants to merge 1 commit into from
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
6 changes: 3 additions & 3 deletions examples/hello-world/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {RestApplication, RestServer} from '@loopback/rest';
import { RestApplication, RestServer } from '@loopback/rest';

export class HelloWorldApplication extends RestApplication {
constructor() {
Expand All @@ -13,8 +13,8 @@ export class HelloWorldApplication extends RestApplication {
// returns the same HTTP response: Hello World!
// Learn more about the concept of Sequence in our docs:
// http://loopback.io/doc/en/lb4/Sequence.html
this.handler((sequence, request, response) => {
sequence.send(response, 'Hello World!');
this.handler((sequence, httpCtx) => {
sequence.send(httpCtx.response, 'Hello World!');
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {HelloWorldApplication} from './application';
import { HelloWorldApplication } from './application';

export async function main() {
const app = new HelloWorldApplication();
Expand Down
20 changes: 10 additions & 10 deletions examples/log-extension/src/providers/log-action.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {inject, Provider, Constructor, Getter} from '@loopback/context';
import {CoreBindings} from '@loopback/core';
import {OperationArgs, ParsedRequest} from '@loopback/rest';
import {getLogMetadata} from '../decorators';
import {EXAMPLE_LOG_BINDINGS, LOG_LEVEL} from '../keys';
import { inject, Provider, Constructor, Getter } from '@loopback/context';
import { CoreBindings } from '@loopback/core';
import { OperationArgs, Request } from '@loopback/rest';
import { getLogMetadata } from '../decorators/log.decorator';
import { EXAMPLE_LOG_BINDINGS, LOG_LEVEL } from '../keys';
import {
LogFn,
TimerFn,
Expand All @@ -19,10 +19,10 @@ import chalk from 'chalk';

export class LogActionProvider implements Provider<LogFn> {
// LogWriteFn is an optional dependency and it falls back to `logToConsole`
@inject(EXAMPLE_LOG_BINDINGS.LOGGER, {optional: true})
@inject(EXAMPLE_LOG_BINDINGS.LOGGER, { optional: true })
writeLog: LogWriterFn = logToConsole;

@inject(EXAMPLE_LOG_BINDINGS.APP_LOG_LEVEL, {optional: true})
@inject(EXAMPLE_LOG_BINDINGS.APP_LOG_LEVEL, { optional: true })
logLevel: LOG_LEVEL = LOG_LEVEL.WARN;

constructor(
Expand All @@ -31,11 +31,11 @@ export class LogActionProvider implements Provider<LogFn> {
@inject.getter(CoreBindings.CONTROLLER_METHOD_NAME)
private readonly getMethod: Getter<string>,
@inject(EXAMPLE_LOG_BINDINGS.TIMER) public timer: TimerFn,
) {}
) { }

value(): LogFn {
const fn = <LogFn>((
req: ParsedRequest,
req: Request,
args: OperationArgs,
// tslint:disable-next-line:no-any
result: any,
Expand All @@ -52,7 +52,7 @@ export class LogActionProvider implements Provider<LogFn> {
}

private async action(
req: ParsedRequest,
req: Request,
args: OperationArgs,
// tslint:disable-next-line:no-any
result: any,
Expand Down
6 changes: 3 additions & 3 deletions examples/log-extension/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

// Types and interfaces exposed by the extension go here

import {ParsedRequest, OperationArgs} from '@loopback/rest';
import { Request, OperationArgs } from '@loopback/rest';

/**
* A function to perform REST req/res logging action
*/
export interface LogFn {
(
req: ParsedRequest,
req: Request,
args: OperationArgs,
// tslint:disable-next-line:no-any
result: any,
Expand All @@ -25,7 +25,7 @@ export interface LogFn {
/**
* Log level metadata
*/
export type LevelMetadata = {level: number};
export type LevelMetadata = { level: number };

/**
* High resolution time as [seconds, nanoseconds]. Used by process.hrtime().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import {
InvokeMethod,
Send,
Reject,
ParsedRequest,
ServerResponse,
HttpContext,
} from '@loopback/rest';
import {get, param} from '@loopback/openapi-v3';
import { get, param } from '@loopback/openapi-v3';
import {
LogMixin,
LOG_LEVEL,
Expand All @@ -31,19 +30,19 @@ import {
createClientForHandler,
expect,
} from '@loopback/testlab';
import {Context, inject} from '@loopback/context';
import { Context, inject } from '@loopback/context';
import chalk from 'chalk';

const SequenceActions = RestBindings.SequenceActions;

import {createLogSpy, restoreLogSpy} from '../log-spy';
import {logToMemory, resetLogs} from '../in-memory-logger';
import { createLogSpy, restoreLogSpy } from '../log-spy';
import { logToMemory, resetLogs } from '../in-memory-logger';

describe('log extension acceptance test', () => {
let app: LogApp;
let spy: SinonSpy;

class LogApp extends LogMixin(RestApplication) {}
class LogApp extends LogMixin(RestApplication) { }

const debugMatch: string = chalk.white(
'DEBUG: /debug :: MyController.debug() => debug called',
Expand Down Expand Up @@ -73,7 +72,7 @@ describe('log extension acceptance test', () => {

it('logs information at DEBUG or higher', async () => {
setAppLogToDebug();
const client: Client = createClientForHandler(app.requestHandler);
const client: Client = createClientForHandler(app.requestListener);

await client.get('/nolog').expect(200, 'nolog called');
expect(spy.called).to.be.False();
Expand All @@ -99,7 +98,7 @@ describe('log extension acceptance test', () => {

it('logs information at INFO or higher', async () => {
setAppLogToInfo();
const client: Client = createClientForHandler(app.requestHandler);
const client: Client = createClientForHandler(app.requestListener);

await client.get('/nolog').expect(200, 'nolog called');
expect(spy.called).to.be.False();
Expand All @@ -125,7 +124,7 @@ describe('log extension acceptance test', () => {

it('logs information at WARN or higher', async () => {
setAppLogToWarn();
const client: Client = createClientForHandler(app.requestHandler);
const client: Client = createClientForHandler(app.requestListener);

await client.get('/nolog').expect(200, 'nolog called');
expect(spy.called).to.be.False();
Expand All @@ -151,7 +150,7 @@ describe('log extension acceptance test', () => {

it('logs information at ERROR', async () => {
setAppLogToError();
const client: Client = createClientForHandler(app.requestHandler);
const client: Client = createClientForHandler(app.requestListener);

await client.get('/nolog').expect(200, 'nolog called');
expect(spy.called).to.be.False();
Expand All @@ -177,7 +176,7 @@ describe('log extension acceptance test', () => {

it('logs no information when logLevel is set to OFF', async () => {
setAppLogToOff();
const client: Client = createClientForHandler(app.requestHandler);
const client: Client = createClientForHandler(app.requestListener);

await client.get('/nolog').expect(200, 'nolog called');
expect(spy.called).to.be.False();
Expand Down Expand Up @@ -212,9 +211,9 @@ describe('log extension acceptance test', () => {
@inject(SequenceActions.SEND) protected send: Send,
@inject(SequenceActions.REJECT) protected reject: Reject,
@inject(EXAMPLE_LOG_BINDINGS.LOG_ACTION) protected logger: LogFn,
) {}
) { }

async handle(req: ParsedRequest, res: ServerResponse) {
async handle({ request: req, response: res }: HttpContext) {
// tslint:disable-next-line:no-any
let args: any = [];
// tslint:disable-next-line:no-any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {sinon} from '@loopback/testlab';
import {ParsedRequest} from '@loopback/rest';
import { sinon } from '@loopback/testlab';
import { Request } from '@loopback/rest';
import {
LogActionProvider,
LogFn,
Expand All @@ -15,13 +15,13 @@ import {
} from '../../..';
import chalk from 'chalk';

import {createLogSpy, restoreLogSpy, createConsoleStub} from '../../log-spy';
import {logToMemory} from '../../in-memory-logger';
import { createLogSpy, restoreLogSpy, createConsoleStub } from '../../log-spy';
import { logToMemory } from '../../in-memory-logger';

describe('LogActionProvider with in-memory logger', () => {
let spy: sinon.SinonSpy;
let logger: LogFn;
const req = <ParsedRequest>{url: '/test'};
const req = <Request>{ url: '/test' };

beforeEach(() => {
spy = createLogSpy();
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('LogActionProvider with in-memory logger', () => {
describe('LogActionProvider with default logger', () => {
let stub: sinon.SinonSpy;
let logger: LogFn;
const req = <ParsedRequest>{url: '/test'};
const req = <Request>{ url: '/test' };

beforeEach(() => {
stub = createConsoleStub();
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('LogActionProvider with default logger', () => {
async function getLogger(logWriter?: LogWriterFn) {
class TestClass {
@log(LOG_LEVEL.ERROR)
test() {}
test() { }
}

const provider = new LogActionProvider(
Expand Down
17 changes: 8 additions & 9 deletions examples/todo/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Context, inject} from '@loopback/context';
import { Context, inject } from '@loopback/context';
import {
FindRoute,
InvokeMethod,
ParsedRequest,
ParseParams,
Reject,
RestBindings,
Send,
SequenceHandler,
HttpContext,
} from '@loopback/rest';
import {ServerResponse} from 'http';

const SequenceActions = RestBindings.SequenceActions;

Expand All @@ -26,16 +25,16 @@ export class MySequence implements SequenceHandler {
@inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
@inject(SequenceActions.SEND) public send: Send,
@inject(SequenceActions.REJECT) public reject: Reject,
) {}
) { }

async handle(req: ParsedRequest, res: ServerResponse) {
async handle({ request, response }: HttpContext) {
try {
const route = this.findRoute(req);
const args = await this.parseParams(req, route);
const route = this.findRoute(request);
const args = await this.parseParams(request, route);
const result = await this.invoke(route, args);
this.send(res, result);
this.send(response, result);
} catch (err) {
this.reject(res, req, err);
this.reject(response, request, err);
}
}
}
14 changes: 7 additions & 7 deletions examples/todo/test/acceptance/application.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {createClientForHandler, expect, supertest} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {TodoListApplication} from '../../src/application';
import {TodoRepository} from '../../src/repositories/';
import {givenTodo} from '../helpers';
import {Todo} from '../../src/models/';
import { createClientForHandler, expect, supertest } from '@loopback/testlab';
import { RestServer } from '@loopback/rest';
import { TodoListApplication } from '../../src/application';
import { TodoRepository } from '../../src/repositories/';
import { givenTodo } from '../helpers';
import { Todo } from '../../src/models/';

describe('Application', () => {
let app: TodoListApplication;
Expand All @@ -24,7 +24,7 @@ describe('Application', () => {
before(givenARestServer);
before(givenTodoRepository);
before(() => {
client = createClientForHandler(server.requestHandler);
client = createClientForHandler(server.requestListener);
});
after(async () => {
await app.stop();
Expand Down
1 change: 1 addition & 0 deletions packages/http-server-express/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
25 changes: 25 additions & 0 deletions packages/http-server-express/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2017,2018. All Rights Reserved.
Node module: @loopback/http-server
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.
20 changes: 20 additions & 0 deletions packages/http-server-express/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# @loopback/http-server-express

Express implementation of http server

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/wiki/Contributing#guidelines)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See [all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
8 changes: 8 additions & 0 deletions packages/http-server-express/docs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"content": [
"index.ts",
"src/http-server-express.ts",
"src/index.ts"
],
"codeSectionDepth": 4
}
6 changes: 6 additions & 0 deletions packages/http-server-express/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/http-server-express
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './dist';
6 changes: 6 additions & 0 deletions packages/http-server-express/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/http-server-express
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = require('./dist');
Loading