Skip to content

Commit 3450ef2

Browse files
committed
added ts types
1 parent 7fce29b commit 3450ef2

File tree

5 files changed

+109
-67
lines changed

5 files changed

+109
-67
lines changed

dist/index.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import ExtendableError from 'extendable-error';
2+
export interface ErrorConfig {
3+
message: string;
4+
time_thrown: string;
5+
data: any;
6+
options: any;
7+
}
8+
export interface ErrorInfo {
9+
message: string;
10+
name: string;
11+
time_thrown: string;
12+
data: string;
13+
path: string;
14+
locations: string;
15+
}
16+
export declare class ApolloError extends ExtendableError {
17+
name: string;
18+
message: string;
19+
time_thrown: string;
20+
data: any;
21+
path: any;
22+
locations: any;
23+
_showLocations: boolean;
24+
constructor(name: string, config: ErrorConfig);
25+
serialize(): ErrorInfo;
26+
}
27+
export declare const isInstance: (e: any) => boolean;
28+
export declare const createError: (name: string, config: ErrorConfig) => ApolloError;
29+
export declare const formatError: (error: any, returnNull?: boolean) => ErrorInfo;

dist/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,92 @@ import ExtendableError from 'extendable-error';
44
const isString = d => Object.prototype.toString.call(d) === '[object String]';
55
const isObject = d => Object.prototype.toString.call(d) === '[object Object]';
66

7-
interface ErrorConfig {
8-
message: string;
9-
time_thrown: string;
10-
data: any,
11-
options: any,
7+
export interface ErrorConfig {
8+
message: string;
9+
time_thrown: string;
10+
data: any,
11+
options: any,
1212
}
1313

14-
class ApolloError extends ExtendableError {
15-
name: string;
16-
message: string;
17-
time_thrown: string;
18-
data: any;
19-
path: any;
20-
locations: any;
21-
_showLocations: boolean=false;
22-
23-
constructor (name:string, config: ErrorConfig) {
24-
super((arguments[2] && arguments[2].message) || '');
25-
26-
const t = (arguments[2] && arguments[2].time_thrown) || (new Date()).toISOString();
27-
const m = (arguments[2] && arguments[2].message) || '';
28-
const configData = (arguments[2] && arguments[2].data) || {};
29-
const d = {...this.data, ...configData}
30-
const opts = ((arguments[2] && arguments[2].options) || {})
31-
32-
this.name = name;
33-
this.message = m;
34-
this.time_thrown = t;
35-
this.data = d;
36-
this._showLocations = !!opts.showLocations;
37-
}
38-
serialize () {
39-
const { name, message, time_thrown, data, _showLocations, path, locations } = this;
40-
41-
let error = {
42-
message,
43-
name,
44-
time_thrown,
45-
data,
46-
path,
47-
locations
48-
};
49-
if (_showLocations) {
50-
error.locations = locations;
51-
error.path = path;
52-
}
53-
return error;
54-
}
14+
export interface ErrorInfo {
15+
message: string;
16+
name: string;
17+
time_thrown: string;
18+
data: string;
19+
path: string;
20+
locations: string;
21+
}
22+
23+
export class ApolloError extends ExtendableError {
24+
name: string;
25+
message: string;
26+
time_thrown: string;
27+
data: any;
28+
path: any;
29+
locations: any;
30+
_showLocations: boolean=false;
31+
32+
constructor (name:string, config: ErrorConfig) {
33+
super((arguments[2] && arguments[2].message) || '');
34+
35+
const t = (arguments[2] && arguments[2].time_thrown) || (new Date()).toISOString();
36+
const m = (arguments[2] && arguments[2].message) || '';
37+
const configData = (arguments[2] && arguments[2].data) || {};
38+
const d = {...this.data, ...configData}
39+
const opts = ((arguments[2] && arguments[2].options) || {})
40+
41+
this.name = name;
42+
this.message = m;
43+
this.time_thrown = t;
44+
this.data = d;
45+
this._showLocations = !!opts.showLocations;
46+
}
47+
48+
serialize (): ErrorInfo {
49+
const { name, message, time_thrown, data, _showLocations, path, locations } = this;
50+
51+
let error: ErrorInfo = {
52+
message,
53+
name,
54+
time_thrown,
55+
data,
56+
path,
57+
locations
58+
};
59+
60+
if (_showLocations) {
61+
error.locations = locations;
62+
error.path = path;
63+
}
64+
65+
return error;
66+
}
5567
}
5668

5769
export const isInstance = e => e instanceof ApolloError;
5870

5971
export const createError = (name:string, config: ErrorConfig) => {
60-
assert(isObject(config), 'createError requires a config object as the second parameter');
61-
assert(isString(config.message), 'createError requires a "message" property on the config object passed as the second parameter');
62-
const e = ApolloError.bind(null, name, config);
63-
return e;
72+
assert(isObject(config), 'createError requires a config object as the second parameter');
73+
assert(isString(config.message), 'createError requires a "message" property on the config object passed as the second parameter');
74+
return new ApolloError(name, config);
6475
};
6576

66-
export const formatError = (error, returnNull = false) => {
67-
const originalError = error ? error.originalError || error : null;
77+
export const formatError = (error, returnNull = false): ErrorInfo => {
78+
const originalError = error ? error.originalError || error : null;
6879

69-
if (!originalError) return returnNull ? null : error;
80+
if (!originalError) return returnNull ? null : error;
7081

71-
const { name } = originalError;
82+
const { name } = originalError;
7283

73-
if (!name || !isInstance(originalError)) return returnNull ? null : error;
84+
if (!name || !isInstance(originalError)) return returnNull ? null : error;
7485

75-
const { time_thrown, message, data, _showLocations } = originalError;
86+
const { time_thrown, message, data, _showLocations } = originalError;
7687

77-
if (_showLocations) {
78-
const { locations, path } = error;
79-
originalError.locations = locations;
80-
originalError.path = path;
81-
}
88+
if (_showLocations) {
89+
const { locations, path } = error;
90+
originalError.locations = locations;
91+
originalError.path = path;
92+
}
8293

83-
return originalError.serialize();
94+
return originalError.serialize();
8495
};

tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
"sourceMap": true,
77
"removeComments": false,
88
"noImplicitAny": false,
9-
"allowJs": true,
10-
"outDir": "./dist"
9+
// "allowJs": true,
10+
"outDir": "./dist",
11+
// "emitDecoratorMetadata": true,
12+
"declaration": true
1113
},
1214
"include": [
1315
"./src/**/*"

0 commit comments

Comments
 (0)