import {z} from 'zod' export type StickySessionSettings = {enabled?: boolean | undefined, sessionHeaders: Array, cookieName?: string | undefined}; export type Protocol = 'tcp' | 'udp' | 'sctp'; export type ContainerPortMappingRequest = {containerPort: number, exposedPort?: (number | null) | undefined, protocols?: Array | undefined}; export type CdnEndpointRequest = {isSslEnabled?: boolean | undefined, stickySessions?: StickySessionSettings | undefined, pullZoneId?: (number | null) | undefined, portMappings?: Array | undefined}; export type AnycastIpProtocolVersion = 'iPv4'; export type AnycastEndpointRequest = {type: AnycastIpProtocolVersion, portMappings: Array}; export type EndpointRequest = {displayName: string, cdn?: CdnEndpointRequest | undefined, anycast?: AnycastEndpointRequest | undefined}; export type CreateApiAppsAppIdContainersContainerIdEndpointsArgs = {appId: string, containerId: string, body: EndpointRequest}; export type CreateApiAppsAppIdContainersContainerIdEndpointsOptions = {baseUrl?: string | undefined, AccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const saveEndpointResponse = z.object({id: 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 }; /** * Add application endpoint * * POST /apps/{appId}/containers/{containerId}/endpoints */ export const createApiAppsAppIdContainersContainerIdEndpoints = async (args: CreateApiAppsAppIdContainersContainerIdEndpointsArgs, options?: CreateApiAppsAppIdContainersContainerIdEndpointsOptions) => { const {baseUrl = "https://api.bunny.net/mc", 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(/\/+$/, '')}/apps/${encodeURIComponent(String(args.appId))}/containers/${encodeURIComponent(String(args.containerId))}/endpoints${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(saveEndpointResponse, await res.json()) };