Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Client

Client for teambition web service.

Hierarchy

Index

Constructors

constructor

Properties

Private _headers

_headers: Payload

Private _host

_host: string

Private _options

_options: ClientOptions

Private _query

_query: Payload

Private _requestOptions

_requestOptions: RequestOptions

Accessors

UA

  • get UA(): string
  • set UA(ua: string): void
  • Set User-Agent to the client.

    Returns string

    User-Agent on the client.

  • Set User-Agent to the client.

    Parameters

    • ua: string

      User-Agent string.

    Returns void

    User-Agent on the client.

headers

host

  • get host(): string

query

requestOptions

Methods

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

forwardHeaders

  • forwardHeaders(req: IncomingMessage | any, ...headers: string[]): this
  • Creates (by Object.create) a new client instance with headers copy from the request.

    Parameters

    • req: IncomingMessage | any

      IncomingMessage object that headers read from.

    • Rest ...headers: string[]

      headers that will be copy into client.

    Returns this

    a new client with with given headers.

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

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 & ExtraResponse>
  • 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 & ExtraResponse>

    a promise with Response

signAppToken

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

    Parameters

    • Default value periodical: number = 3600

      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.

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

  • 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

  • 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?: string): this & T
  • Creates (by Object.create) a new client instance with given service methods.

    Type parameters

    • T

    Parameters

    • serviceMethod: T
    • Default value servicehost: string = ""

      service host for new client.

    Returns this & T

    a new client with with given service methods.

withTenant

  • withTenant(tenantId: string, tenantType?: 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.

    • Default value tenantType: string = "organization"

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

    Returns this

    a new client with with given headers.

Static request

  • 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

    Returns Promise<Response>

    a promise with Response.

Generated using TypeDoc