1 | /**
|
2 | * Copyright(c) Microsoft Corporation.All rights reserved.
|
3 | * Licensed under the MIT License.
|
4 | */
|
5 | const {ServiceBase} = require('./api/serviceBase');
|
6 | const api = require('./api');
|
7 | const dataModels = require('./api/dataModels');
|
8 |
|
9 | /**
|
10 | * Entry into the program flow from the CLI
|
11 | *
|
12 | * This function orchestrates the instantiation
|
13 | * of the service containing the operation to call.
|
14 | * If a body is specified, the typed data model is
|
15 | * created and the source object containing the properties
|
16 | * is passed in. This is necessary to guarantee the
|
17 | * endpoint receives a clean data model.
|
18 | *
|
19 | * @param {*} config The .luisrc file containing the configuration options
|
20 | * @param {*} serviceManifest The manifest entry containing the details of the service to invoke.
|
21 | * @param {*} args The arguments to use as the params for the request
|
22 | * @param {*} requestBody The request body to send to the endpoint
|
23 | *
|
24 | * @returns {Promise<*|Promise|{enumerable}|void|JSON|Promise<any>>}
|
25 | */
|
26 | module.exports = async function qnamaker(config, serviceManifest, args, requestBody) {
|
27 | // Provide the config to the ServiceBase
|
28 | ServiceBase.config = config;
|
29 |
|
30 | // If a request body is specified and a typed data model
|
31 | // is available, create it and pass the source in.
|
32 | // This guarantees the endpoint will get only the
|
33 | // properties it expects since the user can specify
|
34 | // any json file with any number of properties that
|
35 | // may or may not be valid.
|
36 | const {identifier, operation} = serviceManifest;
|
37 | let requestBodyDataModel;
|
38 | // Allow untyped request bodies to seep through unchanged
|
39 | if (requestBody && operation.entityType && dataModels[operation.entityType]) {
|
40 | requestBodyDataModel = dataModels[operation.entityType].fromJSON(requestBody);
|
41 | }
|
42 | // Create the target service and kick off the request.
|
43 | const service = new api[identifier]();
|
44 | const response = await service[operation.name](args, (requestBodyDataModel || requestBody));
|
45 | const text = await response.text();
|
46 | try {
|
47 | return JSON.parse(text);
|
48 | }
|
49 | catch (e) {
|
50 | return text;
|
51 | }
|
52 | };
|