2021-12-31 09:04:13 +05:00
import { EV , EVENTS } from "../common" ;
2022-01-18 12:00:57 +05:00
import { getServerNameFromHost } from "./constants" ;
import { extractHostname } from "./hostname" ;
2021-12-31 09:04:13 +05:00
2020-12-16 12:06:25 +05:00
function get ( url , token ) {
return request ( url , token , "GET" ) ;
}
function deleteRequest ( url , token ) {
return request ( url , token , "DELETE" ) ;
}
function patch ( url , data , token ) {
2021-10-26 09:13:18 +05:00
return bodyRequest ( url , data , token , "PATCH" ) ;
2020-12-16 12:06:25 +05:00
}
2021-06-13 10:00:24 +05:00
patch . json = function ( url , data , token ) {
2021-10-26 09:13:18 +05:00
return bodyRequest ( url , data , token , "PATCH" , "application/json" ) ;
2021-06-13 10:00:24 +05:00
} ;
2020-12-16 12:06:25 +05:00
function post ( url , data , token ) {
2021-10-26 09:13:18 +05:00
return bodyRequest ( url , data , token , "POST" ) ;
2020-12-16 12:06:25 +05:00
}
post . json = function ( url , data , token ) {
2021-10-26 09:13:18 +05:00
return bodyRequest ( url , data , token , "POST" , "application/json" ) ;
2020-12-16 12:06:25 +05:00
} ;
2021-01-03 12:43:53 +05:00
export default {
get ,
post ,
delete : deleteRequest ,
patch ,
} ;
2021-10-26 09:13:18 +05:00
function transformer ( data , type ) {
2020-12-23 11:28:38 +05:00
if ( ! data ) return ;
2021-10-26 09:13:18 +05:00
if ( type === "application/json" ) return JSON . stringify ( data ) ;
2020-12-16 12:06:25 +05:00
else {
return Object . entries ( data )
. map (
( [ key , value ] ) =>
` ${ encodeURIComponent ( key ) } = ${ encodeURIComponent ( value ) } `
)
. join ( "&" ) ;
}
}
2021-12-31 09:04:13 +05:00
/ * *
*
* @ param { Response } response
* @ returns
* /
2020-12-16 12:06:25 +05:00
async function handleResponse ( response ) {
const contentType = response . headers . get ( "content-type" ) ;
if ( contentType && contentType . includes ( "application/json" ) ) {
const json = await response . json ( ) ;
if ( response . ok ) {
return json ;
}
2021-01-03 12:43:53 +05:00
throw new Error ( errorTransformer ( json ) ) ;
2020-12-16 12:06:25 +05:00
} else {
2021-01-03 12:33:40 +05:00
if ( response . status === 429 ) throw new Error ( "You are being rate limited." ) ;
2020-12-16 12:06:25 +05:00
if ( response . ok ) return await response . text ( ) ;
2022-01-07 13:58:06 +05:00
else if ( response . status === 401 ) {
EV . publish ( EVENTS . userUnauthorized , response . url ) ;
throw new Error ( "Unauthorized." ) ;
} else
2020-12-16 12:06:25 +05:00
throw new Error (
` Request failed with status code: ${ response . status } ${ response . statusText } . `
) ;
}
}
async function request ( url , token , method ) {
return handleResponse (
2022-01-18 12:00:57 +05:00
await fetchWrapped ( url , {
2020-12-16 12:06:25 +05:00
method ,
2020-12-16 13:23:14 +05:00
headers : getAuthorizationHeader ( token ) ,
2020-12-16 12:06:25 +05:00
} )
) ;
}
2021-10-26 09:13:18 +05:00
async function bodyRequest (
url ,
data ,
token ,
method ,
contentType = "application/x-www-form-urlencoded"
) {
2020-12-16 12:06:25 +05:00
return handleResponse (
2022-01-18 12:00:57 +05:00
await fetchWrapped ( url , {
2020-12-16 12:06:25 +05:00
method ,
2021-10-26 09:13:18 +05:00
body : transformer ( data , contentType ) ,
2020-12-16 12:06:25 +05:00
headers : {
2020-12-16 13:23:14 +05:00
... getAuthorizationHeader ( token ) ,
2021-10-26 09:13:18 +05:00
"Content-Type" : contentType ,
2020-12-16 12:06:25 +05:00
} ,
} )
) ;
}
2020-12-16 13:23:14 +05:00
function getAuthorizationHeader ( token ) {
return token ? { Authorization : "Bearer " + token } : { } ;
}
2021-01-03 12:43:53 +05:00
function errorTransformer ( errorJson ) {
if ( ! errorJson . error && ! errorJson . errors && ! errorJson . error _description )
return "Unknown error." ;
const { error , error _description , errors } = errorJson ;
if ( errors ) {
return errors . join ( "\n" ) ;
}
switch ( error ) {
case "invalid_grant" : {
switch ( error _description ) {
case "invalid_username_or_password" :
return "Username or password incorrect." ;
default :
2021-12-06 12:47:45 +05:00
return error _description || error ;
2021-01-03 12:43:53 +05:00
}
}
2021-01-03 15:45:14 +05:00
default :
return error _description || error ;
2021-01-03 12:43:53 +05:00
}
}
2021-12-25 12:27:23 +05:00
2022-01-18 12:00:57 +05:00
/ * *
*
* @ param { RequestInfo } input
* @ param { RequestInit } init
* /
async function fetchWrapped ( input , init ) {
try {
const response = await fetch ( input , init ) ;
return response ;
} catch ( e ) {
const host = extractHostname ( input ) ;
const serverName = getServerNameFromHost ( host ) ;
if ( serverName )
throw new Error (
` ${ serverName } is not responding. Please check your internet connection. If the problem persists, feel free email us at support@streetwriters.co. (Reference error: ${ e . message } ) `
) ;
throw e ;
}
}
2021-12-30 12:12:43 +05:00
// /**
// *
// * @param {RequestInfo} resource
// * @param {RequestInit} options
// * @returns
// */
// async function fetchWithTimeout(resource, options = {}) {
// try {
// const { timeout = 8000 } = options;
// const controller = new AbortController();
// const id = setTimeout(() => controller.abort(), timeout);
// const response = await fetch(resource, {
// ...options,
// signal: controller.signal,
// });
// clearTimeout(id);
// return response;
// } catch (e) {
// if (e.name === "AbortError") throw new Error("Request timed out.");
// throw e;
// }
// }