Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 18, 2019
1 parent a6f29dd commit 955c617
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '10'
- '8'
- '6'
13 changes: 13 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Primitive, JsonObject} from 'type-fest';

export type ErrorObject = {
name?: string;
stack?: string;
message?: string;
code?: string;
} & JsonObject;

/**
Serialize an error into a plain object.
*/
export default function serializeError<ErrorType>(error: ErrorType): ErrorType extends Primitive ? ErrorType : ErrorObject;
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const destroyCircular = (from, seen) => {

seen.push(from);

// TODO: Use `Object.entries() when targeting Node.js 8
for (const key of Object.keys(from)) {
const value = from[key];

for (const [key, value] of Object.entries(from)) {
if (typeof value === 'function') {
continue;
}
Expand Down Expand Up @@ -37,7 +34,7 @@ const destroyCircular = (from, seen) => {
return to;
};

module.exports = value => {
const serializeError = value => {
if (typeof value === 'object') {
return destroyCircular(value, []);
}
Expand All @@ -50,3 +47,6 @@ module.exports = value => {

return value;
};

module.exports = serializeError;
module.exports.default = serializeError;
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {expectType} from 'tsd-check';
import serializeError, {ErrorObject} from '.';

const error = new Error('unicorn');

expectType<number>(serializeError(1));
expectType<ErrorObject>(serializeError(error));
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"error",
Expand All @@ -28,8 +29,12 @@
"process",
"send"
],
"dependencies": {
"type-fest": "^0.3.0"
},
"devDependencies": {
"ava": "^0.25.0",
"xo": "^0.23.0"
"ava": "^1.3.1",
"tsd-check": "^0.5.0",
"xo": "^0.24.0"
}
}

0 comments on commit 955c617

Please sign in to comment.