From 052e672bb82b7fbde7836da18fd9ca0e60e798bb Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Mon, 1 Jul 2019 18:09:51 +0100 Subject: [PATCH] Add pino-std-serializers (#36573) * WIP updating oracledb typings to 3.1.2 * WIP * SODA WIP * .d.ts done. Needs tests writing * Finished * Added pool stats methods. Fixed bug with BindParameter being string or number * Making _enableStats optional * Allow parameters to be nulled * Capitalise namespace * Update contribution URL * Revert "Update contribution URL" This reverts commit f329d293dceaf44832ae3424753c1ccb98378df2. * Update contribution URL * Change poolAlias to string. SODA not in preview. Add BindParameters interface * getRows should not return void * Added pino-std-serializers --- types/oracledb/index.d.ts | 12 +- types/pino-std-serializers/index.d.ts | 132 ++++++++++++++++++ .../pino-std-serializers-tests.ts | 69 +++++++++ types/pino-std-serializers/tsconfig.json | 24 ++++ types/pino-std-serializers/tslint.json | 1 + 5 files changed, 232 insertions(+), 6 deletions(-) create mode 100644 types/pino-std-serializers/index.d.ts create mode 100644 types/pino-std-serializers/pino-std-serializers-tests.ts create mode 100644 types/pino-std-serializers/tsconfig.json create mode 100644 types/pino-std-serializers/tslint.json diff --git a/types/oracledb/index.d.ts b/types/oracledb/index.d.ts index 67af4a9344..ce908c4910 100644 --- a/types/oracledb/index.d.ts +++ b/types/oracledb/index.d.ts @@ -1294,8 +1294,8 @@ declare namespace OracleDB { * It should be an array or an object, depending on the structure of the binds parameter. */ bindDefs?: - | Record - | BindDefinition[]; + | Record + | BindDefinition[]; /** * When true, this optional property enables output of the number of rows affected by each input data record. * It can only be set true for INSERT, UPDATE, DELETE or MERGE statements. @@ -1715,8 +1715,8 @@ declare namespace OracleDB { * @since 3.1 */ sessionCallback?: - | string - | ((connection: Connection, requestedTag: string, callback: (error?: DBError) => void) => void); + | string + | ((connection: Connection, requestedTag: string, callback: (error?: DBError) => void) => void); /** * The number of statements to be cached in the statement cache of each connection in the pool. * This optional property overrides the oracledb.stmtCacheSize property. @@ -1752,8 +1752,8 @@ declare namespace OracleDB { * then outBinds is returned as an object. If there are no OUT or IN OUT binds, the value is undefined. */ outBinds: - | Record - | any[]; + | Record + | any[]; /** * For SELECT statements when the resultSet option is true, use the resultSet object to fetch rows. * diff --git a/types/pino-std-serializers/index.d.ts b/types/pino-std-serializers/index.d.ts new file mode 100644 index 0000000000..1f4efd0026 --- /dev/null +++ b/types/pino-std-serializers/index.d.ts @@ -0,0 +1,132 @@ +// Type definitions for pino-std-serializers 2.4 +// Project: https://github.com/pinojs/pino-std-serializers#readme +// Definitions by: Connor Fitzgerald +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.7 + +import { IncomingMessage, ServerResponse } from 'http'; + +export interface SerializedError { + /** + * The name of the object's constructor. + */ + type: string; + /** + * The supplied error message. + */ + message: string; + /** + * The stack when the error was generated. + */ + stack: string; + /** + * Non-enumerable. The original Error object. This will not be included in the logged output. + * This is available for subsequent serializers to use. + */ + raw: Error; + /** + * Any other extra properties that have been attached to the object will also be present on the serialized object. + */ + [key: string]: any; + [key: number]: any; +} + +/** + * Serializes an Error object. + */ +export function err(err: Error): SerializedError; + +export interface SerializedRequest { + /** + * Defaults to `undefined`, unless there is an `id` property already attached to the `request` object or + * to the `request.info` object. Attach a synchronous function to the `request.id` that returns an + * identifier to have the value filled. + */ + id: string | undefined; + /** + * HTTP method. + */ + method: string; + /** + * Request pathname (as per req.url in core HTTP). + */ + url: string; + /** + * Reference to the `headers` object from the request (as per req.headers in core HTTP). + */ + headers: Record; + remoteAddress: string; + remotePort: number; + /** + * Non-enumerable, i.e. will not be in the output, original request object. This is available for subsequent + * serializers to use. In cases where the `request` input already has a `raw` property this will + * replace the original `request.raw` property. + */ + raw: IncomingMessage; +} + +/** + * Serializes a Request object. + */ +export function req(req: IncomingMessage): SerializedRequest; + +/** + * Used internally by Pino for general request logging. + */ +export function mapHttpRequest(req: IncomingMessage): { + req: SerializedRequest +}; + +export interface SerializedResponse { + /** + * HTTP status code. + */ + statusCode: number; + /** + * The headers to be sent in the response. + */ + headers: Record; + /** + * Non-enumerable, i.e. will not be in the output, original response object. This is available for subsequent serializers to use. + */ + raw: ServerResponse; +} + +/** + * Serializes a Response object. + */ +export function res(res: ServerResponse): SerializedResponse; + +/** + * Used internally by Pino for general response logging. + */ +export function mapHttpResponse(res: ServerResponse): { + res: SerializedResponse +}; + +export type CustomErrorSerializer = (err: SerializedError) => Record; + +/** + * A utility method for wrapping the default error serializer. + * This allows custom serializers to work with the already serialized object. + * The customSerializer accepts one parameter — the newly serialized error object — and returns the new (or updated) error object. + */ +export function wrapErrorSerializer(customSerializer: CustomErrorSerializer): (err: Error) => Record; + +export type CustomRequestSerializer = (req: SerializedRequest) => Record; + +/** + * A utility method for wrapping the default response serializer. + * This allows custom serializers to work with the already serialized object. + * The customSerializer accepts one parameter — the newly serialized response object — and returns the new (or updated) response object. + */ +export function wrapRequestSerializer(customSerializer: CustomRequestSerializer): (req: IncomingMessage) => Record; + +export type CustomResponseSerializer = (res: SerializedResponse) => Record; + +/** + * A utility method for wrapping the default request serializer. + * This allows custom serializers to work with the already serialized object. + * The customSerializer accepts one parameter — the newly serialized request object — and returns the new (or updated) request object. + */ +export function wrapResponseSerializer(customSerializer: CustomResponseSerializer): (res: ServerResponse) => Record; diff --git a/types/pino-std-serializers/pino-std-serializers-tests.ts b/types/pino-std-serializers/pino-std-serializers-tests.ts new file mode 100644 index 0000000000..ddf6ed183a --- /dev/null +++ b/types/pino-std-serializers/pino-std-serializers-tests.ts @@ -0,0 +1,69 @@ +import express, { Request, Response } from 'express'; +import { err, req, res, SerializedError, SerializedRequest, wrapErrorSerializer, wrapRequestSerializer, wrapResponseSerializer, SerializedResponse } from 'pino-std-serializers'; + +const customErrorSerializer = (error: SerializedError) => { + return { + myOwnError: { + data: `${error.type}-${error.message}\n\n${error.stack}`, + } + }; +}; + +const customRequestSerializer = (req: SerializedRequest) => { + const { headers, id, method, raw, remoteAddress, remotePort, url } = req; + return { + myOwnRequest: { + data: `${method}-${id}-${remoteAddress}-${remotePort}-${url}`, + headers, + raw, + } + }; +}; + +const customResponseSerializer = (res: SerializedResponse) => { + const { headers, raw, statusCode } = res; + return { + myOwnResponse: { + data: statusCode, + headers, + raw, + } + }; +}; + +const testErr = () => { + const fakeError = new Error('A fake error for testing'); + + const serializedError: SerializedError = err(fakeError); + const mySerializer = wrapErrorSerializer(customErrorSerializer); + + console.log(serializedError); + console.log(mySerializer(fakeError)); +}; + +const startExpress = () => { + return new Promise((resolve, reject) => { + const app = express(); + + app.use((request: Request, response: Response) => { + const serializedRequest: SerializedRequest = req(request); + const myReqSerializer = wrapRequestSerializer(customRequestSerializer); + + console.log(serializedRequest); + console.log(myReqSerializer(request)); + + const myResSerializer = wrapResponseSerializer(customResponseSerializer); + + response.set('Fake-Header', '12345'); + + const serializedResponse = res(response); + + console.log(serializedResponse); + console.log(myResSerializer(response)); + + return response.status(200).send('Success'); + }); + + app.listen(3000, () => resolve(app)); + }); +}; diff --git a/types/pino-std-serializers/tsconfig.json b/types/pino-std-serializers/tsconfig.json new file mode 100644 index 0000000000..aece6963e8 --- /dev/null +++ b/types/pino-std-serializers/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "esModuleInterop": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "pino-std-serializers-tests.ts" + ] +} \ No newline at end of file diff --git a/types/pino-std-serializers/tslint.json b/types/pino-std-serializers/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/pino-std-serializers/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }