ask-sdk-model-runtime
Version:
The base components for ASK SDK Models
234 lines (233 loc) • 8.33 kB
TypeScript
/**
* Represents the interface between ApiClient and a Service Client.
* @export
* @interface ApiClientMessage
*/
export interface ApiClientMessage {
headers: Array<{
key: string;
value: string;
}>;
body?: string;
}
/**
* Represents a request sent from Service Clients to an ApiClient implementation.
* @export
* @interface ApiClientRequest
* @extends {ApiClientMessage}
*/
export interface ApiClientRequest extends ApiClientMessage {
url: string;
method: string;
}
/**
* Represents a response returned by ApiClient implementation to a Service Client.
* @export
* @interface ApiClientResponse
* @extends {ApiClientMessage}
*/
export interface ApiClientResponse extends ApiClientMessage {
/**
* Result code of the attempt to satisfy the request. Normally this
* corresponds to the HTTP status code returned by the server.
*/
statusCode: number;
}
/**
* Represents a response with parsed body.
* @export
* @interface ApiResponse
*/
export interface ApiResponse {
headers: Array<{
key: string;
value: string;
}>;
body?: any;
statusCode: number;
}
/**
* Represents a basic contract for API request execution
* @export
* @interface ApiClient
*/
export interface ApiClient {
/**
* Dispatches a request to an API endpoint described in the request.
* An ApiClient is expected to resolve the Promise in the case an API returns a non-200 HTTP
* status code. The responsibility of translating a particular response code to an error lies with the
* caller to invoke.
* @param {ApiClientRequest} request request to dispatch to the ApiClient
* @returns {Promise<ApiClientResponse>} Response from the ApiClient
* @memberof ApiClient
*/
invoke(request: ApiClientRequest): Promise<ApiClientResponse>;
}
/**
* Default implementation of {@link ApiClient} which uses the native HTTP/HTTPS library of Node.JS.
*/
export declare class DefaultApiClient implements ApiClient {
/**
* Dispatches a request to an API endpoint described in the request.
* An ApiClient is expected to resolve the Promise in the case an API returns a non-200 HTTP
* status code. The responsibility of translating a particular response code to an error lies with the
* caller to invoke.
* @param {ApiClientRequest} request request to dispatch to the ApiClient
* @returns {Promise<ApiClientResponse>} response from the ApiClient
*/
invoke(request: ApiClientRequest): Promise<ApiClientResponse>;
/**
* Converts the header array in {@link ApiClientRequest} to compatible JSON object.
* @private
* @param {{key : string, value : string}[]} header header array from ApiClientRequest}
* @returns {Object.<string, string[]>} header object to pass into HTTP client
*/
private arrayToObjectHeader;
/**
* Converts JSON header object to header array required for {ApiClientResponse}
* @private
* @param {Object.<string, (string|string[])>} header JSON header object returned by HTTP client
* @returns {{key : string, value : string}[]}
*/
private objectToArrayHeader;
/**
* function creating an AskSdk error.
* @param {string} errorScope
* @param {string} errorMessage
* @returns {Error}
*/
private createAskSdkModelRuntimeError;
}
/**
* Represents an interface that provides API configuration options needed by service clients.
* @interface ApiConfiguration
*/
export interface ApiConfiguration {
/**
* Configured ApiClient implementation
*/
apiClient: ApiClient;
/**
* Authorization value to be used on any calls of the service client instance
*/
authorizationValue: string;
/**
* Endpoint to hit by the service client instance
*/
apiEndpoint: string;
}
/**
* Class to be used as the base class for the generated service clients.
*/
export declare abstract class BaseServiceClient {
private static isCodeSuccessful;
private static buildUrl;
private static interpolateParams;
private static buildQueryString;
/**
* ApiConfiguration instance to provide dependencies for this service client
*/
protected apiConfiguration: ApiConfiguration;
private requestInterceptors;
private responseInterceptors;
/**
* Creates new instance of the BaseServiceClient
* @param {ApiConfiguration} apiConfiguration configuration parameter to provide dependencies to service client instance
*/
protected constructor(apiConfiguration: ApiConfiguration);
/**
* Sets array of functions that is going to be executed before the request is send
* @param {Function} requestInterceptor request interceptor function
* @returns {BaseServiceClient}
*/
withRequestInterceptors(...requestInterceptors: Array<(request: ApiClientRequest) => void | Promise<void>>): BaseServiceClient;
/**
* Sets array of functions that is going to be executed after the request is send
* @param {Function} responseInterceptor response interceptor function
* @returns {BaseServiceClient}
*/
withResponseInterceptors(...responseInterceptors: Array<(response: ApiClientResponse) => void | Promise<void>>): BaseServiceClient;
/**
* Invocation wrapper to implement service operations in generated classes
* @param method HTTP method, such as 'POST', 'GET', 'DELETE', etc.
* @param endpoint base API url
* @param path the path pattern with possible placeholders for path parameters in form {paramName}
* @param pathParams path parameters collection
* @param queryParams query parameters collection
* @param headerParams headers collection
* @param bodyParam if body parameter is present it is provided here, otherwise null or undefined
* @param errors maps recognized status codes to messages
* @param nonJsonBody if the body is in JSON format
*/
protected invoke(method: string, endpoint: string, path: string, pathParams: Map<string, string>, queryParams: Array<{
key: string;
value: string;
}>, headerParams: Array<{
key: string;
value: string;
}>, bodyParam: any, errors: Map<number, string>, nonJsonBody?: boolean): Promise<any>;
}
/**
* Represents a Login With Amazon(LWA) access token
*/
export interface AccessToken {
token: string;
expiry: Number;
}
/**
* Represents a request for retrieving a Login With Amazon(LWA) access token
*/
export interface AccessTokenRequest {
clientId: string;
clientSecret: string;
scope?: string;
refreshToken?: string;
}
/**
* Represents a response returned by LWA containing a Login With Amazon(LWA) access token
*/
export interface AccessTokenResponse {
access_token: string;
expires_in: number;
scope: string;
token_type: string;
}
/**
* Represents the authentication configuration for a client ID and client secret
*/
export interface AuthenticationConfiguration {
clientId: string;
clientSecret: string;
accessToken?: string;
refreshToken?: string;
authEndpoint?: string;
}
/**
* Class to be used to call Amazon LWA to retrieve access tokens.
*/
export declare class LwaServiceClient extends BaseServiceClient {
protected static EXPIRY_OFFSET_MILLIS: number;
protected static REFRESH_ACCESS_TOKEN: string;
protected static CLIENT_CREDENTIALS_GRANT_TYPE: string;
protected static LWA_CREDENTIALS_GRANT_TYPE: string;
protected static AUTH_ENDPOINT: string;
protected authenticationConfiguration: AuthenticationConfiguration;
protected tokenStore: {
[cacheKey: string]: AccessToken;
};
protected grantType: string;
constructor(options: {
apiConfiguration: ApiConfiguration;
authenticationConfiguration: AuthenticationConfiguration;
grantType?: string;
});
getAccessTokenForScope(scope: string): Promise<string>;
getAccessToken(scope?: string): Promise<string>;
protected generateAccessToken(accessTokenRequest: AccessTokenRequest): Promise<AccessTokenResponse>;
}
/**
* function creating an AskSdk user agent.
* @param packageVersion
* @param customUserAgent
*/
export declare function createUserAgent(packageVersion: string, customUserAgent: string): string;