import {z} from 'zod' export type UserSimple = {'avatar-thumb-url'?: unknown | undefined, 'created-at'?: unknown | undefined, email: unknown, 'employee-number'?: unknown | undefined, firstname: unknown, fullname?: unknown | undefined, id?: number | undefined, lastname: unknown, login: unknown, 'salesforce-id': unknown, 'updated-at'?: unknown | undefined}; export type UploadError = {'created-at'?: unknown | undefined, 'created-by'?: UserSimple | undefined, 'field-errors': unknown, 'field-with-error'?: unknown | undefined, 'first-column'?: unknown | undefined, id?: number | undefined, row?: number | undefined, type?: unknown | undefined, 'updated-at'?: unknown | undefined, 'updated-by'?: UserSimple | undefined, value?: unknown | undefined}; export type DataFileSource = {'created-at'?: unknown | undefined, 'created-by'?: UserSimple | undefined, 'end-time'?: unknown | undefined, 'file-content-type'?: unknown | undefined, 'file-file-name'?: unknown | undefined, 'file-file-size'?: unknown | undefined, id?: number | undefined, 'integration-id'?: number | undefined, 'integration-run-id'?: number | undefined, progress?: number | undefined, 'records-count'?: number | undefined, 'records-created'?: number | undefined, 'records-updated'?: number | undefined, 'result-text'?: unknown | undefined, 'source-for': unknown, 'start-time'?: unknown | undefined, status?: unknown | undefined, type?: unknown | undefined, 'updated-at'?: unknown | undefined, 'updated-by'?: UserSimple | undefined, 'upload-errors'?: Array | undefined, 'upload-errors-count'?: number | undefined}; export type CreateApiDataFileSourcesLoadFromSftpArgs = {return_object?: ('limited' | 'shallow' | 'none') | undefined, filter?: string | undefined, body: DataFileSource}; export type CreateApiDataFileSourcesLoadFromSftpOptions = {baseUrl?: string | undefined, OAuth2AccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const userSimple = z.object({"avatar-thumb-url": z.string().optional(), "created-at": z.string().optional(), email: z.string(), "employee-number": z.string().optional(), firstname: z.string(), fullname: z.string().optional(), id: z.number().int().optional(), lastname: z.string(), login: z.string(), "salesforce-id": z.string(), "updated-at": z.string().optional()}); export const uploadError = z.object({"created-at": z.string().optional(), "created-by": userSimple.optional(), "field-errors": z.string(), "field-with-error": z.string().optional(), "first-column": z.string().optional(), id: z.number().int().optional(), row: z.number().int().optional(), type: z.string().optional(), "updated-at": z.string().optional(), "updated-by": userSimple.optional(), value: z.string().optional()}); export const dataFileSource = z.object({"created-at": z.string().optional(), "created-by": userSimple.optional(), "end-time": z.string().optional(), "file-content-type": z.string().optional(), "file-file-name": z.string().optional(), "file-file-size": z.string().optional(), id: z.number().int().optional(), "integration-id": z.number().int().optional(), "integration-run-id": z.number().int().optional(), progress: z.number().int().optional(), "records-count": z.number().int().optional(), "records-created": z.number().int().optional(), "records-updated": z.number().int().optional(), "result-text": z.string().optional(), "source-for": z.string(), "start-time": z.string().optional(), status: z.string().optional(), type: z.string().optional(), "updated-at": z.string().optional(), "updated-by": userSimple.optional(), "upload-errors": z.array(uploadError).optional(), "upload-errors-count": z.number().int().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 }; /** * Upload flat file and process via data file source * * POST /data_file_sources/load_from_sftp * * Also accepts auth: OAuth2ClientCredential */ export const createApiDataFileSourcesLoadFromSftp = async (args: CreateApiDataFileSourcesLoadFromSftpArgs, options?: CreateApiDataFileSourcesLoadFromSftpOptions) => { const {baseUrl = "https://r38.coupadev.com/api", fetch: fetchFn = fetch, OAuth2AccessCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.return_object != null) query.append("return_object", String(args.return_object)) if (args.filter != null) query.append("filter", String(args.filter)) if (OAuth2AccessCode !== undefined) headers.set('authorization', `Bearer ${OAuth2AccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/data_file_sources/load_from_sftp${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dataFileSource, await res.json()) };