Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: providing options to Result Handler #1612

Merged
merged 19 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,15 @@ export class Endpoint<
request,
response,
logger,
options,
}: {
method: Method | AuxMethod;
input: Readonly<FlatObject>; // Issue #673: input is immutable, since this.inputSchema is combined with ones of middlewares
request: Request;
response: Response;
logger: AbstractLogger;
options: Partial<OPT>;
}) {
const options = {} as OPT;
for (const def of this.#middlewares) {
if (method === "options" && def.type === "proprietary") {
continue;
Expand Down Expand Up @@ -271,7 +272,6 @@ export class Endpoint<
break;
}
}
return options;
}

async #parseAndRunHandler({
Expand Down Expand Up @@ -308,13 +308,15 @@ export class Endpoint<
logger,
input,
output,
options,
}: {
error: Error | null;
request: Request;
response: Response;
logger: AbstractLogger;
input: FlatObject;
output: FlatObject | null;
options: Partial<OPT>;
}) {
try {
await this.#resultHandler.handler({
Expand All @@ -324,6 +326,7 @@ export class Endpoint<
response,
logger,
input,
options,
});
} catch (e) {
lastResortHandler({
Expand All @@ -348,6 +351,7 @@ export class Endpoint<
siblingMethods?: Method[];
}) {
const method = getActualMethod(request);
const options: Partial<OPT> = {};
let output: FlatObject | null = null;
let error: Error | null = null;
if (config.cors) {
Expand All @@ -366,12 +370,13 @@ export class Endpoint<
}
const input = getInput(request, config.inputSources);
try {
const options = await this.#runMiddlewares({
await this.#runMiddlewares({
method,
input,
request,
response,
logger,
options,
});
if (response.writableEnded) {
return;
Expand All @@ -381,7 +386,11 @@ export class Endpoint<
return;
}
output = await this.#parseOutput(
await this.#parseAndRunHandler({ input, options, logger }),
await this.#parseAndRunHandler({
input,
logger,
options: options as OPT, // ensured the complete OPT by writableEnded condition and try-catch
}),
);
} catch (e) {
error = makeErrorFromAnything(e);
Expand All @@ -393,6 +402,7 @@ export class Endpoint<
response,
error,
logger,
options,
});
}
}
2 changes: 2 additions & 0 deletions src/result-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface ResultHandlerParams<RES> {
input: FlatObject | null;
/** null in case of errors or failures */
output: FlatObject | null;
/** can be empty: check presence of the required property using "in" operator */
options: FlatObject;
RobinTail marked this conversation as resolved.
Show resolved Hide resolved
error: Error | null;
request: Request;
response: Response<RES>;
Expand Down
2 changes: 2 additions & 0 deletions src/server-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const createParserFailureHandler =
response,
input: null,
output: null,
options: {},
logger: getChildLogger
? await getChildLogger({ request, parent: rootLogger })
: rootLogger,
Expand Down Expand Up @@ -59,6 +60,7 @@ export const createNotFoundHandler =
error,
input: null,
output: null,
options: {},
});
} catch (e) {
lastResortHandler({
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/result-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
defaultResultHandler,
withMeta,
} from "../../src";
import { ApiResponse } from "../../src/api-response";
import { ApiResponse } from "../../src";
import { metaProp } from "../../src/metadata";
import { describe, expect, test, vi } from "vitest";
import {
Expand Down Expand Up @@ -51,6 +51,7 @@ describe("ResultHandler", () => {
request: requestMock as unknown as Request,
response: responseMock as unknown as Response,
logger: loggerMock,
options: {},
});
expect(loggerMock.error).toHaveBeenCalledTimes(1);
expect(loggerMock.error.mock.calls[0][0]).toMatch(
Expand Down Expand Up @@ -86,6 +87,7 @@ describe("ResultHandler", () => {
),
input: { something: 453 },
output: { anything: 118 },
options: {},
request: requestMock as unknown as Request,
response: responseMock as unknown as Response,
logger: loggerMock,
Expand All @@ -103,6 +105,7 @@ describe("ResultHandler", () => {
error: createHttpError(404, "Something not found"),
input: { something: 453 },
output: { anything: 118 },
options: {},
request: requestMock as unknown as Request,
response: responseMock as unknown as Response,
logger: loggerMock,
Expand All @@ -120,6 +123,7 @@ describe("ResultHandler", () => {
error: null,
input: { something: 453 },
output: { anything: 118, items: ["One", "Two", "Three"] },
options: {},
request: requestMock as unknown as Request,
response: responseMock as unknown as Response,
logger: loggerMock,
Expand Down Expand Up @@ -165,6 +169,7 @@ describe("ResultHandler", () => {
error: null,
input: { something: 453 },
output: { anything: 118 },
options: {},
request: requestMock as unknown as Request,
response: responseMock as unknown as Response,
logger: loggerMock,
Expand Down
Loading