feat: convert to rpc pattern
This commit is contained in:
@@ -3,15 +3,13 @@ import z from "zod";
|
||||
import { action } from "../../libraries/action.ts";
|
||||
import { BadRequestError } from "../../mod.ts";
|
||||
|
||||
export const addTwoNumbers = action
|
||||
.make("addTwoNumbers")
|
||||
.input({ a: z.number(), b: z.number() })
|
||||
.output({ added: z.number() })
|
||||
.handle(async ({ a, b }) => {
|
||||
export const addNumbers = action
|
||||
.make("number:add")
|
||||
.input(z.tuple([z.number(), z.number()]))
|
||||
.output({ sum: z.number() })
|
||||
.handle(async ([a, b]) => {
|
||||
if (a < 0 || b < 0) {
|
||||
return new BadRequestError("Invalid input numbers added");
|
||||
return new BadRequestError("Invalid numbers provided");
|
||||
}
|
||||
return {
|
||||
added: a + b,
|
||||
};
|
||||
return { sum: a + b };
|
||||
});
|
||||
|
||||
@@ -1,24 +1,31 @@
|
||||
import z from "zod";
|
||||
|
||||
import { procedure } from "../../libraries/procedure.ts";
|
||||
import { Relay } from "../../libraries/relay.ts";
|
||||
import { route } from "../../libraries/route.ts";
|
||||
import { UserSchema } from "./user.ts";
|
||||
|
||||
export const relay = new Relay([
|
||||
route
|
||||
.post("/users")
|
||||
.body(UserSchema.omit({ id: true, createdAt: true }))
|
||||
.response(z.string()),
|
||||
route
|
||||
.get("/users/:userId")
|
||||
.params({ userId: z.string().check(z.uuid()) })
|
||||
.response(UserSchema),
|
||||
route
|
||||
.put("/users/:userId")
|
||||
.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()),
|
||||
]);
|
||||
|
||||
export type RelayRoutes = typeof relay.$inferRoutes;
|
||||
export const relay = new Relay({
|
||||
user: {
|
||||
create: procedure
|
||||
.method("user:create")
|
||||
.params(UserSchema.omit({ id: true, createdAt: true }))
|
||||
.result(z.string()),
|
||||
get: procedure.method("user:get").params(z.string().check(z.uuid())).result(UserSchema),
|
||||
update: procedure.method("user:update").params(
|
||||
z.tuple([
|
||||
z.string(),
|
||||
z.object({
|
||||
name: z.string().optional(),
|
||||
email: z.string().check(z.email()).optional(),
|
||||
}),
|
||||
]),
|
||||
),
|
||||
delete: procedure.method("user:delete").params(z.string().check(z.uuid())),
|
||||
},
|
||||
numbers: {
|
||||
add: procedure
|
||||
.method("number:add")
|
||||
.params(z.tuple([z.number(), z.number()]))
|
||||
.result(z.number()),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,40 +1,42 @@
|
||||
import { RelayAPI } from "../../libraries/api.ts";
|
||||
import { RelayApi } from "../../libraries/api.ts";
|
||||
import { NotFoundError } from "../../mod.ts";
|
||||
import { addTwoNumbers } from "./actions.ts";
|
||||
import { addNumbers } from "./actions.ts";
|
||||
import { relay } from "./relay.ts";
|
||||
import { User } from "./user.ts";
|
||||
|
||||
export let users: User[] = [];
|
||||
|
||||
export const api = new RelayAPI({
|
||||
routes: [
|
||||
relay.route("POST", "/users").handle(async ({ name, email }) => {
|
||||
export const api = new RelayApi({
|
||||
procedures: [
|
||||
relay.procedure("user:create").handle(async ({ name, email }) => {
|
||||
const id = crypto.randomUUID();
|
||||
users.push({ id, name, email, createdAt: new Date() });
|
||||
return id;
|
||||
}),
|
||||
relay.route("GET", "/users/:userId").handle(async ({ userId }) => {
|
||||
relay.procedure("user:get").handle(async (userId) => {
|
||||
const user = users.find((user) => user.id === userId);
|
||||
if (user === undefined) {
|
||||
return new NotFoundError();
|
||||
}
|
||||
return user;
|
||||
}),
|
||||
relay.route("PUT", "/users/:userId").handle(async ({ userId, name, email }) => {
|
||||
relay.procedure("user:update").handle(async ([userId, { name, email }]) => {
|
||||
for (const user of users) {
|
||||
if (user.id === userId) {
|
||||
user.name = name;
|
||||
user.email = email;
|
||||
user.name = name ?? user.name;
|
||||
user.email = email ?? user.email;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}),
|
||||
relay.route("DELETE", "/users/:userId").handle(async ({ userId }) => {
|
||||
relay.procedure("user:delete").handle(async (userId) => {
|
||||
users = users.filter((user) => user.id !== userId);
|
||||
}),
|
||||
relay
|
||||
.route("GET", "/add-two")
|
||||
.actions([addTwoNumbers])
|
||||
.handle(async ({ added }) => added),
|
||||
.procedure("number:add")
|
||||
.actions([[addNumbers, (params) => params]])
|
||||
.handle(async (_, { sum }) => {
|
||||
return sum;
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
@@ -1,29 +1,31 @@
|
||||
import "./mocks/server.ts";
|
||||
|
||||
import { assertEquals, assertObjectMatch, assertRejects } from "@std/assert";
|
||||
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
|
||||
|
||||
import { adapter } from "../adapters/http.ts";
|
||||
import { RelayClient } from "../libraries/client.ts";
|
||||
import { relay, RelayRoutes } from "./mocks/relay.ts";
|
||||
import { HttpAdapter } from "../adapters/http.ts";
|
||||
import { relay } from "./mocks/relay.ts";
|
||||
import { api, users } from "./mocks/server.ts";
|
||||
|
||||
describe("Relay", () => {
|
||||
describe("Procedure", () => {
|
||||
let server: Deno.HttpServer<Deno.NetAddr>;
|
||||
let client: RelayClient<RelayRoutes>;
|
||||
let client: typeof relay.$inferClient;
|
||||
|
||||
beforeAll(() => {
|
||||
beforeAll(async () => {
|
||||
server = Deno.serve(
|
||||
{
|
||||
port: 36573,
|
||||
port: 8080,
|
||||
hostname: "localhost",
|
||||
onListen({ port, hostname }) {
|
||||
console.log(`Listening at http://${hostname}:${port}`);
|
||||
},
|
||||
},
|
||||
async (request) => api.handle(request),
|
||||
async (request) => {
|
||||
const { method, params, id } = await request.json();
|
||||
return api.call(method, params, id);
|
||||
},
|
||||
);
|
||||
client = new RelayClient({ url: "http://localhost:36573", adapter, routes: relay.routes });
|
||||
client = relay.client({
|
||||
adapter: new HttpAdapter("http://localhost:8080"),
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -31,16 +33,16 @@ describe("Relay", () => {
|
||||
});
|
||||
|
||||
it("should successfully relay users", async () => {
|
||||
const userId = await client.post("/users", { name: "John Doe", email: "john.doe@fixture.none" });
|
||||
const userId = await client.user.create({ name: "John Doe", email: "john.doe@fixture.none" });
|
||||
|
||||
assertEquals(typeof userId, "string");
|
||||
assertEquals(users.length, 1);
|
||||
|
||||
const user = await client.get("/users/:userId", { userId });
|
||||
const user = await client.user.get(userId);
|
||||
|
||||
assertEquals(user.createdAt instanceof Date, true);
|
||||
|
||||
await client.put("/users/:userId", { userId }, { name: "Jane Doe", email: "jane.doe@fixture.none" });
|
||||
await client.user.update([userId, { name: "Jane Doe", email: "jane.doe@fixture.none" }]);
|
||||
|
||||
assertEquals(users.length, 1);
|
||||
assertObjectMatch(users[0], {
|
||||
@@ -48,16 +50,16 @@ describe("Relay", () => {
|
||||
email: "jane.doe@fixture.none",
|
||||
});
|
||||
|
||||
await client.delete("/users/:userId", { userId });
|
||||
await client.user.delete(userId);
|
||||
|
||||
assertEquals(users.length, 0);
|
||||
});
|
||||
|
||||
it("should successfully run .actions", async () => {
|
||||
assertEquals(await client.get("/add-two", { a: 1, b: 1 }), 2);
|
||||
assertEquals(await client.numbers.add([1, 1]), 2);
|
||||
});
|
||||
|
||||
it("should reject .actions with error", async () => {
|
||||
await assertRejects(() => client.get("/add-two", { a: -1, b: 1 }), "Invalid input numbers added");
|
||||
await assertRejects(() => client.numbers.add([-1, 1]), "Invalid input numbers added");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user