import {z} from 'zod' export type GetApiV1RecommendationsArgs = {limit?: number | undefined, cursor?: string | undefined, q?: string | undefined, recommendationId?: Array | undefined, resourceId?: Array | undefined, recommendationName?: Array | undefined, category?: Array | undefined, accountId?: Array | undefined, region?: Array | undefined, service?: Array | undefined, status?: ('open' | 'dismissed' | 'all') | undefined, startDate?: string | undefined, endDate?: string | undefined}; export type GetApiV1RecommendationsOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({limit: z.number().int(), has_more: z.boolean(), next_cursor: z.string().nullable(), total: z.number().int().optional()}); export const getApiV1RecommendationsResponse = z.object({data: z.array(z.record(z.string(), z.unknown())), pagination: pagination}); 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 recommendations * * GET /v1/recommendations */ export const getApiV1Recommendations = async (args: GetApiV1RecommendationsArgs, options?: GetApiV1RecommendationsOptions) => { const {baseUrl = "https://api.nops.io", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.q != null) query.append("q", String(args.q)) for (const item of args.recommendationId ?? []) query.append("recommendationId", String(item)) for (const item of args.resourceId ?? []) query.append("resourceId", String(item)) for (const item of args.recommendationName ?? []) query.append("recommendationName", String(item)) for (const item of args.category ?? []) query.append("category", String(item)) for (const item of args.accountId ?? []) query.append("accountId", String(item)) for (const item of args.region ?? []) query.append("region", String(item)) for (const item of args.service ?? []) query.append("service", String(item)) if (args.status != null) query.append("status", String(args.status)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/recommendations${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(getApiV1RecommendationsResponse, await res.json()) };