import {z} from 'zod' export type GetApiApiV1UserApiCredentialsArgs = {limit?: number | undefined, cursor?: string | undefined}; export type GetApiApiV1UserApiCredentialsOptions = {baseUrl?: string | undefined, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiCredential = z.object({id: z.number().int().gte(1), name: z.string().max(80), prefix: z.string(), last4: z.string().min(4).max(4), hint: z.string(), scopes: z.array(z.enum(["public:read", "companies:read", "people:read", "tenders:read", "legal:read", "integrations:read", "exports:read", "account:read"])), status: z.enum(["active", "expired", "revoked"]), created_at: z.string(), expires_at: z.string().nullable().optional(), revoked_at: z.string().nullable().optional(), last_used_at: z.string().nullable().optional(), rotated_from_id: z.number().int().nullable().optional(), legacy_migrated: z.boolean().optional()}); export const getApiApiV1UserApiCredentialsResponse = z.object({credentials: z.array(apiCredential).optional(), allowed_scopes: z.record(z.string(), z.string()).optional(), token_visible_once: z.literal(true).optional(), max_active: z.literal(10).optional(), pagination: z.object({limit: z.number().int().gte(1).lte(50), has_more: z.boolean(), next_cursor: z.string().max(256).nullable()}).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 API credential metadata * * GET /api/v1/user/api-credentials */ export const getApiApiV1UserApiCredentials = async (args: GetApiApiV1UserApiCredentialsArgs, options?: GetApiApiV1UserApiCredentialsOptions) => { const {baseUrl = "https://openmercantil.es", fetch: fetchFn = fetch, cookieAuth} = 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.cursor != null) query.append("cursor", String(args.cursor)) if (cookieAuth !== undefined) cookies.push("ob_sess=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/user/api-credentials${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(getApiApiV1UserApiCredentialsResponse, await res.json()) };