Template
1
0

feat: refactor account

This commit is contained in:
2025-08-12 05:24:20 +02:00
parent 1215a98afc
commit f0630d43b7
25 changed files with 256 additions and 332 deletions

View File

@@ -0,0 +1,23 @@
import { AvatarSchema, ContactSchema, makeSchemaParser, NameSchema } from "@spec/shared";
import { z } from "zod";
import { RoleSchema } from "../access/role.ts";
import { StrategySchema } from "./strategies.ts";
export const AccountSchema = z.object({
id: z.uuid(),
avatar: AvatarSchema.optional(),
name: NameSchema.optional(),
contact: ContactSchema.default({
emails: [],
}),
strategies: z.array(StrategySchema).default([]),
roles: z.array(RoleSchema).default([]),
});
export const AccountDocumentSchema = AccountSchema.omit({ roles: true }).extend({ roles: z.string().array() });
export const parseAccount = makeSchemaParser(AccountSchema);
export type Account = z.infer<typeof AccountSchema>;
export type AccountDocument = z.infer<typeof AccountDocumentSchema>;

View File

@@ -0,0 +1,5 @@
import { create } from "./routes/create.ts";
export const routes = {
create,
};

View File

@@ -0,0 +1,5 @@
import { route } from "@spec/relay";
import { NameSchema } from "@spec/shared";
import z from "zod";
export const create = route.post("/api/v1/accounts").body(z.object({ name: NameSchema }));

View File

@@ -0,0 +1,33 @@
import z from "zod";
const EmailStrategySchema = z.object({
type: z.literal("email"),
value: z.string(),
});
const PasswordStrategySchema = z.object({
type: z.literal("password"),
alias: z.string(),
password: z.string(),
});
const PasskeyStrategySchema = z.object({
type: z.literal("passkey"),
credId: z.string(),
credPublicKey: z.string(),
webauthnUserId: z.string(),
counter: z.number(),
backupEligible: z.boolean(),
backupStatus: z.boolean(),
transports: z.string(),
createdAt: z.date(),
lastUsed: z.date(),
});
export const StrategySchema = z.discriminatedUnion("type", [
EmailStrategySchema,
PasswordStrategySchema,
PasskeyStrategySchema,
]);
export type Strategy = z.infer<typeof StrategySchema>;