import {z} from 'zod' export type GetApiV1WorkspaceWsApiKeyArgs = {workspace: string}; export type GetApiV1WorkspaceWsApiKeyOptions = {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 managementActor = z.object({name: z.string().optional(), email: z.string().optional()}).nullable(); export const managementWorkspaceApiKey = z.object({id: z.string().optional(), masked_api_key: z.string().optional(), is_deleted: z.boolean().optional(), name: z.string().optional(), allowed_domains: z.array(z.string()).optional(), created_at: z.string().optional(), created_by: managementActor.optional(), deleted_at: z.string().nullable().optional(), deleted_by: managementActor.optional()}); export const managementWorkspaceApiKeyListResponse = z.object({meta: managementListMeta.optional(), results: z.array(managementWorkspaceApiKey).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 Workspace API Keys * * GET /v1/{workspace}/ws_api_key/ */ export const getApiV1WorkspaceWsApiKey = async (args: GetApiV1WorkspaceWsApiKeyArgs, options?: GetApiV1WorkspaceWsApiKeyOptions) => { 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/${encodeURIComponent(String(args.workspace))}/ws_api_key/${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(managementWorkspaceApiKeyListResponse, await res.json()) };