Skip to content

Commit

Permalink
feat(core): adds PublicError and generateErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 13, 2019
1 parent a900275 commit d2fbce2
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 44 deletions.
26 changes: 26 additions & 0 deletions packages/core/src/PublicError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ErrorCode } from '~/types';

export default class PublicError extends Error {
public id: string;
public code: ErrorCode;
public source?: Error;
public constructor(
id: string,
code: ErrorCode,
source?: Error | null,
message?: string
) {
super(message);
this.id = id;
this.code = code;
this.source = source || undefined;
}
public get name(): string {
return 'PublicError';
}
public get root(): PublicError {
return this.source instanceof PublicError && this.source !== this
? this.source.root
: this;
}
}
43 changes: 43 additions & 0 deletions packages/core/src/generate/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs from 'fs';
import { CollectionTreeApplication } from '~/types';

export async function writeErrors(
dest: string,
collection: CollectionTreeApplication
): Promise<void> {
fs.writeFileSync(dest, await generateErrors(collection));
}

export async function generateErrors(
collection: CollectionTreeApplication
): Promise<string> {
let content =
'/* eslint-disable */\n' +
'/* tslint:disable */\n' +
'/* This file was automatically generated. DO NOT MODIFY IT BY HAND. */\n\n' +
`import { PublicError } from '@karmic/core';\n\n`;

const entries = Object.entries(collection.types);

const names: string[] = [];
for (const [name, value] of entries) {
if (value.kind === 'error') {
names.push(name);
content +=
`export const ${name} = new PublicError(\n` +
` '${name}',\n` +
` '${value.code}',\n` +
` null,\n` +
` ${value.description ? `'${value.description}'` : 'undefined'}\n` +
`);\n\n`;
}
}

content += `export default {`;
for (const name of names) {
content += `\n ${name},`;
}
if (entries.length) content = content.slice(0, -1) + '\n';

return content + '};\n';
}
1 change: 1 addition & 0 deletions packages/core/src/generate/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './typings';
export * from './errors';
33 changes: 33 additions & 0 deletions packages/core/src/generate/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'fs';
import { CollectionTreeApplication } from '~/types';
import { compile } from 'json-schema-to-typescript';
import camelcase from 'camelcase';

export async function writeTypings(
dest: string,
collection: CollectionTreeApplication
): Promise<void> {
fs.writeFileSync(dest, await generateTypings(collection));
}

export async function generateTypings(
collection: CollectionTreeApplication
): Promise<string> {
let content =
'/* eslint-disable */\n' +
'/* tslint:disable */\n' +
'/* This file was automatically generated. DO NOT MODIFY IT BY HAND. */\n\n';

for (const [key, value] of Object.entries(collection.types)) {
if (value.kind !== 'error') {
content += await compile(
value.schema,
camelcase(key, { pascalCase: true }),
{ bannerComment: '' }
);
content += '\n';
}
}

return content.trim() + '\n';
}
28 changes: 0 additions & 28 deletions packages/core/src/generate/typings/compile.ts

This file was deleted.

16 changes: 0 additions & 16 deletions packages/core/src/generate/typings/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './create';
export * from './generate';
export * from './utils';
export * from './types';
export { default as PublicError } from './PublicError';

0 comments on commit d2fbce2

Please sign in to comment.