From bd032d8aab5d75df673e4591e0c0083d941c473c Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Sat, 6 Jun 2020 12:19:17 -0700 Subject: [PATCH] refactor: ts formatter Signed-off-by: Grant Timmerman --- src/lib/formats/{base64.js => base64.ts} | 6 ++++-- src/lib/formats/json/{formatter.js => formatter.ts} | 6 +++--- src/lib/formats/json/{parser.js => parser.ts} | 9 +++++---- 3 files changed, 12 insertions(+), 9 deletions(-) rename src/lib/formats/{base64.js => base64.ts} (77%) rename src/lib/formats/json/{formatter.js => formatter.ts} (70%) rename src/lib/formats/json/{parser.js => parser.ts} (78%) diff --git a/src/lib/formats/base64.js b/src/lib/formats/base64.ts similarity index 77% rename from src/lib/formats/base64.js rename to src/lib/formats/base64.ts index cd73def6..c459cc3e 100644 --- a/src/lib/formats/base64.js +++ b/src/lib/formats/base64.ts @@ -1,9 +1,11 @@ class Base64Parser { - constructor(decorator) { + decorator: any; + + constructor(decorator: any) { this.decorator = decorator; } - parse(payload) { + parse(payload: any): any { let payloadToParse = payload; if (this.decorator) { payloadToParse = this.decorator.parse(payload); diff --git a/src/lib/formats/json/formatter.js b/src/lib/formats/json/formatter.ts similarity index 70% rename from src/lib/formats/json/formatter.js rename to src/lib/formats/json/formatter.ts index d4bc3d4f..0916c56f 100644 --- a/src/lib/formats/json/formatter.js +++ b/src/lib/formats/json/formatter.ts @@ -3,13 +3,13 @@ class JSONFormatter { * Every internal data structure is JSON by nature, so * no transformation is required */ - format(payload) { + format(payload: any) { return payload; } - toString(payload) { + toString(payload: any) { return JSON.stringify(payload); } } -module.exports = JSONFormatter; +export default JSONFormatter; diff --git a/src/lib/formats/json/parser.js b/src/lib/formats/json/parser.ts similarity index 78% rename from src/lib/formats/json/parser.js rename to src/lib/formats/json/parser.ts index ecdd3f5e..8a861c97 100644 --- a/src/lib/formats/json/parser.js +++ b/src/lib/formats/json/parser.ts @@ -2,16 +2,17 @@ const { isString, isDefinedOrThrow, isStringOrObjectOrThrow -} = require("../../bindings/http/validation/fun.js"); +} = require("../../bindings/http/validation/fun"); const ValidationError = require("../../bindings/http/validation/validation_error.js"); const invalidPayloadTypeError = new ValidationError("invalid payload type, allowed are: string or object"); const nullOrUndefinedPayload = new ValidationError("null or undefined payload"); -const asJSON = (v) => (isString(v) ? JSON.parse(v) : v); +const asJSON = (v: object|string) => (isString(v) ? JSON.parse(v as string) : v); class JSONParser { - constructor(decorator) { + decorator: any + constructor(decorator: Base64Parser) { this.decorator = decorator; } @@ -20,7 +21,7 @@ class JSONParser { * @param {object|string} payload the JSON payload * @return {object} the parsed JSON payload. */ - parse(payload) { + parse(payload: object|string) { if (this.decorator) { payload = this.decorator.parse(payload); }