import {z} from 'zod' export type GetApiV1SigmaScheduledQueryRunsScheduledQueryRunArgs = {expand?: Array | undefined, scheduled_query_run: string, body: Record}; export type GetApiV1SigmaScheduledQueryRunsScheduledQueryRunOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const sigmaScheduledQueryRunError = z.object({message: z.string().max(5000)}); export const fileLink = z.object({created: z.number().int(), expired: z.boolean(), expires_at: z.number().int().nullable().optional(), file: z.union([z.string().max(5000), z.lazy(() => file)]), id: z.string().max(5000), livemode: z.boolean(), metadata: z.record(z.string(), z.string().max(500)), object: z.literal("file_link"), url: z.string().max(5000).nullable().optional()}); export const file: z.ZodType = z.object({created: z.number().int(), expires_at: z.number().int().nullable().optional(), filename: z.string().max(5000).nullable().optional(), id: z.string().max(5000), links: z.object({data: z.array(fileLink), has_more: z.boolean(), object: z.literal("list"), url: z.string().max(5000)}).nullable().optional(), object: z.literal("file"), purpose: z.enum(["account_requirement", "additional_verification", "business_icon", "business_logo", "customer_signature", "dispute_evidence", "document_provider_identity_document", "finance_report_run", "financial_account_statement", "identity_document", "identity_document_downloadable", "issuing_regulatory_reporting", "pci_document", "platform_terms_of_service", "selfie", "sigma_scheduled_query", "tax_document_user_upload", "terminal_android_apk", "terminal_reader_splashscreen", "terminal_wifi_certificate", "terminal_wifi_private_key"]), size: z.number().int(), title: z.string().max(5000).nullable().optional(), type: z.string().max(5000).nullable().optional(), url: z.string().max(5000).nullable().optional()}); export const scheduledQueryRun = z.object({created: z.number().int(), data_load_time: z.number().int(), error: sigmaScheduledQueryRunError.optional(), file: file.nullable().optional(), id: z.string().max(5000), livemode: z.boolean(), object: z.literal("scheduled_query_run"), result_available_until: z.number().int(), sql: z.string().max(100000), status: z.string().max(5000), title: z.string().max(5000)}); 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 }; /** * Retrieve a scheduled query run * * GET /v1/sigma/scheduled_query_runs/{scheduled_query_run} * * Also accepts auth: bearerAuth */ export const getApiV1SigmaScheduledQueryRunsScheduledQueryRun = async (args: GetApiV1SigmaScheduledQueryRunsScheduledQueryRunArgs, options?: GetApiV1SigmaScheduledQueryRunsScheduledQueryRunOptions) => { const {baseUrl = "https://api.stripe.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") for (const item of args.expand ?? []) query.append("expand", String(item)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sigma/scheduled_query_runs/${encodeURIComponent(String(args.scheduled_query_run))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(scheduledQueryRun, await res.json()) };