@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.
@@ -0,0 +1,5 @@
1
+ import type { ProblemOpts } from './problem';
2
+ export interface Codes {
3
+ [key: number]: ProblemOpts | undefined;
4
+ [key: string]: ProblemOpts | undefined;
5
+ }
File without changes
@@ -1,2 +1,2 @@
1
1
  import type { Problem } from '../problem';
2
- export declare type ProblemListener = (problems: Problem[]) => void;
2
+ export type ProblemListener = (problems: Problem[]) => void;
@@ -1,5 +1,5 @@
1
1
  export * from './middleware';
2
- export * from './misc';
3
2
  export * from './parser';
4
3
  export * from './problem';
5
4
  export * from './events';
5
+ export * from './codes';
package/typings/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];
@@ -11,7 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
15
  };
12
16
  Object.defineProperty(exports, "__esModule", { value: true });
13
17
  __exportStar(require("./middleware"), exports);
14
- __exportStar(require("./misc"), exports);
15
18
  __exportStar(require("./parser"), exports);
16
19
  __exportStar(require("./problem"), exports);
17
20
  __exportStar(require("./events"), exports);
21
+ __exportStar(require("./codes"), exports);
@@ -6,4 +6,4 @@ export interface MiddlewareOptions {
6
6
  */
7
7
  parsers: Parser[];
8
8
  }
9
- export declare type PartialMiddlewareOptions = Partial<MiddlewareOptions>;
9
+ export type PartialMiddlewareOptions = Partial<MiddlewareOptions>;
@@ -1,9 +1,2 @@
1
1
  import type { Problem } from '../problem';
2
- import type { ResponseHeaders } from './misc';
3
- export declare type Parser = (input: any) => Problem[];
4
- export interface HTTPErrorDetails {
5
- statusCode?: number;
6
- url: string;
7
- body?: string;
8
- headers?: ResponseHeaders;
9
- }
2
+ export type Parser = (input: unknown) => Problem[];
@@ -17,7 +17,8 @@ export interface IProblem {
17
17
  stack?: string;
18
18
  errorObject?: unknown;
19
19
  __problemVersion: string;
20
- data?: any;
20
+ data?: unknown;
21
+ middleware?: string;
21
22
  }
22
- export declare type JsonProblem = Omit<IProblem, 'stack' | 'errorObject'>;
23
- export declare type ProblemOpts = Omit<IProblem, '__problemVersion'>;
23
+ export type JsonProblem = Omit<IProblem, 'stack' | 'errorObject' | 'middleware'>;
24
+ export type ProblemOpts = Omit<IProblem, '__problemVersion'>;
@@ -0,0 +1,4 @@
1
+ export declare const defaultType = "/errors/default/unknown";
2
+ export declare const defaultTitle = "Unknown error";
3
+ export declare const defaultDetail = "No extended details are available";
4
+ export declare const defaultInstance = "/unknown";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultInstance = exports.defaultDetail = exports.defaultTitle = exports.defaultType = void 0;
4
+ exports.defaultType = '/errors/default/unknown';
5
+ exports.defaultTitle = 'Unknown error';
6
+ exports.defaultDetail = 'No extended details are available';
7
+ exports.defaultInstance = '/unknown';
@@ -1,5 +1,5 @@
1
1
  import { Problem } from '../problem';
2
2
  import type { JsonProblem } from '../typings/problem';
3
- export declare function isJsonProblem(input: any): input is JsonProblem;
3
+ export declare function isJsonProblem(input: unknown): input is JsonProblem;
4
4
  export declare function createFromJson({ title, type, instance, detail, status, data }: JsonProblem): Problem;
5
- export declare function getProblems(input: any): Problem[] | void;
5
+ export declare function getProblems(input: unknown): Problem[] | void;
@@ -2,16 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getProblems = exports.createFromJson = exports.isJsonProblem = void 0;
4
4
  const problem_1 = require("../problem");
5
+ const isProblemArray_1 = require("./isProblemArray");
5
6
  const version_1 = require("./version");
6
7
  const strings = ['title', 'type', 'instance', 'detail', '__problemVersion'];
7
8
  function isJsonProblem(input) {
9
+ if (input === null)
10
+ return false;
8
11
  if (typeof input !== 'object')
9
12
  return false;
10
- if (!strings.every(val => typeof input[val] === 'string'))
13
+ if (!strings.every(val => val in input && typeof input[val] === 'string'))
11
14
  return false;
12
- if (!version_1.isCompatibleVersion(input === null || input === void 0 ? void 0 : input.__problemVersion))
15
+ if (!(0, version_1.isCompatibleVersion)('__problemVersion' in input && input['__problemVersion'] === 'string' ? input['__problemVersion'] : ''))
13
16
  return false;
14
- if (typeof input.status !== 'number')
17
+ if (!('status' in input) || typeof input.status !== 'number')
15
18
  return false;
16
19
  return true;
17
20
  }
@@ -31,7 +34,7 @@ function getProblems(input) {
31
34
  if (input instanceof problem_1.Problem) {
32
35
  return [input];
33
36
  }
34
- if (input instanceof Array && input.every(val => val instanceof problem_1.Problem)) {
37
+ if ((0, isProblemArray_1.isProblemArray)(input)) {
35
38
  return input;
36
39
  }
37
40
  if (input instanceof Array && input.every(val => isJsonProblem(val))) {
@@ -0,0 +1,2 @@
1
+ import { Problem } from '../problem';
2
+ export declare function isProblemArray(input: unknown): input is Problem[];
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isProblemArray = void 0;
4
+ const problem_1 = require("../problem");
5
+ function isProblemArray(input) {
6
+ if (!Array.isArray(input))
7
+ return false;
8
+ return input.every(val => val instanceof problem_1.Problem);
9
+ }
10
+ exports.isProblemArray = isProblemArray;
package/util/misc.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { ProblemOpts } from '../typings';
2
+ export declare function getHttpError(statusCode: number | string): ProblemOpts | undefined;
package/util/misc.js ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = 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);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getHttpError = void 0;
27
+ const allCodes = __importStar(require("../defaults"));
28
+ function getHttpError(statusCode) {
29
+ for (const key in allCodes) {
30
+ // This ensures all codes are written correctly as well, so that's a bonus
31
+ const codeObject = allCodes[key];
32
+ if (typeof statusCode === 'string') {
33
+ const conv = Number(statusCode);
34
+ if (!isNaN(conv))
35
+ statusCode = conv;
36
+ }
37
+ const code = codeObject[statusCode];
38
+ if (!code)
39
+ return;
40
+ return code;
41
+ }
42
+ }
43
+ exports.getHttpError = getHttpError;
package/util/version.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export declare const version = "4.0.0";
1
+ export declare const version = "5.0.0";
2
2
  export declare const major: number;
3
3
  export declare function isCompatibleVersion(inputVersion: string): boolean;
package/util/version.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isCompatibleVersion = exports.major = exports.version = void 0;
4
- exports.version = '4.0.0';
4
+ exports.version = '5.0.0';
5
5
  exports.major = Number(exports.version.split('.')[0]);
6
6
  function isCompatibleVersion(inputVersion) {
7
7
  // TODO: Remove this
package/parsers/http.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { Problem } from '../problem';
2
- import type { HTTPErrorDetails } from '../typings/parser';
3
- export declare function parseHTTPError(input: HTTPErrorDetails): Problem | void;
package/parsers/http.js DELETED
@@ -1,15 +0,0 @@
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;
package/typings/misc.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export interface ResponseHeaders {
2
- [key: string]: string;
3
- }