[go: up one dir, main page]

Skip to content

Commit

Permalink
feat: remove ResponseError
Browse files Browse the repository at this point in the history
  • Loading branch information
predetermined committed Jun 18, 2023
1 parent bf8e544 commit d6fb1dc
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 55 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ app.route("/").respond(Method.GET, (_event) => {
```typescript
const v1 = app.route("/v1").step(async (event) => {
if (!event.request.headers.has("X-Api-Key")) {
throw new ResponseError(
"Missing API key",
Response.json(
{ error: "MISSING_API_KEY" },
{
status: 400,
}
)
event.response = Response.json(
{ error: "MISSING_API_KEY" },
{
status: 400,
}
);
return event.end();
}

const user = await getUserByRequest(event.request);
Expand Down
7 changes: 1 addition & 6 deletions lib/aqua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { serve } from "https://deno.land/std@0.185.0/http/server.ts";
import { Branch } from "./branch.ts";
import { Event, InternalizedEvent } from "./event.ts";
import { Method } from "./method.ts";
import { ResponseError } from "./response-error.ts";

export type AquaOptionsCustomListenFn = ({
handlerFn,
Expand Down Expand Up @@ -47,7 +46,7 @@ export interface AquaOptions {

export type StepFn<_Event extends Event> = (
event: _Event
) => _Event | Promise<_Event> | void;
) => _Event | void | Promise<_Event | void>;

export type RespondFn<_Event extends Event> = (
event: _Event
Expand Down Expand Up @@ -106,10 +105,6 @@ export class Aqua<_Event extends Event = Event> {
} catch (error) {
console.error(error);

if (error instanceof ResponseError) {
return error.response;
}

return new Response(error, { status: 500 });
}
};
Expand Down
16 changes: 7 additions & 9 deletions lib/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ export class Branch<_Event extends Event> {
* // Check whether a header is set and throw otherwise
* .step((event) => {
* if (!event.request.headers.has("X-Api-Key")) {
* throw new ResponseError(
* "Missing API key",
* Response.json(
* { error: "MISSING_API_KEY" },
* {
* status: 400,
* }
* )
* event.response = Response.json(
* { error: "MISSING_API_KEY" },
* {
* status: 400,
* }
* );
* return event.end();
* }
* });
*
Expand All @@ -62,7 +60,7 @@ export class Branch<_Event extends Event> {
* .step((event) => {
* if (event.request.headers.has("early-return")) {
* event.response = Response.json({ data: {} });
* event.end();
* return event.end();
* }
* });
*
Expand Down
15 changes: 12 additions & 3 deletions lib/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ export interface Event {
request: Request;
response: Response;
/**
* Responds to the event with the currently set `response`.
* This function should not be called multiple times.
* @todo Is there maybe a way to allow no statements after this fn has been called?
* Calling `end()` tells Aqua to respond to the event after running the
* current step function.
* Please make sure to return after calling `end()` to not accidentally modify
* the event response any further.
*
* @example
* .step((event) => {
* if (event.request.headers.has("early-return")) {
* event.response = Response.json({ data: {} });
* return event.end();
* }
* });
*/
end(): void;
[key: string]: unknown;
Expand Down
5 changes: 0 additions & 5 deletions lib/fake-aqua.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Aqua, AquaOptions } from "./aqua.ts";
import { ResponseError } from "./response-error.ts";

export class FakeAqua extends Aqua {
constructor(options: AquaOptions = {}) {
Expand All @@ -12,10 +11,6 @@ export class FakeAqua extends Aqua {
try {
return await this.handleRequest(this.createInternalEvent(request));
} catch (error) {
if (error instanceof ResponseError) {
return error.response;
}

return new Response(error, { status: 500 });
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export * from "./branch.ts";
export * from "./event.ts";
export * from "./fake-aqua.ts";
export * from "./method.ts";
export * from "./response-error.ts";
6 changes: 0 additions & 6 deletions lib/response-error.ts

This file was deleted.

12 changes: 4 additions & 8 deletions mod.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from "https://deno.land/std@0.192.0/testing/asserts.ts";
import { FakeAqua, Method, ResponseError } from "./mod.ts";
import { FakeAqua, Method } from "./mod.ts";

Deno.test(async function notFound() {
const app = new FakeAqua();
Expand Down Expand Up @@ -126,7 +126,7 @@ Deno.test(async function addCustomPropertyStep() {
assert((await res.text()) === "bar");
});

Deno.test(async function throwResponseErrorInStep() {
Deno.test(async function failEventInStep() {
const app = new FakeAqua();

app
Expand All @@ -135,12 +135,8 @@ Deno.test(async function throwResponseErrorInStep() {
// just so it doesn't infer `never`
if (event.request.headers.has("test")) return event;

throw new ResponseError(
"failed",
new Response("failed", {
status: 500,
})
);
event.response = new Response("failed", { status: 500 });
return event.end();
})
.respond(Method.GET, (_event) => new Response("succeeded"));

Expand Down
15 changes: 6 additions & 9 deletions usage_playground.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @todo delete this file

import { ResponseError } from "./lib/response-error.ts";
import { Aqua, Method } from "./mod.ts";

const app = new Aqua({
Expand All @@ -18,15 +17,13 @@ const getUserByRequest = (_req: Request) => Promise.resolve({ name: "test" });

const v1 = app.route("/v1").step(async (event) => {
if (!event.request.headers.has("X-Api-Key")) {
throw new ResponseError(
"Missing API key",
Response.json(
{ error: "MISSING_API_KEY" },
{
status: 400,
}
)
event.response = Response.json(
{ error: "MISSING_API_KEY" },
{
status: 400,
}
);
return event.end();
}

const user = await getUserByRequest(event.request);
Expand Down

0 comments on commit d6fb1dc

Please sign in to comment.