axios#AxiosRequestHeaders TypeScript Examples

The following examples show how to use axios#AxiosRequestHeaders. 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: avalanche.ts    From avalanchejs with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected _setHeaders = (headers: any): AxiosRequestHeaders => {
    if (typeof this.headers === "object") {
      for (const [key, value] of Object.entries(this.headers)) {
        headers[`${key}`] = value
      }
    }

    if (typeof this.auth === "string") {
      headers.Authorization = `Bearer ${this.auth}`
    }
    return headers
  }
Example #2
Source File: avalanche.ts    From avalanchejs with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
   * @ignore
   */
  protected _request = async (
    xhrmethod: Method,
    baseurl: string,
    getdata: object,
    postdata: string | object | ArrayBuffer | ArrayBufferView,
    headers: AxiosRequestHeaders = {},
    axiosConfig: AxiosRequestConfig = undefined
  ): Promise<RequestResponseData> => {
    let config: AxiosRequestConfig
    if (axiosConfig) {
      config = {
        ...axiosConfig,
        ...this.requestConfig
      }
    } else {
      config = {
        baseURL: this.url,
        responseType: "text",
        ...this.requestConfig
      }
    }
    config.url = baseurl
    config.method = xhrmethod
    config.headers = headers
    config.data = postdata
    config.params = getdata
    // use the fetch adapter if fetch is available e.g. non Node<17 env
    if (typeof fetch !== "undefined") {
      config.adapter = fetchAdapter
    }
    const resp: AxiosResponse<any> = await axios.request(config)
    // purging all that is axios
    const xhrdata: RequestResponseData = new RequestResponseData(
      resp.data,
      resp.headers,
      resp.status,
      resp.statusText,
      resp.request
    )
    return xhrdata
  }
Example #3
Source File: Session.ts    From insta-fetcher with GNU General Public License v3.0 4 votes vote down vote up
getSessionId = async (username: username, password: password): Promise<session_id> => {
    if (typeof username !== 'string' || typeof password !== 'string') {
        throw new TypeError(`Expected a string, got ${typeof username !== 'string' ? typeof username : typeof password}`);
    }

    try {
        const csrfToken = await getCsrfToken();
        const genHeaders: AxiosRequestHeaders = {
            'X-CSRFToken': csrfToken,
            'user-agent': config.desktop,
            'cache-Control': 'no-cache',
            'content-type': 'application/x-www-form-urlencoded',
            referer: 'https://www.instagram.com/accounts/login/?source=auth_switcher',
            'authority': 'www.instagram.com',
            'origin': 'https://www.instagram.com',
            'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'x-ig-app-id': '936619743392459',
            'x-ig-www-claim': 'hmac.AR3W0DThY2Mu5Fag4sW5u3RhaR3qhFD_5wvYbOJOD9qaPjIf',
            'x-instagram-ajax': '1',
            'x-requested-with': 'XMLHttpRequest',
            'Cookie': 'csrftoken=' + csrfToken + ';'
        }
    
        const { headers, data }: AxiosResponse = await axios({
            method: 'POST',
            url: 'https://www.instagram.com/accounts/login/ajax/',
            data: `username=${username}&enc_password=#PWD_INSTAGRAM_BROWSER:0:${Date.now()}:${password}&queryParams=%7B%22source%22%3A%22auth_switcher%22%7D&optIntoOneTap=false`,
            headers: genHeaders
        });
    
        const { userId: userID, authenticated } = (data);
        if (authenticated) {           
            let session_id: session_id = headers["set-cookie"]?.find(x => x.match(/sessionid=(.*?);/)?.[1])?.match(/sessionid=(.*?);/)?.[1] || '';
            return session_id;
        } else {
            throw new Error('Username or password is incorrect. Please check and try again');
        }
    } catch (error: any | AxiosError) {
        if (axios.isAxiosError(error)) {
            throw error.toJSON()
        } else {
            throw error
        }
    }
}