Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HTTPClient

Hierarchy

  • Client
    • HTTPClient

Index

Constructors

constructor

  • new HTTPClient(options: ClientOptions & RetryOptions): HTTPClient
  • Parameters

    • options: ClientOptions & RetryOptions

    Returns HTTPClient

Properties

UA

UA: string

Set User-Agent to the client.

param

User-Agent string.

headers

headers: Payload
returns

preset headers on the client.

host

host: string
returns

host on the client.

query

query: Payload
returns

preset query on the client.

requestOptions

requestOptions: RequestOptions
returns

preset request options on the client.

Methods

batchSubscribe

  • batchSubscribe<T>(body: any): Promise<T>
  • Type parameters

    • T

    Parameters

    • body: any

    Returns Promise<T>

decodeToken

  • decodeToken(token: string, options?: jwt.DecodeOptions): Payload
  • Decode a JWT token string to literal object payload.

    Parameters

    • token: string

      token to decode.

    • Optional options: jwt.DecodeOptions

      some JWT decode options.

    Returns Payload

    a literal object.

delete

  • delete<T>(url: string, data?: any): Promise<T>
  • request with DELETE method.

    Type parameters

    • T

    Parameters

    • url: string
    • Optional data: any

    Returns Promise<T>

    a promise with Response body

get

  • get<T>(url: string, data?: any): Promise<T>
  • request with GET method.

    Type parameters

    • T

    Parameters

    • url: string
    • Optional data: any

    Returns Promise<T>

    a promise with Response body

getUserClients

  • getUserClients<T>(userId: string): Promise<T>
  • Type parameters

    • T

    Parameters

    • userId: string

    Returns Promise<T>

heartBeat

  • heartBeat<T>(): Promise<T>
  • Type parameters

    • T

    Returns Promise<T>

patch

  • patch<T>(url: string, data?: any): Promise<T>
  • request with PATCH method.

    Type parameters

    • T

    Parameters

    • url: string
    • Optional data: any

    Returns Promise<T>

    a promise with Response body

post

  • post<T>(url: string, data?: any): Promise<T>
  • request with POST method.

    Type parameters

    • T

    Parameters

    • url: string
    • Optional data: any

    Returns Promise<T>

    a promise with Response body

put

  • put<T>(url: string, data?: any): Promise<T>
  • request with PUT method.

    Type parameters

    • T

    Parameters

    • url: string
    • Optional data: any

    Returns Promise<T>

    a promise with Response body

request

  • request(method: string, url: string, data?: any): Promise<Response>
  • request with given method, url and data. It will genenrate a jwt token by signToken, and set to 'Authorization' header. It will merge headers, query and request options that preset into client.

    Parameters

    • method: string

      method to request.

    • url: string

      url to request, it will be resolved with client host.

    • Optional data: any

      data to request.

    Returns Promise<Response>

    a promise with Response

send

  • send<T>(body: any): Promise<T>
  • Type parameters

    • T

    Parameters

    • body: any

    Returns Promise<T>

sign

  • sign<T>(userId: string, source: string): Promise<T>
  • Type parameters

    • T

    Parameters

    • userId: string
    • source: string

    Returns Promise<T>

signAppToken

  • signAppToken(periodical?: undefined | number, options?: jwt.SignOptions): string
  • Creates a periodical changed JWT token string with appId and appSecrets.

    Parameters

    • Optional periodical: undefined | number

      period in seccond, default to 3600s.

    • Optional options: jwt.SignOptions

      some JWT sign options.

    Returns string

    a token string.

signToken

  • signToken(payload: Payload, options?: jwt.SignOptions): string
  • Creates a JWT token string with given payload and client's appSecrets.

    Parameters

    • payload: Payload

      Payload to sign, should be an literal object.

    • Optional options: jwt.SignOptions

      some JWT sign options.

    Returns string

    a token string.

subscribe

  • subscribe<T>(topic: string, sessionId: string): Promise<T>
  • Type parameters

    • T

    Parameters

    • topic: string
    • sessionId: string

    Returns Promise<T>

unsubscribe

  • unsubscribe<T>(topic: string, sessionId: string): Promise<T>
  • Type parameters

    • T

    Parameters

    • topic: string
    • sessionId: string

    Returns Promise<T>

verifyToken

  • verifyToken(token: string, options?: jwt.VerifyOptions): Payload
  • Decode and verify a JWT token string to literal object payload. if verify failure, it will throw a 401 error (creates by 'http-errors' module)

    Parameters

    • token: string

      token to decode.

    • Optional options: jwt.VerifyOptions

      some JWT verify options.

    Returns Payload

    a literal object.

withHeaders

  • withHeaders(headers: Payload): this
  • Creates (by Object.create) a new client instance with given headers.

    Parameters

    • headers: Payload

      headers that will be copy into client.

    Returns this

    a new client with with given headers.

withOperator

  • withOperator(operatorId: string): this
  • Creates (by withHeaders) a new client instance with given X-Operator-ID.

    Parameters

    • operatorId: string

      that will be added to header as X-Operator-ID.

    Returns this

    a new client with with given headers.

withOptions

  • withOptions(options: RequestOptions): this
  • Creates (by Object.create) a new client instance with given request options.

    Parameters

    • options: RequestOptions

      request options that will be copy into client.

    Returns this

    a new client with with given request options.

withQuery

  • withQuery(query: Payload): this
  • Creates (by Object.create) a new client instance with given query.

    Parameters

    • query: Payload

      query that will be copy into client.

    Returns this

    a new client with with given query.

withService

  • withService<T>(serviceMethod: T, servicehost?: undefined | string): this & T
  • Creates (by Object.create) a new client instance with given service methods.

    Type parameters

    • T

    Parameters

    • serviceMethod: T
    • Optional servicehost: undefined | string

      service host for new client.

    Returns this & T

    a new client with with given service methods.

withTenant

  • withTenant(tenantId: string, tenantType?: undefined | string): this
  • Creates (by withHeaders) a new client instance with given X-Tenant-Id and X-Tenant-Type.

    Parameters

    • tenantId: string

      that will be added to header as X-Tenant-Id.

    • Optional tenantType: undefined | string

      that will be added to header as X-Tenant-Type.

    Returns this

    a new client with with given headers.

Static request

  • request(options: RequestOptions & request.UrlOptions): Promise<Response>
  • a retryable request, wrap of https://github.com/request/request. When the connection fails with one of ECONNRESET, ENOTFOUND, ESOCKETTIMEDOUT, ETIMEDOUT, ECONNREFUSED, EHOSTUNREACH, EPIPE, EAI_AGAIN, the request will automatically be re-attempted as these are often recoverable errors and will go away on retry.

    Parameters

    • options: RequestOptions & request.UrlOptions

      request options.

    Returns Promise<Response>

    a promise with Response.

Generated using TypeDoc