import {z} from 'zod' export type GetApiApiV3DatasourcesDatasourceNameExternalAuthLinksArgs = {datasource_name: string, page?: number | undefined, page_size?: number | undefined}; export type GetApiApiV3DatasourcesDatasourceNameExternalAuthLinksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const externalAuthLinkListItem = z.object({id: z.number().int().optional(), data_source: z.string().optional(), data_source_title: z.string().optional(), status: z.enum(["not_used", "used", "expired", "revoked_manually"]).optional(), created_at: z.string().optional(), sent_to_email: z.string().optional(), sender: z.string().optional(), revoked_at: z.string().nullable().optional(), revoked_by: z.string().optional()}); export const paginatedExternalAuthLinkList = z.object({count: z.number().int().optional(), next: z.string().optional(), previous: z.string().optional(), results: z.array(externalAuthLinkListItem).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 external authentication links * * GET /api/v3/datasources/{datasource_name}/external-auth-links * * Also accepts auth: cookieAuth */ export const getApiApiV3DatasourcesDatasourceNameExternalAuthLinks = async (args: GetApiApiV3DatasourcesDatasourceNameExternalAuthLinksArgs, options?: GetApiApiV3DatasourcesDatasourceNameExternalAuthLinksOptions) => { const {baseUrl = "https://embedded.improvado.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/datasources/${encodeURIComponent(String(args.datasource_name))}/external-auth-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(paginatedExternalAuthLinkList, await res.json()) };