Skip to content

Commit

Permalink
Fixed #65
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 23, 2024
1 parent 48eefbe commit 987037e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.16.0

- Fixed error handling with wrong JSON format (#65)
- Replaced `request` with `axios`
- Updated dependencies

Expand Down
12 changes: 8 additions & 4 deletions src/server/helpers/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ export function toAbsolute(directory: string) {
return (file: string) => join(directory, file);
}

export function asJson<T = {}>(file: string): T {
export function asJson<T = {}>(file: string, defaultValue: T): T {
if (existsSync(file)) {
const content = readFileSync(file, 'utf8');
return JSON.parse(content);
try {
const content = readFileSync(file, 'utf8');
return JSON.parse(content);
} catch {
return defaultValue;
}
}

return undefined;
return defaultValue;
}

export function asScript(file: string) {
Expand Down
6 changes: 3 additions & 3 deletions src/server/helpers/json-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export class JsonStore<T> {

insert(item: T) {
const file = this.file;
const items = asJson<Array<T>>(file) || [];
const items = asJson<Array<T>>(file, []);
items.push(item);
toFile(file, items);
}

delete(predicate?: (item: T) => boolean) {
const file = this.file;
const items = asJson<Array<T>>(file);
const items = asJson<Array<T>>(file, undefined);

if (items && items.length) {
for (let i = items.length; i--; ) {
Expand All @@ -35,7 +35,7 @@ export class JsonStore<T> {

select(predicate?: (item: T) => boolean) {
const file = this.file;
const items = asJson<Array<T>>(file) || [];
const items = asJson<Array<T>>(file, []);
return items.filter(predicate || all);
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/injectors/har-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export default class HarInjector implements KrasInjector {
}

private load(fileName: string, position: number) {
const content = asJson(fileName);
const content = asJson(fileName, undefined);
const entries = findEntries(content);
const files = entries.map((entry) => this.transformEntry(fileName, entry));
this.unload(fileName);
Expand Down
2 changes: 1 addition & 1 deletion src/server/injectors/json-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class JsonInjector implements KrasInjector {
}

private load(fileName: string, position: number) {
const content = asJson(fileName);
const content = asJson(fileName, []);
const items = Array.isArray(content) ? content : [content];

for (const item of items) {
Expand Down

0 comments on commit 987037e

Please sign in to comment.