Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

Index

Constructors

Properties

addMethod: RequestMethod = 'POST'

Default HTTP request method for the "add" method

advURL: string = ''

Advanced part of URL for a request of all request methods (it is concatenated with the base part)

baseAddURL?: string

Base part of URL for a request for the "add" method

example
class Profile extends Provider {
// baseURL request methods despite the "add" is used this URL
basePeekURL: 'profile/info'
baseAddURL: 'profile/info/add'
}
baseDelURL?: string

Base part of URL for a request for the "del" method

example
class Profile extends Provider {
// baseURL request methods despite the "del" is used this URL
basePeekURL: 'profile/info'
baseDelURL: 'profile/info/del'
}
baseGetURL?: string

Base part of URL for a request for the "get" method

example
class Profile extends Provider {
// For all request methods despite the "get" is used this URL
baseURL: 'profile/info'
baseGetURL: 'profile/info/get'
}
basePeekURL?: string

Base part of URL for a request for the "peek" method

example
class Profile extends Provider {
// For all request methods despite the "peek" is used this URL
baseURL: 'profile/info'
basePeekURL: 'profile/info/peek'
}
baseURL: string = ''

Base part of URL for a request of all request methods

example
class Profile extends Provider {
baseURL: 'profile/info'
}
baseUpdURL?: string

Base part of URL for a request for the "upd" method

example
class Profile extends Provider {
// baseURL request methods despite the "upd" is used this URL
basePeekURL: 'profile/info'
baseUpdURL: 'profile/info/upd'
}
customMethod: CanUndef<RequestMethod>

HTTP request method for all request methods. This parameter will override other method parameters, such as "getMethod" or "delMethod".

delMethod: RequestMethod = 'DELETE'

Default HTTP request method for the "del" method

eventName: CanUndef<ModelMethod>

Event name for requests. Please notice that all request methods except "get", "peek" and "request" emit events by default.

extraProviders?: FunctionalExtraProviders<unknown>

List of additional data providers for the "get" method. It can be useful if you have some providers that you want combine to one.

example
class User extends Provider {
baseURL: 'user/info',

extraProviders: {
balance: {
provider: 'UserBalance'
},

hobby: {
provider: 'UserHobby'
},
}
}
getMethod: RequestMethod = 'GET'

Default HTTP request method for the "get" method

globalEmitter: EventEmitter2 = emitter

Global event emitter to broadcast provider events

mocks?: Mocks
deprecated
see

[[Provider.mocks]]

peekMethod: RequestMethod = 'HEAD'

Default HTTP request method for the "peek" method

socketURL?: string

URL for a socket connection

updMethod: RequestMethod = 'PUT'

Default HTTP request method for the "upd" method

decoders: DecodersMap = {}

Map of data decoder sequences. The key of a map element represent a name of the provider method: 'get', 'post', etc. The value of a map element represent a sequence of decoders for the specified provider method.

see

Decoders

example
class MyProvider extends Provider {
static decoders = {
get: [fromJSON],
upd: [fromBuffer]
};
}
encoders: EncodersMap = {}

Map of data encoder sequences. The key of a map element represent a name of the provider method: 'get', 'post', etc. The value of a map element represent a sequence of encoders for the specified provider method.

see

Encoders

example
class MyProvider extends Provider {
static encoders = {
get: [convertToJSON],
upd: [convertToBuffer]
};
}
middlewares: Middlewares<unknown> = {}

Sequence of middlewares that is provided to the request function. An object form is easily to extend, bur you can choose any different form.

see

Middlewares

example
import request from 'core/request';

class Parent extends Provider {
static middlewares = {
attachGeoCoords() { ... }
};
}

class Children extends Parent {
static middlewares = {
...Parent.middlewares,
addSession() {
...
}
};
}
mocks?: Mocks

Map of data mocks. This object can be used with a middleware that implements API for data mocking, for example attachMock from 'core/data/middlewares'.

The key of a map element represent a method request type: 'GET', 'POST', etc. The value of a map element represent a list of parameters to match.

see

Middlewares

example
import { attachMock } from 'core/data/middlewares';

class MyProvider extends Provider {
static mocks = {
GET: [
// The mock for a GET request with a query parameter that contains
// `search=foo` parameter
{
status: 200,

// For the mock response won't be applied decoders
// (by default, `true`)
decoders: false,

query: {
search: 'foo'
},

// The response
response: {
data: [
'bla',
'baz
]
}
}
],

POST: [
// The mock is catches all POST requests and dynamically generated responses
{
response(params, response) {
if (!params.opts.query?.data) {
response.status = 400;
return;
}

response.status = 200;
response.responseType = 'string';
return 'ok';
}
}
]
};

static middlewares = {attachMock};
}
request: { <D>(path: string, opts?: CreateRequestOptions<D>): RequestPromise<D>; <D>(opts: CreateRequestOptions<D>): typeof __type; <D, A>(path: string, resolver: RequestResolver<D, A>, opts?: CreateRequestOptions<D>): RequestFunctionResponse<D, A extends infer V[] ? V[] : unknown[]> } = request

Type declaration

    • Creates a new remote request with the specified options

      example
      request('bla/get').then(({data, response}) => {
      console.log(data, response.status);
      });

      Type parameters

      • D = unknown

      Parameters

      Returns RequestPromise<D>

    • Returns a wrapped request constructor with the specified options. This overload helps to organize the "builder" pattern.

      example
      request({okStatuses: 200})({method: 'POST'})('bla/get').then(({data, response}) => {
      console.log(data, response.status);
      });

      Type parameters

      • D = unknown

      Parameters

      Returns typeof __type

    • Returns a function to create a new remote request with the specified options. This overload helps to create a factory of requests.

      example
      // Modifying the current URL
      request('https://foo.com', (url, env, ...args) => args.join('/'))('bla', 'baz') // https://foo.com/bla/baz

      // Replacing the current URL
      request('https://foo.com', () => ['https://bla.com', 'bla', 'baz'])() // https://bla.com/bla/baz

      Type parameters

      • D = unknown

      • A: any[] = unknown[]

      Parameters

      • path: string

        request path URL

      • resolver: RequestResolver<D, A>

        function to resolve a request: it takes a request URL, request environment, and arguments from invoking the outer function and can modify some request parameters. Also, if the function returns a new string, the string will be appended to the request URL, or if the function returns a string wrapped with an array, the string fully overrides the original URL.

      • Optional opts: CreateRequestOptions<D>

        request options

      Returns RequestFunctionResponse<D, A extends infer V[] ? V[] : unknown[]>

Accessors

  • get globalEvent(): EventEmitter2
  • Alias for the request function

    Returns { <D>(path: string, opts?: CreateRequestOptions<D>): RequestPromise<D>; <D>(opts: CreateRequestOptions<D>): typeof __type; <D, A>(path: string, resolver: RequestResolver<D, A>, opts?: CreateRequestOptions<D>): RequestFunctionResponse<D, A extends infer V[] ? V[] : unknown[]> }

      • Creates a new remote request with the specified options

        example
        request('bla/get').then(({data, response}) => {
        console.log(data, response.status);
        });

        Type parameters

        • D = unknown

        Parameters

        Returns RequestPromise<D>

      • Returns a wrapped request constructor with the specified options. This overload helps to organize the "builder" pattern.

        example
        request({okStatuses: 200})({method: 'POST'})('bla/get').then(({data, response}) => {
        console.log(data, response.status);
        });

        Type parameters

        • D = unknown

        Parameters

        Returns typeof __type

      • Returns a function to create a new remote request with the specified options. This overload helps to create a factory of requests.

        example
        // Modifying the current URL
        request('https://foo.com', (url, env, ...args) => args.join('/'))('bla', 'baz') // https://foo.com/bla/baz

        // Replacing the current URL
        request('https://foo.com', () => ['https://bla.com', 'bla', 'baz'])() // https://bla.com/bla/baz

        Type parameters

        • D = unknown

        • A: any[] = unknown[]

        Parameters

        • path: string

          request path URL

        • resolver: RequestResolver<D, A>

          function to resolve a request: it takes a request URL, request environment, and arguments from invoking the outer function and can modify some request parameters. Also, if the function returns a new string, the string will be appended to the request URL, or if the function returns a string wrapped with an array, the string fully overrides the original URL.

        • Optional opts: CreateRequestOptions<D>

          request options

        Returns RequestFunctionResponse<D, A extends infer V[] ? V[] : unknown[]>

Methods

  • Finds an element from an object by the specified parameters

    example
    class MyProvider extends Provider {}
    MyProvider.select([{test: 1}], {where: {test: 1}}) // {test: 1}

    Type parameters

    • T = unknown

    Parameters

    • obj: unknown

      object to search

    • params: SelectParams

      search parameters

    Returns CanUndef<T>