Skip to content

Commit 4800993

Browse files
Migrate middlwares names to be consistent with verb-convention
1 parent 97340cc commit 4800993

9 files changed

Lines changed: 50 additions & 50 deletions

File tree

packages/cors/src/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { compose, typesOf } from '@thrty/core';
22
import { args } from '@thrty/testing';
33
import { NotFoundError } from '@thrty/http-errors';
4-
import { httpErrorHandler } from '@thrty/http-error-handler';
4+
import { catchHttpErrors } from '@thrty/http-error-handler';
55
import { APIGatewayProxyEvent, APIGatewayProxyHandler, APIGatewayProxyResult } from 'aws-lambda';
66
import { handleCors } from './index';
77

@@ -150,12 +150,12 @@ describe('preflight', () => {
150150
});
151151
});
152152

153-
describe('with httpErrorHandler', () => {
153+
describe('with catchHttpErrors', () => {
154154
beforeAll(() => {
155155
handler = compose(
156156
typesOf<APIGatewayProxyHandler>(),
157157
handleCors(),
158-
httpErrorHandler({ logger: false }),
158+
catchHttpErrors({ logger: false }),
159159
)(async () => {
160160
throw new NotFoundError('Not found');
161161
});

packages/error-handler/src/index.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { compose, eventType } from '@thrty/core';
22
import { inject } from '@thrty/inject';
33
import { args } from '@thrty/testing';
4-
import { errorHandler } from './index';
4+
import { catchErrors } from './index';
55

66
class MappedError extends Error {
77
constructor(public readonly cause: unknown) {
@@ -10,11 +10,11 @@ class MappedError extends Error {
1010
}
1111
}
1212

13-
describe('errorHandler', () => {
13+
describe('catchErrors', () => {
1414
it('passes through when the handler does not throw', async () => {
1515
const handler = compose(
1616
eventType<{ ok: true }>(),
17-
errorHandler({ logger: false }),
17+
catchErrors({ logger: false }),
1818
)(async () => 'ok');
1919

2020
await expect(handler(...args<{ ok: true }>({ ok: true }))).resolves.toBe('ok');
@@ -24,7 +24,7 @@ describe('errorHandler', () => {
2424
const error = new Error('boom');
2525
const handler = compose(
2626
eventType<{}>(),
27-
errorHandler({ logger: false }),
27+
catchErrors({ logger: false }),
2828
)(async () => {
2929
throw error;
3030
});
@@ -37,7 +37,7 @@ describe('errorHandler', () => {
3737
const error = new Error('boom');
3838
const handler = compose(
3939
eventType<{}>(),
40-
errorHandler({ logger: { error: logError } }),
40+
catchErrors({ logger: { error: logError } }),
4141
)(async () => {
4242
throw error;
4343
});
@@ -49,7 +49,7 @@ describe('errorHandler', () => {
4949
it('substitutes the result when onError returns a value', async () => {
5050
const handler = compose(
5151
eventType<{ id: string }>(),
52-
errorHandler({
52+
catchErrors({
5353
logger: false,
5454
onError: (_error, { event }) => ({ fallback: event.id }),
5555
}),
@@ -65,7 +65,7 @@ describe('errorHandler', () => {
6565
it('propagates a mapped error when onError throws', async () => {
6666
const handler = compose(
6767
eventType<{}>(),
68-
errorHandler({
68+
catchErrors({
6969
logger: false,
7070
onError: (error) => {
7171
throw new MappedError(error);
@@ -81,7 +81,7 @@ describe('errorHandler', () => {
8181
it('propagates a mapped error when onError returns Promise.reject(...)', async () => {
8282
const handler = compose(
8383
eventType<{}>(),
84-
errorHandler({
84+
catchErrors({
8585
logger: false,
8686
onError: (error) => Promise.reject(new MappedError(error)),
8787
}),
@@ -96,7 +96,7 @@ describe('errorHandler', () => {
9696
const onError = jest.fn().mockResolvedValue('handled');
9797
const handler = compose(
9898
eventType<{ id: string }>(),
99-
errorHandler({ logger: false, onError }),
99+
catchErrors({ logger: false, onError }),
100100
)(async () => {
101101
throw new Error('boom');
102102
});
@@ -111,7 +111,7 @@ describe('errorHandler', () => {
111111
const logError = jest.fn();
112112
const handler = compose(
113113
eventType<{}>(),
114-
errorHandler({
114+
catchErrors({
115115
logger: { error: logError },
116116
onError: () => undefined as unknown as void,
117117
}),
@@ -128,7 +128,7 @@ describe('errorHandler', () => {
128128
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
129129
const handler = compose(
130130
eventType<{}>(),
131-
errorHandler({
131+
catchErrors({
132132
logger: false,
133133
onError: () => undefined as unknown as void,
134134
}),
@@ -148,7 +148,7 @@ describe('errorHandler', () => {
148148
inject({
149149
logger: () => ({ error: logError }),
150150
}),
151-
errorHandler({
151+
catchErrors({
152152
onError: () => undefined as unknown as void,
153153
}),
154154
)(async () => {
@@ -167,7 +167,7 @@ describe('errorHandler', () => {
167167
inject({
168168
logger: () => ({ error: depsLogger }),
169169
}),
170-
errorHandler({
170+
catchErrors({
171171
logger: { error: optionsLogger },
172172
onError: () => undefined as unknown as void,
173173
}),
@@ -183,7 +183,7 @@ describe('errorHandler', () => {
183183
it('awaits async onError', async () => {
184184
const handler = compose(
185185
eventType<{}>(),
186-
errorHandler({
186+
catchErrors({
187187
logger: false,
188188
onError: () =>
189189
new Promise<string>((resolve) => setTimeout(() => resolve('async-handled'), 1)),

packages/error-handler/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface ErrorLogger {
55
[log: string]: any;
66
}
77

8-
export interface ErrorHandlerOptions<TEvent, TContext, TResult> {
8+
export interface CatchErrorsOptions<TEvent, TContext, TResult> {
99
/**
1010
* Called for every caught error, after logging. Return
1111
* `Promise.reject(...)` to propagate (the original or a mapped error);
@@ -33,9 +33,9 @@ export interface ErrorHandlerOptions<TEvent, TContext, TResult> {
3333
* semantics are preserved. Provide `onError` to map the error or substitute
3434
* a result (e.g. an HTTP response).
3535
*/
36-
export const errorHandler =
36+
export const catchErrors =
3737
<TEvent, TContext, TResult>(
38-
options: ErrorHandlerOptions<TEvent, TContext, TResult> = {},
38+
options: CatchErrorsOptions<TEvent, TContext, TResult> = {},
3939
): Middleware<TEvent, TEvent, Promise<TResult>, Promise<TResult>, TContext, TContext> =>
4040
(handler) =>
4141
async (event, context, ...rest: any[]) => {

packages/http-error-handler/src/index.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
APIGatewayProxyEvent,
1414
APIGatewayProxyResult as AwsLambdaAPIGatewayProxyResult,
1515
} from 'aws-lambda';
16-
import { httpErrorHandler } from './index';
16+
import { catchHttpErrors } from './index';
1717

1818
type APIGatewayProxyResult = Omit<AwsLambdaAPIGatewayProxyResult, 'body'> & {
1919
body?: string;
@@ -23,7 +23,7 @@ describe('simple setup', () => {
2323
let throwError: jest.Mock;
2424
let handler = compose(
2525
types<APIGatewayEvent, Promise<APIGatewayProxyResult>>(),
26-
httpErrorHandler({ logger: false }),
26+
catchHttpErrors({ logger: false }),
2727
)(async (event) => {
2828
throwError();
2929

@@ -126,7 +126,7 @@ describe('simple setup', () => {
126126
describe('blacklist', () => {
127127
const handler = compose(
128128
types<APIGatewayEvent, Promise<APIGatewayProxyResult>>(),
129-
httpErrorHandler({
129+
catchHttpErrors({
130130
logger: false,
131131
blacklist: [{ alternativeMessage: 'Error', statusCode: 404 }],
132132
}),
@@ -184,7 +184,7 @@ describe('context.deps.logger', () => {
184184
inject({
185185
logger: () => ({ error: logError }),
186186
}),
187-
httpErrorHandler(),
187+
catchHttpErrors(),
188188
)(async (_event) => {
189189
throwError();
190190

@@ -220,7 +220,7 @@ describe('options.logger', () => {
220220
logError = jest.fn();
221221
handler = compose(
222222
types<APIGatewayEvent, Promise<APIGatewayProxyResult>>(),
223-
httpErrorHandler({
223+
catchHttpErrors({
224224
logger: { error: logError },
225225
}),
226226
)(async (event) => {

packages/http-error-handler/src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Middleware } from '@thrty/core';
2-
import { errorHandler } from '@thrty/error-handler';
2+
import { catchErrors } from '@thrty/error-handler';
33
import { BaseError } from '@thrty/http-errors';
44
import { APIGatewayProxyResult } from 'aws-lambda';
55

@@ -14,7 +14,7 @@ interface ErrorLogger {
1414
[log: string]: any;
1515
}
1616

17-
export interface HttpErrorHandlerOptions {
17+
export interface CatchHttpErrorsOptions {
1818
/**
1919
* Logger to use for logging errors.
2020
* If not provided, console will be used.
@@ -35,20 +35,20 @@ export interface HttpErrorHandlerOptions {
3535
safeBaseError?: any;
3636
}
3737

38-
type ResolvedHttpErrorHandlerOptions = Required<
39-
Pick<HttpErrorHandlerOptions, 'blacklist' | 'safeBaseError'>
38+
type ResolvedCatchHttpErrorsOptions = Required<
39+
Pick<CatchHttpErrorsOptions, 'blacklist' | 'safeBaseError'>
4040
>;
4141

42-
type HttpErrorHandlerRequiredEvents = {
42+
type CatchHttpErrorsRequiredEvents = {
4343
path: string;
4444
httpMethod: string;
4545
};
4646

47-
export const httpErrorHandler = <T extends HttpErrorHandlerRequiredEvents, C, R>(
48-
options: HttpErrorHandlerOptions = {},
47+
export const catchHttpErrors = <T extends CatchHttpErrorsRequiredEvents, C, R>(
48+
options: CatchHttpErrorsOptions = {},
4949
): Middleware<T, T, Promise<R>, Promise<R>, C, C> => {
5050
const resolvedOptions = { ...defaultOptions, ...options };
51-
return errorHandler<T, C, R>({
51+
return catchErrors<T, C, R>({
5252
logger: options.logger,
5353
onError: (error) => {
5454
const { statusCode, message, ...errorProps } = getSafeResponse(resolvedOptions, error);
@@ -66,7 +66,7 @@ export const httpErrorHandler = <T extends HttpErrorHandlerRequiredEvents, C, R>
6666
});
6767
};
6868

69-
export const getSafeResponse = (options: ResolvedHttpErrorHandlerOptions, error?: any) => {
69+
export const getSafeResponse = (options: ResolvedCatchHttpErrorsOptions, error?: any) => {
7070
const isSafeErrorInstance = error instanceof options.safeBaseError;
7171
if (!isSafeErrorInstance) {
7272
return {
@@ -95,7 +95,7 @@ const internalServerError = { statusCode: 500, alternativeMessage: 'InternalServ
9595
const forbiddenError = { statusCode: 403, alternativeMessage: 'Forbidden' };
9696
const unauthorizedError = { statusCode: 401, alternativeMessage: 'Unauthorized' };
9797

98-
const defaultOptions: ResolvedHttpErrorHandlerOptions = {
98+
const defaultOptions: ResolvedCatchHttpErrorsOptions = {
9999
blacklist: [internalServerError, forbiddenError, unauthorizedError, unknownError],
100100
safeBaseError: BaseError,
101101
};

packages/http-json-body-parser/src/index.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { APIGatewayProxyEvent } from 'aws-lambda';
22
import { compose, types, of } from '@thrty/core';
3-
import { parseJson } from './index';
3+
import { parseRequestBody } from './index';
44

55
let handler: any;
66
const body = { name: 'bob', age: 12 };
77

88
beforeEach(() => {
99
handler = compose(
1010
types<APIGatewayProxyEvent, any>(),
11-
parseJson(),
11+
parseRequestBody(),
1212
)(async (event) => {
1313
return event.jsonBody;
1414
});
1515
});
1616

17-
it('should return parseJson body', async () => {
17+
it('should parse the request body as JSON', async () => {
1818
const jsonBody = await handler({ body: JSON.stringify(body) });
1919
expect(jsonBody).toEqual(body);
2020
});
@@ -24,7 +24,7 @@ describe('given body type is specified', () => {
2424
it('should not throw ts errors', () => {
2525
handler = compose(
2626
types<APIGatewayProxyEvent, any>(),
27-
parseJson(of<{ id: string; description: string }>),
27+
parseRequestBody(of<{ id: string; description: string }>),
2828
)(async (event) => {
2929
event.jsonBody.id;
3030
event.jsonBody.description;
@@ -35,7 +35,7 @@ describe('given body type is specified', () => {
3535
it('should throw ts error', () => {
3636
handler = compose(
3737
types<APIGatewayProxyEvent, any>(),
38-
parseJson(of<{ id: string; description: string }>),
38+
parseRequestBody(of<{ id: string; description: string }>),
3939
)(async (event) => {
4040
// @ts-expect-error
4141
event.jsonBody.unknown;

packages/http-json-body-parser/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Middleware, TypeRef } from '@thrty/core';
22

3-
export interface ParseJsonRequiredEvent {
3+
export interface ParseRequestBodyRequiredEvent {
44
body: string | null;
55
}
6-
export const parseJson =
7-
<T extends ParseJsonRequiredEvent, C, R, TBody = object>(
6+
export const parseRequestBody =
7+
<T extends ParseRequestBodyRequiredEvent, C, R, TBody = object>(
88
bodyType?: TypeRef<TBody>,
99
): Middleware<T, T & { jsonBody: TBody }, R, R, C, C> =>
1010
(handler) =>

packages/http-json-body-serializer/src/index.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { compose, types, of } from '@thrty/core';
22
import { args } from '@thrty/testing';
33
import { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
4-
import { serializeJson } from './index';
4+
import { serializeResponseBody } from './index';
55

66
describe('given no body type is specified', () => {
77
const createHandler = () =>
88
compose(
99
types<APIGatewayEvent, Promise<APIGatewayProxyResult>>(),
10-
serializeJson(),
10+
serializeResponseBody(),
1111
)(async (event) => {
1212
return {
1313
statusCode: 200,
@@ -45,7 +45,7 @@ describe('given body type is specified', () => {
4545
const createHandler = () =>
4646
compose(
4747
types<APIGatewayEvent, Promise<APIGatewayProxyResult>>(),
48-
serializeJson(of<Message>),
48+
serializeResponseBody(of<Message>),
4949
)(async (event) => {
5050
return {
5151
statusCode: 200,
@@ -81,7 +81,7 @@ describe('given body type is specified but not returned properly', () => {
8181
it('should throw ts error', () => {
8282
compose(
8383
types<APIGatewayEvent, Promise<APIGatewayProxyResult>>(),
84-
serializeJson(of<{ id: string; description: string }>),
84+
serializeResponseBody(of<{ id: string; description: string }>),
8585
// @ts-expect-error
8686
)(async (event) => {
8787
return {

packages/http-json-body-serializer/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { APIGatewayProxyResult } from 'aws-lambda';
22
import { Middleware, TypeRef } from '@thrty/core';
33

4-
export type SerializeJsonOptions<TBody = object> = Omit<APIGatewayProxyResult, 'body'> & {
4+
export type SerializeResponseBodyOptions<TBody = object> = Omit<APIGatewayProxyResult, 'body'> & {
55
body?: TBody;
66
};
7-
export const serializeJson =
8-
<E, C, R1 extends APIGatewayProxyResult, R2 extends SerializeJsonOptions<TBody>, TBody>(
7+
export const serializeResponseBody =
8+
<E, C, R1 extends APIGatewayProxyResult, R2 extends SerializeResponseBodyOptions<TBody>, TBody>(
99
bodyType?: TypeRef<TBody>,
1010
): Middleware<E, E, Promise<R1>, Promise<R2>, C, C> =>
1111
(next) =>

0 commit comments

Comments
 (0)