Skip to content

Commit 6f4ffdc

Browse files
Merge pull request #22 from iamdanthedev/master
Typescript support
2 parents 7fce29b + d84ea3f commit 6f4ffdc

File tree

7 files changed

+111
-60
lines changed

7 files changed

+111
-60
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# intellij idea
2+
.idea
3+
4+
15
# Logs
26
logs
37
*.log

dist/index.d.ts

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

dist/index.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+43-42
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
{
2-
"name": "apollo-errors",
3-
"version": "1.5.1",
4-
"description": "Machine-readable custom errors for Apollostack's GraphQL server",
5-
"main": "dist/index.js",
6-
"scripts": {
7-
"test": "make test"
8-
},
9-
"repository": {
10-
"type": "git",
11-
"url": "git+https://github.com/thebigredgeek/apollo-errors.git"
12-
},
13-
"keywords": [
14-
"apollostack",
15-
"graphql",
16-
"apollo-server",
17-
"apollo-client",
18-
"error",
19-
"api"
20-
],
21-
"author": "Andrew E. Rhyne <[email protected]>",
22-
"license": "MIT",
23-
"bugs": {
24-
"url": "https://github.com/thebigredgeek/apollo-errors/issues"
25-
},
26-
"homepage": "https://github.com/thebigredgeek/apollo-errors#readme",
27-
"dependencies": {
28-
"assert": "^1.4.1",
29-
"extendable-error": "^0.1.5"
30-
},
31-
"devDependencies": {
32-
"babel-cli": "^6.18.0",
33-
"babel-core": "^6.17.0",
34-
"babel-eslint": "^7.0.0",
35-
"babel-preset-es2015": "^6.16.0",
36-
"babel-register": "^6.18.0",
37-
"chai": "^3.5.0",
38-
"eslint": "^3.8.1",
39-
"eslint-plugin-babel": "^3.3.0",
40-
"mocha": "^3.1.2",
41-
"rimraf": "^2.5.4",
42-
"typescript": "^2.5.2"
43-
}
2+
"name": "apollo-errors",
3+
"version": "1.6.3",
4+
"description": "Machine-readable custom errors for Apollostack's GraphQL server",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"test": "make test"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/thebigredgeek/apollo-errors.git"
12+
},
13+
"keywords": [
14+
"apollostack",
15+
"graphql",
16+
"apollo-server",
17+
"apollo-client",
18+
"error",
19+
"api"
20+
],
21+
"author": "Andrew E. Rhyne <[email protected]>",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/thebigredgeek/apollo-errors/issues"
25+
},
26+
"homepage": "https://github.com/thebigredgeek/apollo-errors#readme",
27+
"dependencies": {
28+
"assert": "^1.4.1",
29+
"extendable-error": "^0.1.5"
30+
},
31+
"devDependencies": {
32+
"babel-cli": "^6.18.0",
33+
"babel-core": "^6.17.0",
34+
"babel-eslint": "^7.0.0",
35+
"babel-preset-es2015": "^6.16.0",
36+
"babel-register": "^6.18.0",
37+
"chai": "^3.5.0",
38+
"eslint": "^3.8.1",
39+
"eslint-plugin-babel": "^3.3.0",
40+
"mocha": "^3.1.2",
41+
"rimraf": "^2.5.4",
42+
"typescript": "^2.5.2"
43+
},
44+
"typings": "dist/index.d.ts"
4445
}

src/index.ts

+27-13
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,41 @@ 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 {
7+
export interface ErrorConfig {
88
message: string;
9+
time_thrown?: string;
10+
data?: any;
11+
options?: {
12+
showPath?: boolean;
13+
showLocations?: boolean;
14+
};
15+
}
16+
17+
export interface ErrorInfo {
18+
message: string;
19+
name: string;
920
time_thrown: string;
10-
data: any,
11-
options: any,
21+
data?: {};
22+
path?: string;
23+
locations?: any;
1224
}
1325

14-
class ApolloError extends ExtendableError {
26+
export class ApolloError extends ExtendableError {
1527
name: string;
1628
message: string;
1729
time_thrown: string;
1830
data: any;
1931
path: any;
2032
locations: any;
21-
_showLocations: boolean=false;
33+
_showLocations: boolean = false;
2234

23-
constructor (name:string, config: ErrorConfig) {
35+
constructor(name: string, config: ErrorConfig) {
2436
super((arguments[2] && arguments[2].message) || '');
2537

2638
const t = (arguments[2] && arguments[2].time_thrown) || (new Date()).toISOString();
2739
const m = (arguments[2] && arguments[2].message) || '';
2840
const configData = (arguments[2] && arguments[2].data) || {};
29-
const d = {...this.data, ...configData}
41+
const d = { ...this.data, ...configData }
3042
const opts = ((arguments[2] && arguments[2].options) || {})
3143

3244
this.name = name;
@@ -35,35 +47,37 @@ class ApolloError extends ExtendableError {
3547
this.data = d;
3648
this._showLocations = !!opts.showLocations;
3749
}
38-
serialize () {
50+
51+
serialize(): ErrorInfo {
3952
const { name, message, time_thrown, data, _showLocations, path, locations } = this;
4053

41-
let error = {
54+
let error: ErrorInfo = {
4255
message,
4356
name,
4457
time_thrown,
4558
data,
4659
path,
4760
locations
4861
};
62+
4963
if (_showLocations) {
5064
error.locations = locations;
5165
error.path = path;
5266
}
67+
5368
return error;
5469
}
5570
}
5671

5772
export const isInstance = e => e instanceof ApolloError;
5873

59-
export const createError = (name:string, config: ErrorConfig) => {
74+
export const createError = (name: string, config: ErrorConfig) => {
6075
assert(isObject(config), 'createError requires a config object as the second parameter');
6176
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;
77+
return new ApolloError(name, config);
6478
};
6579

66-
export const formatError = (error, returnNull = false) => {
80+
export const formatError = (error, returnNull = false): ErrorInfo => {
6781
const originalError = error ? error.originalError || error : null;
6882

6983
if (!originalError) return returnNull ? null : error;

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"sourceMap": true,
77
"removeComments": false,
88
"noImplicitAny": false,
9-
"allowJs": true,
10-
"outDir": "./dist"
9+
"outDir": "./dist",
10+
"declaration": true
1111
},
1212
"include": [
1313
"./src/**/*"

0 commit comments

Comments
 (0)