@emoyly/problem 4.1.7 → 4.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/LICENSE +21 -0
  2. package/dist/README.md +59 -0
  3. package/dist/SECURITY.md +12 -0
  4. package/dist/defaults/4xx.d.ts +147 -0
  5. package/dist/defaults/4xx.js +151 -0
  6. package/dist/defaults/5xx.d.ts +57 -0
  7. package/dist/defaults/5xx.js +61 -0
  8. package/dist/defaults/aws.d.ts +12 -0
  9. package/dist/defaults/aws.js +16 -0
  10. package/dist/defaults/cloudflare.d.ts +47 -0
  11. package/dist/defaults/cloudflare.js +52 -0
  12. package/dist/defaults/iis.d.ts +17 -0
  13. package/dist/defaults/iis.js +21 -0
  14. package/dist/defaults/index.d.ts +7 -0
  15. package/dist/defaults/index.js +17 -0
  16. package/dist/defaults/nginx.d.ts +32 -0
  17. package/dist/defaults/nginx.js +36 -0
  18. package/dist/defaults/others.d.ts +37 -0
  19. package/dist/defaults/others.js +40 -0
  20. package/dist/index.d.ts +4 -0
  21. package/dist/index.js +34 -0
  22. package/dist/middleware/axios.d.ts +13 -0
  23. package/dist/middleware/axios.js +36 -0
  24. package/dist/middleware/base.d.ts +24 -0
  25. package/dist/middleware/base.js +49 -0
  26. package/dist/middleware/express.d.ts +19 -0
  27. package/dist/middleware/express.js +57 -0
  28. package/dist/package.json +45 -0
  29. package/dist/parsers/axios.d.ts +3 -0
  30. package/dist/parsers/axios.js +39 -0
  31. package/dist/parsers/http.d.ts +3 -0
  32. package/dist/parsers/http.js +15 -0
  33. package/dist/parsers/jsonwebtoken.d.ts +3 -0
  34. package/dist/parsers/jsonwebtoken.js +96 -0
  35. package/dist/parsers/mikroorm.d.ts +3 -0
  36. package/dist/parsers/mikroorm.js +15 -0
  37. package/dist/parsers/tsoa.d.ts +3 -0
  38. package/dist/parsers/tsoa.js +17 -0
  39. package/dist/problem.d.ts +13 -0
  40. package/dist/problem.js +50 -0
  41. package/dist/tsconfig.tsbuildinfo +3797 -0
  42. package/dist/typings/events.d.ts +2 -0
  43. package/dist/typings/events.js +2 -0
  44. package/dist/typings/index.d.ts +5 -0
  45. package/dist/typings/index.js +17 -0
  46. package/dist/typings/middleware.d.ts +9 -0
  47. package/dist/typings/middleware.js +2 -0
  48. package/dist/typings/misc.d.ts +3 -0
  49. package/dist/typings/misc.js +2 -0
  50. package/dist/typings/parser.d.ts +9 -0
  51. package/dist/typings/parser.js +2 -0
  52. package/dist/typings/problem.d.ts +23 -0
  53. package/dist/typings/problem.js +12 -0
  54. package/dist/util/events.d.ts +16 -0
  55. package/dist/util/events.js +29 -0
  56. package/dist/util/getProblems.d.ts +5 -0
  57. package/dist/util/getProblems.js +45 -0
  58. package/dist/util/version.d.ts +3 -0
  59. package/dist/util/version.js +20 -0
  60. package/dist/yarn.lock +2076 -0
  61. package/package.json +5 -11
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.otherErrors = exports.codesNginx = exports.codesIis = exports.codesCloudflare = exports.codesAws = exports.codes5xx = exports.codes4xx = void 0;
4
+ var _4xx_1 = require("../defaults/4xx");
5
+ Object.defineProperty(exports, "codes4xx", { enumerable: true, get: function () { return _4xx_1.statusCodes; } });
6
+ var _5xx_1 = require("../defaults/5xx");
7
+ Object.defineProperty(exports, "codes5xx", { enumerable: true, get: function () { return _5xx_1.statusCodes; } });
8
+ var aws_1 = require("../defaults/aws");
9
+ Object.defineProperty(exports, "codesAws", { enumerable: true, get: function () { return aws_1.statusCodes; } });
10
+ var cloudflare_1 = require("../defaults/cloudflare");
11
+ Object.defineProperty(exports, "codesCloudflare", { enumerable: true, get: function () { return cloudflare_1.statusCodes; } });
12
+ var iis_1 = require("../defaults/iis");
13
+ Object.defineProperty(exports, "codesIis", { enumerable: true, get: function () { return iis_1.statusCodes; } });
14
+ var nginx_1 = require("../defaults/nginx");
15
+ Object.defineProperty(exports, "codesNginx", { enumerable: true, get: function () { return nginx_1.statusCodes; } });
16
+ var others_1 = require("../defaults/others");
17
+ Object.defineProperty(exports, "otherErrors", { enumerable: true, get: function () { return others_1.otherErrors; } });
@@ -0,0 +1,32 @@
1
+ export declare const statusCodes: {
2
+ 444: {
3
+ status: number;
4
+ title: string;
5
+ type: string;
6
+ };
7
+ 494: {
8
+ status: number;
9
+ title: string;
10
+ type: string;
11
+ };
12
+ 495: {
13
+ status: number;
14
+ title: string;
15
+ type: string;
16
+ };
17
+ 496: {
18
+ status: number;
19
+ title: string;
20
+ type: string;
21
+ };
22
+ 497: {
23
+ status: number;
24
+ title: string;
25
+ type: string;
26
+ };
27
+ 499: {
28
+ status: number;
29
+ title: string;
30
+ type: string;
31
+ };
32
+ };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ // Source: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.statusCodes = void 0;
5
+ exports.statusCodes = {
6
+ 444: {
7
+ status: 494,
8
+ title: 'No Response',
9
+ type: '/errors/defaults/nginx/noresponse',
10
+ },
11
+ 494: {
12
+ status: 494,
13
+ title: 'Request header too large',
14
+ type: '/errors/defaults/nginx/headertoolarge',
15
+ },
16
+ 495: {
17
+ status: 495,
18
+ title: 'SSL Certificate Error',
19
+ type: '/errors/defaults/nginx/sslcerterr'
20
+ },
21
+ 496: {
22
+ status: 496,
23
+ title: 'SSL Certificate Required',
24
+ type: '/errors/defaults/nginx/sslcertrequired'
25
+ },
26
+ 497: {
27
+ status: 497,
28
+ title: 'HTTP Request Sent to HTTPS Port',
29
+ type: '/errors/defaults/nginx/httpreqtohttpsport'
30
+ },
31
+ 499: {
32
+ status: 499,
33
+ title: 'Client Closed Request',
34
+ type: '/errors/defaults/nginx/clientclosedreq'
35
+ }
36
+ };
@@ -0,0 +1,37 @@
1
+ export declare const otherErrors: {
2
+ unknown: {
3
+ status: number;
4
+ title: string;
5
+ type: string;
6
+ };
7
+ corsError: {
8
+ status: number;
9
+ title: string;
10
+ type: string;
11
+ };
12
+ problemParseError: {
13
+ status: number;
14
+ title: string;
15
+ type: string;
16
+ };
17
+ problemJsonError: {
18
+ status: number;
19
+ title: string;
20
+ type: string;
21
+ };
22
+ notAnError: {
23
+ status: number;
24
+ title: string;
25
+ type: string;
26
+ };
27
+ networkError: {
28
+ status: number;
29
+ title: string;
30
+ type: string;
31
+ };
32
+ inputValidationError: {
33
+ status: number;
34
+ title: string;
35
+ type: string;
36
+ };
37
+ };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.otherErrors = void 0;
4
+ exports.otherErrors = {
5
+ 'unknown': {
6
+ status: 500,
7
+ title: 'Unknown error',
8
+ type: '/errors/defaults/others/unknown',
9
+ },
10
+ 'corsError': {
11
+ status: 401,
12
+ title: 'Request not allowed by CORS policy',
13
+ type: '/errors/defaults/others/corsdenied',
14
+ },
15
+ 'problemParseError': {
16
+ status: 1000,
17
+ title: 'Could not parse problem details',
18
+ type: '/errors/defaults/others/problemparseerror',
19
+ },
20
+ 'problemJsonError': {
21
+ status: 1000,
22
+ title: 'Could not parse problem JSON',
23
+ type: '/errors/defaults/others/problemjsonerror',
24
+ },
25
+ 'notAnError': {
26
+ status: 1000,
27
+ title: 'Not an error',
28
+ type: '/errors/defaults/others/notanerror',
29
+ },
30
+ 'networkError': {
31
+ status: 1000,
32
+ title: 'Network error',
33
+ type: '/errors/defaults/others/networkerror'
34
+ },
35
+ 'inputValidationError': {
36
+ status: 400,
37
+ title: 'Input failed validation',
38
+ type: '/errors/defaults/others/validationerror'
39
+ }
40
+ };
@@ -0,0 +1,4 @@
1
+ export { Problem } from './problem';
2
+ export * as defaults from './defaults';
3
+ export { default as events } from './util/events';
4
+ export * from './typings';
package/dist/index.js ADDED
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
22
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
23
+ };
24
+ var __importDefault = (this && this.__importDefault) || function (mod) {
25
+ return (mod && mod.__esModule) ? mod : { "default": mod };
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.events = exports.defaults = exports.Problem = void 0;
29
+ var problem_1 = require("./problem");
30
+ Object.defineProperty(exports, "Problem", { enumerable: true, get: function () { return problem_1.Problem; } });
31
+ exports.defaults = __importStar(require("./defaults"));
32
+ var events_1 = require("./util/events");
33
+ Object.defineProperty(exports, "events", { enumerable: true, get: function () { return __importDefault(events_1).default; } });
34
+ __exportStar(require("./typings"), exports);
@@ -0,0 +1,13 @@
1
+ import type { PartialMiddlewareOptions } from '../typings/middleware';
2
+ import { MiddlewareBase } from './base';
3
+ declare type InterceptorArray = [undefined, (error: any) => Promise<never>];
4
+ export declare class AxiosMiddleware extends MiddlewareBase {
5
+ name: string;
6
+ constructor(options?: PartialMiddlewareOptions);
7
+ interceptor: (error: any) => Promise<never>;
8
+ /**
9
+ * @example instance.interceptors.response.use(...middleware.use())
10
+ */
11
+ use: () => InterceptorArray;
12
+ }
13
+ export {};
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AxiosMiddleware = void 0;
16
+ const base_1 = require("./base");
17
+ const axios_1 = __importDefault(require("../parsers/axios"));
18
+ const events_1 = __importDefault(require("../util/events"));
19
+ class AxiosMiddleware extends base_1.MiddlewareBase {
20
+ constructor(options) {
21
+ super({
22
+ 'parsers': [axios_1.default]
23
+ }, options);
24
+ this.name = 'axios';
25
+ this.interceptor = (error) => __awaiter(this, void 0, void 0, function* () {
26
+ const problems = yield this.parse(error);
27
+ events_1.default.emit(problems);
28
+ return Promise.reject(problems);
29
+ });
30
+ /**
31
+ * @example instance.interceptors.response.use(...middleware.use())
32
+ */
33
+ this.use = () => [undefined, this.interceptor];
34
+ }
35
+ }
36
+ exports.AxiosMiddleware = AxiosMiddleware;
@@ -0,0 +1,24 @@
1
+ import { Problem } from '../problem';
2
+ import type { ProblemOpts } from '../typings';
3
+ import type { MiddlewareOptions, PartialMiddlewareOptions } from '../typings/middleware';
4
+ /**
5
+ * Middleware collects errors from somewhere, transforms them into something recognizable by a parser, and then passes them to the parsers, and then returns an array of Problems to whatever is using the middleware
6
+ */
7
+ export declare abstract class MiddlewareBase {
8
+ options: MiddlewareOptions;
9
+ abstract name: string;
10
+ /**
11
+ * When nothing is returned from the parsers, just throw in a default "fallback" Problem object, so that a Problem is still actually returned/emitted
12
+ */
13
+ enableFallback: boolean;
14
+ constructor(defaultOptions: MiddlewareOptions, options?: PartialMiddlewareOptions);
15
+ /**
16
+ * Call this function when you want to use the middleware function
17
+ */
18
+ abstract use(): any;
19
+ /**
20
+ * Parse input using parsers
21
+ */
22
+ parse: (input: any) => Promise<Problem[]>;
23
+ fallback: ProblemOpts;
24
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MiddlewareBase = void 0;
13
+ const defaults_1 = require("../defaults");
14
+ const problem_1 = require("../problem");
15
+ const getProblems_1 = require("../util/getProblems");
16
+ /**
17
+ * Middleware collects errors from somewhere, transforms them into something recognizable by a parser, and then passes them to the parsers, and then returns an array of Problems to whatever is using the middleware
18
+ */
19
+ class MiddlewareBase {
20
+ constructor(defaultOptions, options) {
21
+ /**
22
+ * When nothing is returned from the parsers, just throw in a default "fallback" Problem object, so that a Problem is still actually returned/emitted
23
+ */
24
+ this.enableFallback = true;
25
+ /**
26
+ * Parse input using parsers
27
+ */
28
+ this.parse = (input) => __awaiter(this, void 0, void 0, function* () {
29
+ const problems = [];
30
+ const prob = getProblems_1.getProblems(input);
31
+ if (prob)
32
+ return prob;
33
+ for (const parse of this.options.parsers) {
34
+ const resp = parse(input);
35
+ if (!resp.length)
36
+ continue;
37
+ problems.push(...resp);
38
+ break;
39
+ }
40
+ if (!problems.length && this.enableFallback) {
41
+ problems.push(new problem_1.Problem(Object.assign(Object.assign({}, this.fallback), { 'errorObject': input })));
42
+ }
43
+ return problems;
44
+ });
45
+ this.fallback = defaults_1.otherErrors.unknown;
46
+ this.options = Object.assign(Object.assign({}, defaultOptions), options);
47
+ }
48
+ }
49
+ exports.MiddlewareBase = MiddlewareBase;
@@ -0,0 +1,19 @@
1
+ import type { NextFunction, Request, Response } from 'express';
2
+ import type { PartialMiddlewareOptions } from '../typings/middleware';
3
+ import { MiddlewareBase } from './base';
4
+ export declare class ExpressMiddleware extends MiddlewareBase {
5
+ name: string;
6
+ middleware: (error: any, req: Request, res: Response, next: NextFunction) => void;
7
+ /**
8
+ * This function applies the middleware to your Express application. Put this at the very end of your middleware/routes.
9
+ * If you want to also handle 404 errors, check out the notFound middleware function.
10
+ * @example app.use(middleware.use())
11
+ */
12
+ use: () => (error: any, req: Request, res: Response, next: NextFunction) => void;
13
+ constructor(options?: PartialMiddlewareOptions);
14
+ /**
15
+ * Put this right before the middleware.use() to catch all 404 errors and respond with a Problem instead of the default Express response
16
+ * @example app.use(middleware.notFound)
17
+ */
18
+ notFound: (req: Request, res: Response, next: NextFunction) => void;
19
+ }
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ExpressMiddleware = void 0;
7
+ const base_1 = require("./base");
8
+ const problem_1 = require("../problem");
9
+ const defaults_1 = require("../defaults");
10
+ const events_1 = __importDefault(require("../util/events"));
11
+ class ExpressMiddleware extends base_1.MiddlewareBase {
12
+ constructor(options) {
13
+ super({
14
+ 'parsers': []
15
+ }, options);
16
+ this.name = 'express';
17
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
+ this.middleware = (error, req, res, next) => {
19
+ this.parse(error)
20
+ .then(problems => {
21
+ var _a, _b;
22
+ for (const problem of problems) {
23
+ if (problem.instance === '/unknown') {
24
+ problem.instance = req.originalUrl;
25
+ }
26
+ }
27
+ events_1.default.emit(problems);
28
+ res
29
+ .contentType('application/problem+json')
30
+ .status((_b = (_a = problems[0]) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 500)
31
+ .json(problems.map(val => val.toObject()));
32
+ })
33
+ .catch(err => {
34
+ res
35
+ .contentType('application/problem+json')
36
+ .status(500)
37
+ .json([new problem_1.Problem(Object.assign(Object.assign({}, defaults_1.otherErrors.unknown), { 'instance': req.originalUrl, 'status': 500 })).toObject()]);
38
+ throw err;
39
+ });
40
+ };
41
+ /**
42
+ * This function applies the middleware to your Express application. Put this at the very end of your middleware/routes.
43
+ * If you want to also handle 404 errors, check out the notFound middleware function.
44
+ * @example app.use(middleware.use())
45
+ */
46
+ this.use = () => this.middleware;
47
+ /**
48
+ * Put this right before the middleware.use() to catch all 404 errors and respond with a Problem instead of the default Express response
49
+ * @example app.use(middleware.notFound)
50
+ */
51
+ this.notFound = (req, res, next) => {
52
+ const error = new problem_1.Problem(Object.assign(Object.assign({}, defaults_1.codes4xx[404]), { 'instance': req.originalUrl, 'detail': `No API endpoints exist on ${req.url} for request method ${req.method}` }));
53
+ return next(error);
54
+ };
55
+ }
56
+ }
57
+ exports.ExpressMiddleware = ExpressMiddleware;
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@emoyly/problem",
3
+ "version": "4.1.6",
4
+ "description": "A simple error library based around the RFC-7807 standard with optional support for Sentry.io and Express",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "repository": "https://github.com/emoyly/problem",
8
+ "author": "Emil Petersen <emoyly@gmail.com>",
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "lint": "eslint src",
13
+ "copy": "node scripts/copy"
14
+ },
15
+ "devDependencies": {
16
+ "@mikro-orm/core": "^4.5.9",
17
+ "@types/express": "^4.17.11",
18
+ "@types/jsonwebtoken": "^8.5.1",
19
+ "@types/node": "^15.0.1",
20
+ "@typescript-eslint/eslint-plugin": "^4.22.0",
21
+ "@typescript-eslint/parser": "^4.22.0",
22
+ "eslint": "^7.25.0",
23
+ "fs-extra": "^9.1.0",
24
+ "jsonwebtoken": "^8.5.1",
25
+ "tsoa": "^3.7.0",
26
+ "typescript": "^4.2.4"
27
+ },
28
+ "peerDependencies": {
29
+ "@mikro-orm/core": "^4.5.9",
30
+ "jsonwebtoken": "^8.5.1",
31
+ "tsoa": "^3.7.0"
32
+ },
33
+ "peerDependenciesMeta": {
34
+ "@mikro-orm/core": {
35
+ "optional": true
36
+ },
37
+ "jsonwebtoken": {
38
+ "optional": true
39
+ },
40
+ "tsoa": {
41
+ "optional": true
42
+ }
43
+ },
44
+ "packageManager": "yarn@3.2.0"
45
+ }
@@ -0,0 +1,3 @@
1
+ import { Parser } from '../typings/parser';
2
+ declare const parse: Parser;
3
+ export default parse;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const others_1 = require("../defaults/others");
4
+ const problem_1 = require("../problem");
5
+ const getProblems_1 = require("../util/getProblems");
6
+ const http_1 = require("./http");
7
+ const parse = (input) => {
8
+ var _a, _b, _c, _d, _e, _f;
9
+ const request = input.request;
10
+ if ((_a = input === null || input === void 0 ? void 0 : input.response) === null || _a === void 0 ? void 0 : _a.data) {
11
+ const problems = getProblems_1.getProblems((_b = input === null || input === void 0 ? void 0 : input.response) === null || _b === void 0 ? void 0 : _b.data);
12
+ if (problems)
13
+ return problems;
14
+ }
15
+ if (input === null || input === void 0 ? void 0 : input.response) {
16
+ const url = `${(_c = input.config) === null || _c === void 0 ? void 0 : _c.baseURL}${(_d = input.config) === null || _d === void 0 ? void 0 : _d.url}`;
17
+ const problem = http_1.parseHTTPError({
18
+ 'body': input.response.data,
19
+ 'headers': input.response.headers,
20
+ 'statusCode': input.response.status,
21
+ url
22
+ });
23
+ if (problem)
24
+ return [problem];
25
+ }
26
+ if (request) {
27
+ const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
28
+ const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
29
+ if (isBrowser && request instanceof XMLHttpRequest) {
30
+ return [new problem_1.Problem(Object.assign(Object.assign({}, others_1.otherErrors.networkError), { 'instance': (_e = input === null || input === void 0 ? void 0 : input.config) === null || _e === void 0 ? void 0 : _e.url }))];
31
+ }
32
+ // Probably a ClientRequest
33
+ if (isNode && 'pipe' in request && 'destroy' in request) {
34
+ return [new problem_1.Problem(Object.assign(Object.assign({}, others_1.otherErrors.networkError), { 'instance': (_f = input === null || input === void 0 ? void 0 : input.config) === null || _f === void 0 ? void 0 : _f.url }))];
35
+ }
36
+ }
37
+ return [];
38
+ };
39
+ exports.default = parse;
@@ -0,0 +1,3 @@
1
+ import { Problem } from '../problem';
2
+ import type { HTTPErrorDetails } from '../typings/parser';
3
+ export declare function parseHTTPError(input: HTTPErrorDetails): Problem | void;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseHTTPError = void 0;
4
+ const defaults_1 = require("../defaults");
5
+ const problem_1 = require("../problem");
6
+ const codes = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, defaults_1.codes4xx), defaults_1.codes5xx), defaults_1.codesAws), defaults_1.codesCloudflare), defaults_1.codesIis), defaults_1.codesNginx);
7
+ function parseHTTPError(input) {
8
+ var _a;
9
+ if (input.statusCode) {
10
+ const found = (_a = Object.entries(codes).find(([key]) => { var _a; return key === ((_a = input.statusCode) === null || _a === void 0 ? void 0 : _a.toString()); })) === null || _a === void 0 ? void 0 : _a[1];
11
+ if (found)
12
+ return new problem_1.Problem(Object.assign({}, found));
13
+ }
14
+ }
15
+ exports.parseHTTPError = parseHTTPError;
@@ -0,0 +1,3 @@
1
+ import { Parser } from '../typings/parser';
2
+ declare const parse: Parser;
3
+ export default parse;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const problem_1 = require("../problem");
4
+ const jsonwebtoken_1 = require("jsonwebtoken");
5
+ const errorMap = [
6
+ {
7
+ 'search': 'invalid token',
8
+ 'result': {
9
+ 'type': '/errors/jsonwebtoken/invalidtoken',
10
+ 'status': 400,
11
+ 'title': 'Invalid JSON Web Token',
12
+ }
13
+ },
14
+ {
15
+ 'search': 'jwt malformed',
16
+ 'result': {
17
+ 'type': '/errors/jsonwebtoken/malformed',
18
+ 'status': 400,
19
+ 'title': 'Malformed JSON Web Token',
20
+ }
21
+ },
22
+ {
23
+ 'search': 'jwt signature is required',
24
+ 'result': {
25
+ 'type': '/errors/jsonwebtoken/signaturerequired',
26
+ 'status': 400,
27
+ 'title': 'JSON Web Token is missing, but required',
28
+ }
29
+ },
30
+ {
31
+ 'search': 'invalid signature',
32
+ 'result': {
33
+ 'type': '/errors/jsonwebtoken/invalidsignature',
34
+ 'status': 400,
35
+ 'title': 'The JSON Web Token signature is invalid',
36
+ }
37
+ },
38
+ {
39
+ 'search': /^(jwt audience invalid\. expected:)/,
40
+ 'result': {
41
+ 'type': '/errors/jsonwebtoken/invalidaud',
42
+ 'status': 400,
43
+ 'title': 'The JSON Web Token audience is invalid',
44
+ }
45
+ },
46
+ {
47
+ 'search': /^(jwt issuer invalid\. expected:)/,
48
+ 'result': {
49
+ 'type': '/errors/jsonwebtoken/invalidiss',
50
+ 'status': 400,
51
+ 'title': 'The JSON Web Token issuer is invalid',
52
+ }
53
+ },
54
+ {
55
+ 'search': /^(jwt id invalid\. expected:)/,
56
+ 'result': {
57
+ 'type': '/errors/jsonwebtoken/invalidid',
58
+ 'status': 400,
59
+ 'title': 'The JSON Web Token id is invalid',
60
+ }
61
+ },
62
+ {
63
+ 'search': /^(jwt subject invalid\. expected:)/,
64
+ 'result': {
65
+ 'type': '/errors/jsonwebtoken/invalidsubject',
66
+ 'status': 400,
67
+ 'title': 'The JSON Web Token subject is invalid',
68
+ }
69
+ }
70
+ ];
71
+ const parse = (input) => {
72
+ if (input instanceof jsonwebtoken_1.JsonWebTokenError && input.name === 'JsonWebTokenError') {
73
+ const found = errorMap.find(val => typeof val.search === 'string' ? val.search === input.message : val.search.test(input.message));
74
+ if (found) {
75
+ return [new problem_1.Problem(found.result)];
76
+ }
77
+ }
78
+ else if (input instanceof jsonwebtoken_1.TokenExpiredError && input.name === 'TokenExpiredError') {
79
+ return [new problem_1.Problem({
80
+ 'title': 'The JSON Web Token has expired',
81
+ 'type': '/errors/jsonwebtoken/tokenexpired',
82
+ 'detail': `The JSON Web Token expired at ${input.expiredAt}`,
83
+ 'status': 400,
84
+ })];
85
+ }
86
+ else if (input instanceof jsonwebtoken_1.NotBeforeError && input.name === 'NotBeforeError') {
87
+ return [new problem_1.Problem({
88
+ 'title': 'The JSON Web Token is not valid yet',
89
+ 'type': '/errors/jsonwebtoken/notbefore',
90
+ 'detail': `The JSON Web Token is not valid before ${input.date}`,
91
+ 'status': 400,
92
+ })];
93
+ }
94
+ return [];
95
+ };
96
+ exports.default = parse;
@@ -0,0 +1,3 @@
1
+ import { Parser } from '../typings/parser';
2
+ declare const parse: Parser;
3
+ export default parse;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const problem_1 = require("../problem");
4
+ const _4xx_1 = require("../defaults/4xx");
5
+ const core_1 = require("@mikro-orm/core");
6
+ // TODO: Make this less bad
7
+ const parse = (input) => {
8
+ if (!(input instanceof core_1.NotFoundError)) {
9
+ return [];
10
+ }
11
+ return [
12
+ new problem_1.Problem(_4xx_1.statusCodes['404'])
13
+ ];
14
+ };
15
+ exports.default = parse;
@@ -0,0 +1,3 @@
1
+ import { Parser } from '../typings/parser';
2
+ declare const parse: Parser;
3
+ export default parse;