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
This commit is contained in:
Connor Fitzgerald 2019-07-01 18:09:51 +01:00 committed by Ryan Cavanaugh
parent f1dd462706
commit 052e672bb8
5 changed files with 232 additions and 6 deletions

View File

@ -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<string, BindDefinition>
| BindDefinition[];
| Record<string, BindDefinition>
| 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<string, any>
| any[];
| Record<string, any>
| any[];
/**
* For SELECT statements when the resultSet option is true, use the resultSet object to fetch rows.
*

132
types/pino-std-serializers/index.d.ts vendored Normal file
View File

@ -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 <https://github.com/connorjayfitzgerald>
// 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<string, string>;
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<string, string>;
/**
* 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<string, any>;
/**
* 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<string, any>;
export type CustomRequestSerializer = (req: SerializedRequest) => Record<string, any>;
/**
* 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<string, any>;
export type CustomResponseSerializer = (res: SerializedResponse) => Record<string, any>;
/**
* 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<string, any>;

View File

@ -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));
});
};

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }