Nuxt Nation カンファレンスが開催されます。11月12〜13日にご参加ください。

api-party
nuxt-api-party

サーバープロキシと動的なコンポーザブルで、あらゆるAPIに安全に接続

Nuxt API Party module

Nuxt API Party

Nuxt モジュールで、あらゆるAPIに安全に接続できます。

特徴

セットアップ

!TIP📖 ドキュメントを読む

npx nuxi@latest module add nuxt-api-party

基本的な使い方

!TIP📖 ドキュメントを読む

Nuxt API Party を Nuxt 設定に追加し、apiParty モジュールオプションに次のプロパティを持つエンドポイントオブジェクトを設定して、最初の API 接続を準備します。

// `nuxt.config.ts`
export default defineNuxtConfig({
  modules: ['nuxt-api-party'],

  apiParty: {
    endpoints: {
      jsonPlaceholder: {
        url: process.env.JSON_PLACEHOLDER_API_BASE_URL!,
        // Global headers sent with each request
        headers: {
          Authorization: `Bearer ${process.env.JSON_PLACEHOLDER_API_TOKEN}`
        }
      }
    }
  }
})

API を jsonPlaceholder と呼ぶ場合、生成されるコンポーザブルは次のようになります。

  • $jsonPlaceholder$fetch と同様に、レスポンスデータを返します。
  • useJsonPlaceholderData複数の値useFetch と同様に返します。

これらのコンポーザブルをテンプレートまたはコンポーネントで使用します。

<script setup lang="ts">
const { data, refresh, error, status, clear } = await useJsonPlaceholderData('posts/1')
</script>

<template>
  <h1>{{ data?.title }}</h1>
  <pre>{{ JSON.stringify(data, undefined, 2) }}</pre>
</template>

!TIP 必要な数のAPIを接続できます。それらを endpoints オブジェクトに追加するだけです。

💻 開発

  1. このリポジトリをクローンします
  2. corepack enable を使用してCorepack を有効にします
  3. pnpm install を使用して依存関係をインストールします
  4. pnpm run dev:prepare を実行します
  5. pnpm run dev を使用して開発サーバーを開始します

特別な感謝

  • このパッケージの初期バージョンをスポンサーしてくれた Dennis Baum に感謝します。
  • ロゴピクセルアートを提供してくれた Maronbeere に感謝します。

ライセンス

MIT ライセンス © 2022-現在 Johann Schopplich