@studiocms/cfetch
Il s’agit d’une intégration Astro^ qui fournit une fonction de récupération mise en cache pour Astro SSR.
Utilisation
Section intitulée « Utilisation »Prérequis
Section intitulée « Prérequis »- 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.
Installation
Section intitulée « Installation »-
Installez l’intégration automatiquement à l’aide de la CLI d’Astro :
Fenêtre de terminal npm run studiocms add @studiocms/cfetchFenêtre de terminal pnpm run studiocms add @studiocms/cfetchFenêtre de terminal yarn run studiocms add @studiocms/cfetchOu installez-la manuellement :
Fenêtre de terminal npm i @studiocms/cfetchFenêtre de terminal pnpm add @studiocms/cfetchFenêtre de terminal yarn add @studiocms/cfetch -
Installez les dépendances homologues
Si votre gestionnaire de paquets n’installe pas automatiquement les dépendances homologues, vous devrez vous assurer que
Effectest installé.Fenêtre de terminal npm i effectFenêtre de terminal pnpm add effectFenêtre de terminal yarn add effect -
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(),],});
Utilisation
Section intitulée « Utilisation »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>;Type CachedResponse<T>
Section intitulée « Type CachedResponse<T> »interface CachedResponse<T> { data: T; ok: boolean; status: number; statusText: string; headers: Record<string, string>;}Type CFetchConfig
Section intitulée « Type CFetchConfig »interface CFetchConfig { ttl?: Duration.DurationInput; tags?: string[]; key?: string; verbose?: boolean;}cFetchEffect
Section intitulée « cFetchEffect »Interface
Section intitulée « Interface »const cFetchEffect: <T>( url: string | URL, parser: (response: Response) => Promise<T>, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Effect.Effect<CachedResponse<T>, FetchError, never>Exemple d’utilisation
Section intitulée « Exemple d’utilisation »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>*/cFetchEffectJson
Section intitulée « cFetchEffectJson »Interface
Section intitulée « Interface »const cFetchEffectJson: <T>( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Effect.Effect<CachedResponse<T>, FetchError, never>Exemple d’utilisation
Section intitulée « Exemple d’utilisation »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>*/cFetchEffectText
Section intitulée « cFetchEffectText »Interface
Section intitulée « Interface »const cFetchEffectText: ( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Effect.Effect<CachedResponse<string>, FetchError, never>Exemple d’utilisation
Section intitulée « Exemple d’utilisation »import { cFetchEffectText } from "c:fetch"
const effect = cFetchEffectText( 'https://example.com', { method: "GET" });/*Type renvoyé : Effect.Effect<CachedResponse<string>, FetchError, never>*/cFetchEffectBlob
Section intitulée « cFetchEffectBlob »Interface
Section intitulée « Interface »const cFetchEffectBlob: ( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Effect.Effect<CachedResponse<Blob>, FetchError, never>Exemple d’utilisation
Section intitulée « Exemple d’utilisation »import { cFetchEffectBlob } from "c:fetch"
const effect = cFetchEffectBlob( 'https://example.com/image.png', { method: "GET" });/*Type renvoyé : Effect.Effect<CachedResponse<Blob>, FetchError, never>*/Fonctions
Section intitulée « Fonctions »Toutes les fonctions ont le modèle de retour suivant ou sont dérivées de :
CachedResponse<T>;Interface
Section intitulée « Interface »const cFetch: <T>( url: string | URL, parser: (response: Response) => Promise<T>, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Promise<CachedResponse<T>>Exemple d’utilisation
Section intitulée « Exemple d’utilisation »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; }>*/cFetchJson
Section intitulée « cFetchJson »Interface
Section intitulée « Interface »const cFetchJson: <T>( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Promise<CachedResponse<T>>Exemple d’utilisation
Section intitulée « Exemple d’utilisation »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; }>*/cFetchText
Section intitulée « cFetchText »Interface
Section intitulée « Interface »const cFetchText: ( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Promise<CachedResponse<string>>Exemple d’utilisation
Section intitulée « Exemple d’utilisation »import { cFetchText } from "c:fetch"
const effect = await cFetchText( 'https://example.com', { method: "GET" });/*Type renvoyé : CachedResponse<string>*/cFetchBlob
Section intitulée « cFetchBlob »Interface
Section intitulée « Interface »const cFetchBlob: ( url: string | URL, options?: RequestInit | undefined, cacheConfig?: CFetchConfig | undefined) => Promise<CachedResponse<Blob>>Exemple d’utilisation
Section intitulée « Exemple d’utilisation »import { cFetchBlob } from "c:fetch"
const effect = await cFetchBlob( 'https://example.com/image.png', { method: "GET" });/*Type renvoyé : CachedResponse<Blob>*/