Options
All
  • Public
  • Public/Protected
  • All
Menu

Class that contains functionalities related to the HTTP protocol and its most common requests

Hierarchy

  • HTTPManager

Index

Constructors

constructor

  • Class that contains functionalities related to the HTTP protocol and its most common requests

    Parameters

    • Default value asynchronous: boolean = true

      Specify if the HTTP manager instance will work in asynchronous or synchronous mode. (Synchronous mode is NOT recommended on client side languages)

    Returns HTTPManager

Properties

Private _globalPostParams

_globalPostParams: {}

A list of key value pairs that define post parameters that will be sent ALWAYS with all the requests that are performed by this class. We can use this feature for example to always send a token to web services, or other globally sent post values

Type declaration

  • [key: string]: any

Private _queues

_queues: { isRunning: boolean; name: string; pendingRequests: HTTPManagerBaseRequest[] }[] = []

Structure containing all the created request queues and their status

asynchronous

asynchronous: boolean = true

Defines if the http comunications made by this class will be synchronous (code execution will be stopped while waiting for the response) or asynchronous (execution flow will continue and response will be processed once received) Note: Synchronous requests are normally NOT, NOT a good idea on client side languages

baseUrl

baseUrl: string = ""

If we want to use relative urls on all the requests that are executed by this class, we can define here a root url. All the request urls will then be composed as baseUrl + requestUrl.

This property is useful when all the requests in our application share the same root url, which can be defined here.

internetCheckLocations

internetCheckLocations: string[] = ['https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js','https://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js','https://code.jquery.com/jquery-3.2.1.slim.min.js']

Defines a list with internet urls that will be used to test network availability by the isInternetAvailable() method. We mainly use globally available CDN urls, cause these are not blocked by cross-orining policies on the browsers and are widely available and replicated. It may be interesting to add your own server side url at the bengining of this list, so it will be the first one to be tested, and you will also check that your server is correctly responding. Note that when an url request is successful, process ends and internet connection is considered to be working.

isOnlyHttps

isOnlyHttps: boolean = true

If this flag is enabled, any request that is made by this service which uses http:// instead of https:// will throw an exception. When disabled, non secure http:// requests will be allowed

timeout

timeout: number = 0

Defines how much miliseconds will the http requests wait before failing with a timeout. If set to 0, no value will be specifically defined, so the platform default will be used.

Static Private ERROR_TIMEOUT

ERROR_TIMEOUT: string = " ms Timeout reached"

Error message that is used when a timeout happens

Methods

Private _composeUrl

  • _composeUrl(baseUrl: string, relativeUrl: string): string
  • Auxiliary method to join two urls: A base one, and a relative one

    If a full absolute url is passed to the relativeUrl variable, the result of this method will be the relative one, ignoring any possible value on baseUrl.

    Parameters

    • baseUrl: string
    • relativeUrl: string

    Returns string

Private _executeXmlHttprequestSend

  • _executeXmlHttprequestSend(xmlHttprequest: XMLHttpRequest, url: string): void
  • Auxiliary method to call the send method for an XMLHttpRequest with more explanatory error checking

    NOTE: this method is exclusive for the typescript / javascript versions of turbocommons

    Parameters

    • xmlHttprequest: XMLHttpRequest
    • url: string

    Returns void

Private _generateValidRequestsList

Private _startQueue

  • _startQueue(name: string): void
  • Auxiliary method that is used to begin executing the http requests that are pending on the specified queue. A recursive operation will be used to launch the next http request once the previous has totally finished.

    Parameters

    • name: string

      The name for the queue we want to start

    Returns void

    void

countQueues

  • countQueues(): number
  • Get the number of created queues. Some may be running and some may be not

    see

    this.queue()

    Returns number

    The number of existing queues

createQueue

  • createQueue(name: string): void
  • Create a new http queue. Requests can then be added to this queue with the queue() method.

    see

    this.queue()

    Parameters

    • name: string

      The name we want to define for this queue

    Returns void

    void

deleteGlobalPostParam

  • deleteGlobalPostParam(parameterName: string): void
  • Delete a previously created global POST parameter so it is not sent with all the http manager requests anymore

    Parameters

    • parameterName: string

      The name of the POST parameter that will be deleted

    Returns void

deleteQueue

  • deleteQueue(name: string): void
  • Remove the specified queue from this manager. Make sure the queue is not running when calling this method, or an exception will happen

    see

    this.queue()

    Parameters

    • name: string

      The name for the queue we want to remove

    Returns void

    void

execute

  • execute(requests: string | string[] | HTTPManagerBaseRequest | HTTPManagerBaseRequest[], finishedCallback?: ((results: { code: number; errorMsg: string; isError: boolean; response: any; url: string }[], anyError: boolean) => void) | null, progressCallback?: null | ((completedUrl: string, totalRequests: number) => void)): void
  • Launch one or more http requests without caring about their execution order.

    Parameters

    • requests: string | string[] | HTTPManagerBaseRequest | HTTPManagerBaseRequest[]

      One or more requests to be inmediately launched (at the same time if possible). Each request can be defined as a string that will be used as a GET request url, or as an HTTPManagerBaseRequest instance in case we want to define parameters and callbacks.

    • Default value finishedCallback: ((results: { code: number; errorMsg: string; isError: boolean; response: any; url: string }[], anyError: boolean) => void) | null = null

      A method to be executed once all the http requests have finished (either succesfully or with errors). The callback will receive two parameters: results (an array with information about each request result in the same order as provided to this method) and anyError (true if any of the requests has failed)

    • Default value progressCallback: null | ((completedUrl: string, totalRequests: number) => void) = null

      Executed after each one of the urls finishes (either successfully or with an error). A string with the requested url and the total requests to perform will be passed to this method.

    Returns void

    void

generateUrlQueryString

  • generateUrlQueryString(keyValuePairs: {} | HashMapObject): string
  • This method generates a GET url query from a set of key/value pairs

    A query string is the part of an url that contains the GET parameters. It is placed after the ? symbol and contains a list of parameters and values that are sent to the url.

    see

    https://en.wikipedia.org/wiki/Query_string

    see

    HashMapObject

    Parameters

    • keyValuePairs: {} | HashMapObject

      An object or a HashMapObject containing key/value pairs that will be used to construct the query string. Note that when a value is an object or array, it will be encoded as a JSON string on the resulting query

    Returns string

    A valid query string that can be used with any url: http://www.url.com?query_string (Note that ? symbol is not included)

getGlobalPostParam

  • getGlobalPostParam(parameterName: string): any
  • Get the value for a previously defined global POST parameter

    Parameters

    • parameterName: string

      The name of the POST parameter that we want to read

    Returns any

    The parameter value

getUrlHeaders

  • getUrlHeaders(url: string, successCallback: (headersArray: string[]) => void, errorCallback: (errorMsg: string, errorCode: number) => void): void
  • Get the Http headers for a given url. Note that crossdomain security rules may prevent this method from working correctly

    Parameters

    • url: string

      The url for which we want to get the http headers.

    • successCallback: (headersArray: string[]) => void

      Executed when headers are read. An array of strings will be passed to this method containing all the read headers with each header line as an array element.

        • (headersArray: string[]): void
        • Parameters

          • headersArray: string[]

          Returns void

    • errorCallback: (errorMsg: string, errorCode: number) => void

      Executed if headers cannot be read. A string containing the error description and the error code will be passed to this method.

        • (errorMsg: string, errorCode: number): void
        • Parameters

          • errorMsg: string
          • errorCode: number

          Returns void

    Returns void

    void

isGlobalPostParam

  • isGlobalPostParam(parameterName: string): boolean
  • Check if the specified parameter name is defined as a global POST parameter

    Parameters

    • parameterName: string

      The name of the POST parameter that we want to check

    Returns boolean

    True if the parameter exists, false otherwise

isInternetAvailable

  • isInternetAvailable(yesCallback: () => void, noCallback: () => void): void
  • Tells if there's currently a working internet connection available or not.

    Parameters

    • yesCallback: () => void

      Executed if the internet connection is available and working

        • (): void
        • Returns void

    • noCallback: () => void

      Executed if the internet connection is NOT available

        • (): void
        • Returns void

    Returns void

    void

isQueueRunning

  • isQueueRunning(name: string): boolean
  • Check if the specified queue is currently executing http requests

    see

    this.queue()

    Parameters

    • name: string

      The name for the queue we want to check

    Returns boolean

    boolean True if the specified queue is actually running its http requests

loadResourcesFromList

  • loadResourcesFromList(urlToListOfResources: string, baseUrl: string, successCallback: (resourcesList: string[], resourcesData: string[]) => void, errorCallback: (errorUrl: string, errorMsg: string, errorCode: number) => void, progressCallback?: ((completedUrl: string) => void) | null): void
  • Given a url with a list of resources (normally files), this method will perform a request for each one of them and store the whole file contents as an element of a result array. After all the process completes, the array containing all the loaded data will be available by the successCallback method.

    This is a technique that allows us to read a big list of files from an http server without needing to write much code. We simply put the files on the server, create a list with all the file names, provide the base url for all the files, and call this method. When the process succeeds, we will have all the files data loaded and ready to be used. We have also a progress callback that will notify us when each one of the files is correctly loaded.

    Parameters

    • urlToListOfResources: string

      An url that gives us the list of resources to be loaded (normally a plain list of file names)

    • baseUrl: string

      A url that will be used as the root for all the files of the list when the load is performed. This usually is the path to the url folder that contains the files. Each request to a file will be composed with this baseUrl + the respective entry of the file on urlToListOfResources

    • successCallback: (resourcesList: string[], resourcesData: string[]) => void

      Executed once all the resources have been loaded. Two parameters will be passed to this method: An array with The list of resources as they are defined on the urlToListOfResources, and an array containing all the data for each one of these resources.

        • (resourcesList: string[], resourcesData: string[]): void
        • Parameters

          • resourcesList: string[]
          • resourcesData: string[]

          Returns void

    • errorCallback: (errorUrl: string, errorMsg: string, errorCode: number) => void

      Executed if a failure happens on any of the requests. The url that caused the error, the error description and the error code will be passed to this method.

        • (errorUrl: string, errorMsg: string, errorCode: number): void
        • Parameters

          • errorUrl: string
          • errorMsg: string
          • errorCode: number

          Returns void

    • Default value progressCallback: ((completedUrl: string) => void) | null = null

      Executed after each one of the resources is correctly loaded. A string with the correctly requested url will be passed to this method.

    Returns void

    void

queue

  • Sequentially launch one or more http requests to the specified queue, one after the other. Each request will start inmediately after the previous one is finished (either succesfully or with an error). We can have several independent queues that run their requests at the same time.

    Parameters

    • requests: string | string[] | HTTPManagerBaseRequest | HTTPManagerBaseRequest[]

      One or more requests that must be added to the specified queue. Each request can be defined as a string that will be used as a GET request url, or as an HTTPManagerBaseRequest instance in case we want to define parameters and callbacks. Requests will be sequentially executed one after the other in the same order. If the specified queue contains requests that have not finished yet, they will be executed before the ones provided here.

    • queueName: string

      The name for an existing queue (created with this.createQueue()) where the specified requests will be added

    • Default value finishedCallback: (() => void) | null = null

    Returns void

    void

setGlobalPostParam

  • setGlobalPostParam(parameterName: string, value: string): void
  • Set the value for a POST parameter that will be stored as a global POST parameter which will be always sent with all the http manager requests

    Parameters

    • parameterName: string

      The name of the POST parameter that will be always sent to all the http requests

    • value: string

      The value that the POST parameter will have

    Returns void

urlExists

  • urlExists(url: string, yesCallback: () => void, noCallback: () => void): void
  • Test if the specified url exists by trying to connect to it. Note that crossdomain security rules may prevent this method from working correctly if you try to check the existence of an url that does not allow CORS outside your application domain.

    Parameters

    • url: string

      A full valid internet address to check

    • yesCallback: () => void

      Executed if the url exists

        • (): void
        • Returns void

    • noCallback: () => void

      Executed if the url does not exist (or is not accessible).

        • (): void
        • Returns void

    Returns void

    void

Generated using TypeDoc