🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

nuxt-fetch

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nuxt-fetch

A Universal Fetch implementation for Nuxt.js.

latest
Source
npmnpm
Version
0.1.3
Version published
Maintainers
1
Created
Source

Nuxt Fetch

A Nuxt.js module which brings in the Fetch API to both client and server.

Usage

This module injects the instance method this.$fetcher to the Vue instance and is available in context as $fetcher on the Nuxt context.

nuxt.config.js

module.exports = {
  modules: ["nuxt-fetch"],
};

Example

pages/index.vue

export default {
  name: "Home",
  async asyncData({ $fetcher }) {
    const result = await $fetcher("/api/posts/1").then((res) => res.json());
    console.log(result); // { id: 1, title: "Hello world!", description: "..." }
  },
  methods: {
    async postSomething() {
      await this.$fetcher("/api/create/", {
        method: "post",
        headers: {
          "content-type": "application/json",
        },
        body: JSON.stringify({
          id: "2",
          title: "New Post",
          description: "...",
        }),
      });
    },
  },
};

Options

By default fetch works with relative URLs on the server too by checking the environment. So you can request something like /api everywhere. The following options are currently available:

nuxt.config.js

module.exports = {
  // Construct a different baseURL e.g. http://0.0.0.0:5000
  fetch: {
    host: "0.0.0.0",
    port: "5000",
  },
};

Keywords

nuxt

FAQs

Package last updated on 19 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts