Template
1
0

feat: split client to separate class

This commit is contained in:
2025-04-20 11:56:40 +00:00
parent 2e8d6b76d7
commit 221452893e
11 changed files with 220 additions and 195 deletions

View File

@@ -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),
],
});