import {z} from 'zod' export type EdgeScriptTypes = 0 | 1 | 2; export type SourceCodeRepositorySettingsModel = {Id?: (number | null) | undefined, Name?: (string | null) | undefined, Private?: boolean | undefined, TemplateUrl?: (string | null) | undefined}; export type DeployConfigurationModel = {Branch?: (string | null) | undefined, InstallCommand?: (string | null) | undefined, BuildCommand?: (string | null) | undefined, EntryFile?: (string | null) | undefined, CreateWorkflow?: boolean | undefined}; export type SourceCodeIntegrationModel = {IntegrationId?: number | undefined, RepositorySettings?: (SourceCodeRepositorySettingsModel | null) | undefined, DeployConfiguration?: (DeployConfigurationModel | null) | undefined}; export type AddEdgeScriptModel = {Name?: (string | null) | undefined, Code?: (string | null) | undefined, ScriptType?: EdgeScriptTypes | undefined, CreateLinkedPullZone?: boolean | undefined, LinkedPullZoneName?: (string | null) | undefined, Integration?: (SourceCodeIntegrationModel | null) | undefined}; export type CreateApiComputeScriptArgs = {body: AddEdgeScriptModel}; export type CreateApiComputeScriptOptions = {baseUrl?: string | undefined, AccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const edgeScriptTypes = z.union([z.literal(0), z.literal(1), z.literal(2)]); export const edgeScriptVariableModel = z.object({Id: z.number().int().optional(), Name: z.string().nullable().optional(), Required: z.boolean().optional(), DefaultValue: z.string().nullable().optional()}); export const linkedPullZone = z.object({Id: z.number().int().optional(), PullZoneName: z.string().nullable().optional(), DefaultHostname: z.string().nullable().optional()}); export const sourceCodeRepositorySettingsModel = z.object({Id: z.number().int().nullable().optional(), Name: z.string().nullable().optional(), Private: z.boolean().optional(), TemplateUrl: z.string().nullable().optional()}); export const deployConfigurationModel = z.object({Branch: z.string().nullable().optional(), InstallCommand: z.string().nullable().optional(), BuildCommand: z.string().nullable().optional(), EntryFile: z.string().nullable().optional(), CreateWorkflow: z.boolean().optional()}); export const sourceCodeIntegrationModel = z.object({IntegrationId: z.number().int().optional(), RepositorySettings: sourceCodeRepositorySettingsModel.nullable().optional(), DeployConfiguration: deployConfigurationModel.nullable().optional()}); export const edgeScriptModel = z.object({Id: z.number().int().optional(), Name: z.string().nullable().optional(), LastModified: z.string().optional(), ScriptType: edgeScriptTypes.optional(), CurrentReleaseId: z.number().int().optional(), EdgeScriptVariables: z.array(edgeScriptVariableModel).nullable().optional(), Deleted: z.boolean().optional(), LinkedPullZones: z.array(linkedPullZone).nullable().optional(), Integration: sourceCodeIntegrationModel.nullable().optional(), DefaultHostname: z.string().nullable().optional(), SystemHostname: z.string().nullable().optional(), DeploymentKey: z.string().nullable().optional(), RepositoryId: z.number().int().nullable().optional(), IntegrationId: z.number().int().nullable().optional(), MonthlyCost: z.number().optional(), MonthlyRequestCount: z.number().int().optional(), MonthlyCpuTime: z.number().int().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 }; /** * Add Edge Script * * POST /compute/script * * Also accepts auth: bearer */ export const createApiComputeScript = async (args: CreateApiComputeScriptArgs, options?: CreateApiComputeScriptOptions) => { const {baseUrl = "https://api.bunny.net", fetch: fetchFn = fetch, AccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AccessKey !== undefined) headers.set("AccessKey", AccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/compute/script${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(edgeScriptModel, await res.json()) };