got#OptionsOfUnknownResponseBody TypeScript Examples
The following examples show how to use
got#OptionsOfUnknownResponseBody.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: module.getdata.ts From multi-downloader-nx with MIT License | 4 votes |
getData = async <T = string>(options: Options) => {
const regionHeaders = {};
const gOptions = {
url: options.url,
http2: true,
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:70.0) Gecko/20100101 Firefox/70.0',
'Accept-Encoding': 'gzip',
...regionHeaders
}
} as OptionsOfUnknownResponseBody;
if(options.responseType) {
gOptions.responseType = options.responseType;
}
if(options.baseUrl){
gOptions.prefixUrl = options.baseUrl;
gOptions.url = gOptions.url?.toString().replace(/^\//,'');
}
if(options.querystring){
gOptions.url += `?${new URLSearchParams(options.querystring).toString()}`;
}
if(options.auth){
gOptions.method = 'POST';
const newHeaders = {
...gOptions.headers,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'https://ww.funimation.com',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0'
};
gOptions.headers = newHeaders;
gOptions.body = `username=${encodeURIComponent(options.auth.user)}&password=${encodeURIComponent(options.auth.pass)}`;
}
if(options.useToken && options.token){
gOptions.headers = {
...gOptions.headers,
Authorization: `Token ${options.token}`
};
}
if(options.dinstid){
gOptions.headers = {
...gOptions.headers,
devicetype: 'Android Phone'
};
}
// debug
gOptions.hooks = {
beforeRequest: [
(gotOpts) => {
if(options.debug){
console.log('[DEBUG] GOT OPTIONS:');
console.log(gotOpts);
}
}
]
};
try {
const res = await got(gOptions);
if(res.body && (options.responseType !== 'buffer' && (res.body as string).match(/^</))){
throw { name: 'HTMLError', res };
}
return {
ok: true,
res: {
...res,
body: res.body as T
},
};
}
catch(_error){
const error = _error as {
name: string,
} & ReadError & {
res: Response<unknown>
};
if(options.debug){
console.log(error);
}
if(error.response && error.response.statusCode && error.response.statusMessage){
console.log(`[ERROR] ${error.name} ${error.response.statusCode}: ${error.response.statusMessage}`);
}
else if(error.name && error.name == 'HTMLError' && error.res && error.res.body){
console.log(`[ERROR] ${error.name}:`);
console.log(error.res.body);
}
else{
console.log(`[ERROR] ${error.name}: ${error.code||error.message}`);
}
return {
ok: false,
error,
};
}
}