import {z} from 'zod' export type AccountV1LoginRequest = {accountType?: number | undefined, password?: string | undefined, portal?: string | undefined, rememberMe?: boolean | undefined, username?: string | undefined}; export type CreateApiV1AccountLoginArgs = {body: AccountV1LoginRequest}; export type CreateApiV1AccountLoginOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const accountV1AuthTokens = z.object({accessToken: z.string().optional(), expiresAt: z.string().optional(), refreshToken: z.string().optional(), tokenType: z.string().optional()}); export const accountV1UserInfo = z.object({accountType: z.number().int().optional(), allowTokenBindSource: z.boolean().optional(), avatarUrl: z.string().optional(), createdAt: z.string().optional(), email: z.string().optional(), hasPassword: z.boolean().optional(), inviteCode: z.string().optional(), locale: z.string().optional(), nickname: z.string().optional(), permissions: z.array(z.string()).optional(), phone: z.string().optional(), realnameRequired: z.boolean().optional(), realnameStatus: z.string().optional(), registeredAt: z.string().optional(), timezone: z.string().optional(), twoFaEnabled: z.boolean().optional(), userId: z.string().optional(), username: z.string().optional()}); export const accountV1LoginReply = z.object({auth: accountV1AuthTokens.optional(), twoFaRequired: z.boolean().optional(), twoFaToken: z.string().optional(), user: accountV1UserInfo.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Login 账号登录,成功返回 access_token + refresh_token;若开启 2FA 返回 two_fa_required=true * * POST /v1/account/login */ export const createApiV1AccountLogin = async (args: CreateApiV1AccountLoginArgs, options: CreateApiV1AccountLoginOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/account/login${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(accountV1LoginReply, await res.json()) };