import {z} from 'zod' export type GetApiCompaniesCompanyIdDataHistoryDatasetIdOptions = {baseUrl?: string | undefined, authHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTime = z.string(); export const pullOperation = z.object({id: z.string(), companyId: z.string(), connectionId: z.string(), dataType: z.string(), status: z.enum(["Initial", "Queued", "Fetching", "MapQueued", "Mapping", "Complete", "FetchError", "MapError", "InternalError", "ProcessingQueued", "Processing", "ProcessingError", "ValidationQueued", "Validating", "ValidationError", "AuthError", "Cancelled", "NotSupported", "RateLimitError", "PermissionsError", "PrerequisiteNotMet"]), statusDescription: z.string().nullable().optional(), errorMessage: z.string().nullable().optional(), requested: dateTime, completed: dateTime.optional(), progress: z.number().int(), isCompleted: z.boolean(), isErrored: z.boolean()}); 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 pull operation * * GET /companies/{companyId}/data/history/{datasetId} */ export const getApiCompaniesCompanyIdDataHistoryDatasetId = async (options?: GetApiCompaniesCompanyIdDataHistoryDatasetIdOptions) => { const {baseUrl = "https://api.codat.io", fetch: fetchFn = fetch, authHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (authHeader !== undefined) headers.set("Authorization", authHeader) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companyId))}/data/history/${encodeURIComponent(String(args.datasetId))}${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(pullOperation, await res.json()) };