Skip to content

Commit

Permalink
refactor: ts formatter
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
  • Loading branch information
grant committed Jun 6, 2020
1 parent 9857eda commit bd032d8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/lib/formats/base64.js → src/lib/formats/base64.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

0 comments on commit bd032d8

Please sign in to comment.