import getConfig from "../config" ; export enum ServiceTypes { Rustc , Cargo , Clang , Service } export interface IServiceRequestTask { file : string ; name : string ; output : string ; console : string ; success : boolean ; } export interface IServiceRequest { success : boolean ; message ? : string ; tasks ? : IServiceRequestTask [ ] ; output ? : string ; wasmBindgenJs ? : string ; } export async function getServiceURL ( to ) { const config = await getConfig ( ) ; switch ( to ) { case ServiceTypes . Rustc : return config . rustc ; case ServiceTypes . Cargo : return config . cargo ; case ServiceTypes . Clang : return config . clang ; case ServiceTypes . Service : return config . serviceUrl ; default : throw new Error ( `Invalid ServiceType: ${ to }` ) ; } } export async function parseJSONResponse ( response ) { const text = await response . text ( ) ; if ( response . status === 200 ) { try { return JSON . parse ( text ) ; } catch ( _ ) { } } return { success : false , message : text . replace ( /(^<pre>)|(<\/pre>$)/gi , "" ) , } ; } export async function sendRequestJSON ( content , to ) { const url = await getServiceURL ( to ) ; const response = await fetch ( url , { method : "POST" , body : JSON . stringify ( content ) , headers : new Headers ( { "Content-Type" : "application/json" } ) } ) ; return parseJSONResponse ( response ) ; } export async function sendRequest ( content , to ) { const url = await getServiceURL ( to ) ; const response = await fetch ( url , { method : "POST" , body : content , headers : new Headers ( { "Content-Type" : "application/x-www-form-urlencoded" } ) } ) ; return parseJSONResponse ( response ) ; }