feat: split client to separate class
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import z from "zod";
|
||||
|
||||
import { adapter } from "../../adapters/http.ts";
|
||||
import { Relay } from "../../libraries/relay.ts";
|
||||
import { route } from "../../libraries/route.ts";
|
||||
import { UserSchema } from "./user.ts";
|
||||
|
||||
export const relay = new Relay({ url: "http://localhost:36573", adapter }, [
|
||||
export const relay = new Relay([
|
||||
route
|
||||
.post("/users")
|
||||
.body(UserSchema.omit({ id: true, createdAt: true }))
|
||||
@@ -21,3 +20,5 @@ export const relay = new Relay({ url: "http://localhost:36573", adapter }, [
|
||||
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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Api } from "../../libraries/api.ts";
|
||||
import { RelayAPI } from "../../libraries/api.ts";
|
||||
import { NotFoundError } from "../../mod.ts";
|
||||
import { addTwoNumbers } from "./actions.ts";
|
||||
import { relay } from "./relay.ts";
|
||||
@@ -6,33 +6,35 @@ import { User } from "./user.ts";
|
||||
|
||||
export let users: User[] = [];
|
||||
|
||||
export const api = new Api([
|
||||
relay.route("POST", "/users").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 }) => {
|
||||
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 }) => {
|
||||
for (const user of users) {
|
||||
if (user.id === userId) {
|
||||
user.name = name;
|
||||
user.email = email;
|
||||
break;
|
||||
export const api = new RelayAPI({
|
||||
routes: [
|
||||
relay.route("POST", "/users").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 }) => {
|
||||
const user = users.find((user) => user.id === userId);
|
||||
if (user === undefined) {
|
||||
return new NotFoundError();
|
||||
}
|
||||
}
|
||||
}),
|
||||
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),
|
||||
]);
|
||||
return user;
|
||||
}),
|
||||
relay.route("PUT", "/users/:userId").handle(async ({ userId, name, email }) => {
|
||||
for (const user of users) {
|
||||
if (user.id === userId) {
|
||||
user.name = name;
|
||||
user.email = email;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}),
|
||||
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),
|
||||
],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user