feat: refactor account
This commit is contained in:
23
spec/modules/account/account.ts
Normal file
23
spec/modules/account/account.ts
Normal 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>;
|
||||
5
spec/modules/account/mod.ts
Normal file
5
spec/modules/account/mod.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { create } from "./routes/create.ts";
|
||||
|
||||
export const routes = {
|
||||
create,
|
||||
};
|
||||
5
spec/modules/account/routes/create.ts
Normal file
5
spec/modules/account/routes/create.ts
Normal 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 }));
|
||||
33
spec/modules/account/strategies.ts
Normal file
33
spec/modules/account/strategies.ts
Normal 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>;
|
||||
Reference in New Issue
Block a user