UNPKG

7.39 kBJavaScriptView Raw
1var __assign = (this && this.__assign) || function () {
2 __assign = Object.assign || function(t) {
3 for (var s, i = 1, n = arguments.length; i < n; i++) {
4 s = arguments[i];
5 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6 t[p] = s[p];
7 }
8 return t;
9 };
10 return __assign.apply(this, arguments);
11};
12var __read = (this && this.__read) || function (o, n) {
13 var m = typeof Symbol === "function" && o[Symbol.iterator];
14 if (!m) return o;
15 var i = m.call(o), r, ar = [], e;
16 try {
17 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18 }
19 catch (error) { e = { error: error }; }
20 finally {
21 try {
22 if (r && !r.done && (m = i["return"])) m.call(i);
23 }
24 finally { if (e) throw e.error; }
25 }
26 return ar;
27};
28var __spread = (this && this.__spread) || function () {
29 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
30 return ar;
31};
32import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
33var logger = new Logger('Predictions');
34var PredictionsClass = /** @class */ (function () {
35 /**
36 * Initialize Predictions with AWS configurations
37 * @param {PredictionsOptions} options - Configuration object for Predictions
38 */
39 function PredictionsClass(options) {
40 this._options = options;
41 this._convertPluggables = [];
42 this._identifyPluggables = [];
43 this._interpretPluggables = [];
44 }
45 PredictionsClass.prototype.getModuleName = function () {
46 return 'Predictions';
47 };
48 /**
49 * add plugin/pluggable into Predictions category
50 * @param {Object} pluggable - an instance of the plugin/pluggable
51 **/
52 PredictionsClass.prototype.addPluggable = function (pluggable) {
53 if (this.getPluggable(pluggable.getProviderName())) {
54 throw new Error("Pluggable with name " + pluggable.getProviderName() + " has already been added.");
55 }
56 var pluggableAdded = false;
57 if (this.implementsConvertPluggable(pluggable)) {
58 this._convertPluggables.push(pluggable);
59 pluggableAdded = true;
60 }
61 if (this.implementsIdentifyPluggable(pluggable)) {
62 this._identifyPluggables.push(pluggable);
63 pluggableAdded = true;
64 }
65 if (this.implementsInterpretPluggable(pluggable)) {
66 this._interpretPluggables.push(pluggable);
67 pluggableAdded = true;
68 }
69 if (pluggableAdded) {
70 this.configurePluggable(pluggable);
71 }
72 };
73 /**
74 * Get the plugin object
75 * @param providerName - the name of the plugin
76 */
77 PredictionsClass.prototype.getPluggable = function (providerName) {
78 var pluggable = this.getAllProviders().find(function (pluggable) { return pluggable.getProviderName() === providerName; });
79 if (pluggable === undefined) {
80 logger.debug('No plugin found with providerName=>', providerName);
81 return null;
82 }
83 else
84 return pluggable;
85 };
86 /**
87 * Remove the plugin object
88 * @param providerName - the name of the plugin
89 */
90 PredictionsClass.prototype.removePluggable = function (providerName) {
91 this._convertPluggables = this._convertPluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
92 this._identifyPluggables = this._identifyPluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
93 this._interpretPluggables = this._interpretPluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
94 return;
95 };
96 /**
97 * To make both top level providers and category level providers work with same interface and configuration
98 * this method duplicates Predictions config into parent level config (for top level provider) and
99 * category level config (such as convert, identify etc) and pass both to each provider.
100 */
101 PredictionsClass.prototype.configure = function (options) {
102 var _this = this;
103 var predictionsConfig = options ? options.predictions || options : {};
104 predictionsConfig = __assign(__assign({}, predictionsConfig), options);
105 this._options = Object.assign({}, this._options, predictionsConfig);
106 logger.debug('configure Predictions', this._options);
107 this.getAllProviders().forEach(function (pluggable) {
108 return _this.configurePluggable(pluggable);
109 });
110 };
111 PredictionsClass.prototype.interpret = function (input, options) {
112 var pluggableToExecute = this.getPluggableToExecute(this._interpretPluggables, options);
113 return pluggableToExecute.interpret(input);
114 };
115 PredictionsClass.prototype.convert = function (input, options) {
116 var pluggableToExecute = this.getPluggableToExecute(this._convertPluggables, options);
117 return pluggableToExecute.convert(input);
118 };
119 PredictionsClass.prototype.identify = function (input, options) {
120 var pluggableToExecute = this.getPluggableToExecute(this._identifyPluggables, options);
121 return pluggableToExecute.identify(input);
122 };
123 // tslint:disable-next-line: max-line-length
124 PredictionsClass.prototype.getPluggableToExecute = function (pluggables, providerOptions) {
125 // Give preference to provider name first since it is more specific to this call, even if
126 // there is only one provider configured to error out if the name provided is not the one matched.
127 if (providerOptions && providerOptions.providerName) {
128 return __spread(pluggables).find(function (pluggable) {
129 return pluggable.getProviderName() === providerOptions.providerName;
130 });
131 }
132 else {
133 if (pluggables.length === 1) {
134 return pluggables[0];
135 }
136 else {
137 throw new Error('More than one or no providers are configured, ' +
138 'Either specify a provider name or configure exactly one provider');
139 }
140 }
141 };
142 PredictionsClass.prototype.getAllProviders = function () {
143 return __spread(this._convertPluggables, this._identifyPluggables, this._interpretPluggables);
144 };
145 PredictionsClass.prototype.configurePluggable = function (pluggable) {
146 var categoryConfig = Object.assign({}, this._options['predictions'], // Parent predictions config for the top level provider
147 this._options[pluggable.getCategory().toLowerCase()] // Actual category level config
148 );
149 pluggable.configure(categoryConfig);
150 };
151 PredictionsClass.prototype.implementsConvertPluggable = function (obj) {
152 return obj && typeof obj.convert === 'function';
153 };
154 PredictionsClass.prototype.implementsIdentifyPluggable = function (obj) {
155 return obj && typeof obj.identify === 'function';
156 };
157 PredictionsClass.prototype.implementsInterpretPluggable = function (obj) {
158 return obj && typeof obj.interpret === 'function';
159 };
160 return PredictionsClass;
161}());
162export { PredictionsClass };
163export var Predictions = new PredictionsClass({});
164Amplify.register(Predictions);
165//# sourceMappingURL=Predictions.js.map
\No newline at end of file