Skip to content

Commit

Permalink
feat: add update, isOfType, and toJSON methods
Browse files Browse the repository at this point in the history
  • Loading branch information
imabp committed Aug 16, 2022
1 parent e8adc19 commit 0b06364
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/problem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { httpObject, ProblemInterface } from "./types";
import {
httpObject,
ProblemInterface,
ToJsonParamType,
UpdateProblemParamType,
} from "./types";

export enum COPY_MODE {
SKIP_PROPS = "skipProps",
Expand Down Expand Up @@ -45,15 +50,28 @@ export class Problem extends Error implements ProblemInterface {
}
}

toJSON(problem: Problem, includeStack = false): ProblemInterface {
const { name, message, stack, ...rest } = problem;
toJSON({ includeStack = false, stringify = false }: ToJsonParamType) {
const thisContext = this;
const { stack, ...rest } = thisContext;

const jsonObject = {
...rest,
};
if (includeStack) {
if (stringify) return JSON.stringify(thisContext);
return {
...thisContext,
stack: this.stack,
};
}

if (stringify) return JSON.stringify({ ...rest });
return { ...rest };
}

if (includeStack) jsonObject.stack = stack;
isOfType = (type: string) => this.type === type;

return jsonObject;
async update({ updates }: UpdateProblemParamType) {
const thisContext = this;
await Object.keys(updates).forEach((i) => {
thisContext[i] = updates[i];
}, thisContext);
}
}

0 comments on commit 0b06364

Please sign in to comment.