@openstax/ts-utils 1.12.2 → 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.
- package/dist/cjs/errors/index.d.ts +14 -0
- package/dist/cjs/errors/index.js +17 -1
- package/dist/cjs/middleware/apiErrorHandler.d.ts +12 -4
- package/dist/cjs/middleware/apiErrorHandler.js +6 -3
- package/dist/cjs/routing/validators/zod.d.ts +4 -0
- package/dist/cjs/routing/validators/zod.js +12 -0
- package/dist/cjs/services/authProvider/index.d.ts +4 -3
- package/dist/cjs/services/authProvider/index.js +9 -6
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/errors/index.d.ts +14 -0
- package/dist/esm/errors/index.js +15 -0
- package/dist/esm/middleware/apiErrorHandler.d.ts +12 -4
- package/dist/esm/middleware/apiErrorHandler.js +5 -2
- package/dist/esm/routing/validators/zod.d.ts +4 -0
- package/dist/esm/routing/validators/zod.js +8 -0
- package/dist/esm/services/authProvider/index.d.ts +4 -3
- package/dist/esm/services/authProvider/index.js +9 -6
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +6 -2
|
@@ -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
|
*
|
package/dist/cjs/errors/index.js
CHANGED
|
@@ -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
|
|
4
|
-
|
|
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
|
-
|
|
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,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;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { FetchConfig } from '../../fetch';
|
|
2
|
-
import type { HttpHeaders } from '../../routing';
|
|
2
|
+
import type { HttpHeaders, QueryParams } from '../../routing';
|
|
3
3
|
export interface TokenUser {
|
|
4
4
|
id: number;
|
|
5
5
|
name: string;
|
|
@@ -35,15 +35,16 @@ export declare type AuthProvider = {
|
|
|
35
35
|
getAuthorizedFetchConfig: () => Promise<FetchConfig>;
|
|
36
36
|
};
|
|
37
37
|
export declare type CookieAuthProviderRequest = {
|
|
38
|
-
headers: HttpHeaders;
|
|
39
38
|
cookies?: string[];
|
|
39
|
+
headers: HttpHeaders;
|
|
40
|
+
queryStringParameters?: QueryParams;
|
|
40
41
|
};
|
|
41
42
|
export declare type CookieAuthProvider = (inputs: {
|
|
42
43
|
request: CookieAuthProviderRequest;
|
|
43
44
|
}) => AuthProvider;
|
|
44
45
|
export declare type StubAuthProvider = (user: User | undefined) => AuthProvider;
|
|
45
46
|
export declare const stubAuthProvider: (user?: User | undefined) => AuthProvider;
|
|
46
|
-
export declare const getAuthTokenOrCookie: (request: CookieAuthProviderRequest, cookieName: string) => [string, {
|
|
47
|
+
export declare const getAuthTokenOrCookie: (request: CookieAuthProviderRequest, cookieName: string, queryKey?: string) => [string, {
|
|
47
48
|
Authorization: string;
|
|
48
49
|
}] | [string, {
|
|
49
50
|
cookie: string;
|
|
@@ -12,12 +12,15 @@ const stubAuthProvider = (user) => ({
|
|
|
12
12
|
getAuthorizedFetchConfig: () => Promise.resolve(user ? { headers: { Authorization: user.uuid } } : {})
|
|
13
13
|
});
|
|
14
14
|
exports.stubAuthProvider = stubAuthProvider;
|
|
15
|
-
const getAuthTokenOrCookie = (request, cookieName) => {
|
|
16
|
-
var _a;
|
|
15
|
+
const getAuthTokenOrCookie = (request, cookieName, queryKey = 'auth') => {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
const authParam = request.queryStringParameters ? request.queryStringParameters[queryKey] : undefined;
|
|
17
18
|
const authHeader = (0, helpers_2.getHeader)(request.headers, 'authorization');
|
|
18
|
-
const cookieValue = cookie_1.default.parse(((_a = request.cookies) === null || _a === void 0 ? void 0 : _a.join('; '))
|
|
19
|
-
return
|
|
20
|
-
? (0, helpers_1.tuple)(
|
|
21
|
-
:
|
|
19
|
+
const cookieValue = cookie_1.default.parse((_b = (_a = request.cookies) === null || _a === void 0 ? void 0 : _a.join('; ')) !== null && _b !== void 0 ? _b : '')[cookieName];
|
|
20
|
+
return typeof authParam === 'string'
|
|
21
|
+
? (0, helpers_1.tuple)(authParam, { Authorization: `Bearer ${authParam}` })
|
|
22
|
+
: authHeader && authHeader.length >= 8 && authHeader.startsWith('Bearer ')
|
|
23
|
+
? (0, helpers_1.tuple)(authHeader.slice(7), { Authorization: authHeader })
|
|
24
|
+
: (0, helpers_1.tuple)(cookieValue, { cookie: cookie_1.default.serialize(cookieName, cookieValue) });
|
|
22
25
|
};
|
|
23
26
|
exports.getAuthTokenOrCookie = getAuthTokenOrCookie;
|