import axios , { AxiosRequestConfig } from "axios" ; import { ISandbox } from "codesandbox-import-util-types" ; import { values } from "lodash" ; import { decamelizeKeys } from "humps" ; import { getToken } from "../cfg" ; import { CREATE_SANDBOX_URL , GET_USER_URL , verifyUserTokenUrl , CREATE_UPLOAD_URL , } from "./url" ; const DatauriParser = require ( "datauri/parser" ) ; const callApi = async ( options ) => { try { const response = await axios ( options ) ; return response . data . data ; } catch ( e ) { if ( e . response && e . response . data && e . response . data . errors ) { e . message = values ( e . response . data . errors ) [ 0 ] ; } throw e ; } } ; export async function uploadSandbox ( sandbox ) { const token = await getToken ( ) ; if ( token == null ) { throw new Error ( "You're not signed in" ) ; } const sandboxData = { ... decamelizeKeys ( sandbox ) , from_cli : true } ; const options = { data : { sandbox : sandboxData } , headers : { Authorization : `Bearer ${ token }` } , method : "POST" , url : CREATE_SANDBOX_URL } ; return callApi ( options ) ; } export async function fetchUser ( token ) { const Authorization = `Bearer ${ token }` ; const options = { headers : { Authorization } , method : "GET" , url : GET_USER_URL } ; return callApi ( options ) ; } export async function verifyUser ( token ) { const options = { method : "GET" , url : verifyUserTokenUrl ( token ) } ; return callApi ( options ) ; } export async function createUpload ( filename , buffer ) { const parser = new DatauriParser ( ) ; parser . format ( filename , buffer ) ; const uri = parser . content ; const token = await getToken ( ) ; if ( token == null ) { throw new Error ( "You're not signed in" ) ; } const options = { data : { name : filename , content : uri } , headers : { Authorization : `Bearer ${ token }` } , method : "POST" , url : CREATE_UPLOAD_URL } ; return callApi ( options ) ; }