import {z} from 'zod' export type AbstractFunctionParameter = Record; export type AbstractFunctionRequest = {functionId?: string | undefined, title?: string | undefined, description?: string | undefined, className?: string | undefined, parameters?: {[key: string]: AbstractFunctionParameter} | undefined, exits?: ({TRUE?: string | undefined, FALSE?: string | undefined, VALID?: string | undefined, INVALID?: string | undefined, NEXT?: string | undefined, TERMINATE?: string | undefined, ERROR?: string | undefined}) | undefined}; export type AbstractGlobalParameter = {name?: string | undefined, type: string, constraints?: {[key: string]: string} | undefined}; export type WorkflowTrigger = {enabled?: boolean | undefined, firstFunction?: string | undefined}; export type LookupType = 'CALCULATION' | 'STATIC' | 'LOOKUP'; export type AbstractLookupResponse = {destinationField: string, type: LookupType}; export type TransformationExportPayload = {orgId: string, transformationId: string, name: string, code: string, description: string, sourceSystem: string, destinationSystem: string, sourceTable: string, destinationTable: string, transformations: Array}; export type DestinationType = 'WEBHOOK' | 'NETSUITE' | 'STRIPE' | 'STRIPE_TEST' | 'AWS' | 'PADDLE' | 'PADDLE_SANDBOX' | 'SALESFORCE' | 'XERO' | 'CHARGEBEE' | 'QUICKBOOKS' | 'QUICKBOOKS_SANDBOX' | 'M3TER'; export type AuthType = 'OAUTH_CODE' | 'OAUTH_CLIENT_CREDENTIALS' | 'HTTP_BASIC' | 'M3TER_SIGNED_REQUEST' | 'AWS_INTEGRATION' | 'PADDLE' | 'NETSUITE' | 'CHARGEBEE' | 'M3TER_APP_SIGNATURE' | 'M3TER_SERVICE_USER' | 'STRIPE_SIGNED_REQUEST' | 'OPSGENIE_KEY' | 'SAP_BYD' | 'HUBSPOT_ACCESS_TOKEN' | 'HUBSPOT_CLIENT_SECRET' | 'SLACK_WEBHOOK' | 'SAGE_INTACCT_CLIENT_CREDENTIALS' | 'SAGE_INTACCT_CLIENT_SECRET' | 'STATIC_API_KEY' | 'GCP_WIF' | 'SNS_REQUEST'; export type CredentialsExportPayload = {credentialsId: string, name: string, destinationType: DestinationType, type: AuthType}; export type WebhookExportPayload = {webhookDestinationId: string, webhookUrl: string}; export type WorkflowPayload = {name: string, code: string, description: string, originalOrgId: string, version: number, versionDescription: string, functions: Array, globalParameters: Array, trigger: WorkflowTrigger, transformations: Array, credentials: Array, webhooks: Array}; export type CreateApiOrganizationsOrgIdIntegrationdestinationsWorkflowsImportArgs = {orgId: string, body: WorkflowPayload}; export type CreateApiOrganizationsOrgIdIntegrationdestinationsWorkflowsImportOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowTrigger = z.object({enabled: z.boolean().optional(), firstFunction: z.string().optional()}); export const abstractFunctionParameter = z.object({}); export const abstractFunctionRequest = z.object({functionId: z.string().optional(), title: z.string().optional(), description: z.string().optional(), className: z.string().optional(), parameters: z.record(z.string(), abstractFunctionParameter).optional(), exits: z.object({TRUE: z.string().optional(), FALSE: z.string().optional(), VALID: z.string().optional(), INVALID: z.string().optional(), NEXT: z.string().optional(), TERMINATE: z.string().optional(), ERROR: z.string().optional()}).optional()}); export const abstractGlobalParameter = z.object({name: z.string().optional(), type: z.string(), constraints: z.record(z.string(), z.string()).optional()}); export const versionDetailResponse = z.object({version: z.number().int().optional(), description: z.string().optional(), dtCreated: z.string().optional(), createdBy: z.string().optional(), draftVersion: z.string().optional(), templateId: z.string().optional(), platform: z.boolean().optional(), templateVersion: z.number().int().optional()}); export const workflowDestinationResponse = z.object({trigger: workflowTrigger.optional(), functions: z.array(abstractFunctionRequest).optional(), globalParameters: z.array(abstractGlobalParameter).optional(), templateOverrides: z.record(z.string(), z.record(z.string(), abstractFunctionParameter)).optional(), enabled: z.boolean().optional(), validationErrors: z.record(z.string(), z.array(z.string())).optional(), currentVersion: z.number().int().optional(), workflowVersion: z.number().int().optional(), draft: z.boolean().optional(), useDraftVersion: z.boolean().optional(), versions: z.array(versionDetailResponse).optional(), drafts: z.array(versionDetailResponse).optional(), id: z.string(), version: z.number().int().optional(), dtCreated: z.string().optional(), dtLastModified: z.string().optional(), createdBy: z.string().optional(), lastModifiedBy: z.string().optional(), name: z.string().optional(), description: z.string().optional(), code: z.string().optional(), active: z.boolean().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 }; /** * Export Workflow Version * * POST /organizations/{orgId}/integrationdestinations/workflows/import */ export const createApiOrganizationsOrgIdIntegrationdestinationsWorkflowsImport = async (args: CreateApiOrganizationsOrgIdIntegrationdestinationsWorkflowsImportArgs, options?: CreateApiOrganizationsOrgIdIntegrationdestinationsWorkflowsImportOptions) => { const {baseUrl = "https://api.m3ter.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.orgId))}/integrationdestinations/workflows/import${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(workflowDestinationResponse, await res.json()) };