got#ReadError TypeScript Examples

The following examples show how to use got#ReadError. 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 vote down vote up
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,
    };
  }
}
Example #2
Source File: module.req.ts    From multi-downloader-nx with MIT License 4 votes vote down vote up
async getData<T = string> (durl: string, params?: Params) {
    params = params || {};
    // options
    const options: Options & {
      minVersion?: string,
      maxVersion?: string
      curlDebug?: boolean
    } = {
      method: params.method ? params.method : 'GET',
      headers: {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:90.0) Gecko/20100101 Firefox/90.0',
      },
    };
    // additional params
    if(params.headers){
      options.headers = {...options.headers, ...params.headers};
    }
    if(options.method == 'POST'){
      (options.headers as Headers)['Content-Type'] = 'application/x-www-form-urlencoded';
    }
    if(params.body){
      options.body = params.body;
    }
    if(params.binary == true){
      options.responseType = 'buffer';
    }
    if(typeof params.followRedirect == 'boolean'){
      options.followRedirect = params.followRedirect;
    }
    // if auth
    const loc = new URL(durl);
    // avoid cloudflare protection
    // debug
    options.hooks = {
      beforeRequest: [
        (options) => {
          if(this.debug){
            console.log('[DEBUG] GOT OPTIONS:');
            console.log(options);
          }
        }
      ]
    };
    if(this.debug){ 
      options.curlDebug = true;
    }
    // try do request
    try {
      const res = await got(durl.toString(), options) as unknown as Response<T>;
      return {
        ok: true,
        res
      };
    }
    catch(_error){
      const error = _error as {
        name: string
      } & ReadError & {
        res: Response<unknown>
      };
      if(error.response && error.response.statusCode && error.response.statusMessage){
        console.log(`[ERROR] ${error.name} ${error.response.statusCode}: ${error.response.statusMessage}`);
      }
      else{
        console.log(`[ERROR] ${error.name}: ${error.code || error.message}`);
      }
      if(error.response && !error.res){
        error.res = error.response;
        const docTitle = (error.res.body as string).match(/<title>(.*)<\/title>/);
        if(error.res.body && docTitle){
          console.log('[ERROR]', docTitle[1]);
        }
      }
      if(error.res && error.res.body && error.response.statusCode 
        && error.response.statusCode != 404 && error.response.statusCode != 403){
        console.log('[ERROR] Body:', error.res.body);
      }
      return {
        ok: false,
        error,
      };
    }
  }