Skip to main content

Module fetch

Module fetch 

Source
Expand description

Fetch - live Workers AI model catalog retrieval over HTTPS.

Typed blocking GET of /accounts/<id>/ai/models/search?format=openrouter&per_page=1000&hide_experimental=false&include_deprecated=false (feedback 01 contract) with the token carried exclusively in the `Authorization: Bearer *** header.

Pagination (feedback 01: “handle pagination even if Cloudflare later caps per_page”): fetch_catalog_from_api follows cursor/next-page markers - result_info.cursor, result_info.page/total_pages, a top-level cursor, or a top-level next field - merging each page’s data array until the marker disappears or [MAX_CATALOG_PAGES] pages were pulled. A payload with no pagination field behaves exactly as before (single page).

Error taxonomy (feedback 01/03): missing token → CloudflareError::MissingEnv (defensive - the core’s Config already guarantees a non-empty token); 403 → CloudflareError::AuthRejected with a scope classification from the envelope code (invalid token / wrong account scope / Workers AI permission / generic - see [auth_scope_for]); 401 → CloudflareError::Api with the envelope code (defensive - the catalog endpoint rejects auth failures as 403); rate limit → CloudflareError::Api code 429 plus a Retry-After hint; transient 5xx → CloudflareError::Http; malformed JSON or a payload without a data array → CloudflareError::NoDataArray; transport/network failure → CloudflareError::Http.

Security: the token never reaches a URL, an error string, or a log line. Every text source that could feed an error (response body, transport message, body-read failure) is scrubbed of the token before it is mapped.

Constants§

FETCH_TIMEOUT
Overall per-request timeout for the catalog GET (15s, task contract).

Functions§

auth_header
The Authorization header value - factored out for unit testing. This is the ONLY place the token leaves SecretString.
catalog_url
The exact catalog endpoint URL for one account (feedback 01 contract).
fetch_catalog_from_api
Fetch the live catalog from the Cloudflare API, following pagination.