Skip to content

Commit

Permalink
remove deprecated circular-json
Browse files Browse the repository at this point in the history
  • Loading branch information
jsebrech committed Feb 5, 2019
1 parent b6ac3ac commit 957e709
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ APIKEY=

Obtain the APIKEY by taking out a contract on the `chatbot-api` service on [api-store.antwerpen.be](https://api-store.antwerpen.be). The ACCESS_TOKEN value can be obtained at the bottom of the settings screen ("API token") on [chatbots.antwerpen.be](https://chatbots.antwerpen.be).

(Add `-o` or `-a` extensions where needed to the host names to access the dev or acceptation environments.)
(Add `-o` or `-a` extensions where needed to the host names to access the development or acceptance environments.)

Run the service:

Expand Down
24 changes: 24 additions & 0 deletions example/helpers/circularJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// encode to JSON while dealing with circular references
// adapted from https://stackoverflow.com/a/11616993/20980
const stringify = (o: any) => {
const cache: any[] = [];
return JSON.stringify(o, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Duplicate reference found
try {
// If this value does not reference a parent it can be deduped
return JSON.parse(JSON.stringify(value));
} catch (error) {
// discard key if value cannot be deduped
return;
}
}
// Store value in our collection
cache.push(value);
}
return value;
});
};

export { stringify };
8 changes: 4 additions & 4 deletions example/middleware/error.middle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response, NextFunction } from 'express';
// some errors have circular references, avoid errors when encoding them
import JSON = require('circular-json');
import {stringify} from '../helpers/circularJson';
export default (err: any, req: Request, res: Response, next: NextFunction) => {
res.setHeader('Content-Type', 'application/json');
if (err.name === 'ValidationError') {
Expand All @@ -14,10 +14,10 @@ export default (err: any, req: Request, res: Response, next: NextFunction) => {
}
if (err.name === 'ChatBotError') {
const status = err.status || 500;
return res.status(status).send(JSON.stringify(err));
return res.status(status).send(stringify(err));
}
if (err.status) {
return res.status(err.status).send(JSON.stringify(err));
return res.status(err.status).send(stringify(err));
}
return res.status(500).send(JSON.stringify(err));
return res.status(500).send(stringify(err));
};
52 changes: 30 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@types/joi": "^13.0.8",
"axios": "^0.18.0",
"body-parser": "^1.18.3",
"circular-json": "^0.5.9",
"cors": "^2.8.4",
"dotenv-safe": "^5.0.1",
"express": "^4.16.3",
Expand All @@ -37,7 +36,6 @@
"typescript": "^2.8.3"
},
"devDependencies": {
"@types/circular-json": "^0.4.0",
"@types/cors": "~2.8.3",
"@types/dotenv-safe": "~4.0.1",
"@types/express": "^4.11.1",
Expand Down

0 comments on commit 957e709

Please sign in to comment.