import {z} from 'zod' export type GetApiTunnelkeysArgs = {limit?: number | undefined, before?: number | undefined, after?: number | undefined, startafter?: number | undefined, withdisabled?: number | undefined}; export type GetApiTunnelkeysOptions = {baseUrl?: string | undefined, apiKeyAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const successProperty = z.boolean(); export const tunnelKeyObject = z.object({id: z.number().int().optional(), userid: z.number().int().optional(), public_key: z.string().optional(), disabled: z.union([z.literal(0), z.literal(1)]).optional(), created_at: z.string().optional(), private_key: z.string().nullable().optional()}); export const tunnelKeyListResponse = z.object({success: successProperty.optional(), limit: z.number().int().optional(), size: z.number().int().optional(), continues: z.boolean().optional(), data: z.array(tunnelKeyObject).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 tunnel keys for the current user * * GET /tunnelkeys/ */ export const getApiTunnelkeys = async (args: GetApiTunnelkeysArgs, options?: GetApiTunnelkeysOptions) => { const {baseUrl = "https://dashboard.hologram.io/api/1", fetch: fetchFn = fetch, apiKeyAuth} = 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.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.startafter != null) query.append("startafter", String(args.startafter)) if (args.withdisabled != null) query.append("withdisabled", String(args.withdisabled)) if (apiKeyAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyAuth.username}:${apiKeyAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/tunnelkeys/${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(tunnelKeyListResponse, await res.json()) };