UNPKG

ask-smapi-sdk

Version:
80 lines 3.58 kB
"use strict"; /* * Copyright 2020 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.ModelIntrospector = void 0; /** * Introspects on the Skill Management API (SMAPI) model. */ class ModelIntrospector { constructor(modelJson, customizationProcessor) { if (!modelJson) { const modelContent = require('ask-smapi-model/spec.json'); this.modelJson = modelContent; } else { this.modelJson = modelJson; } this.customizationProcessor = customizationProcessor; this.processModel(); } /** * Returns the operation definition for a given Skill Management API (SMAPI) operation. * @param operationName operation name */ getOperationDefinition(operationName) { return this.operations.get(operationName); } /** * Returns a Map of all Skill Management API (SMAPI) operations */ getOperations() { return this.operations; } processModel() { const modelDefinitions = this.modelJson.definitions; this.definitions = new Map(Object.keys(modelDefinitions).map((key) => [key, modelDefinitions[key]])); const operationDefinitions = []; Object.keys(this.modelJson.paths).forEach((pathKey) => { Object.keys(this.modelJson.paths[pathKey]).forEach((operationKey) => { operationDefinitions.push(this.modelJson.paths[pathKey][operationKey]); }); }); this.operations = new Map(); for (const operationDefinition of operationDefinitions) { const apiOperationName = operationDefinition['x-operation-name']; if (apiOperationName) { const apiVersion = parseInt(apiOperationName.substring(apiOperationName.length - 1), 10); const apiOperation = { apiOperationName, apiVersion, description: operationDefinition.description, params: operationDefinition.parameters, customizationMetadata: {}, }; const processedOperationName = this.customizationProcessor ? this.customizationProcessor.processOperationName(apiOperationName) : apiOperationName; if (this.customizationProcessor) { this.customizationProcessor.processOperation(processedOperationName, apiOperation, this.definitions); for (const param of apiOperation.params) { this.customizationProcessor.processParameter(param, apiOperation, this.definitions); } } if (!this.operations.has(processedOperationName) || apiVersion > this.operations.get(processedOperationName).apiVersion) { this.operations.set(processedOperationName, apiOperation); } } } } } exports.ModelIntrospector = ModelIntrospector; //# sourceMappingURL=ModelIntrospector.js.map