Created
November 19, 2024 17:59
-
-
Save zolotyh/f0ebd94c84e4087534bc7556c1e8e728 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios, { AxiosInstance } from "axios" | |
const dummy = (input: any): any => ({ | |
// Сюда можно добавить логику сериализации | |
}) | |
class Ticket { | |
constructor(private api: AxiosInstance) { | |
console.log(this.api); | |
} | |
getTicket = async () => { } | |
} | |
class User { | |
constructor( | |
private api: AxiosInstance, | |
private ticket: Ticket) { console.log(this.ticket, this.api) } | |
} | |
export const buildAPI = () => { | |
const instance = axios.create() | |
// В перехватчики можно закидывать различную логику, например логика серриализаци и дессиариализаци | |
instance.interceptors.request.use(dummy); | |
// Можно разбивать на несколько функций | |
instance.interceptors.request.use(dummy); | |
instance.interceptors.response.use(dummy); | |
// Здесь будет расти сложность, возможно потребуется DI | |
const ticket = new Ticket(instance); | |
const user = new User(instance, ticket) | |
return { | |
ticket, | |
user, | |
} | |
} | |
buildAPI().ticket.getTicket(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment