import axios , { AxiosInstance , AxiosResponse } from 'axios' ; import { BASE_URL , API_URL } from './url' ; import { SimpleSandbox , Sandbox , Directory , Module } from './type' ; function getData ( res ) { return res . data . data ; } export default class Client { request : AxiosInstance ; constructor ( token ) { const headers = { } ; if ( token ) { headers . Authorization = `Bearer ${ token }` ; } this . request = axios . create ( { baseURL : API_URL , headers , } ) ; } async verifyUser ( token ) { const res = await this . request . get ( `/auth/verify/${ token }` ) ; return getData ( res ) ; } async fetchSandboxes ( ) { const res = await this . request . get ( '/sandboxes' ) ; return getData ( res ) ; } async fetchSandbox ( id ) { const res = await this . request . get ( `/sandboxes/${ id }` ) ; return getData ( res ) ; } async forkSandbox ( id ) { const res = await this . request . post ( `/sandboxes/${ id }/fork` ) ; return getData ( res ) ; } async updateSandbox ( id , sandbox ) { const res = await this . request . put ( `/sandboxes/${ id }` , { sandbox } ) ; return getData ( res ) ; } async createModule ( id , module ) { const res = await this . request . post ( `/sandboxes/${ id }/modules` , { module , } ) ; return getData ( res ) ; } async updateModule ( id , moduleId , module ) { return await this . request . put ( `/sandboxes/${ id }/modules/${ moduleId }` , { module , } ) ; } async deleteModule ( id , moduleId ) { return await this . request . delete ( `/sandboxes/${ id }/modules/${ moduleId }` ) ; } async createDirectory ( id , directory ) { const res = await this . request . post ( `/sandboxes/${ id }/directories` , { directory , } ) ; return getData ( res ) ; } async deleteDirectory ( id , dirId ) { return await this . request . delete ( `/sandboxes/${ id }/directories/${ dirId }` ) ; } async updateDirectory ( id , dirId , directory ) { return await this . request . put ( `/sandboxes/${ id }/directories/${ dirId }` , { directory , } ) ; } }