Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: combine all parsers into a single file #269

Merged
merged 2 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/parsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import CONSTANTS from "./constants";
import { isString, isDefinedOrThrow, isStringOrObjectOrThrow, ValidationError } from "./event/validation";

export abstract class Parser {
abstract parse(payload: Record<string, unknown> | string): unknown;
}

export class JSONParser implements Parser {
decorator?: Base64Parser;
constructor(decorator?: Base64Parser) {
this.decorator = decorator;
}

/**
* Parses the payload with an optional decorator
* @param {object|string} payload the JSON payload
* @return {object} the parsed JSON payload.
*/
parse(payload: Record<string, unknown> | string): string {
if (this.decorator) {
payload = this.decorator.parse(payload);
}

isDefinedOrThrow(payload, new ValidationError("null or undefined payload"));
isStringOrObjectOrThrow(payload, new ValidationError("invalid payload type, allowed are: string or object"));
const parseJSON = (v: Record<string, unknown> | string): string => (isString(v) ? JSON.parse(v as string) : v);
return parseJSON(payload);
}
}

export class PassThroughParser extends Parser {
parse(payload: unknown): unknown {
return payload;
}
}

const jsonParser = new JSONParser();
export const parserByContentType: { [key: string]: Parser } = {
[CONSTANTS.MIME_JSON]: jsonParser,
[CONSTANTS.MIME_CE_JSON]: jsonParser,
[CONSTANTS.DEFAULT_CONTENT_TYPE]: jsonParser,
[CONSTANTS.DEFAULT_CE_CONTENT_TYPE]: jsonParser,
[CONSTANTS.MIME_OCTET_STREAM]: new PassThroughParser(),
};

export class Base64Parser implements Parser {
decorator?: Parser;

constructor(decorator?: Parser) {
this.decorator = decorator;
}

parse(payload: Record<string, unknown> | string): string {
let payloadToParse = payload;
if (this.decorator) {
payloadToParse = this.decorator.parse(payload) as string;
}

return Buffer.from(payloadToParse as string, "base64").toString();
}
}

export interface MappedParser {
name: string;
parser: Parser;
}

export class DateParser extends Parser {
parse(payload: string): Date {
return new Date(Date.parse(payload));
}
}

export const parserByEncoding: { [key: string]: { [key: string]: Parser } } = {
base64: {
[CONSTANTS.MIME_CE_JSON]: new JSONParser(new Base64Parser()),
[CONSTANTS.MIME_OCTET_STREAM]: new PassThroughParser(),
},
};
18 changes: 0 additions & 18 deletions src/parsers/base64.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/parsers/date.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/parsers/index.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/parsers/json.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/parsers/mapped.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/parsers/parser.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/parsers/pass_through.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/integration/parser_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "mocha";
import { expect } from "chai";

import { JSONParser as Parser } from "../../src/parsers/";
import { JSONParser as Parser } from "../../src/parsers";
import { ValidationError } from "../../src/";

describe("JSON Event Format Parser", () => {
Expand Down