fetch.ts

view source

Declarations
#

8 declarations

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; }> import {deserialize_cache} from '@fuzdev/fuz_util/fetch.js';

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; }, { ...; }>> import {fetch_value} from '@fuzdev/fuz_util/fetch.js';

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 happening 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; }>>

generics

fetch_value<TValue = any, TParams = undefined>
TValue
default any
TParams
default undefined

FetchValueCache
#

fetch.ts view source

ZodMap<ZodString, ZodObject<{ key: ZodString; url: ZodString; params: ZodAny; value: ZodAny; etag: ZodNullable<ZodString>; last_modified: ZodNullable<...>; }, $strip>> import type {FetchValueCache} from '@fuzdev/fuz_util/fetch.js';

FetchValueCacheItem
#

fetch.ts view source

ZodObject<{ key: ZodString; url: ZodString; params: ZodAny; value: ZodAny; etag: ZodNullable<ZodString>; last_modified: ZodNullable<ZodString>; }, $strip> import type {FetchValueCacheItem} from '@fuzdev/fuz_util/fetch.js';

FetchValueCacheKey
#

fetch.ts view source

ZodString import type {FetchValueCacheKey} from '@fuzdev/fuz_util/fetch.js';

FetchValueOptions
#

fetch.ts view source

FetchValueOptions<TValue, TParams> import type {FetchValueOptions} from '@fuzdev/fuz_util/fetch.js';

generics

FetchValueOptions<TValue, TParams = undefined>
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 import {serialize_cache} from '@fuzdev/fuz_util/fetch.js';

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 import {to_fetch_value_cache_key} from '@fuzdev/fuz_util/fetch.js';

url

type string

params

type any

method

type string

returns

FetchValueCacheKey

Depends on
#