Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Fetcher

HTTP client for the browser, Node or React Native. Created by AltogicClient during initialization. The client library uses cross-fetch under the hood to make requests to you app's execution environment.

When creating the client if apiKey is specified in ClientOptions, Fetcher adds the provided apiKey to an Authorization header and sends it in all RESTful API requests to your backend app.

Similarly, if there is an active user session, Fetcher also adds the session token to a Session header and sends it in all RESTful API requests to your backend app.

export

Hierarchy

  • Fetcher

Index

Constructors

  • Creates an instance of Fetcher.

    Parameters

    • apiClient: AltogicClient
    • restUrl: string

      The base URL that will be prepended to all RESTful API calls

    • headers: KeyValuePair

      The default headers that will be sent in each RESTful API request to the app's execution environment

    Returns Fetcher

Properties

apiClient: AltogicClient

Reference to the Altogic client object

headers: KeyValuePair

The default headers that will be sent in each RESTful API request to the execution environment

restUrl: string

The base URL that will be prepended to all RESTful API calls

session: null | Session

The user session information. After the user is signed in using its app credentials or any Oauth2 provider credentials and if the session is created for this sign in then the user session information is stored in this field. If a session is available then the session token is added to the default headers of the Fetcher

Methods

  • #handleRequest(method: "GET" | "POST" | "PUT" | "DELETE", path: string, query: null | KeyValuePair, headers: null | KeyValuePair, body: any, resolveType?: "text" | "arraybuffer" | "blob" | "json"): Promise<{ data: any; errors: null | APIError }>
  • Internal method to handle all public request methods (get, post, put and delete). If the request response is an invalid session token error, invalidates the current user session.

    Parameters

    • method: "GET" | "POST" | "PUT" | "DELETE"

      The request method

    • path: string

      The path of the request, needs to start with a slash 'character' e.g., /users,

    • query: null | KeyValuePair

      The query string parameters that will be sent to your app backend. This is a simpel JSON object with key and value pairs.

    • headers: null | KeyValuePair

      Additional request headers which will be merged with default headers

    • body: any

      Request body if any. If provided can be a JSON, FormData, Blob or File object. For file uploads you can use FormData, Blob or File object.

    • resolveType: "text" | "arraybuffer" | "blob" | "json" = "json"

      Type of data to return as a response of the request. By default response data is parsed to JSON. Possible values are json, text, blob and arraybuffer.

    Returns Promise<{ data: any; errors: null | APIError }>

    Returns a promise. The returned response includes two components data and errors. If errors occured during the execution of the request then errors object is returned and tha data is marked as null. If no errors occured then depending on the type of the request the data object holds a single JSON object, an array of json objects, plain text, Blob or ArrayBuffer and the errors object is marked as null. If the response returns no data back then both erros and data marked as null.

  • clearSession(): void
  • Clears the session info of the user from the Fetcher. Basically removes the Session header from the default request headers until a new session value is provided.

    Returns void

    void

  • delete(path: string, body?: null | object | FormData, query?: null | KeyValuePair, headers?: null | KeyValuePair, resolveType?: "text" | "arraybuffer" | "blob" | "json"): Promise<{ data: any; errors: null | APIError }>
  • Makes a DELETE request to backend app execution environment.

    Parameters

    • path: string

      The path of the request that will be appended to the {restUrl}

    • body: null | object | FormData = null

      The body of the request

    • query: null | KeyValuePair = {}

      Query string parameters as key:value pair object

    • headers: null | KeyValuePair = {}

      Additional request headers that will be sent with the request

    • resolveType: "text" | "arraybuffer" | "blob" | "json" = "json"

      Type of data to return as a response of the request. By default response data is parsed to JSON. Possible values are json, text, blob and arraybuffer.

    Returns Promise<{ data: any; errors: null | APIError }>

    Returns a promise. The returned response includes two components data and errors. If errors occured during the execution of the request then errors object is returned and tha data is marked as null. If no errors occured then depending on the type of the request the data object holds a single JSON object, an array of json objects, plain text, Blob or ArrayBuffer and the errors object is marked as null. If the response returns no data back then both erros and data marked as null.

  • get(path: string, query?: null | KeyValuePair, headers?: null | KeyValuePair, resolveType?: "text" | "arraybuffer" | "blob" | "json"): Promise<{ data: any; errors: null | APIError }>
  • Makes a GET request to backend app execution environment.

    Parameters

    • path: string

      The path of the request that will be appended to the {restUrl}

    • query: null | KeyValuePair = {}

      Query string parameters as key:value pair object

    • headers: null | KeyValuePair = {}

      Additional request headers that will be sent with the request

    • resolveType: "text" | "arraybuffer" | "blob" | "json" = "json"

      Type of data to return as a response of the request. By default response data is parsed to JSON. Possible values are json, text, blob and arraybuffer.

    Returns Promise<{ data: any; errors: null | APIError }>

    Returns a promise. The returned response includes two components data and errors. If errors occured during the execution of the request then errors object is returned and tha data is marked as null. If no errors occured then depending on the type of the request the data object holds a single JSON object, an array of json objects, plain text, Blob or ArrayBuffer and the errors object is marked as null. If the response returns no data back then both erros and data marked as null.

  • getBaseUrl(): string
  • getClientKey(): string
  • getSessionToken(): string
  • post(path: string, body?: null | object | FormData, query?: null | KeyValuePair, headers?: null | KeyValuePair, resolveType?: "text" | "arraybuffer" | "blob" | "json"): Promise<{ data: any; errors: null | APIError }>
  • Makes a POST request to backend app execution environment.

    Parameters

    • path: string

      The path of the request that will be appended to the {restUrl}

    • body: null | object | FormData = null

      The body of the request

    • query: null | KeyValuePair = {}

      Query string parameters as key:value pair object

    • headers: null | KeyValuePair = {}

      Additional request headers that will be sent with the request

    • resolveType: "text" | "arraybuffer" | "blob" | "json" = "json"

      Type of data to return as a response of the request. By default response data is parsed to JSON. Possible values are json, text, blob and arraybuffer.

    Returns Promise<{ data: any; errors: null | APIError }>

    Returns a promise. The returned response includes two components data and errors. If errors occured during the execution of the request then errors object is returned and tha data is marked as null. If no errors occured then depending on the type of the request the data object holds a single JSON object, an array of json objects, plain text, Blob or ArrayBuffer and the errors object is marked as null. If the response returns no data back then both erros and data marked as null.

  • put(path: string, body?: null | object | FormData, query?: null | KeyValuePair, headers?: null | KeyValuePair, resolveType?: "text" | "arraybuffer" | "blob" | "json"): Promise<{ data: any; errors: null | APIError }>
  • Makes a PUT request to backend app execution environment.

    Parameters

    • path: string

      The path of the request that will be appended to the {restUrl}

    • body: null | object | FormData = null

      The body of the request

    • query: null | KeyValuePair = {}

      Query string parameters as key:value pair object

    • headers: null | KeyValuePair = {}

      Additional request headers that will be sent with the request

    • resolveType: "text" | "arraybuffer" | "blob" | "json" = "json"

      Type of data to return as a response of the request. By default response data is parsed to JSON. Possible values are json, text, blob and arraybuffer.

    Returns Promise<{ data: any; errors: null | APIError }>

    Returns a promise. The returned response includes two components data and errors. If errors occured during the execution of the request then errors object is returned and tha data is marked as null. If no errors occured then depending on the type of the request the data object holds a single JSON object, an array of json objects, plain text, Blob or ArrayBuffer and the errors object is marked as null. If the response returns no data back then both erros and data marked as null.

  • setSession(session: null | Session): void
  • Sets the session of the user that will be used by Fetcher. Adds the new session token to the Session request header.

    Parameters

    • session: null | Session

      Session info object

    Returns void

    void

  • upload(path: string, body: any, query?: null | KeyValuePair, headers?: null | KeyValuePair, progressCallback?: any): Promise<{ data: any; errors: null | APIError }>
  • Uploads a file using XMLHttpRequest object instead of fetcher in order to track upload progress and call a callback function.

    Parameters

    • path: string

      The path of the request that will be appended to the {restUrl}

    • body: any

      The body of the request

    • query: null | KeyValuePair = {}

      Query string parameters as key:value pair object

    • headers: null | KeyValuePair = {}

      Additional request headers that will be sent with the request

    • progressCallback: any = null

      Callback function that will be called during file upload to inform the progres

    Returns Promise<{ data: any; errors: null | APIError }>

    Returns a promise. The returned response includes two components data and errors. If errors occured during the execution of the request then errors object is returned and tha data is marked as null. If no errors occured then a single JSON object providing information about the uploaded file is returned and the errors object is marked as null.

Generated using TypeDoc