import {z} from 'zod' export type GetApiV1IntegrationsTypeArgs = {type: string, pageSize?: number | undefined, skipToken?: string | undefined}; export type GetApiV1IntegrationsTypeOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commonKeyValuePair = z.object({key: z.string(), value: z.string()}); export const integrationsArrayParameter = z.object({key: z.string(), values: z.array(z.string())}); export const integrationsIntegration = z.object({id: z.string(), type: z.string(), displayName: z.string().optional(), clientId: z.string(), parameters: z.array(commonKeyValuePair), arrayParameters: z.array(integrationsArrayParameter), attributes: z.array(commonKeyValuePair), credentialsRefs: z.array(commonKeyValuePair), entityIds: z.array(z.string()).optional()}); export const commonPageInfo = z.object({prevPage: z.string(), nextPage: z.string()}); export const getApiV1IntegrationsTypeResponse = z.object({items: z.array(integrationsIntegration), pageInfo: commonPageInfo}); 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 }; /** * List integration instances by type * * GET /v1/integrations/{type} */ export const getApiV1IntegrationsType = async (args: GetApiV1IntegrationsTypeArgs, options?: GetApiV1IntegrationsTypeOptions) => { const {baseUrl = "https://api.na-01.cloud.solarwinds.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.skipToken != null) query.append("skipToken", String(args.skipToken)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/integrations/${encodeURIComponent(String(args.type))}${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(getApiV1IntegrationsTypeResponse, await res.json()) };