What is the best way I wrap axios in adonis? #3844
Answered
by
Julien-R44
positivedeveloper36
asked this question in
Help
-
My code in controller like this :
I use axios directly for requests in the controller like that. Whether in adonis we can create an index file to wrapping axios. So we don't call axios directly in the controller. So there is one global file to make a request. So it looks more clean and tidy Please help me. Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
Julien-R44
Aug 6, 2022
Replies: 1 comment 3 replies
-
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'))
// ...
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Julien-R44
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just make a basic class with static methods like this :
And use it in your controller like that :