Skip to content

What is the best way I wrap axios in adonis? #3844

Discussion options

You must be logged in to vote

Just make a basic class with static methods like this :

import axios from 'axios'

export class MyApi {
  private static readonly BASE_URL = 'https://my-api.com'
  private static client = axios.create({ baseURL: MyApi.BASE_URL })

  public static async getProduct(productId: string) {
    const { data } = await this.client.get(`/product/${productId}`)
    return data
  }

  // Other methods ...
}

And use it in your controller like that :

import { MyApi } from '@/services/MyApi'

export default class Mycontroller {
  public async handleFileUpload(request) {
    const product = await MyApi.getProduct(request.param('id'))
    
    // ...
  }
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@positivedeveloper36
Comment options

@positivedeveloper36
Comment options

@RomainLanz
Comment options

Answer selected by Julien-R44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants