Skip to content

@univ-lehavre/atlas-fetch-one-api-page ​

Low-level HTTP Effect wrapper for paginated API requests. Handles URL building, headers, JSON parsing, and rate-limit header extraction.

Installation ​

bash
pnpm add @univ-lehavre/atlas-fetch-one-api-page

Usage ​

typescript
import { fetchOnePage } from '@univ-lehavre/atlas-fetch-one-api-page';

const result = await Effect.runPromise(
  fetchOnePage<MyResponse>(
    new URL('https://api.example.com/items'),
    { filter: 'type:article', per_page: 25 },
    'my-app/1.0 (mailto:contact@example.com)',
  ),
);

console.log(result.data);       // typed response body
console.log(result.rateLimit);  // X-RateLimit-* headers if present

API ​

fetchOnePage<T>(endpointURL, params, userAgent) ​

Fetches a single page from an API endpoint.

ParameterTypeDescription
endpointURLURLBase URL of the endpoint
paramsQueryQuery parameters appended to the URL
userAgentstringValue for the User-Agent header

Returns Effect<PageResult<T>, FetchError | ResponseParseError> where PageResult<T> is:

typescript
interface PageResult<T> {
  data: T;
  rateLimit?: RateLimitInfo;
}

parseRateLimitHeaders(headers) ​

Extracts X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Credits-Used, and X-RateLimit-Reset from response headers. Returns RateLimitInfo | undefined.

RateLimitInfo ​

typescript
interface RateLimitInfo {
  limit: number;
  remaining: number;
  creditsUsed: number;
  resetInSeconds: number;
}

Lower-level utilities ​

ExportDescription
buildURL(baseUrl, params)Builds a URL with query string
buildHeaders(userAgent)Creates Headers with User-Agent
URLToResponse(url, method, headers)Performs the HTTP request
responseToJSON<T>(response)Parses JSON from a Response
fetchJSON<T>(url, method, headers)Combines request + JSON parse
FetchErrorTagged error for network failures
ResponseParseErrorTagged error for JSON parse failures

Classes ​

ClassDescription
FetchErrorError thrown when the fetch function fails.
ResponseParseError-

Interfaces ​

InterfaceDescription
PageResult-
RateLimitInfo-

Type Aliases ​

Type AliasDescription
Query-

Functions ​

FunctionDescription
buildHeadersBuild the headers for the API request.
buildURLBuild the full URL with query parameters.
fetchJSONFetch JSON data from a URL.
fetchOnePageFetch one page of results from an API endpoint.
parseRateLimitHeaders-
responseToJSON-
URLToResponse-