Aller au contenu

Il s’agit d’une intégration Astro^ qui fournit une fonction de récupération mise en cache pour Astro SSR.

  • Utilisation avec un projet Astro SSR : Bien que vous puissiez importer et utiliser ceci dans un projet Astro SSG (statique), cela ne présenterait aucun avantage car les pages statiques d’Astro sont pré-rendues.
  1. Installez l’intégration automatiquement à l’aide de la CLI d’Astro :

    Fenêtre de terminal
    npm run studiocms add @studiocms/cfetch

    Ou installez-la manuellement :

    Fenêtre de terminal
    npm i @studiocms/cfetch
  2. Installez les dépendances homologues

    Si votre gestionnaire de paquets n’installe pas automatiquement les dépendances homologues, vous devrez vous assurer que Effect est installé.

    Fenêtre de terminal
    npm i effect
  3. Ajoutez l’intégration à votre configuration Astro.

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

Cette intégration comprend différentes versions de fonctions de récupération mises en cache et d’Effects^ pour vous permettre de contrôler pleinement la façon dont vous travaillez avec vos données.

Toutes les méthodes d’Effects ont le schéma de retour suivant ou sont dérivées de

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
}
);
/*
Type renvoyé :
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" }
);
/*
Type renvoyé :
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" }
);
/*
Type renvoyé :
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" }
);
/*
Type renvoyé :
Effect.Effect<CachedResponse<Blob>, FetchError, never>
*/

Toutes les fonctions ont le modèle de retour suivant ou sont dérivées de :

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" }
);
/*
Type renvoyé :
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" }
);
/*
Type renvoyé :
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" }
);
/*
Type renvoyé :
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" }
);
/*
Type renvoyé :
CachedResponse<Blob>
*/