import {z} from 'zod' export type GetApiMagicLinksArgs = {id_in_provider_system?: string | undefined, page_size?: number | undefined, order?: ('asc' | 'desc') | undefined, cursor?: string | undefined}; export type GetApiMagicLinksOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const magicLinkResource = z.object({id: z.string(), name: z.string(), products: z.array(z.string()), id_in_provider_system: z.string(), recipient_id: z.string().optional(), host: z.string(), link: z.string(), has_been_used: z.boolean(), created_at: z.string(), bucket_name: z.string(), vendor: z.string(), available_models: z.array(z.string()), bucket_access_id: z.string().optional(), bucket_vendor: z.string().optional(), ssh_public_key: z.string().optional(), redirect_url: z.string().optional(), set_destination_enabled: z.boolean(), metastore: z.string().optional(), catalog: z.string().optional(), bucket_host: z.string().optional(), table_bucket_arn: z.string().optional()}); export const listMagicLinkResponse = z.object({magic_links: z.array(magicLinkResource)}); export const getApiMagicLinksResponse = z.object({status: z.literal("success"), data: listMagicLinkResponse, message: z.string(), has_next: z.boolean(), next_url: z.string().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 }; /** * List magic links * * GET /magic-links */ export const getApiMagicLinks = async (args: GetApiMagicLinksArgs, options?: GetApiMagicLinksOptions) => { const {baseUrl = "https://api.prequel.co", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id_in_provider_system != null) query.append("id_in_provider_system", String(args.id_in_provider_system)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.order != null) query.append("order", String(args.order)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/magic-links${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiMagicLinksResponse, await res.json()) };