import {z} from 'zod' export type GetApiApiDeploymentsOptions = {baseUrl: string, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiDeploymentsResponse = z.array(z.object({id: z.string(), name: z.string(), description: z.string().optional(), provider: z.enum(["aws_s3", "azure_storage", "google_cloud_storage"]), configuration: z.unknown(), organisationId: z.string(), organisation: z.object({id: z.string(), name: z.string(), displayName: z.string(), onboarded: z.boolean(), license: z.string().optional(), licenseData: z.unknown(), url: z.string().optional(), themeLogo: z.string(), themePrimaryColor: z.string(), preferences: z.unknown(), createdAt: z.string(), updatedAt: z.string(), deletedAt: z.string().optional()}), createdAt: z.string(), updatedAt: z.string(), deletedAt: z.string().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 }; /** * List deployments * * GET /api/deployments * * Also accepts auth: internalAuth */ export const getApiApiDeployments = async (options: GetApiApiDeploymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, personalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (personalAccessToken !== undefined) headers.set('authorization', `Bearer ${personalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/deployments${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(getApiApiDeploymentsResponse, await res.json()) };