import {z} from 'zod' export type JsonNode = {empty?: boolean | undefined, array?: boolean | undefined, null?: boolean | undefined, object?: boolean | undefined, float?: boolean | undefined, string?: boolean | undefined, number?: boolean | undefined, missingNode?: boolean | undefined, valueNode?: boolean | undefined, nodeType?: ('ARRAY' | 'BINARY' | 'BOOLEAN' | 'MISSING' | 'NULL' | 'NUMBER' | 'OBJECT' | 'POJO' | 'STRING') | undefined, pojo?: boolean | undefined, integralNumber?: boolean | undefined, floatingPointNumber?: boolean | undefined, short?: boolean | undefined, int?: boolean | undefined, long?: boolean | undefined, double?: boolean | undefined, bigDecimal?: boolean | undefined, bigInteger?: boolean | undefined, textual?: boolean | undefined, boolean?: boolean | undefined, binary?: boolean | undefined, container?: boolean | undefined, embeddedValue?: boolean | undefined} | null; export type IntegrationPostDto = {status?: ('ACTIVE' | 'INACTIVE' | 'REMOVED') | undefined, apiName?: ('BOX' | 'CLOVER' | 'STRIPE' | 'PLAID' | 'KNOX' | 'UBER' | 'FRESHBOOKS' | 'HARVEST' | 'KW' | 'FRESHBOOKS2' | 'CAPITAL_ONE' | 'SQUARE' | 'DOTLOOP' | 'REALOGY' | 'UPWORK' | 'SHOPIFY' | 'DEDUCTR' | 'ENTERPRISE' | 'MOXIWORKS' | 'APPDIRECT' | 'LONEWOLF' | 'LONEWOLF_TRANSACTIONDESK' | 'SAMPLE_BANK' | 'TAXBOT' | 'GUSTO' | 'XERO' | 'UNIT' | 'MOOV' | 'QBO' | 'QUALPAY' | 'BENCH' | 'PARTNER_IMPLEMENTATION' | 'CHECK' | 'RUTTER' | 'WAVE' | 'MANUAL_ENTRY' | 'CSV_IMPORT' | 'ADYEN' | 'PAYABLI') | undefined, apiAccessToken?: string | undefined, customData?: JsonNode | undefined}; export type CreateApiV6IntegrationsIdArgs = {id: string, body: IntegrationPostDto}; export type CreateApiV6IntegrationsIdOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const integrationGetDto = z.object({id: z.string(), status: z.enum(["ACTIVE", "INACTIVE", "REMOVED"]), apiName: z.enum(["BOX", "CLOVER", "STRIPE", "PLAID", "KNOX", "UBER", "FRESHBOOKS", "HARVEST", "KW", "FRESHBOOKS2", "CAPITAL_ONE", "SQUARE", "DOTLOOP", "REALOGY", "UPWORK", "SHOPIFY", "DEDUCTR", "ENTERPRISE", "MOXIWORKS", "APPDIRECT", "LONEWOLF", "LONEWOLF_TRANSACTIONDESK", "SAMPLE_BANK", "TAXBOT", "GUSTO", "XERO", "UNIT", "MOOV", "QBO", "QUALPAY", "BENCH", "PARTNER_IMPLEMENTATION", "CHECK", "RUTTER", "WAVE", "MANUAL_ENTRY", "CSV_IMPORT", "ADYEN", "PAYABLI"]), iconUrl: z.string().nullable(), apiUsername: z.string().nullable().optional(), lastSyncedDate: z.string().nullable().optional(), apiAccessToken: z.string(), apiInstitutionId: z.string().nullable().optional(), apiInstitutionName: z.string().nullable().optional(), apiErrorType: z.string().nullable().optional(), apiErrorCode: z.string().nullable().optional(), additionalConsentRequired: z.boolean()}); export const successResponseIntegrationGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: integrationGetDto.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 }; /** * Save integration * * POST /v6/integrations/{id} */ export const createApiV6IntegrationsId = async (args: CreateApiV6IntegrationsIdArgs, options?: CreateApiV6IntegrationsIdOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, EmbedToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (EmbedToken !== undefined) headers.set('authorization', `Bearer ${EmbedToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/integrations/${encodeURIComponent(String(args.id))}${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(successResponseIntegrationGetDto, await res.json()) };