got#GotReturn TypeScript Examples

The following examples show how to use got#GotReturn. 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: download.ts    From server with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Downloads a File from Drive using Streaming Request
 *
 * @param {ITokenDoc} token - Token Document from Database
 * @param {string} fileId - ID of the Folder or File
 * @returns {GotReturn} - Promise Resolving to File Data
 */
export default function (token: ITokenDoc, fileId: string): GotReturn {
  const apiUrl = api.get(fileId);
  const params = {
    alt: 'media',
  };
  return googleApiRequest.stream<TDriveUrlType>(apiUrl, token, params);
}
Example #2
Source File: requester.ts    From server with GNU General Public License v3.0 4 votes vote down vote up
googleRequest: IGoogleRequest = {
  /**
   * Makes a GET Google API Request
   *
   * @param {string} api - Google API URL
   * @param {ITokenDoc} token - Relevant Token Document from Database
   * @param {Record<string, string | number | boolean>} params - Data to be Embedded in Request
   * @param {Record<string, string>} headers - Additional Headers to be Sent
   * @returns {Promise<IGoogleResponse>} - Response from the API
   */
  get: async <T extends string, U = Record<string, unknown>>(
    api: T,
    token: ITokenDoc,
    params?: Record<string, string | number | boolean>,
    headers?: Record<string, string>,
  ): Promise<IGoogleResponse<U>> => {
    const url = constructURL<T>(api, params);
    const getHeaders = constructHeaders('get', token, headers);
    const response = await axios.get<U>(url, {
      headers: getHeaders,
    });
    return handleResponse<U>(response);
  },

  /**
   * Makes a POST Google API Request
   *
   * @param {string} api - Google API URL
   * @param {ITokenDoc} token - Relevant Token Document from Database
   * @param {Record<string, string | number | boolean>} data - Data to be sent in Request
   * @param {Record<string, string | number | boolean>} params - params to be attached to URL
   * @param {Record<string, string>} headers - Additional Headers to be Sent
   * @returns {Promise<IGoogleResponse>} - Response from the API
   */
  post: async <
    T extends string,
    U = Record<string, unknown>,
    V = Record<string, unknown>,
  >(
    api: T,
    token: ITokenDoc,
    data?: U,
    params?: Record<string, string>,
    headers?: Record<string, string>,
  ): Promise<IGoogleResponse<V>> => {
    const url = constructURL<T>(api, params);
    const getHeaders = constructHeaders('post', token, headers);
    const response = await axios.post<V>(url, data, {
      headers: getHeaders,
    });
    return handleResponse<V>(response);
  },

  /**
   * Makes a POST Google API Request
   *
   * @param {string} api - Google API URL
   * @param {ITokenDoc} token - Relevant Token Document from Database
   * @param {Record<string, string | number | boolean>} data - Data to be sent in Request
   * @param {Record<string, string | number | boolean>} params - params to be attached to URL
   * @param {Record<string, string>} headers - Additional Headers to be Sent
   * @returns {Promise<IGoogleResponse>} - Response from the API
   */
  patch: async <
    T extends string,
    U = Record<string, unknown>,
    V = Record<string, unknown>,
  >(
    api: T,
    token: ITokenDoc,
    data?: U,
    params?: Record<string, string>,
    headers?: Record<string, string>,
  ): Promise<IGoogleResponse<V>> => {
    const url = constructURL<T>(api, params);
    const getHeaders = constructHeaders('post', token, headers);
    const response = await axios.patch<V>(url, data, {
      headers: getHeaders,
    });
    return handleResponse<V>(response);
  },

  /**
   * Makes a DELETE Google API Request
   *
   * @param {string} api - Google API URL
   * @param {ITokenDoc} token - Relevant Token Document from Database
   * @param {Record<string, string | number | boolean>} data - Data to be sent in Request
   * @param {Record<string, string>} headers - Additional Headers to be Sent
   * @returns {Promise<IGoogleResponse>} - Response from the API
   */
  delete: async <T extends string, U = Record<string, unknown>>(
    api: T,
    token: ITokenDoc,
    data?: Record<string, unknown>,
    headers?: Record<string, string>,
  ): Promise<IGoogleResponse<U>> => {
    const url = constructURL<T>(api);
    const getHeaders = constructHeaders('post', token, headers);
    const response = await axios.delete<U>(url, {
      headers: getHeaders,
      data,
    });
    return handleResponse<U>(response);
  },

  /**
   * Makes a Streaming Request to Google API
   *
   * @param {string} api - Google API URL
   * @param {ITokenDoc} token - Relevant Token Document from Database
   * @param {Record<string, string | number | boolean>} params - params to be attached to URL
   * @returns {GotReturn} - Response from the API
   */
  stream: <T extends string>(
    api: T,
    token: ITokenDoc,
    params?: Record<string, string>,
  ): GotReturn => {
    const url = constructURL<T>(api, params);
    const getHeaders = constructHeaders('get', token);
    return got.stream(url, {
      headers: getHeaders,
    });
  },
}