@studiocms/cfetch
Esta página aún no está disponible en tu idioma.
This is an Astro integration^ that provides a cacheable fetch function for Astro SSR
Prerequisites
Section titled “Prerequisites”- Using with an Astro SSR project, While you could import and use this in an Astro SSG (static) project, it would have no benefit as Astro Static pages are pre-rendered.
Installation
Section titled “Installation”-
Install the integration automatically using the Astro CLI:
Ventana de terminal npm run studiocms add @studiocms/cfetchVentana de terminal pnpm run studiocms add @studiocms/cfetchVentana de terminal yarn run studiocms add @studiocms/cfetchOr install it manually:
Ventana de terminal npm i @studiocms/cfetchVentana de terminal pnpm add @studiocms/cfetchVentana de terminal yarn add @studiocms/cfetch -
Install peer dependencies
If your package manager does not automatically install peer dependencies, you will need to ensure
Effectis installed.Ventana de terminal npm i effectVentana de terminal pnpm add effectVentana de terminal yarn add effect -
Add the integration to your astro config
astro.config.mts import { defineConfig } from 'astro/config';import cFetch from "@studiocms/cfetch";export default defineConfig({integrations: [cFetch(),],});
This integration includes various versions of cached fetch functions and Effects^ to allow full control of how you work with your data.
Effects
Section titled “Effects”All Effects have the following return pattern or derivatives there of
Effect.Effect<CachedResponse<T>, FetchError, never>;CachedResponse<T> type
Section titled “CachedResponse<T> type”interface CachedResponse<T> { data: T; ok: boolean; status: number; statusText: string; headers: Record<string, string>;}CFetchConfig type
Section titled “CFetchConfig type”interface CFetchConfig { ttl?: Duration.DurationInput; tags?: string[]; key?: string; verbose?: boolean;}cFetchEffect
Section titled “cFetchEffect”Interface
Section titled “Interface”const cFetchEffect: <T>( url: string | URL, parser: (response: Response) => Promise<T>, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Effect.Effect<CachedResponse<T>, FetchError, never>Example Usage
Section titled “Example Usage”import { cFetchEffect, Duration } from "c:fetch"
const effect = cFetchEffect<{ foo: string; bar: number; }>( 'https://api.example.com/data', (res) => res.json(), { method: "GET" }, { ttl?: Duration.hours(1), tags?: ['example'], key?: "api-data-fetch", verbose?: false });/*Return type: Effect.Effect<CachedResponse<{ foo: string; bar: number; }>, FetchError, never>*/cFetchEffectJson
Section titled “cFetchEffectJson”Interface
Section titled “Interface”const cFetchEffectJson: <T>( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Effect.Effect<CachedResponse<T>, FetchError, never>Example Usage
Section titled “Example Usage”import { cFetchEffectJson } from "c:fetch"
const effect = cFetchEffectJson<{ foo: string; bar: number; }>( 'https://api.example.com/data', { method: "GET" });/*Return type: Effect.Effect<CachedResponse<{ foo: string; bar: number; }>, FetchError, never>*/cFetchEffectText
Section titled “cFetchEffectText”Interface
Section titled “Interface”const cFetchEffectText: ( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Effect.Effect<CachedResponse<string>, FetchError, never>Example Usage
Section titled “Example Usage”import { cFetchEffectText } from "c:fetch"
const effect = cFetchEffectText( 'https://example.com', { method: "GET" });/*Return type: Effect.Effect<CachedResponse<string>, FetchError, never>*/cFetchEffectBlob
Section titled “cFetchEffectBlob”Interface
Section titled “Interface”const cFetchEffectBlob: ( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Effect.Effect<CachedResponse<Blob>, FetchError, never>Example Usage
Section titled “Example Usage”import { cFetchEffectBlob } from "c:fetch"
const effect = cFetchEffectBlob( 'https://example.com/image.png', { method: "GET" });/*Return type: Effect.Effect<CachedResponse<Blob>, FetchError, never>*/Functions
Section titled “Functions”All Functions have the following return pattern or derivatives there of
CachedResponse<T>;cFetch
Section titled “cFetch”Interface
Section titled “Interface”const cFetch: <T>( url: string | URL, parser: (response: Response) => Promise<T>, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Promise<CachedResponse<T>>Example Usage
Section titled “Example Usage”import { cFetch } from "c:fetch"
const effect = await cFetch<{ foo: string; bar: number; }>( 'https://api.example.com/data', (res) => res.json(), { method: "GET" });/*Return type: CachedResponse<{ foo: string; bar: number; }>*/cFetchJson
Section titled “cFetchJson”Interface
Section titled “Interface”const cFetchJson: <T>( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Promise<CachedResponse<T>>Example Usage
Section titled “Example Usage”import { cFetchJson } from "c:fetch"
const effect = await cFetchJson<{ foo: string; bar: number; }>( 'https://api.example.com/data', { method: "GET" });/*Return type: CachedResponse<{ foo: string; bar: number; }>*/cFetchText
Section titled “cFetchText”Interface
Section titled “Interface”const cFetchText: ( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Promise<CachedResponse<string>>Example Usage
Section titled “Example Usage”import { cFetchText } from "c:fetch"
const effect = await cFetchText( 'https://example.com', { method: "GET" });/*Return type: CachedResponse<string>*/cFetchBlob
Section titled “cFetchBlob”Interface
Section titled “Interface”const cFetchBlob: ( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Promise<CachedResponse<Blob>>Example Usage
Section titled “Example Usage”import { cFetchBlob } from "c:fetch"
const effect = await cFetchBlob( 'https://example.com/image.png', { method: "GET" });/*Return type: CachedResponse<Blob>*/