@emoyly/problem 7.0.4 → 7.0.5

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 (53) hide show
  1. package/README.md +4 -1
  2. package/SECURITY.md +2 -2
  3. package/cjs/middleware/base.js +10 -20
  4. package/cjs/parsers/jsonwebtoken.js +6 -2
  5. package/cjs/parsers/mikroorm.js +1 -1
  6. package/cjs/parsers/tsoa.js +2 -1
  7. package/cjs/parsers/zod.d.ts +3 -0
  8. package/cjs/parsers/zod.js +19 -0
  9. package/cjs/tsconfig.tsbuildinfo +1 -1
  10. package/cjs/typings/parser.d.ts +1 -1
  11. package/cjs/util/getProblems.d.ts +7 -1
  12. package/cjs/util/getProblems.js +19 -6
  13. package/cjs/util/index.d.ts +1 -1
  14. package/cjs/util/index.js +1 -1
  15. package/cjs/util/problemArray.d.ts +7 -0
  16. package/cjs/util/problemArray.js +21 -0
  17. package/cjs/util/version.d.ts +1 -1
  18. package/cjs/util/version.js +1 -1
  19. package/esm/middleware/base.js +10 -20
  20. package/esm/parsers/jsonwebtoken.js +6 -2
  21. package/esm/parsers/mikroorm.js +1 -1
  22. package/esm/parsers/tsoa.js +2 -1
  23. package/esm/parsers/zod.d.ts +3 -0
  24. package/esm/parsers/zod.js +17 -0
  25. package/esm/tsconfig.tsbuildinfo +1 -1
  26. package/esm/typings/parser.d.ts +1 -1
  27. package/esm/util/getProblems.d.ts +7 -1
  28. package/esm/util/getProblems.js +19 -6
  29. package/esm/util/index.d.ts +1 -1
  30. package/esm/util/index.js +1 -1
  31. package/esm/util/problemArray.d.ts +7 -0
  32. package/esm/util/problemArray.js +18 -0
  33. package/esm/util/version.d.ts +1 -1
  34. package/esm/util/version.js +1 -1
  35. package/package.json +28 -17
  36. package/scripts/ensureCorrectVersion.js +1 -1
  37. package/src/middleware/base.ts +9 -21
  38. package/src/parsers/axios.ts +0 -1
  39. package/src/parsers/jsonwebtoken.ts +6 -3
  40. package/src/parsers/mikroorm.ts +1 -1
  41. package/src/parsers/tsoa.ts +2 -1
  42. package/src/parsers/zod.ts +21 -0
  43. package/src/typings/parser.ts +1 -1
  44. package/src/util/getProblems.ts +21 -8
  45. package/src/util/index.ts +1 -1
  46. package/src/util/misc.ts +1 -0
  47. package/src/util/problemArray.ts +21 -0
  48. package/src/util/version.ts +1 -1
  49. package/cjs/util/isProblemArray.d.ts +0 -2
  50. package/cjs/util/isProblemArray.js +0 -9
  51. package/esm/util/isProblemArray.d.ts +0 -2
  52. package/esm/util/isProblemArray.js +0 -6
  53. package/src/util/isProblemArray.ts +0 -6
@@ -4,7 +4,7 @@ exports.isJsonProblem = isJsonProblem;
4
4
  exports.createFromJson = createFromJson;
5
5
  exports.getProblems = getProblems;
6
6
  const problem_js_1 = require("../problem.js");
7
- const isProblemArray_js_1 = require("./isProblemArray.js");
7
+ const problemArray_js_1 = require("./problemArray.js");
8
8
  const version_js_1 = require("./version.js");
9
9
  const strings = ['title', 'type', 'instance', 'detail', '__problemVersion'];
10
10
  function isJsonProblem(input) {
@@ -23,23 +23,36 @@ function isJsonProblem(input) {
23
23
  function createFromJson({ title, type, instance, detail, status, data }) {
24
24
  return new problem_js_1.Problem({ title, type, instance, detail, status, data });
25
25
  }
26
- function getProblems(input) {
26
+ /**
27
+ * Get an array of Problems from a given input.
28
+ * Handles existing Problems, arrays of Problems, JSON Problems, JSON Problem arrays and optionally attempts to parse any input with the given parsers.
29
+ * Only returns an array of Problems if it can successfully parse the input.
30
+ */
31
+ function getProblems(input, parsers) {
27
32
  if (typeof input === 'string') {
28
33
  try {
29
34
  const p = JSON.parse(input);
30
35
  input = p;
36
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
31
37
  }
32
38
  catch (err) { /**/ }
33
39
  }
34
40
  if (input instanceof problem_js_1.Problem)
35
41
  return [input];
36
- if ((0, isProblemArray_js_1.isProblemArray)(input))
42
+ if ((0, problemArray_js_1.isProblemArray)(input, false))
37
43
  return input;
38
- if (Array.isArray(input) && input.every(val => isJsonProblem(val))) {
39
- const _input = input;
40
- return _input.map(val => createFromJson(val));
44
+ if (Array.isArray(input) && input.length > 0 && input.every(val => isJsonProblem(val))) {
45
+ return input.map(val => createFromJson(val));
41
46
  }
42
47
  if (isJsonProblem(input)) {
43
48
  return [createFromJson(input)];
44
49
  }
50
+ if (parsers) {
51
+ for (const parser of parsers) {
52
+ const problems = parser(input);
53
+ if (!problems || !problems.length)
54
+ continue;
55
+ return problems;
56
+ }
57
+ }
45
58
  }
@@ -1,4 +1,4 @@
1
1
  export * from './getProblems.js';
2
2
  export * from './defaults.js';
3
- export * from './isProblemArray.js';
3
+ export * from './problemArray.js';
4
4
  export * from './misc.js';
package/cjs/util/index.js CHANGED
@@ -16,5 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./getProblems.js"), exports);
18
18
  __exportStar(require("./defaults.js"), exports);
19
- __exportStar(require("./isProblemArray.js"), exports);
19
+ __exportStar(require("./problemArray.js"), exports);
20
20
  __exportStar(require("./misc.js"), exports);
@@ -0,0 +1,7 @@
1
+ import { Problem } from '../problem.js';
2
+ /**
3
+ * Checks whether a given object is an array of Problem objects
4
+ * @param input Object to test
5
+ * @param allowEmpty If true or undefined, an empty array is considered a Problem array. If false, an empty array is not considered a Problem array.
6
+ */
7
+ export declare function isProblemArray(input: unknown, allowEmpty?: boolean): input is Problem[];
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isProblemArray = isProblemArray;
4
+ const problem_js_1 = require("../problem.js");
5
+ /**
6
+ * Checks whether a given object is an array of Problem objects
7
+ * @param input Object to test
8
+ * @param allowEmpty If true or undefined, an empty array is considered a Problem array. If false, an empty array is not considered a Problem array.
9
+ */
10
+ function isProblemArray(input, allowEmpty) {
11
+ if (!Array.isArray(input))
12
+ return false;
13
+ if (input.length < 1) {
14
+ if (typeof allowEmpty !== 'boolean') {
15
+ console.warn(new Error('@emoyly/problem -> isProblemArray: input is an empty array, and therefore is considered a Problem array. If this is not the intended behavior, consider setting the "onlyWhenContent" argument to true.'));
16
+ return true;
17
+ }
18
+ return allowEmpty;
19
+ }
20
+ return input.every(val => val instanceof problem_js_1.Problem);
21
+ }
@@ -1,3 +1,3 @@
1
- export declare const version = "7.0.4";
1
+ export declare const version = "7.0.5";
2
2
  export declare const major: number;
3
3
  export declare function isCompatibleVersion(inputVersion: string): boolean;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.major = exports.version = void 0;
4
4
  exports.isCompatibleVersion = isCompatibleVersion;
5
- exports.version = '7.0.4';
5
+ exports.version = '7.0.5';
6
6
  exports.major = Number(exports.version.split('.')[0]);
7
7
  function isCompatibleVersion(inputVersion) {
8
8
  // TODO: Remove this
@@ -17,26 +17,16 @@ export class MiddlewareBase {
17
17
  * Parse input using parsers
18
18
  */
19
19
  parse = async (input) => {
20
- const problems = [];
21
- const prob = getProblems(input);
22
- if (!prob) {
23
- for (const parse of this.options.parsers) {
24
- const resp = parse(input);
25
- if (!resp.length)
26
- continue;
27
- problems.push(...resp);
28
- break;
29
- }
30
- if (!problems.length && this.enableFallback) {
31
- problems.push(new Problem({
32
- ...this.fallback,
33
- 'errorObject': input,
34
- 'stack': (typeof input === 'object' && input !== null && 'stack' in input && typeof input.stack === 'string') ? input.stack : undefined
35
- }));
36
- }
37
- }
38
- else {
39
- problems.push(...prob);
20
+ const problems = getProblems(input, this.options.parsers);
21
+ if (!problems || !Array.isArray(problems) || problems.length < 1) {
22
+ if (this.enableFallback)
23
+ return [new Problem({
24
+ ...this.fallback,
25
+ 'errorObject': input,
26
+ 'stack': (typeof input === 'object' && input !== null && 'stack' in input && typeof input.stack === 'string') ? input.stack : undefined,
27
+ 'middleware': this.name
28
+ })];
29
+ return [];
40
30
  }
41
31
  for (const p of problems) {
42
32
  if (!p.middleware)
@@ -70,7 +70,10 @@ const parse = (input) => {
70
70
  if (input instanceof JsonWebTokenError && input.name === 'JsonWebTokenError') {
71
71
  const found = errorMap.find(val => typeof val.search === 'string' ? val.search === input.message : val.search.test(input.message));
72
72
  if (found) {
73
- return [new Problem(found.result)];
73
+ return [new Problem({
74
+ ...found.result,
75
+ 'errorObject': input,
76
+ })];
74
77
  }
75
78
  }
76
79
  else if (input instanceof TokenExpiredError && input.name === 'TokenExpiredError') {
@@ -79,6 +82,7 @@ const parse = (input) => {
79
82
  'type': '/errors/jsonwebtoken/tokenexpired',
80
83
  'detail': `The JSON Web Token expired at ${input.expiredAt}`,
81
84
  'status': 400,
85
+ 'errorObject': input,
82
86
  })];
83
87
  }
84
88
  else if (input instanceof NotBeforeError && input.name === 'NotBeforeError') {
@@ -87,8 +91,8 @@ const parse = (input) => {
87
91
  'type': '/errors/jsonwebtoken/notbefore',
88
92
  'detail': `The JSON Web Token is not valid before ${input.date}`,
89
93
  'status': 400,
94
+ 'errorObject': input,
90
95
  })];
91
96
  }
92
- return [];
93
97
  };
94
98
  export default parse;
@@ -4,7 +4,7 @@ import { NotFoundError } from '@mikro-orm/core';
4
4
  // TODO: Make this less bad
5
5
  const parse = (input) => {
6
6
  if (!(input instanceof NotFoundError)) {
7
- return [];
7
+ return;
8
8
  }
9
9
  return [
10
10
  new Problem({
@@ -3,7 +3,7 @@ import { Problem } from '../problem.js';
3
3
  import { otherErrors } from '../defaults/others.js';
4
4
  const parse = (input) => {
5
5
  if (!(input instanceof ValidateError)) {
6
- return [];
6
+ return;
7
7
  }
8
8
  // TODO: Give actual useful responses instead of this shit
9
9
  return [
@@ -15,6 +15,7 @@ const parse = (input) => {
15
15
  'fields': input.fields,
16
16
  },
17
17
  'stack': input.stack,
18
+ 'errorObject': input
18
19
  })
19
20
  ];
20
21
  };
@@ -0,0 +1,3 @@
1
+ import type { Parser } from '../typings/parser.js';
2
+ declare const parse: Parser;
3
+ export default parse;
@@ -0,0 +1,17 @@
1
+ import { ZodError } from 'zod';
2
+ import { Problem } from '../problem.js';
3
+ import { otherErrors } from '../defaults/others.js';
4
+ const parse = (input) => {
5
+ if (!(input instanceof ZodError)) {
6
+ return;
7
+ }
8
+ return [new Problem({
9
+ ...otherErrors.inputValidationError,
10
+ 'detail': input.message,
11
+ 'status': 400,
12
+ 'stack': input.stack,
13
+ 'errorObject': input,
14
+ 'data': input.issues
15
+ })];
16
+ };
17
+ export default parse;