Skip to content

Commit

Permalink
Update typings for hapi
Browse files Browse the repository at this point in the history
  • Loading branch information
puzpuzpuz committed Sep 21, 2019
1 parent e54d2d2 commit 75c02e7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
104 changes: 52 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,58 @@ async function find (entityId) {
}
```

## How to use it - Step 2 (Koa users)

Use the middleware provided by the library before the first middleware that needs to have access to request ids. Note that some middlewares may cause CLS context to get lost. To avoid such issues, you should use any third party middleware that does not need access to request ids *before* you use this middleware.

```javascript
const Koa = require('koa')
const rTracer = require('cls-rtracer')

const app = new Koa()
// any third party middleware that does not need access to request ids goes here
// ...

app.use(rTracer.koaMiddleware())
// optionally, you can override default middleware config:
// app.use(rTracer.koaMiddleware({
// useHeader: true,
// headerName: 'X-Your-Request-Header'
// }))

// all code in middlewares, starting from here, has access to request ids
```

Obtain request id in middlewares on the incoming request:

```javascript
// an example middleware for a generic find entity endpoint
// router config is skipped for the sake of simplicity
app.use(async (ctx) => {
const entity = await entityService.find(req.params.id)
// you can obtain the request id here
const requestId = rTracer.id()
console.log(`requestId: ${requestId}`)

ctx.body = entity
})
```

You can access the same request id from code that does not have access to the Koa's `ctx` object.

```javascript
// an imaginary entity-service.js
async function find (entityId) {
// you can obtain the request id here
const requestId = rTracer.id()
// ...
}
```

### Koa v1 support

For Koa v1 use the `koaV1Middleware(options)` function.

## How to use it - Step 2 (Hapi users)

Use the plugin provided by the library before the first route that needs to have access to request ids. Note that some plugins may cause CLS context to get lost. To avoid such issues, you should use any third party plugins that does not need access to request ids *before* you register this plugin.
Expand Down Expand Up @@ -184,58 +236,6 @@ async function find (entityId) {
}
```

## How to use it - Step 2 (Koa users)

Use the middleware provided by the library before the first middleware that needs to have access to request ids. Note that some middlewares may cause CLS context to get lost. To avoid such issues, you should use any third party middleware that does not need access to request ids *before* you use this middleware.

```javascript
const Koa = require('koa')
const rTracer = require('cls-rtracer')

const app = new Koa()
// any third party middleware that does not need access to request ids goes here
// ...

app.use(rTracer.koaMiddleware())
// optionally, you can override default middleware config:
// app.use(rTracer.koaMiddleware({
// useHeader: true,
// headerName: 'X-Your-Request-Header'
// }))

// all code in middlewares, starting from here, has access to request ids
```

Obtain request id in middlewares on the incoming request:

```javascript
// an example middleware for a generic find entity endpoint
// router config is skipped for the sake of simplicity
app.use(async (ctx) => {
const entity = await entityService.find(req.params.id)
// you can obtain the request id here
const requestId = rTracer.id()
console.log(`requestId: ${requestId}`)

ctx.body = entity
})
```

You can access the same request id from code that does not have access to the Koa's `ctx` object.

```javascript
// an imaginary entity-service.js
async function find (entityId) {
// you can obtain the request id here
const requestId = rTracer.id()
// ...
}
```

### Koa v1 support

For Koa v1 use the `koaV1Middleware(options)` function.

## Integration with loggers

The main use case for this library is request id generation and logging automation. You can integrate with any logger library in a single place and get request ids in logs across your Express application.
Expand Down
8 changes: 5 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export interface IOptions {
headerName?: string
}

export interface HapiPlugin<T> {
register: (server: object, options: T) => void | Promise<void>;
export interface IHapiPlugin<T> {
name: string
once: boolean
register: (server: any, options: T) => void | Promise<void>
}

export declare const id: () => string | undefined
Expand Down Expand Up @@ -40,4 +42,4 @@ export declare const koaV1Middleware: (
options?: IOptions,
) => GeneratorFunction

export declare const hapiPlugin: HapiPlugin<IOptions>
export declare const hapiPlugin: IHapiPlugin<IOptions>

0 comments on commit 75c02e7

Please sign in to comment.