import {z} from 'zod' export type GetApiV1WorkspaceOptions = {baseUrl?: string | undefined, ServiceTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const managementListMeta = z.object({count: z.number().int().optional(), limit: z.number().int().nullable().optional(), offset: z.number().int().nullable().optional()}); export const managementWorkspace = z.object({uid: z.string().optional(), slug: z.string().optional(), data_center: z.record(z.string(), z.unknown()).optional(), is_expired: z.boolean().optional(), has_limit_reached: z.boolean().optional(), name: z.string().optional(), description: z.string().nullable().optional(), mode: z.enum(["production", "sandbox", "internal_sandbox"]).optional()}); export const managementWorkspaceListResponse = z.object({meta: managementListMeta.optional(), results: z.array(managementWorkspace).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 Workspaces * * GET /v1/workspace/ */ export const getApiV1Workspace = async (options?: GetApiV1WorkspaceOptions) => { const {baseUrl = "https://management-api.suprsend.com", fetch: fetchFn = fetch, ServiceTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ServiceTokenAuth !== undefined) headers.set("Authorization", ServiceTokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspace/${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(managementWorkspaceListResponse, await res.json()) };