@emoyly/problem 4.1.12 → 5.0.0

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.
package/defaults/4xx.js CHANGED
@@ -147,5 +147,5 @@ exports.statusCodes = {
147
147
  status: 451,
148
148
  title: 'Unavailable For Legal Reasons (RFC 7725)',
149
149
  type: 'errors/defaults/4xx/unavailableforlegalreasons'
150
- },
150
+ }
151
151
  };
package/defaults/5xx.js CHANGED
@@ -57,5 +57,5 @@ exports.statusCodes = {
57
57
  status: 510,
58
58
  title: 'Not Extended (RFC 2774)',
59
59
  type: 'errors/defaults/5xx/notextended'
60
- },
60
+ }
61
61
  };
package/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export { Problem } from './problem';
2
2
  export * as defaults from './defaults';
3
3
  export { default as events } from './util/events';
4
4
  export * from './typings';
5
+ export * from './util/isProblemArray';
package/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -32,3 +36,4 @@ exports.defaults = __importStar(require("./defaults"));
32
36
  var events_1 = require("./util/events");
33
37
  Object.defineProperty(exports, "events", { enumerable: true, get: function () { return __importDefault(events_1).default; } });
34
38
  __exportStar(require("./typings"), exports);
39
+ __exportStar(require("./util/isProblemArray"), exports);
@@ -1,10 +1,10 @@
1
1
  import type { PartialMiddlewareOptions } from '../typings/middleware';
2
2
  import { MiddlewareBase } from './base';
3
- declare type InterceptorArray = [undefined, (error: any) => Promise<never>];
3
+ type InterceptorArray = [undefined, (error: unknown) => Promise<never>];
4
4
  export declare class AxiosMiddleware extends MiddlewareBase {
5
5
  name: string;
6
6
  constructor(options?: PartialMiddlewareOptions);
7
- interceptor: (error: any) => Promise<never>;
7
+ interceptor: (error: unknown) => Promise<never>;
8
8
  /**
9
9
  * @example instance.interceptors.response.use(...middleware.use())
10
10
  */
@@ -15,10 +15,10 @@ export declare abstract class MiddlewareBase {
15
15
  /**
16
16
  * Call this function when you want to use the middleware function
17
17
  */
18
- abstract use(): any;
18
+ abstract use(): unknown;
19
19
  /**
20
20
  * Parse input using parsers
21
21
  */
22
- parse: (input: any) => Promise<Problem[]>;
22
+ parse: (input: unknown) => Promise<Problem[]>;
23
23
  fallback: ProblemOpts;
24
24
  }
@@ -27,18 +27,25 @@ class MiddlewareBase {
27
27
  */
28
28
  this.parse = (input) => __awaiter(this, void 0, void 0, function* () {
29
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;
30
+ const prob = (0, getProblems_1.getProblems)(input);
31
+ if (!prob) {
32
+ for (const parse of this.options.parsers) {
33
+ const resp = parse(input);
34
+ if (!resp.length)
35
+ continue;
36
+ problems.push(...resp);
37
+ break;
38
+ }
39
+ if (!problems.length && this.enableFallback) {
40
+ problems.push(new problem_1.Problem(Object.assign(Object.assign({}, this.fallback), { 'errorObject': input, 'stack': (typeof input === 'object' && input !== null && 'stack' in input && typeof input.stack === 'string') ? input.stack : undefined })));
41
+ }
39
42
  }
40
- if (!problems.length && this.enableFallback) {
41
- problems.push(new problem_1.Problem(Object.assign(Object.assign({}, this.fallback), { 'errorObject': input, 'stack': input === null || input === void 0 ? void 0 : input.stack })));
43
+ else {
44
+ problems.push(...prob);
45
+ }
46
+ for (const p of problems) {
47
+ if (!p.middleware)
48
+ p.middleware = this.name;
42
49
  }
43
50
  return problems;
44
51
  });
@@ -3,13 +3,13 @@ import type { PartialMiddlewareOptions } from '../typings/middleware';
3
3
  import { MiddlewareBase } from './base';
4
4
  export declare class ExpressMiddleware extends MiddlewareBase {
5
5
  name: string;
6
- middleware: (error: any, req: Request, res: Response, next: NextFunction) => void;
6
+ middleware: (error: unknown, req: Request, res: Response, next: NextFunction) => void;
7
7
  /**
8
8
  * This function applies the middleware to your Express application. Put this at the very end of your middleware/routes.
9
9
  * If you want to also handle 404 errors, check out the notFound middleware function.
10
10
  * @example app.use(middleware.use())
11
11
  */
12
- use: () => (error: any, req: Request, res: Response, next: NextFunction) => void;
12
+ use: () => (error: unknown, req: Request, res: Response, next: NextFunction) => void;
13
13
  constructor(options?: PartialMiddlewareOptions);
14
14
  /**
15
15
  * Put this right before the middleware.use() to catch all 404 errors and respond with a Problem instead of the default Express response
package/package.json CHANGED
@@ -1,27 +1,32 @@
1
1
  {
2
2
  "name": "@emoyly/problem",
3
- "version": "4.1.12",
3
+ "version": "5.0.0",
4
4
  "description": "A simple error library based around the RFC-7807 standard with optional support for Sentry.io and Express",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
7
7
  "repository": "https://github.com/emoyly/problem",
8
8
  "author": "Emil Petersen <emoyly@gmail.com>",
9
9
  "license": "MIT",
10
+ "scripts": {
11
+ "prepublish": "node ../scripts/ensureCorrectVersion.js"
12
+ },
10
13
  "devDependencies": {
11
- "@mikro-orm/core": "^4.5.9",
12
- "@types/express": "^4.17.11",
13
- "@types/jsonwebtoken": "^8.5.1",
14
- "@types/node": "^15.0.1",
15
- "@typescript-eslint/eslint-plugin": "^4.22.0",
16
- "@typescript-eslint/parser": "^4.22.0",
17
- "eslint": "^7.25.0",
18
- "fs-extra": "^9.1.0",
19
- "jsonwebtoken": "^8.5.1",
20
- "tsoa": "^3.7.0",
21
- "typescript": "^4.2.4"
14
+ "@mikro-orm/core": "^5.6.11",
15
+ "@types/express": "^4.17.17",
16
+ "@types/jsonwebtoken": "^9.0.1",
17
+ "@types/node": "^18.14.0",
18
+ "@typescript-eslint/eslint-plugin": "^5.53.0",
19
+ "@typescript-eslint/parser": "^5.53.0",
20
+ "axios": "^1.3.3",
21
+ "eslint": "^8.34.0",
22
+ "fs-extra": "^11.1.0",
23
+ "jsonwebtoken": "^9.0.0",
24
+ "tsoa": "^5.1.1",
25
+ "typescript": "^4.9.5"
22
26
  },
23
27
  "peerDependencies": {
24
28
  "@mikro-orm/core": "^4.5.9",
29
+ "axios": "*",
25
30
  "jsonwebtoken": "^8.5.1",
26
31
  "tsoa": "^3.7.0"
27
32
  },
@@ -29,6 +34,9 @@
29
34
  "@mikro-orm/core": {
30
35
  "optional": true
31
36
  },
37
+ "axios": {
38
+ "optional": true
39
+ },
32
40
  "jsonwebtoken": {
33
41
  "optional": true
34
42
  },
@@ -1,3 +1,3 @@
1
- import { Parser } from '../typings/parser';
1
+ import type { Parser } from '../typings/parser';
2
2
  declare const parse: Parser;
3
3
  export default parse;
package/parsers/axios.js CHANGED
@@ -1,37 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const axios_1 = require("axios");
3
4
  const others_1 = require("../defaults/others");
4
5
  const problem_1 = require("../problem");
5
6
  const getProblems_1 = require("../util/getProblems");
6
- const http_1 = require("./http");
7
+ const misc_1 = require("../util/misc");
7
8
  const parse = (input) => {
8
- var _a, _b, _c, _d, _e, _f;
9
+ var _a, _b, _c, _d, _e;
10
+ if (typeof input !== 'object' || input == null)
11
+ return [];
12
+ if (!(0, axios_1.isAxiosError)(input))
13
+ return [];
9
14
  const request = input.request;
10
15
  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);
16
+ const problems = (0, getProblems_1.getProblems)((_b = input === null || input === void 0 ? void 0 : input.response) === null || _b === void 0 ? void 0 : _b.data);
12
17
  if (problems)
13
18
  return problems;
14
19
  }
15
20
  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];
21
+ const opts = (0, misc_1.getHttpError)(input.response.status);
22
+ if (opts)
23
+ return [new problem_1.Problem(Object.assign(Object.assign({}, opts), { 'instance': (_c = input === null || input === void 0 ? void 0 : input.config) === null || _c === void 0 ? void 0 : _c.url, 'stack': input === null || input === void 0 ? void 0 : input.stack, 'errorObject': input }))];
25
24
  }
25
+ // TODO: Fix this, this seems bad
26
26
  if (request) {
27
27
  const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
28
28
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
29
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 }))];
30
+ return [new problem_1.Problem(Object.assign(Object.assign({}, others_1.otherErrors.networkError), { 'instance': (_d = input === null || input === void 0 ? void 0 : input.config) === null || _d === void 0 ? void 0 : _d.url }))];
31
31
  }
32
32
  // Probably a ClientRequest
33
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 }))];
34
+ 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 }))];
35
35
  }
36
36
  }
37
37
  return [];
@@ -1,3 +1,3 @@
1
- import { Parser } from '../typings/parser';
1
+ import type { Parser } from '../typings/parser';
2
2
  declare const parse: Parser;
3
3
  export default parse;
@@ -1,3 +1,3 @@
1
- import { Parser } from '../typings/parser';
1
+ import type { Parser } from '../typings/parser';
2
2
  declare const parse: Parser;
3
3
  export default parse;
package/parsers/tsoa.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { Parser } from '../typings/parser';
1
+ import type { Parser } from '../typings/parser';
2
2
  declare const parse: Parser;
3
3
  export default parse;
package/problem.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { IProblem, ProblemOpts, JsonProblem } from './typings/problem';
1
+ import type { IProblem, ProblemOpts, JsonProblem } from './typings/problem';
2
2
  export declare class Problem extends Error implements IProblem {
3
3
  type: string;
4
4
  title: string;
@@ -7,7 +7,8 @@ export declare class Problem extends Error implements IProblem {
7
7
  instance: string;
8
8
  errorObject?: unknown;
9
9
  __problemVersion: string;
10
- data: any;
11
- constructor({ type, title, status, detail, instance, stack, errorObject, data }: ProblemOpts);
10
+ data: unknown;
11
+ middleware?: string;
12
+ constructor({ type, title, status, detail, instance, stack, errorObject, data, middleware }: ProblemOpts);
12
13
  toObject: () => JsonProblem;
13
14
  }
package/problem.js CHANGED
@@ -1,16 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Problem = void 0;
4
+ const defaults_1 = require("./util/defaults");
4
5
  const version_1 = require("./util/version");
5
6
  class Problem extends Error {
6
- constructor({ type, title, status, detail, instance, stack, errorObject, data }) {
7
+ constructor({ type, title, status, detail, instance, stack, errorObject, data, middleware }) {
7
8
  var _a;
8
9
  super((_a = detail !== null && detail !== void 0 ? detail : title) !== null && _a !== void 0 ? _a : 'HTTP Problem Details');
9
- this.type = '/errors/default/unknown';
10
- this.title = 'Unknown error';
10
+ this.type = defaults_1.defaultType;
11
+ this.title = defaults_1.defaultTitle;
11
12
  this.status = 500;
12
- this.detail = 'No extended details are available';
13
- this.instance = '/unknown';
13
+ this.detail = defaults_1.defaultDetail;
14
+ this.instance = defaults_1.defaultInstance;
14
15
  this.__problemVersion = version_1.version;
15
16
  this.data = undefined;
16
17
  this.toObject = () => {
@@ -35,6 +36,8 @@ class Problem extends Error {
35
36
  this.instance = instance;
36
37
  if (errorObject)
37
38
  this.errorObject = errorObject;
39
+ if (middleware)
40
+ this.middleware = middleware;
38
41
  if (stack) {
39
42
  this.stack = stack;
40
43
  }