import {z} from 'zod' export type GetApiOrganizationsOrgIdIntegrationconfigsAvailableArgs = {orgId: string, systemName?: string | undefined, entityType?: string | undefined}; export type GetApiOrganizationsOrgIdIntegrationconfigsAvailableOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const destinationType = z.enum(["WEBHOOK", "NETSUITE", "STRIPE", "STRIPE_TEST", "AWS", "PADDLE", "PADDLE_SANDBOX", "SALESFORCE", "XERO", "CHARGEBEE", "QUICKBOOKS", "QUICKBOOKS_SANDBOX", "M3TER"]); export const configOptions = z.object({empty: z.boolean().optional()}); export const destinationConfiguration = z.object({apiName: destinationType, destination: z.string(), displayName: z.string(), destinationConfigOptions: configOptions, authConfigOptions: configOptions, released: z.boolean(), authType: z.string(), categories: z.array(z.string())}); export const entityConfiguration = z.object({readableName: z.string(), destinations: z.array(destinationConfiguration), entityConfigurationOptions: configOptions, released: z.boolean()}); export const availableConfigurationResponse = z.object({supportedEntities: z.record(z.string(), entityConfiguration), selectedConfigurations: z.record(z.string(), configOptions), globalConfigurationOptions: configOptions}); 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 Available Configurations * * GET /organizations/{orgId}/integrationconfigs/available */ export const getApiOrganizationsOrgIdIntegrationconfigsAvailable = async (args: GetApiOrganizationsOrgIdIntegrationconfigsAvailableArgs, options?: GetApiOrganizationsOrgIdIntegrationconfigsAvailableOptions) => { const {baseUrl = "https://api.m3ter.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.systemName != null) query.append("systemName", String(args.systemName)) if (args.entityType != null) query.append("entityType", String(args.entityType)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.orgId))}/integrationconfigs/available${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(availableConfigurationResponse, await res.json()) };