Saltearse al contenido

@studiocms/cfetch

Esta es una integración de Astro^ que proporciona una función fetch con caché para proyectos Astro SSR

  • Usando con un proyecto Astro SSR, aunque podrías importar y usar esto en un proyecto Astro SSG (estático), no tendría ningún beneficio ya que las páginas estáticas de Astro se pre-renderizan.
  1. Instala la integración automáticamente usando la CLI de Astro:

    Ventana de terminal
    npm run studiocms add @studiocms/cfetch

    O instálala manualmente:

    Ventana de terminal
    npm i @studiocms/cfetch
  2. Instala las dependencias homólogas

    Si tu gestor de paquetes no instala automáticamente las dependencias homólogas, necesitarás asegurarte de que Effect está instalado.

    Ventana de terminal
    npm i effect
  3. Añade la integración a tu configuración de Astro

    astro.config.mts
    import { defineConfig } from 'astro/config';
    import cFetch from "@studiocms/cfetch";
    export default defineConfig({
    integrations: [
    cFetch(),
    ],
    });

Esta integración incluye varias versiones de funciones fetch con caché y Effects^ para permitir un control completo de cómo trabajas con tus datos.

Todos los efectos tienen el siguiente patrón de retorno o derivados de él.

Effect.Effect<CachedResponse<T>, FetchError, never>;
interface CachedResponse<T> {
data: T;
ok: boolean;
status: number;
statusText: string;
headers: Record<string, string>;
}
interface CFetchConfig {
ttl?: Duration.DurationInput;
tags?: string[];
key?: string;
verbose?: boolean;
}
const cFetchEffect: <T>(
url: string | URL,
parser: (response: Response) => Promise<T>,
options?: RequestInit | undefined,
cacheConfig?: CFetchConfig | undefined
) => Effect.Effect<CachedResponse<T>, FetchError, never>
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
}
);
/*
Tipo de retorno:
Effect.Effect<CachedResponse<{ foo: string; bar: number; }>, FetchError, never>
*/
const cFetchEffectJson: <T>(
url: string | URL,
options?: RequestInit | undefined,
cacheConfig?: CFetchConfig | undefined
) => Effect.Effect<CachedResponse<T>, FetchError, never>
import { cFetchEffectJson } from "c:fetch"
const effect = cFetchEffectJson<{ foo: string; bar: number; }>(
'https://api.example.com/data',
{ method: "GET" }
);
/*
Tipo de retorno:
Effect.Effect<CachedResponse<{ foo: string; bar: number; }>, FetchError, never>
*/
const cFetchEffectText: (
url: string | URL,
options?: RequestInit | undefined,
cacheConfig?: CFetchConfig | undefined
) => Effect.Effect<CachedResponse<string>, FetchError, never>
import { cFetchEffectText } from "c:fetch"
const effect = cFetchEffectText(
'https://example.com',
{ method: "GET" }
);
/*
Tipo de retorno:
Effect.Effect<CachedResponse<string>, FetchError, never>
*/
const cFetchEffectBlob: (
url: string | URL,
options?: RequestInit | undefined,
cacheConfig?: CFetchConfig | undefined
) => Effect.Effect<CachedResponse<Blob>, FetchError, never>
import { cFetchEffectBlob } from "c:fetch"
const effect = cFetchEffectBlob(
'https://example.com/image.png',
{ method: "GET" }
);
/*
Tipo de retorno:
Effect.Effect<CachedResponse<Blob>, FetchError, never>
*/

Todas las funciones tienen el siguiente patrón de retorno o derivados de él.

CachedResponse<T>;
const cFetch: <T>(
url: string | URL,
parser: (response: Response) => Promise<T>,
options?: RequestInit | undefined,
cacheConfig?: CFetchConfig | undefined
) => Promise<CachedResponse<T>>
import { cFetch } from "c:fetch"
const effect = await cFetch<{ foo: string; bar: number; }>(
'https://api.example.com/data',
(res) => res.json(),
{ method: "GET" }
);
/*
Tipo de retorno:
CachedResponse<{ foo: string; bar: number; }>
*/
const cFetchJson: <T>(
url: string | URL,
options?: RequestInit | undefined,
cacheConfig?: CFetchConfig | undefined
) => Promise<CachedResponse<T>>
import { cFetchJson } from "c:fetch"
const effect = await cFetchJson<{ foo: string; bar: number; }>(
'https://api.example.com/data',
{ method: "GET" }
);
/*
Tipo de retorno:
CachedResponse<{ foo: string; bar: number; }>
*/
const cFetchText: (
url: string | URL,
options?: RequestInit | undefined,
cacheConfig?: CFetchConfig | undefined
) => Promise<CachedResponse<string>>
import { cFetchText } from "c:fetch"
const effect = await cFetchText(
'https://example.com',
{ method: "GET" }
);
/*
Tipo de retorno:
CachedResponse<string>
*/
const cFetchBlob: (
url: string | URL,
options?: RequestInit | undefined,
cacheConfig?: CFetchConfig | undefined
) => Promise<CachedResponse<Blob>>
import { cFetchBlob } from "c:fetch"
const effect = await cFetchBlob(
'https://example.com/image.png',
{ method: "GET" }
);
/*
Tipo de retorno:
CachedResponse<Blob>
*/