feat: error when action handler is missing
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@valkyr/relay",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"exports": {
|
||||
".": "./mod.ts",
|
||||
"./http": "./adapters/http.ts"
|
||||
|
||||
@@ -126,7 +126,10 @@ export class Api<TRoutes extends Route[]> {
|
||||
if (result.success === false) {
|
||||
return toResponse(new BadRequestError("Invalid action input", z.prettifyError(result.error)));
|
||||
}
|
||||
const output = (await action.state.handle?.(result.data)) ?? {};
|
||||
if (action.state.handle === undefined) {
|
||||
return toResponse(new InternalServerError(`Action '${action.state.name}' is missing handler.`));
|
||||
}
|
||||
const output = await action.state.handle(result.data);
|
||||
for (const key in output) {
|
||||
context[key] = output[key];
|
||||
}
|
||||
|
||||
@@ -24,6 +24,13 @@ export class Relay<TRoutes extends Route[]> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override relay url configuration.
|
||||
*/
|
||||
set url(value: string) {
|
||||
this.config.url = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a route for the given method/path combination which can be further extended
|
||||
* for serving incoming third party requests.
|
||||
|
||||
@@ -19,4 +19,5 @@ export const relay = new Relay({ url: "http://localhost:36573", adapter }, [
|
||||
.params({ userId: z.string().check(z.uuid()) })
|
||||
.body(UserSchema.omit({ id: true, createdAt: true })),
|
||||
route.delete("/users/:userId").params({ userId: z.string().check(z.uuid()) }),
|
||||
route.get("/add-two").search({ a: z.coerce.number(), b: z.coerce.number() }).response(z.number()),
|
||||
]);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Api } from "../../libraries/api.ts";
|
||||
import { NotFoundError } from "../../mod.ts";
|
||||
import { addTwoNumbers } from "./actions.ts";
|
||||
import { relay } from "./relay.ts";
|
||||
import { User } from "./user.ts";
|
||||
|
||||
@@ -30,4 +31,8 @@ export const api = new Api([
|
||||
relay.route("DELETE", "/users/:userId").handle(async ({ userId }) => {
|
||||
users = users.filter((user) => user.id !== userId);
|
||||
}),
|
||||
relay
|
||||
.route("GET", "/add-two")
|
||||
.actions([addTwoNumbers])
|
||||
.handle(async ({ added }) => added),
|
||||
]);
|
||||
|
||||
@@ -48,4 +48,8 @@ describe("Relay", () => {
|
||||
|
||||
assertEquals(users.length, 0);
|
||||
});
|
||||
|
||||
it("should successfully run .actions", async () => {
|
||||
assertEquals(await relay.get("/add-two", { a: 1, b: 1 }), 2);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user