import {z} from 'zod' export type GetApiDataCredentialsArgs = {access_role?: ('collaborator' | 'operator' | 'admin' | 'owner') | undefined, Accept?: ('application/vnd.nexla.api.v1+json' | 'application/json') | undefined, credentials_type?: string | undefined}; export type GetApiDataCredentialsOptions = {baseUrl?: string | undefined, NexlaSessionToken?: string | undefined, fetch?: typeof fetch | undefined}; export const owner = z.object({id: z.number().int().optional(), full_name: z.string().optional(), email: z.string().optional()}); export const org = z.object({id: z.number().int().optional(), name: z.string().optional(), email_domain: z.string().optional(), email: z.unknown(), client_identifier: z.unknown()}); export const accessRoles = z.array(z.enum(["owner", "collaborator", "operator", "admin"])); export const getApiDataCredentialsResponse = z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().nullable().optional(), owner: owner.optional(), org: org.optional(), access_roles: accessRoles.optional(), credentials_version: z.string().optional(), credentials_type: z.string().optional(), connector: z.object({id: z.number().int().optional(), type: z.string().optional(), connection_type: z.string().optional(), name: z.string().optional(), description: z.string().optional(), nexset_api_compatible: z.boolean().optional()}).optional(), verified_status: z.string().optional(), verified_at: z.string().optional(), copied_from_id: z.number().int().nullable().optional(), updated_at: z.string().optional(), created_at: z.string().optional(), vendor: z.object({id: z.number().int().optional(), name: z.string().optional(), display_name: z.string().optional(), connection_type: z.string().optional()}).optional(), template_config: z.object({}).nullable().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 }; /** * Get All Credentials * * GET /data_credentials */ export const getApiDataCredentials = async (args: GetApiDataCredentialsArgs, options?: GetApiDataCredentialsOptions) => { const {baseUrl = "https://dataops.nexla.io/nexla-api", fetch: fetchFn = fetch, NexlaSessionToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.access_role != null) query.append("access_role", String(args.access_role)) if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args.credentials_type != null) query.append("credentials_type", String(args.credentials_type)) if (NexlaSessionToken !== undefined) headers.set('authorization', `Bearer ${NexlaSessionToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/data_credentials${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(getApiDataCredentialsResponse, await res.json()) };