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 Amplify.register(this);
45 }
46 PredictionsClass.prototype.getModuleName = function () {
47 return 'Predictions';
48 };
49 /**
50 * add plugin/pluggable into Predictions category
51 * @param {Object} pluggable - an instance of the plugin/pluggable
52 **/
53 PredictionsClass.prototype.addPluggable = function (pluggable) {
54 if (this.getPluggable(pluggable.getProviderName())) {
55 throw new Error("Pluggable with name " + pluggable.getProviderName() + " has already been added.");
56 }
57 var pluggableAdded = false;
58 if (this.implementsConvertPluggable(pluggable)) {
59 this._convertPluggables.push(pluggable);
60 pluggableAdded = true;
61 }
62 if (this.implementsIdentifyPluggable(pluggable)) {
63 this._identifyPluggables.push(pluggable);
64 pluggableAdded = true;
65 }
66 if (this.implementsInterpretPluggable(pluggable)) {
67 this._interpretPluggables.push(pluggable);
68 pluggableAdded = true;
69 }
70 if (pluggableAdded) {
71 this.configurePluggable(pluggable);
72 }
73 };
74 /**
75 * Get the plugin object
76 * @param providerName - the name of the plugin
77 */
78 PredictionsClass.prototype.getPluggable = function (providerName) {
79 var pluggable = this.getAllProviders().find(function (pluggable) { return pluggable.getProviderName() === providerName; });
80 if (pluggable === undefined) {
81 logger.debug('No plugin found with providerName=>', providerName);
82 return null;
83 }
84 else
85 return pluggable;
86 };
87 /**
88 * Remove the plugin object
89 * @param providerName - the name of the plugin
90 */
91 PredictionsClass.prototype.removePluggable = function (providerName) {
92 this._convertPluggables = this._convertPluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
93 this._identifyPluggables = this._identifyPluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
94 this._interpretPluggables = this._interpretPluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
95 return;
96 };
97 /**
98 * To make both top level providers and category level providers work with same interface and configuration
99 * this method duplicates Predictions config into parent level config (for top level provider) and
100 * category level config (such as convert, identify etc) and pass both to each provider.
101 */
102 PredictionsClass.prototype.configure = function (options) {
103 var _this = this;
104 var predictionsConfig = options ? options.predictions || options : {};
105 predictionsConfig = __assign(__assign({}, predictionsConfig), options);
106 this._options = Object.assign({}, this._options, predictionsConfig);
107 logger.debug('configure Predictions', this._options);
108 this.getAllProviders().forEach(function (pluggable) {
109 return _this.configurePluggable(pluggable);
110 });
111 };
112 PredictionsClass.prototype.interpret = function (input, options) {
113 var pluggableToExecute = this.getPluggableToExecute(this._interpretPluggables, options);
114 return pluggableToExecute.interpret(input);
115 };
116 PredictionsClass.prototype.convert = function (input, options) {
117 var pluggableToExecute = this.getPluggableToExecute(this._convertPluggables, options);
118 return pluggableToExecute.convert(input);
119 };
120 PredictionsClass.prototype.identify = function (input, options) {
121 var pluggableToExecute = this.getPluggableToExecute(this._identifyPluggables, options);
122 return pluggableToExecute.identify(input);
123 };
124 // tslint:disable-next-line: max-line-length
125 PredictionsClass.prototype.getPluggableToExecute = function (pluggables, providerOptions) {
126 // Give preference to provider name first since it is more specific to this call, even if
127 // there is only one provider configured to error out if the name provided is not the one matched.
128 if (providerOptions && providerOptions.providerName) {
129 return __spread(pluggables).find(function (pluggable) {
130 return pluggable.getProviderName() === providerOptions.providerName;
131 });
132 }
133 else {
134 if (pluggables.length === 1) {
135 return pluggables[0];
136 }
137 else {
138 throw new Error('More than one or no providers are configured, ' +
139 'Either specify a provider name or configure exactly one provider');
140 }
141 }
142 };
143 PredictionsClass.prototype.getAllProviders = function () {
144 return __spread(this._convertPluggables, this._identifyPluggables, this._interpretPluggables);
145 };
146 PredictionsClass.prototype.configurePluggable = function (pluggable) {
147 var categoryConfig = Object.assign({}, this._options['predictions'], // Parent predictions config for the top level provider
148 this._options[pluggable.getCategory().toLowerCase()] // Actual category level config
149 );
150 pluggable.configure(categoryConfig);
151 };
152 PredictionsClass.prototype.implementsConvertPluggable = function (obj) {
153 return obj && typeof obj.convert === 'function';
154 };
155 PredictionsClass.prototype.implementsIdentifyPluggable = function (obj) {
156 return obj && typeof obj.identify === 'function';
157 };
158 PredictionsClass.prototype.implementsInterpretPluggable = function (obj) {
159 return obj && typeof obj.interpret === 'function';
160 };
161 return PredictionsClass;
162}());
163export { PredictionsClass };
164export var Predictions = new PredictionsClass({});
165//# sourceMappingURL=Predictions.js.map
\No newline at end of file