Coder Social home page Coder Social logo

fastify-axios's Introduction

fastify-axios

js-standard-style codecov

A plugin for Fastify that adds support for sending requests via axios, a promise based HTTP(s) client for node.js and browser.

Under the hood axios http client is used, the options passed to register will be used as the default arguments while creating the axios http client.

Install

npm install fastify-axios

Default usage

Just add it to the project generated via fastify-cli, create-fastify-app, or with register in you application as below.

You can access the axios instance via fastify.axios, which can be used to send HTTP(s) requests via methods GET, POST, PUT, DELETE etc. Here a simple example

'use strict'

module.exports = async function (fastify, opts) {
  fastify.register(require('fastify-axios'))

  // request via axios.get
  const { data, status } = await fastify.axios.get('https://nodejs.org/en/')
  console.log('body size: %d', data.length)
  console.log('status: %d', status)
}

Alternatively you can specify default args to your axios instance. You can take a look at the default parameters here https://github.com/axios/axios:

'use strict'

module.exports = async function (fastify, opts) {
  fastify.register(require('fastify-axios'), {
    baseURL: 'https://nodejs.org'
  })

  // request via axios.get to https://nodejs.org/en/
  const { data, status } = await fastify.axios.get('/en/')
  console.log('body size: %d', data.length)
  console.log('status: %d', status)
}

Add more clients

It's possibile to add more than one axios client to your fastify instance. Here's how:

'use strict'

module.exports = async function (fastify, opts) {
  fastify.register(require('fastify-axios'), {
    clients: {
      v1: {
        baseURL: 'https://v1.api.com',
        headers: {
          'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJxyz'
        }
      },
      v2: {
        baseURL: 'https://v2.api.com',
        headers: {
          'Authorization': 'Bearer UtOkO3UI9lPY1h3h9ygTn8pD0Va2pFDcWCNbSKlf2HE'
        }
      }
    }
  })

  // Now you can use the apis in the following way
  const { dataV1, statusV1 } = await fastify.axios.v1.get('/ping')
  const { dataV2, statusV2 } = await fastify.axios.v2.get('/ping')

  // do something with dataV1 and dataV2
}

License

Licensed under MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.