All Projects → davidedantonio → fastify-axios

davidedantonio / fastify-axios

Licence: MIT license
Add axios http client to your fastify instance

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to fastify-axios

create-fastify-app
An utility that help you to generate or add plugin to your Fastify project
Stars: ✭ 53 (+89.29%)
Mutual labels:  fastify, nodejs-library, fastify-plugin
fastify-cors
Fastify CORS
Stars: ✭ 212 (+657.14%)
Mutual labels:  fastify, fastify-plugin
fastify-passport
Use passport strategies for authentication within a fastify application
Stars: ✭ 150 (+435.71%)
Mutual labels:  fastify, fastify-plugin
fastify-autoroutes
fastest way to map directories to URLs in fastify
Stars: ✭ 70 (+150%)
Mutual labels:  fastify, fastify-plugin
fastify-hasura
A Fastify plugin to have fun with Hasura.
Stars: ✭ 30 (+7.14%)
Mutual labels:  fastify, fastify-plugin
fastify-etag
Automatically generate etags for HTTP responses, for Fastify
Stars: ✭ 61 (+117.86%)
Mutual labels:  fastify, fastify-plugin
fastify-file-upload
Fastify plugin for uploading files
Stars: ✭ 68 (+142.86%)
Mutual labels:  fastify, fastify-plugin
fastify-csrf
A fastify csrf plugin.
Stars: ✭ 88 (+214.29%)
Mutual labels:  fastify, fastify-plugin
fastify-accepts
Add accepts parser to fastify
Stars: ✭ 51 (+82.14%)
Mutual labels:  fastify, fastify-plugin
fastify-postgres
Fastify PostgreSQL connection plugin
Stars: ✭ 144 (+414.29%)
Mutual labels:  fastify, fastify-plugin
fastify-vue
A nuxt.js fastify plugin
Stars: ✭ 27 (-3.57%)
Mutual labels:  fastify, fastify-plugin
session
Session plugin for fastify
Stars: ✭ 52 (+85.71%)
Mutual labels:  fastify, fastify-plugin
fastify-webpack-hmr
Webpack hot module reloading for Fastify
Stars: ✭ 29 (+3.57%)
Mutual labels:  fastify, fastify-plugin
fastify-leveldb
Plugin to share a common LevelDB connection across Fastify.
Stars: ✭ 19 (-32.14%)
Mutual labels:  fastify, fastify-plugin
fastify-awilix
Dependency injection support for fastify
Stars: ✭ 52 (+85.71%)
Mutual labels:  fastify, fastify-plugin
fastify-openapi-glue
A plugin for Fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.
Stars: ✭ 94 (+235.71%)
Mutual labels:  fastify, fastify-plugin
fastify-vite
This plugin lets you load a Vite client application and set it up for Server-Side Rendering (SSR) with Fastify.
Stars: ✭ 497 (+1675%)
Mutual labels:  fastify, fastify-plugin
fastify-caching
A Fastify plugin to facilitate working with cache headers
Stars: ✭ 123 (+339.29%)
Mutual labels:  fastify, fastify-plugin
fastify-cron
Run cron jobs alongside your Fastify server 👷
Stars: ✭ 32 (+14.29%)
Mutual labels:  fastify, fastify-plugin
fastify-loader
The route loader for the cool kids!
Stars: ✭ 17 (-39.29%)
Mutual labels:  fastify, fastify-plugin

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].