aurelia-fetch-client

  • Version 1.8.2
  • Published
  • 196 kB
  • 1 dependency
  • MIT license

Install

npm i aurelia-fetch-client
yarn add aurelia-fetch-client
pnpm add aurelia-fetch-client

Overview

A simple client based on the Fetch standard.

Index

Variables

variable retryStrategy

const retryStrategy: { fixed: 0; incremental: 1; exponential: 2; random: 3 };

    Functions

    function json

    json: (body: any, replacer?: any) => string;
    • Serialize an object to JSON. Useful for easily creating JSON fetch request bodies.

      Parameter body

      The object to be serialized to JSON.

      Parameter replacer

      The JSON.stringify replacer used when serializing.

      Returns

      A JSON string.

    Classes

    class HttpClient

    class HttpClient {}
    • An HTTP client based on the Fetch API.

    constructor

    constructor();
    • Creates an instance of HttpClient.

    property activeRequestCount

    activeRequestCount: number;
    • The current number of active requests. Requests being processed by interceptors are considered active.

    property baseUrl

    baseUrl: string;
    • The base URL set by the config.

    property defaults

    defaults: RequestInit;
    • The default request init to merge with values specified at request time.

    property interceptors

    interceptors: Interceptor[];
    • The interceptors to be run during requests.

    property isConfigured

    isConfigured: boolean;
    • Indicates whether or not the client has been configured.

    property isRequesting

    isRequesting: boolean;
    • Indicates whether or not the client is currently making one or more requests.

    method buildRequest

    buildRequest: (input: string | Request, init: RequestInit) => Request;

      method configure

      configure: (
      config:
      | RequestInit
      | HttpClientConfiguration
      | ((config: HttpClientConfiguration) => void)
      ) => HttpClient;
      • Configure this client with default settings to be used by all requests.

        Parameter config

        A configuration object, or a function that takes a config object and configures it.

        Returns

        The chainable instance of this HttpClient.

      method delete

      delete: (
      input: Request | string,
      body?: any,
      init?: RequestInit
      ) => Promise<Response>;
      • Calls fetch with request method set to DELETE.

        Parameter input

        The resource that you wish to fetch. Either a Request object, or a string containing the URL of the resource.

        Parameter body

        The body of the request.

        Parameter init

        An options object containing settings to be applied to the Request.

        Returns

        A Promise for the Response from the fetch request.

      method fetch

      fetch: (input: Request | string, init?: RequestInit) => Promise<Response>;
      • Starts the process of fetching a resource. Default configuration parameters will be applied to the Request. The constructed Request will be passed to registered request interceptors before being sent. The Response will be passed to registered Response interceptors before it is returned.

        See also https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

        Parameter input

        The resource that you wish to fetch. Either a Request object, or a string containing the URL of the resource.

        Parameter init

        An options object containing settings to be applied to the Request.

        Returns

        A Promise for the Response from the fetch request.

      method get

      get: (input: Request | string, init?: RequestInit) => Promise<Response>;
      • Calls fetch as a GET request.

        Parameter input

        The resource that you wish to fetch. Either a Request object, or a string containing the URL of the resource.

        Parameter init

        An options object containing settings to be applied to the Request.

        Returns

        A Promise for the Response from the fetch request.

      method patch

      patch: (
      input: Request | string,
      body?: any,
      init?: RequestInit
      ) => Promise<Response>;
      • Calls fetch with request method set to PATCH.

        Parameter input

        The resource that you wish to fetch. Either a Request object, or a string containing the URL of the resource.

        Parameter body

        The body of the request.

        Parameter init

        An options object containing settings to be applied to the Request.

        Returns

        A Promise for the Response from the fetch request.

      method post

      post: (
      input: Request | string,
      body?: any,
      init?: RequestInit
      ) => Promise<Response>;
      • Calls fetch with request method set to POST.

        Parameter input

        The resource that you wish to fetch. Either a Request object, or a string containing the URL of the resource.

        Parameter body

        The body of the request.

        Parameter init

        An options object containing settings to be applied to the Request.

        Returns

        A Promise for the Response from the fetch request.

      method put

      put: (
      input: Request | string,
      body?: any,
      init?: RequestInit
      ) => Promise<Response>;
      • Calls fetch with request method set to PUT.

        Parameter input

        The resource that you wish to fetch. Either a Request object, or a string containing the URL of the resource.

        Parameter body

        The body of the request.

        Parameter init

        An options object containing settings to be applied to the Request.

        Returns

        A Promise for the Response from the fetch request.

      class HttpClientConfiguration

      class HttpClientConfiguration {}
      • A class for configuring HttpClients.

      property baseUrl

      baseUrl: string;
      • The base URL to be prepended to each Request's url before sending.

      property defaults

      defaults: RequestInit;
      • Default values to apply to init objects when creating Requests. Note that defaults cannot be applied when Request objects are manually created because Request provides its own defaults and discards the original init object. See also https://developer.mozilla.org/en-US/docs/Web/API/Request/Request

      property interceptors

      interceptors: Interceptor[];
      • Interceptors to be added to the HttpClient.

      method rejectErrorResponses

      rejectErrorResponses: () => HttpClientConfiguration;
      • Causes Responses whose status codes fall outside the range 200-299 to reject. The fetch API only rejects on network errors or other conditions that prevent the request from completing, meaning consumers must inspect Response.ok in the Promise continuation to determine if the server responded with a success code. This method adds a response interceptor that causes Responses with error codes to be rejected, which is common behavior in HTTP client libraries.

        Returns

        The chainable instance of this configuration object.

      method useStandardConfiguration

      useStandardConfiguration: () => HttpClientConfiguration;
      • Applies a configuration that addresses common application needs, including configuring same-origin credentials, and using rejectErrorResponses.

        Returns

        The chainable instance of this configuration object.

      method withBaseUrl

      withBaseUrl: (baseUrl: string) => HttpClientConfiguration;
      • Sets the baseUrl.

        Parameter baseUrl

        The base URL.

        Returns

        The chainable instance of this configuration object.

      method withDefaults

      withDefaults: (defaults: RequestInit) => HttpClientConfiguration;
      • Sets the defaults.

        Parameter defaults

        The defaults.

        Returns

        The chainable instance of this configuration object.

      method withInterceptor

      withInterceptor: (interceptor: Interceptor) => HttpClientConfiguration;
      • Adds an interceptor to be run on all requests or responses.

        Parameter interceptor

        An object with request, requestError, response, or responseError methods. request and requestError act as resolve and reject handlers for the Request before it is sent. response and responseError act as resolve and reject handlers for the Response after it has been received.

        Returns

        The chainable instance of this configuration object.

      method withRetry

      withRetry: (config?: RetryConfiguration) => HttpClientConfiguration;

        class RetryInterceptor

        class RetryInterceptor implements Interceptor {}

          constructor

          constructor(retryConfig?: RetryConfiguration);

            property retryConfig

            retryConfig: RetryConfiguration;

              method request

              request: (request: Request) => Request;

                method response

                response: (response: Response, request?: Request) => Response;

                  method responseError

                  responseError: (
                  error: Response,
                  request?: Request,
                  httpClient?: HttpClient
                  ) => Promise<Response>;

                    Interfaces

                    interface Interceptor

                    interface Interceptor {}
                    • Interceptors can process requests before they are sent, and responses before they are returned to callers.

                    property request

                    request?: (request: Request) => Request | Response | Promise<Request | Response>;
                    • Called with the request before it is sent. Request interceptors can modify and return the request, or return a new one to be sent. If desired, the interceptor may return a Response in order to short-circuit the HTTP request itself.

                      Parameter request

                      The request to be sent.

                      Returns

                      The existing request, a new request or a response; or a Promise for any of these.

                    property requestError

                    requestError?: (error: any) => Request | Response | Promise<Request | Response>;
                    • Handles errors generated by previous request interceptors. This function acts as a Promise rejection handler. It may rethrow the error to propagate the failure, or return a new Request or Response to recover.

                      Parameter error

                      The rejection value from the previous interceptor.

                      Returns

                      The existing request, a new request or a response; or a Promise for any of these.

                    property response

                    response?: (
                    response: Response,
                    request?: Request
                    ) => Response | Promise<Response>;
                    • Called with the response after it is received. Response interceptors can modify and return the Response, or create a new one to be returned to the caller.

                      Parameter response

                      The response.

                      Returns

                      The response; or a Promise for one.

                    property responseError

                    responseError?: (
                    error: any,
                    request?: Request,
                    httpClient?: HttpClient
                    ) => Response | Promise<Response>;
                    • Handles fetch errors and errors generated by previous interceptors. This function acts as a Promise rejection handler. It may rethrow the error to propagate the failure, or return a new Response to recover.

                      Parameter error

                      The rejection value from the fetch request or from a previous interceptor.

                      Returns

                      The response; or a Promise for one.

                    interface RequestInit

                    interface RequestInit {}
                    • The init object used to initialize a fetch Request. See https://developer.mozilla.org/en-US/docs/Web/API/Request/Request

                    property body

                    body?:
                    | Blob
                    | BufferSource
                    | FormData
                    | URLSearchParams
                    | ReadableStream
                    | string
                    | null;
                    • Any body that you want to add to your request: this can be a Blob, BufferSource, FormData, URLSearchParams, ReadableStream, or USVString object.

                      Note that a request using the GET or HEAD method cannot have a body.

                    property cache

                    cache?: string;
                    • The cache mode you want to use for the request: default, no-store, reload, no-cache, or force-cache.

                    property credentials

                    credentials?: string;
                    • The request credentials you want to use for the request: omit, same-origin, or include. The default is omit.

                      In Chrome the default is same-origin before Chrome 47 and include starting with Chrome 47.

                    property headers

                    headers?: Headers | Object;
                    • Any headers you want to add to your request, contained within a Headers object or an object literal with ByteString values.

                    property integrity

                    integrity?: string;
                    • Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).

                    property method

                    method?: string;
                    • The request method, e.g., GET, POST.

                    property mode

                    mode?: string;
                    • The mode you want to use for the request, e.g., cors, no-cors, same-origin, or navigate. The default is cors.

                      In Chrome the default is no-cors before Chrome 47 and same-origin starting with Chrome 47.

                    property redirect

                    redirect?: string;
                    • The redirect mode to use: follow, error, or manual.

                      In Chrome the default is follow before Chrome 47 and manual starting with Chrome 47.

                    property referrer

                    referrer?: string;
                    • A USVString specifying no-referrer, client, or a URL. The default is client.

                    property signal

                    signal?: AbortSignal | null;
                    • An AbortSignal to set request’s signal.

                    interface RetryConfiguration

                    interface RetryConfiguration {}

                      property beforeRetry

                      beforeRetry?: (
                      request: Request,
                      client: HttpClient
                      ) => Request | Promise<Request>;

                        property counter

                        counter?: number;

                          property doRetry

                          doRetry?: (response: Response, request: Request) => boolean | Promise<boolean>;

                            property interval

                            interval?: number;

                              property maxRandomInterval

                              maxRandomInterval?: number;

                                property maxRetries

                                maxRetries: number;

                                  property minRandomInterval

                                  minRandomInterval?: number;

                                    property requestClone

                                    requestClone?: Request;

                                      property strategy

                                      strategy?: number | ((retryCount: number) => number);

                                        Type Aliases

                                        type ValidInterceptorMethodName

                                        type ValidInterceptorMethodName = keyof Interceptor;

                                          Package Files (1)

                                          Dependencies (1)

                                          Dev Dependencies (22)

                                          Peer Dependencies (0)

                                          No peer dependencies.

                                          Badge

                                          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/aurelia-fetch-client.

                                          • Markdown
                                            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/aurelia-fetch-client)
                                          • HTML
                                            <a href="https://www.jsdocs.io/package/aurelia-fetch-client"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>