@openstax/ts-utils 1.12.3 → 1.13.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.
@@ -1,3 +1,4 @@
1
+ import type { JsonCompatibleStruct } from '../routing';
1
2
  /**
2
3
  * Returns true if the error is defined in this library
3
4
  */
@@ -17,6 +18,19 @@ export declare class InvalidRequestError extends Error {
17
18
  static matches: (e: any) => e is typeof InvalidRequestError;
18
19
  constructor(message?: string);
19
20
  }
21
+ /**
22
+ * Validation Error
23
+ *
24
+ * `ValidationError.matches(error)` is a reliable way to check if an error is an
25
+ * `ValidationError`; `instanceof` checks may not work if code is split into multiple bundles
26
+ */
27
+ export declare class ValidationError extends Error {
28
+ static readonly TYPE = "ValidationError";
29
+ static matches: (e: any) => e is typeof ValidationError;
30
+ private data;
31
+ constructor(data: JsonCompatibleStruct);
32
+ getData(): JsonCompatibleStruct;
33
+ }
20
34
  /**
21
35
  * Unauthorized error
22
36
  *
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SessionExpiredError = exports.NotFoundError = exports.UnauthorizedError = exports.InvalidRequestError = exports.isAppError = void 0;
3
+ exports.SessionExpiredError = exports.NotFoundError = exports.UnauthorizedError = exports.ValidationError = exports.InvalidRequestError = exports.isAppError = void 0;
4
4
  /*
5
5
  * if code is split into multiple bundles, sometimes each bundle
6
6
  * will get its own definition of this module and then instanceof checks
@@ -35,6 +35,22 @@ class InvalidRequestError extends Error {
35
35
  exports.InvalidRequestError = InvalidRequestError;
36
36
  InvalidRequestError.TYPE = 'InvalidRequestError';
37
37
  InvalidRequestError.matches = errorIsType(InvalidRequestError);
38
+ /**
39
+ * Validation Error
40
+ *
41
+ * `ValidationError.matches(error)` is a reliable way to check if an error is an
42
+ * `ValidationError`; `instanceof` checks may not work if code is split into multiple bundles
43
+ */
44
+ class ValidationError extends Error {
45
+ constructor(data) {
46
+ super(InvalidRequestError.TYPE);
47
+ this.data = data;
48
+ }
49
+ getData() { return this.data; }
50
+ }
51
+ exports.ValidationError = ValidationError;
52
+ ValidationError.TYPE = 'ValidationError';
53
+ ValidationError.matches = errorIsType(ValidationError);
38
54
  /**
39
55
  * Unauthorized error
40
56
  *
@@ -1,8 +1,17 @@
1
+ import { InvalidRequestError, NotFoundError, SessionExpiredError, UnauthorizedError, ValidationError } from '../errors';
1
2
  import type { ApiResponse } from '../routing';
2
3
  import type { Logger } from '../services/logger';
3
- declare type Handlers = {
4
- [key: string]: (e: Error, logger: Logger) => ApiResponse<number, any>;
4
+ export declare type DefaultErrors = {
5
+ UnauthorizedError: UnauthorizedError;
6
+ SessionExpiredError: SessionExpiredError;
7
+ NotFoundError: NotFoundError;
8
+ InvalidRequestError: InvalidRequestError;
9
+ ValidationError: ValidationError;
5
10
  };
11
+ export declare type Handlers<E> = {
12
+ [T in keyof E]: (e: E[T], logger: Logger) => ApiResponse<number, any>;
13
+ };
14
+ export declare const defaultHandlers: Handlers<DefaultErrors>;
6
15
  /**
7
16
  * Creates an error handler. Provides default handlers for `UnauthorizedError`,
8
17
  * `SessionExpiredError`, `NotFoundError`, and `InvalidRequestError`. User-specified
@@ -11,5 +20,4 @@ declare type Handlers = {
11
20
  *
12
21
  * @param inputHandlers a map of errors to handler functions
13
22
  */
14
- export declare const createErrorHandler: (inputHandlers?: Handlers | undefined) => (e: Error, logger: Logger) => Promise<ApiResponse<number, any>>;
15
- export {};
23
+ export declare const createErrorHandler: <Errors = DefaultErrors>(inputHandlers?: Handlers<Errors> | undefined) => (e: Error, logger: Logger) => Promise<ApiResponse<number, any>>;
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createErrorHandler = void 0;
3
+ exports.createErrorHandler = exports.defaultHandlers = void 0;
4
4
  const errors_1 = require("../errors");
5
5
  const routing_1 = require("../routing");
6
6
  const logger_1 = require("../services/logger");
7
- const defaultHandlers = {
7
+ exports.defaultHandlers = {
8
8
  UnauthorizedError: () => (0, routing_1.apiTextResponse)(401, '401 UnauthorizedError'),
9
9
  SessionExpiredError: () => (0, routing_1.apiTextResponse)(440, '440 SessionExpiredError'),
10
10
  NotFoundError: (e) => (0, routing_1.apiTextResponse)(404, `404 ${e.message}`),
11
11
  InvalidRequestError: (e) => (0, routing_1.apiTextResponse)(400, `400 ${e.message}`),
12
+ ValidationError: (e) => (0, routing_1.apiJsonResponse)(422, e.getData()),
12
13
  };
13
14
  /**
14
15
  * Creates an error handler. Provides default handlers for `UnauthorizedError`,
@@ -19,11 +20,13 @@ const defaultHandlers = {
19
20
  * @param inputHandlers a map of errors to handler functions
20
21
  */
21
22
  const createErrorHandler = (inputHandlers) => {
22
- const handlers = { ...defaultHandlers, ...inputHandlers };
23
+ const handlers = { ...exports.defaultHandlers, ...inputHandlers };
23
24
  return async (e, logger) => {
24
25
  const name = (0, errors_1.isAppError)(e) ? e.constructor.TYPE : e.constructor.name;
25
26
  const handler = handlers[name];
26
27
  if (handler) {
28
+ // convincing typescript that this error is the right kind for the handler
29
+ // we looked up based on the errors name is very annoying
27
30
  return handler(e, logger);
28
31
  }
29
32
  logger.logEvent(logger_1.Level.Error, {
@@ -0,0 +1,4 @@
1
+ import type { z } from 'zod';
2
+ export declare const zodPayloadValidator: <T>(validator: z.ZodType<T, z.ZodTypeDef, T>) => (input: {
3
+ [key: string]: any;
4
+ }) => input is T;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zodPayloadValidator = void 0;
4
+ const errors_1 = require("../../errors");
5
+ const zodPayloadValidator = (validator) => (input) => {
6
+ const result = validator.safeParse(input);
7
+ if (result.success) {
8
+ return true;
9
+ }
10
+ throw new errors_1.ValidationError(result.error.format());
11
+ };
12
+ exports.zodPayloadValidator = zodPayloadValidator;