feat: update tests
This commit is contained in:
@@ -5,19 +5,18 @@ import { Relay } from "../../libraries/relay.ts";
|
||||
import { route } from "../../libraries/route.ts";
|
||||
import { UserSchema } from "./user.ts";
|
||||
|
||||
export const relay = new Relay({ adapter: http }, [
|
||||
export const relay = new Relay({ url: "http://localhost:36573", adapter: http }, [
|
||||
route
|
||||
.post("/users")
|
||||
.body(UserSchema.omit({ id: true }))
|
||||
.body(UserSchema.omit({ id: true, createdAt: true }))
|
||||
.response(z.string()),
|
||||
route.get("/users").response(z.array(UserSchema)),
|
||||
route
|
||||
.get("/users/:userId")
|
||||
.params({ userId: z.string().check(z.uuid()) })
|
||||
.response(UserSchema.or(z.undefined())),
|
||||
.response(UserSchema),
|
||||
route
|
||||
.put("/users/:userId")
|
||||
.params({ userId: z.string().check(z.uuid()) })
|
||||
.body(UserSchema.omit({ id: true })),
|
||||
.body(UserSchema.omit({ id: true, createdAt: true })),
|
||||
route.delete("/users/:userId").params({ userId: z.string().check(z.uuid()) }),
|
||||
]);
|
||||
|
||||
@@ -1,32 +1,33 @@
|
||||
import { Api } from "../../libraries/api.ts";
|
||||
import { NotFoundError } from "../../mod.ts";
|
||||
import { relay } from "./relay.ts";
|
||||
import { User } from "./user.ts";
|
||||
|
||||
export let users: User[] = [];
|
||||
|
||||
relay.route("POST", "/users").handle(async ({ name, email }) => {
|
||||
const id = crypto.randomUUID();
|
||||
users.push({ id, name, email });
|
||||
return id;
|
||||
});
|
||||
|
||||
relay.route("GET", "/users").handle(async () => {
|
||||
return users;
|
||||
});
|
||||
|
||||
relay.route("GET", "/users/:userId").handle(async ({ userId }) => {
|
||||
return users.find((user) => user.id === userId);
|
||||
});
|
||||
|
||||
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 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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
relay.route("DELETE", "/users/:userId").handle(async ({ userId }) => {
|
||||
users = users.filter((user) => user.id === userId);
|
||||
});
|
||||
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);
|
||||
}),
|
||||
]);
|
||||
|
||||
@@ -4,6 +4,7 @@ export const UserSchema = z.object({
|
||||
id: z.string().check(z.uuid()),
|
||||
name: z.string(),
|
||||
email: z.string().check(z.email()),
|
||||
createdAt: z.coerce.date(),
|
||||
});
|
||||
|
||||
export type User = z.infer<typeof UserSchema>;
|
||||
|
||||
Reference in New Issue
Block a user