import {z} from 'zod' export type MagicLinkValidateSchema = {app_id?: string | undefined, email: string, hash: string, password?: string | undefined, token?: string | undefined}; export type CreateApiV1SharesShareIdMagicLinkValidateArgs = {share_id: string, body: MagicLinkValidateSchema}; export type CreateApiV1SharesShareIdMagicLinkValidateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const shareTokenSchema = z.object({app_id: z.string(), drm: z.enum(["none", "standard"]), email: z.string(), expires: z.string(), is_internal_user: z.boolean(), object_id: z.string(), object_type: z.string(), roles: z.array(z.string()), share_id: z.string(), share_session_id: z.string().nullable().optional(), share_user_id: z.string(), system_domain_id: z.string(), token: z.string(), user_id: z.string(), watermark: z.enum(["none", "standard"])}); 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 }; /** * Validates the email and single-use hash, and returns a share authorization token. * * POST /v1/shares/{share_id}/magic_link/validate/ */ export const createApiV1SharesShareIdMagicLinkValidate = async (args: CreateApiV1SharesShareIdMagicLinkValidateArgs, options: CreateApiV1SharesShareIdMagicLinkValidateOptions) => { 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/shares/${encodeURIComponent(String(args.share_id))}/magic_link/validate/${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(shareTokenSchema, await res.json()) };