import {z} from 'zod' export type SnippetName = string; export type SnippetTitle = string; export type SnippetContent = string; export type SnippetVisibilityStatus = 'public' | 'private'; export type PublishedStatus = 'published' | 'unpublished'; export type Description = string; export type CreatePortalSnippetRequest = {name: SnippetName, title?: SnippetTitle | undefined, content: SnippetContent, visibility?: SnippetVisibilityStatus | undefined, status?: PublishedStatus | undefined, description?: Description | undefined}; export type CreateApiV3PortalsPortalIdSnippetsArgs = {portalId: string, body: CreatePortalSnippetRequest}; export type CreateApiV3PortalsPortalIdSnippetsOptions = {baseUrl?: string | undefined, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string(); export const snippetName = z.string().max(512); export const snippetTitle = z.string().max(512); export const snippetContent = z.string().max(1000000); export const visibilityStatus = z.enum(["public", "private"]); export const publishedStatus = z.enum(["published", "unpublished"]); export const description = z.string().max(160); export const createdAt = z.string(); export const updatedAt = z.string(); export const portalSnippetResponse = z.object({id: uUID, name: snippetName, title: snippetTitle.optional(), content: snippetContent, visibility: visibilityStatus, status: publishedStatus, description: description.optional(), created_at: createdAt, updated_at: updatedAt}); 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 }; /** * Create Snippet * * POST /v3/portals/{portalId}/snippets * * Also accepts auth: systemAccountAccessToken, konnectAccessToken, serviceAccessToken */ export const createApiV3PortalsPortalIdSnippets = async (args: CreateApiV3PortalsPortalIdSnippetsArgs, options?: CreateApiV3PortalsPortalIdSnippetsOptions) => { const {baseUrl = "https://global.api.konghq.com", fetch: fetchFn = fetch, personalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (personalAccessToken !== undefined) headers.set('authorization', `Bearer ${personalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/portals/${encodeURIComponent(String(args.portalId))}/snippets${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(portalSnippetResponse, await res.json()) };