Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
aigoncharov committed Feb 11, 2022
1 parent d4ec10b commit afd6461
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Many thanks to [@mcollina](https://github.com/mcollina) for the idea of combinin
- [Koa](#koa)
- [Fastify](#fastify)
- [Any other framework or library](#any-other-framework-or-library)
- [Set custom CLS storage](#set-custom-cls-storage)
- [In depth](#in-depth)
- [How it works](#how-it-works)
- [Does it work only for loggers?](#does-it-work-only-for-loggers)
Expand Down Expand Up @@ -142,7 +143,7 @@ app.get('/test', (req, res) => {
### Any other framework or library

```ts
import { clsProxify, clsProxifyNamespace } from 'cls-proxify'
import { clsProxify, getClsHookedStorage } from 'cls-proxify'
import AbstractWebServer from 'abstract-web-server'

const logger = {
Expand All @@ -154,17 +155,17 @@ const app = new AbstractWebServer()
// Assuming this AbstractWebServer supports some form of middlewares
app.use((request, response, next) => {
// Assuming your request and response are event emitters
clsProxifyNamespace.bindEmitter(request)
clsProxifyNamespace.bindEmitter(response)
getClsHookedStorage().namespace.bindEmitter(request)
getClsHookedStorage().namespace.bindEmitter(response)

clsProxifyNamespace.run(() => {
getClsHookedStorage().namespace.run(() => {
const headerRequestID = request.headers.Traceparent
// this value will be accesible in CLS by key 'cls-proxify'
// it will be used as a proxy for `loggerCls`
const loggerProxy = {
info: (msg: string) => `${headerRequestID}: ${msg}`,
}
setClsProxyValue(loggerProxy)
getClsHookedStorage().set(loggerProxy)

next()
})
Expand All @@ -179,6 +180,30 @@ app.get('/test', (req, res) => {
})
```

### Set custom CLS storage

```ts
import { clsProxify, setClsHookedStorage, ClsHookedStorage, ClsProxifyStorage } from 'cls-proxify'
import AbstractWebServer from 'abstract-web-server'

// You can subclass existing ClsHookedStorage
class CustomClsStorage extends ClsHookedStorage {
// Override namespace
public readonly namespace = createNamespace('myNamespace')
// Or override CLS key
protected readonly key = 'yoda'
}
setClsHookedStorage(new CustomClsStorage())

// Or you can implement your own storage from scratch.
// Just make sure it conforms to `ClsProxifyStorage` interface.
class SecretStorage<T> implements ClsProxifyStorage<T> {
set(proxy: T): void {}
get(): T | undefined {}
}
setClsHookedStorage(new SecretStorage())
```

## In depth

### How it works
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './core'
export { setClsHookedStorage, ClsHookedStorage, ClsProxifyStorage } from './cls'
export { getClsHookedStorage, setClsHookedStorage, ClsHookedStorage, ClsProxifyStorage } from './cls'

0 comments on commit afd6461

Please sign in to comment.