fetch.ts

Declarations
#

8 declarations

view source

deserialize_cache
#

fetch.ts view source

(serialized: string): Map<string, { key: string; url: string; params: any; value: any; etag: string | null; last_modified: string | null; }>

Converts a serialized cache string to a FetchValueCache.

serialized

type string

returns

Map<string, { key: string; url: string; params: any; value: any; etag: string | null; last_modified: string | null; }>

fetch_value
#

fetch.ts view source

<TValue = any, TParams = undefined>(url: string | URL, options?: FetchValueOptions<TValue, TParams> | undefined): Promise<Result<{ value: TValue; headers: Headers; }, { ...; }>>

Specializes fetch with some slightly different behavior and additional features:

- throws on ratelimit errors to mitigate unintentional abuse - optional parse function called on the return value - optional cache (different from the browser cache, the caller can serialize it so e.g. dev setups can avoid hitting the network) - optional simplified API for authorization and data types (you can still provide headers directly)

Unlike fetch, this throws on ratelimits (status code 429) to halt whatever is happpening in its tracks to avoid accidental abuse, but returns a Result in all other cases. Handling ratelimit headers with more sophistication gets tricky because behavior differs across services. (e.g. Mastodon returns an ISO string for x-ratelimit-reset, but GitHub returns Date.now()/1000, and other services may do whatever, or even use a different header)

It's also stateless to avoid the complexity and bugs, so we don't try to track x-ratelimit-remaining per domain.

If the value is cached, only the cached safe subset of the headers are returned. (currently just etag and last-modified) Otherwise the full res.headers are included.

url

type string | URL

options?

type FetchValueOptions<TValue, TParams> | undefined
optional

returns

Promise<Result<{ value: TValue; headers: Headers; }, { status: number; message: string; }>>

FetchValueCache
#

fetch.ts view source

ZodMap<ZodString, ZodObject<{ key: ZodString; url: ZodString; params: ZodAny; value: ZodAny; etag: ZodNullable<ZodString>; last_modified: ZodNullable<...>; }, $strip>>

FetchValueCacheItem
#

fetch.ts view source

ZodObject<{ key: ZodString; url: ZodString; params: ZodAny; value: ZodAny; etag: ZodNullable<ZodString>; last_modified: ZodNullable<ZodString>; }, $strip>

FetchValueCacheKey
#

FetchValueOptions
#

fetch.ts view source

FetchValueOptions<TValue, TParams>

generics

TValue

TParams

default undefined

request

The request.headers take precedence over the headers computed from other options.

type RequestInit

params

type TParams

parse

type (v: any) => TValue

token

type string | null

cache

type FetchValueCache | null

return_early_from_cache

type boolean

log

type Logger

fetch

type typeof globalThis.fetch

serialize_cache
#

fetch.ts view source

(cache: Map<string, { key: string; url: string; params: any; value: any; etag: string | null; last_modified: string | null; }>): string

Converts FetchValueCache to a JSON string.

cache

type Map<string, { key: string; url: string; params: any; value: any; etag: string | null; last_modified: string | null; }>

returns

string

to_fetch_value_cache_key
#

fetch.ts view source

(url: string, params: any, method: string): FetchValueCacheKey

url

type string

params

type any

method

type string

returns

FetchValueCacheKey

Depends on
#