ask-sdk-v1adapter
Version:
Adapter from v1 Alexa Node.js SDK to v2
123 lines • 5.94 kB
JavaScript
"use strict";
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListManagementService = void 0;
const serviceError_1 = require("./serviceError");
const v1ApiClient_1 = require("./v1ApiClient");
class ListManagementService {
constructor(apiClient) {
this.apiClient = apiClient || new v1ApiClient_1.V1ApiClient();
this.listManagementPath = '/v2/householdlists/';
this.apiEndpoint = 'https://api.amazonalexa.com';
}
setApiEndpoint(apiEndpoint) {
this.apiEndpoint = apiEndpoint;
}
getApiEndpoint() {
return this.apiEndpoint;
}
async getListsMetadata(token) {
const uri = this.apiEndpoint + this.listManagementPath;
const headers = this.buildHeaders(token);
const response = await this.apiClient.get(uri, headers);
return this.validateApiResponse(response);
}
// eslint-disable-next-line @typescript-eslint/ban-types
async createList(listObject, token) {
const uri = this.apiEndpoint + this.listManagementPath;
const headers = this.buildHeaders(token, listObject);
const response = await this.apiClient.post(uri, headers, JSON.stringify(listObject));
return this.validateApiResponse(response);
}
async getList(listId, itemStatus, token) {
const uri = `${this.apiEndpoint + this.listManagementPath + listId}/${itemStatus}`;
const headers = this.buildHeaders(token);
const response = await this.apiClient.get(uri, headers);
return this.validateApiResponse(response);
}
// eslint-disable-next-line @typescript-eslint/ban-types
async updateList(listId, listObject, token) {
const uri = this.apiEndpoint + this.listManagementPath + listId;
const headers = this.buildHeaders(token, listObject);
const response = await this.apiClient.put(uri, headers, JSON.stringify(listObject));
return this.validateApiResponse(response);
}
async deleteList(listId, token) {
const uri = this.apiEndpoint + this.listManagementPath + listId;
const headers = this.buildHeaders(token);
const response = await this.apiClient.delete(uri, headers);
const isResponseCodeValid = response.statusCode >= 200 && response.statusCode < 300;
if (isResponseCodeValid) {
return;
}
throw new serviceError_1.ServiceError(response.statusCode, JSON.stringify(response.body));
}
// eslint-disable-next-line @typescript-eslint/ban-types
async createListItem(listId, listItemObject, token) {
const uri = `${this.apiEndpoint + this.listManagementPath + listId}/items`;
const headers = this.buildHeaders(token, listItemObject);
const response = await this.apiClient.post(uri, headers, JSON.stringify(listItemObject));
return this.validateApiResponse(response);
}
async getListItem(listId, itemId, token) {
const uri = `${this.apiEndpoint + this.listManagementPath + listId}/items/${itemId}`;
const headers = this.buildHeaders(token);
const response = await this.apiClient.get(uri, headers);
return this.validateApiResponse(response);
}
// eslint-disable-next-line @typescript-eslint/ban-types
async updateListItem(listId, itemId, listItemObject, token) {
const uri = `${this.apiEndpoint + this.listManagementPath + listId}/items/${itemId}`;
const headers = this.buildHeaders(token, listItemObject);
const response = await this.apiClient.put(uri, headers, JSON.stringify(listItemObject));
return this.validateApiResponse(response);
}
async deleteListItem(listId, itemId, token) {
const uri = `${this.apiEndpoint + this.listManagementPath + listId}/items/${itemId}`;
const headers = this.buildHeaders(token);
const response = await this.apiClient.delete(uri, headers);
const isResponseCodeValid = response.statusCode >= 200 && response.statusCode < 300;
if (isResponseCodeValid) {
return;
}
throw new serviceError_1.ServiceError(response.statusCode, JSON.stringify(response.body));
}
// eslint-disable-next-line @typescript-eslint/ban-types
buildHeaders(token, body) {
const headers = [];
headers.push({ key: 'Authorization', value: `Bearer ${token}` });
if (body) {
headers.push({ key: 'Content-type', value: 'application/json' });
headers.push({ key: 'Content-length', value: Buffer.byteLength(JSON.stringify(body), 'utf8').toString() });
}
return headers;
}
// eslint-disable-next-line @typescript-eslint/ban-types
validateApiResponse(apiClientResponse) {
const isResponseCodeValid = apiClientResponse.statusCode >= 200 && apiClientResponse.statusCode < 300;
let responseBody;
try {
responseBody = apiClientResponse.body && JSON.parse(apiClientResponse.body);
}
catch (err) {
responseBody = apiClientResponse.body;
}
if (isResponseCodeValid) {
return responseBody;
}
throw new serviceError_1.ServiceError(apiClientResponse.statusCode, JSON.stringify(responseBody));
}
}
exports.ListManagementService = ListManagementService;
//# sourceMappingURL=listManagementService.js.map