///
import { IncomingMessage } from 'http';
import request from 'request';
import jwt from 'jsonwebtoken';
/**
* Options for request retrying.
*/
export interface RetryOptions {
retryDelay?: number;
maxAttempts?: number;
retryErrorCodes?: string[];
}
/**
* Extra attributes for response.
*/
export interface ExtraResponse {
attempts: number;
originalUrl: string;
originalMethod: string;
}
/**
* Options for request Client.
*/
export interface ClientOptions {
appId: string;
appSecrets: string[];
timeout?: number;
host?: string;
pool?: any;
maxSockets?: number;
strictSSL?: boolean;
time?: boolean;
certChain?: Buffer;
privateKey?: Buffer;
rootCert?: string | Buffer | string[] | Buffer[];
useQuerystring?: boolean;
}
export interface Payload {
[key: string]: any;
}
export declare type RequestOptions = request.CoreOptions & RetryOptions;
export declare type Response = request.Response & ExtraResponse;
/**
* Client for teambition web service.
*/
export declare class Client {
/**
* 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.
* @param options request options.
* @returns a promise with Response.
*/
static request(options: RequestOptions & request.UrlOptions): Promise;
private _options;
private _host;
private _headers;
private _query;
private _requestOptions;
constructor(options: ClientOptions & RetryOptions);
/**
* @returns User-Agent on the client.
*/
get UA(): string;
/**
* Set User-Agent to the client.
* @param ua User-Agent string.
*/
set UA(ua: string);
/**
* @returns host on the client.
*/
get host(): string;
/**
* @returns preset headers on the client.
*/
get headers(): Payload;
/**
* @returns preset query on the client.
*/
get query(): Payload;
/**
* @returns preset request options on the client.
*/
get requestOptions(): RequestOptions;
/**
* Creates (by Object.create) a **new client** instance with given service methods.
* @param servicePrototype service methods that will be mount to client.
* @param servicehost service host for new client.
* @returns a **new client** with with given service methods.
*/
withService(serviceMethod: T, servicehost?: string): this & T;
/**
* Creates (by Object.create) a **new client** instance with given request options.
* @param options request options that will be copy into client.
* @returns a **new client** with with given request options.
*/
withOptions(options: RequestOptions): this;
/**
* Creates (by Object.create) a **new client** instance with given headers.
* @param headers headers that will be copy into client.
* @returns a **new client** with with given headers.
*/
withHeaders(headers: Payload): this;
/**
* Creates (by Object.create) a **new client** instance with headers copy from the request.
* @param req IncomingMessage object that headers read from.
* @param headers headers that will be copy into client.
* @returns a **new client** with with given headers.
*/
forwardHeaders(req: IncomingMessage | any, ...headers: string[]): this;
/**
* Creates (by Object.create) a **new client** instance with given query.
* @param query query that will be copy into client.
* @returns a **new client** with with given query.
*/
withQuery(query: Payload): this;
/**
* Creates (by withHeaders) a **new client** instance with given `X-Tenant-Id` and `X-Tenant-Type`.
* @param tenantId that will be added to header as `X-Tenant-Id`.
* @param tenantType that will be added to header as `X-Tenant-Type`.
* @returns a **new client** with with given headers.
*/
withTenant(tenantId: string, tenantType?: string): this;
/**
* Creates (by withHeaders) a **new client** instance with given `X-Operator-ID`.
* @param operatorId that will be added to header as `X-Operator-ID`.
* @returns a **new client** with with given headers.
*/
withOperator(operatorId: string): this;
/**
* Creates a JWT token string with given payload and client's appSecrets.
* @param payload Payload to sign, should be an literal object.
* @param options some JWT sign options.
* @returns a token string.
*/
signToken(payload: Payload, options?: jwt.SignOptions): string;
/**
* Creates a periodical changed JWT token string with appId and appSecrets.
* @param payload Payload to sign, should be an literal object.
* @param periodical period in seccond, default to 3600s.
* @param options some JWT sign options.
* @returns a token string.
*/
signAppToken(periodical?: number, options?: jwt.SignOptions): string;
/**
* Decode a JWT token string to literal object payload.
* @param token token to decode.
* @param options some JWT decode options.
* @returns a literal object.
*/
decodeToken(token: string, options?: jwt.DecodeOptions): 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)
* @param token token to decode.
* @param options some JWT verify options.
* @returns a literal object.
*/
verifyToken(token: string, options?: jwt.VerifyOptions): Payload;
/**
* 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.
* @param method method to request.
* @param url url to request, it will be resolved with client host.
* @param data data to request.
* @returns a promise with Response
*/
request(method: string, url: string, data?: any): Promise;
/**
* request with `GET` method.
* @returns a promise with Response body
*/
get(url: string, data?: any): Promise;
/**
* request with `POST` method.
* @returns a promise with Response body
*/
post(url: string, data?: any): Promise;
/**
* request with `PUT` method.
* @returns a promise with Response body
*/
put(url: string, data?: any): Promise;
/**
* request with `PATCH` method.
* @returns a promise with Response body
*/
patch(url: string, data?: any): Promise;
/**
* request with `DELETE` method.
* @returns a promise with Response body
*/
delete(url: string, data?: any): Promise;
}
/**.
* @returns true if response' statusCode in [200, 300)
*/
export declare function isSuccess(res: request.RequestResponse): boolean;
/**.
* @returns a promise that delay with given ms time.
*/
export declare function delay(ms: number): Promise;
/**.
* @returns a Response body or throw a error.
*/
export declare function assertRes(res: Response): T;
export declare function urlJoin(base?: string, to?: string): string;