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

docs: replace (req, res) with RequestContext #1325

Merged
merged 1 commit into from
May 15, 2018
Merged
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
8 changes: 4 additions & 4 deletions docs/site/Context.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ as an example, we can create custom sequences that:
Let's see this in action:

```ts
import {DefaultSequence, RestBindings} from '@loopback/rest';
import {DefaultSequence, RestBindings, RequestContext} from '@loopback/rest';

class MySequence extends DefaultSequence {
async handle(request: ParsedRequest, response: ServerResponse) {
// we provide these value for convenience (taken from the Context)
// but they are still available in the sequence/request context
async handle(context: RequestContext) {
// RequestContext provides request/response properties for convenience
// and performance, but they are still available in the context too
const req = await this.ctx.get(RestBindings.Http.REQUEST);
const res = await this.ctx.get(RestBindings.Http.RESPONSE);
this.send(res, `hello ${req.query.name}`);
Expand Down
16 changes: 8 additions & 8 deletions docs/site/Creating-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ The idiomatic solution has two parts:
```ts
class AppSequence implements SequenceHandler {
constructor(
@inject(RestBindings.Http.CONTEXT) protected ctx: Context,
@inject(RestBindings.SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
@inject(RestBindings.SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
@inject(RestBindings.SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
Expand All @@ -223,18 +222,19 @@ The idiomatic solution has two parts:
@inject('authentication.actions.authenticate') protected authenticate: AuthenticateFn
) {}

async handle(req: ParsedRequest, res: ServerResponse) {
async handle(context: RequestContext) {
try {
const route = this.findRoute(req);
const {request, response} = context;
const route = this.findRoute(request);

// Invoke the new action:
const user = await this.authenticate(req);
const user = await this.authenticate(request);

const args = await parseOperationArgs(req, route);
const args = await parseOperationArgs(request, route);
const result = await this.invoke(route, args);
this.send(res, result);
} catch (err) {
this.reject(res, req, err);
this.send(response, result);
} catch (error) {
this.reject(context, err);
}
}
}
Expand Down
28 changes: 15 additions & 13 deletions docs/site/Implementing-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,14 +639,15 @@ export class MySequence implements SequenceHandler {
@inject(RestSequenceActions.REJECT) protected reject: Reject,
) {}

async handle(req: ParsedRequest, res: ServerResponse) {
async handle(context: RequestContext) {
try {
const route = this.findRoute(req);
const args = await this.parseParams(req, route);
const {request, response} = context;
const route = this.findRoute(request);
const args = await this.parseParams(requset, 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(context, err);
}
}
}
Expand All @@ -660,23 +661,24 @@ Now it's time to customize the default sequence to print a common log line. Edit
the `handle` method as follows:

```ts
async handle(req: ParsedRequest, res: ServerResponse) {
async handle(context: RequestContext) {
try {
const route = this.findRoute(req);
const args = await this.parseParams(req, route);
const {request, response} = context;
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);
this.log([
req.socket.remoteAddress,
request.socket.remoteAddress,
'-',
'-',
`[${strftime('%d/%b/%Y:%H:%M:%S %z', new Date())}]`,
`"${req.method} ${req.path} HTTP/${req.httpVersion}"`,
res.statusCode,
`"${request.method} ${request.path} HTTP/${request.httpVersion}"`,
response.statusCode,
'-',
].join(' '));
} catch (err) {
this.reject(res, req, err);
this.reject(context, err);
}
}
```
Expand Down
15 changes: 8 additions & 7 deletions packages/authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,19 @@ export class MySequence implements SequenceHandler {
protected authenticateRequest: AuthenticateFn,
) {}

async handle(req: ParsedRequest, res: ServerResponse) {
async handle(context: RequestContext) {
try {
const route = this.findRoute(req);
const {request, response} = context;
const route = this.findRoute(request);

// This is the important line added to the default sequence implementation
await this.authenticateRequest(req);
await this.authenticateRequest(request);

const args = await this.parseParams(req, route);
const args = await this.parseParams(request, route);
const result = await this.invoke(route, args);
this.send(res, result);
} catch (err) {
this.reject(res, req, err);
this.send(response, result);
} catch (error) {
this.reject(context, error);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class LogSequence implements SequenceHandler {
@inject(ExtensionStarterBindings.LOG_ACTION) protected logger: LogFn,
) {}

async handle(req: ParsedRequest, res: ServerResponse) {
async handle(context: RequestContext) {
const {request, response} = context;

// We define these variable outside so they can be accessed by logger.
let args: any = [];
let result: any;
Expand All @@ -86,17 +88,17 @@ class LogSequence implements SequenceHandler {
const start = this.logger.startTimer();

try {
const route = this.findRoute(req);
args = await this.parseParams(req, route);
const route = this.findRoute(request);
args = await this.parseParams(request, route);
result = await this.invoke(route, args);
this.send(res, result);
} catch (err) {
result = err; // so we can log the error message in the logger
this.reject(res, req, err);
this.send(response, result);
} catch (error) {
result = error; // so we can log the error message in the logger
this.reject(context, error);
}

// We call the logger function given to us by LogProvider
this.logger(req, args, result, start);
this.logger(request, args, result, start);
}
}
```
Expand Down