-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from mojaloop/feat/code-quality-plugin
feat: added logging plugin; removed ts-ignores
- Loading branch information
Showing
15 changed files
with
179 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
components: | ||
schemas: | ||
IdType: | ||
type: string | ||
enum: | ||
- MSISDN | ||
- ACCOUNT_NO | ||
- PERSONAL_ID | ||
- BUSINESS | ||
- DEVICE | ||
- ACCOUNT_ID | ||
- IBAN | ||
- ALIAS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './httpClient'; | ||
export * from './Logger'; | ||
export * from './repo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,4 @@ | |
|
||
'use strict'; | ||
|
||
export * from './implementations'; | ||
export * from './MemoryTokenMappingStorageRepo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './loggingPlugin'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Plugin, Request, ResponseToolkit } from '@hapi/hapi'; | ||
import { ILogger } from '../domain'; | ||
import { ReqAppState } from './types'; | ||
|
||
type PluginOptions = { | ||
logger: ILogger; | ||
} | ||
|
||
export const loggingPlugin: Plugin<PluginOptions> = { | ||
name: 'loggingPlugin', | ||
version: '1.0.0', | ||
once: true, | ||
register: async (server, options) => { | ||
const { logger } = options; | ||
|
||
server.ext({ | ||
type: 'onPreHandler', | ||
method: (req: Request, h: ResponseToolkit) => { | ||
const { path, method, info} = req; | ||
const { id, remoteAddress, received} = info; | ||
const context = { | ||
id, remoteAddress, path, method, received, | ||
}; | ||
Object.assign(req.app, { context }); | ||
logger.info(`[--> req] ${method.toUpperCase()} ${path}`, context); | ||
|
||
return h.continue; | ||
}, | ||
}); | ||
|
||
server.ext({ | ||
type: 'onPreResponse', | ||
method: (req: Request, h: ResponseToolkit) => { | ||
const { context } = req.app as ReqAppState; // todo: think, how to specify req.app type | ||
const { path, method, response } = req; | ||
const responseTimeSec = ((Date.now() - context.received) / 1000).toFixed(3); | ||
|
||
const statusCode = response instanceof Error | ||
? response.output.statusCode | ||
: response.statusCode; | ||
logger.info(`[<-- ${statusCode}][${responseTimeSec} s] ${method.toUpperCase()} ${path}`, context); | ||
|
||
return h.continue; | ||
}, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export type ReqAppState = { | ||
context: { | ||
id: string; | ||
remoteAddress: string; | ||
path: string; | ||
method: string; | ||
received: number; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.