ask-cli
Version:
Alexa Skills Kit (ASK) Command Line Interfaces
99 lines (98 loc) • 4.49 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmapiApiClient = void 0;
const httpClient = __importStar(require("../http-client"));
/**
* Implementation of {@link ApiClient} that leverages the httpclient class which supports setting Proxies and publishing telemetry metrics.
* This is use while building a CustomSmapiClientBuilder instead of using the DefaultApiClient
*/
class SmapiApiClient {
/**
* Builds an instance of the SmapiApiClient class
* @param doDebug Set to true to tell the httpClient to print request information to the terminal
* @param profile The configured ask profile to use for authorization
* @param fullResponse Set to true to tell the httpClient to print the full response from the service
*/
constructor(doDebug, profile, fullResponse) {
this.doDebug = doDebug;
this.profile = profile;
this.fullResponse = fullResponse;
}
/**
* Dispatches a request to an API endpoint described in the request.
* @param {ApiClientRequest} request request to dispatch to the ApiClient
* @returns {Promise<ApiClientResponse>} response from the ApiClient
*/
invoke(request) {
return new Promise(async (resolve, reject) => {
var _a;
const headers = {};
let data = request.body;
(_a = request.headers) === null || _a === void 0 ? void 0 : _a.forEach(header => {
if (header.key.toLowerCase() === "content-type" &&
header.value.toLowerCase() === "application/x-www-form-urlencoded") {
data = this.convertUrlEncodedToJson(request.body);
headers[header.key] = "application/json";
}
else {
headers[header.key] = header.value;
}
});
const requestOptions = {
url: request.url,
method: request.method,
headers: headers,
body: data,
};
httpClient.request(requestOptions, "SMAPI_API_CLIENT", this.doDebug, (requestError, requestResponse) => {
if (requestError) {
return reject(new Error(requestError.errorMessage || requestError));
}
const dataContent = (requestResponse === null || requestResponse === void 0 ? void 0 : requestResponse.body) || {};
resolve({
statusCode: requestResponse === null || requestResponse === void 0 ? void 0 : requestResponse.statusCode,
body: this.sanitizeDataContent(dataContent),
headers: requestResponse === null || requestResponse === void 0 ? void 0 : requestResponse.headers,
});
});
});
}
convertUrlEncodedToJson(urlEncoded) {
var result = {};
urlEncoded === null || urlEncoded === void 0 ? void 0 : urlEncoded.split('&').forEach((entry) => {
const keyValueSplit = entry.split('=');
if (keyValueSplit && keyValueSplit.length > 1) {
result[keyValueSplit[0]] = decodeURIComponent(keyValueSplit[1] || '');
}
});
return result;
}
sanitizeDataContent(dataContent) {
return JSON.stringify(dataContent, (k, v) => { if (k !== "_links")
return v; });
}
}
exports.SmapiApiClient = SmapiApiClient;