dataverse-utils 2.7.4 → 2.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/deploy.js CHANGED
@@ -1,64 +1,64 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const prompts_1 = __importDefault(require("prompts"));
7
- const fs_1 = __importDefault(require("fs"));
8
- const path_1 = __importDefault(require("path"));
9
- const logger_1 = require("./logger");
10
- const assemblyDeploy_1 = require("./assemblyDeploy");
11
- const webResourceDeploy_1 = require("./webResourceDeploy");
12
- const node_1 = require("dataverse-webapi/lib/node");
13
- const auth_1 = require("./auth");
14
- async function deploy(type, files) {
15
- if (!type || (type !== 'webresource' && type !== 'assembly' && type !== 'pcf')) {
16
- const invalid = type !== undefined && type !== 'webresource' && type !== 'assembly' && type !== 'pcf';
17
- const invalidMessage = invalid ? `${type} is not a valid project type. ` : '';
18
- const { typePrompt } = await (0, prompts_1.default)({
19
- type: 'select',
20
- name: 'typePrompt',
21
- message: `${invalidMessage}select project type to deploy`,
22
- choices: [
23
- { title: 'web resource', value: 'webresource' },
24
- { title: 'pcf', value: 'webresource' },
25
- { title: 'plugin or workflow activity', value: 'assembly' }
26
- ]
27
- });
28
- type = typePrompt;
29
- }
30
- const currentPath = '.';
31
- const credsFile = await fs_1.default.promises.readFile(path_1.default.resolve(currentPath, 'dataverse.config.json'), 'utf8');
32
- if (credsFile == null) {
33
- logger_1.logger.warn('unable to find dataverse.config.json file');
34
- return;
35
- }
36
- const creds = JSON.parse(credsFile).connection;
37
- let token = null;
38
- try {
39
- token = await (0, auth_1.getAccessToken)(creds.tenant, creds.server);
40
- }
41
- catch (error) {
42
- (0, auth_1.onTokenFailure)(creds.server, error.message);
43
- return;
44
- }
45
- if (token == null || token.accessToken == null) {
46
- (0, auth_1.onTokenFailure)(creds.server);
47
- return;
48
- }
49
- const apiConfig = new node_1.WebApiConfig('8.2', token.accessToken, creds.server);
50
- switch (type) {
51
- case 'webresource':
52
- await (0, webResourceDeploy_1.webResourceDeploy)(creds, apiConfig, files);
53
- break;
54
- case 'assembly':
55
- await (0, assemblyDeploy_1.assemblyDeploy)(creds, apiConfig);
56
- break;
57
- case 'pcf':
58
- logger_1.logger.error('PCF deploy coming soon');
59
- break;
60
- default:
61
- break;
62
- }
63
- }
64
- exports.default = deploy;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const prompts_1 = __importDefault(require("prompts"));
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const logger_1 = require("./logger");
10
+ const assemblyDeploy_1 = require("./assemblyDeploy");
11
+ const webResourceDeploy_1 = require("./webResourceDeploy");
12
+ const node_1 = require("dataverse-webapi/lib/node");
13
+ const auth_1 = require("./auth");
14
+ async function deploy(type, files) {
15
+ if (!type || (type !== 'webresource' && type !== 'assembly' && type !== 'pcf')) {
16
+ const invalid = type !== undefined && type !== 'webresource' && type !== 'assembly' && type !== 'pcf';
17
+ const invalidMessage = invalid ? `${type} is not a valid project type. ` : '';
18
+ const { typePrompt } = await (0, prompts_1.default)({
19
+ type: 'select',
20
+ name: 'typePrompt',
21
+ message: `${invalidMessage}select project type to deploy`,
22
+ choices: [
23
+ { title: 'web resource', value: 'webresource' },
24
+ { title: 'pcf', value: 'webresource' },
25
+ { title: 'plugin or workflow activity', value: 'assembly' }
26
+ ]
27
+ });
28
+ type = typePrompt;
29
+ }
30
+ const currentPath = '.';
31
+ const credsFile = await fs_1.default.promises.readFile(path_1.default.resolve(currentPath, 'dataverse.config.json'), 'utf8');
32
+ if (credsFile == null) {
33
+ logger_1.logger.warn('unable to find dataverse.config.json file');
34
+ return;
35
+ }
36
+ const creds = JSON.parse(credsFile).connection;
37
+ let token = null;
38
+ try {
39
+ token = await (0, auth_1.getAccessToken)(creds.tenant, creds.server);
40
+ }
41
+ catch (error) {
42
+ (0, auth_1.onTokenFailure)(creds.server, error.message);
43
+ return;
44
+ }
45
+ if (token == null || token.accessToken == null) {
46
+ (0, auth_1.onTokenFailure)(creds.server);
47
+ return;
48
+ }
49
+ const apiConfig = new node_1.WebApiConfig('8.2', token.accessToken, creds.server);
50
+ switch (type) {
51
+ case 'webresource':
52
+ await (0, webResourceDeploy_1.webResourceDeploy)(creds, apiConfig, files);
53
+ break;
54
+ case 'assembly':
55
+ await (0, assemblyDeploy_1.assemblyDeploy)(creds, apiConfig);
56
+ break;
57
+ case 'pcf':
58
+ logger_1.logger.error('PCF deploy coming soon');
59
+ break;
60
+ default:
61
+ break;
62
+ }
63
+ }
64
+ exports.default = deploy;
package/lib/generate.js CHANGED
@@ -1,99 +1,99 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const prompts_1 = __importDefault(require("prompts"));
7
- const fs_1 = __importDefault(require("fs"));
8
- const path_1 = __importDefault(require("path"));
9
- const logger_1 = require("./logger");
10
- const dataverse_service_1 = require("./dataverse.service");
11
- const node_1 = require("dataverse-webapi/lib/node");
12
- const auth_1 = require("./auth");
13
- async function generate(table) {
14
- while (!table) {
15
- const { tablePrompt } = await (0, prompts_1.default)({
16
- type: 'text',
17
- name: 'tablePrompt',
18
- message: `enter table to generate`
19
- });
20
- table = tablePrompt;
21
- }
22
- const currentPath = '.';
23
- const credsFile = await fs_1.default.promises.readFile(path_1.default.resolve(currentPath, 'dataverse.config.json'), 'utf8');
24
- if (credsFile == null) {
25
- logger_1.logger.warn('unable to find dataverse.config.json file');
26
- return;
27
- }
28
- const creds = JSON.parse(credsFile).connection;
29
- let token = null;
30
- try {
31
- token = await (0, auth_1.getAccessToken)(creds.tenant, creds.server);
32
- }
33
- catch (error) {
34
- (0, auth_1.onTokenFailure)(creds.server, error.message);
35
- return;
36
- }
37
- if (token == null || token.accessToken == null) {
38
- (0, auth_1.onTokenFailure)(creds.server);
39
- return;
40
- }
41
- const apiConfig = new node_1.WebApiConfig('8.2', token.accessToken, creds.server);
42
- let metadata = {};
43
- logger_1.logger.info('Retrieve table metadata');
44
- try {
45
- metadata = await (0, dataverse_service_1.getTableMetadata)(table, apiConfig);
46
- }
47
- catch (error) {
48
- logger_1.logger.error(error.message);
49
- return;
50
- }
51
- // Build code file from metadata
52
- const codeFile = [
53
- `class ${metadata.schemaName} {`,
54
- '\r\n',
55
- ` LogicalName = '${metadata.logicalName}';`,
56
- '\r\n',
57
- ` SchemaName = '${metadata.schemaName}';`,
58
- '\r\n',
59
- ` EntitySetName = '${metadata.entitySetName}';`,
60
- '\r\n',
61
- '\r\n',
62
- ' Fields = {',
63
- '\r\n',
64
- metadata.fields.map(f => {
65
- return ` '${f.schemaName}': '${f.logicalName}'`;
66
- }).join(',\r\n'),
67
- '\r\n',
68
- ' }',
69
- '\r\n',
70
- '\r\n',
71
- metadata.choices.map(c => {
72
- const field = metadata.fields.find(f => f.logicalName === c.column);
73
- return ` ${field?.schemaName ?? c.column} = ${metadata.schemaName}_${field?.schemaName ?? c.column};`;
74
- }).join('\r\n'),
75
- '\r\n',
76
- '}',
77
- '\r\n',
78
- '\r\n',
79
- metadata.choices.map(c => {
80
- const field = metadata.fields.find(f => f.logicalName === c.column);
81
- return [
82
- `export enum ${metadata.schemaName}_${field?.schemaName ?? c.column} {`,
83
- '\r\n',
84
- c.options.map(x => ` '${x.text.replace(`'`, `\\'`)}' = ${x.value}`).join(',\r\n'),
85
- '\r\n',
86
- '}'
87
- ].join('');
88
- }).join('\r\n\r\n'),
89
- '\r\n',
90
- '\r\n',
91
- `export default new ${metadata.schemaName}();`
92
- ].join('');
93
- if (!fs_1.default.existsSync(path_1.default.resolve(currentPath, 'src', 'scripts', 'models'))) {
94
- fs_1.default.mkdirSync(path_1.default.resolve(currentPath, 'src', 'scripts', 'models'));
95
- }
96
- fs_1.default.writeFileSync(path_1.default.resolve(currentPath, 'src', 'scripts', 'models', `${metadata.schemaName}.ts`), codeFile);
97
- logger_1.logger.done(`Table metadata output to models/${metadata.schemaName}.ts`);
98
- }
99
- exports.default = generate;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const prompts_1 = __importDefault(require("prompts"));
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const logger_1 = require("./logger");
10
+ const dataverse_service_1 = require("./dataverse.service");
11
+ const node_1 = require("dataverse-webapi/lib/node");
12
+ const auth_1 = require("./auth");
13
+ async function generate(table) {
14
+ while (!table) {
15
+ const { tablePrompt } = await (0, prompts_1.default)({
16
+ type: 'text',
17
+ name: 'tablePrompt',
18
+ message: `enter table to generate`
19
+ });
20
+ table = tablePrompt;
21
+ }
22
+ const currentPath = '.';
23
+ const credsFile = await fs_1.default.promises.readFile(path_1.default.resolve(currentPath, 'dataverse.config.json'), 'utf8');
24
+ if (credsFile == null) {
25
+ logger_1.logger.warn('unable to find dataverse.config.json file');
26
+ return;
27
+ }
28
+ const creds = JSON.parse(credsFile).connection;
29
+ let token = null;
30
+ try {
31
+ token = await (0, auth_1.getAccessToken)(creds.tenant, creds.server);
32
+ }
33
+ catch (error) {
34
+ (0, auth_1.onTokenFailure)(creds.server, error.message);
35
+ return;
36
+ }
37
+ if (token == null || token.accessToken == null) {
38
+ (0, auth_1.onTokenFailure)(creds.server);
39
+ return;
40
+ }
41
+ const apiConfig = new node_1.WebApiConfig('8.2', token.accessToken, creds.server);
42
+ let metadata = {};
43
+ logger_1.logger.info('Retrieve table metadata');
44
+ try {
45
+ metadata = await (0, dataverse_service_1.getTableMetadata)(table, apiConfig);
46
+ }
47
+ catch (error) {
48
+ logger_1.logger.error(error.message);
49
+ return;
50
+ }
51
+ // Build code file from metadata
52
+ const codeFile = [
53
+ `class ${metadata.schemaName} {`,
54
+ '\r\n',
55
+ ` LogicalName = '${metadata.logicalName}';`,
56
+ '\r\n',
57
+ ` SchemaName = '${metadata.schemaName}';`,
58
+ '\r\n',
59
+ ` EntitySetName = '${metadata.entitySetName}';`,
60
+ '\r\n',
61
+ '\r\n',
62
+ ' Fields = {',
63
+ '\r\n',
64
+ metadata.fields.map(f => {
65
+ return ` '${f.schemaName}': '${f.logicalName}'`;
66
+ }).join(',\r\n'),
67
+ '\r\n',
68
+ ' }',
69
+ '\r\n',
70
+ '\r\n',
71
+ metadata.choices.map(c => {
72
+ const field = metadata.fields.find(f => f.logicalName === c.column);
73
+ return ` ${field?.schemaName ?? c.column} = ${metadata.schemaName}_${field?.schemaName ?? c.column};`;
74
+ }).join('\r\n'),
75
+ '\r\n',
76
+ '}',
77
+ '\r\n',
78
+ '\r\n',
79
+ metadata.choices.map(c => {
80
+ const field = metadata.fields.find(f => f.logicalName === c.column);
81
+ return [
82
+ `export enum ${metadata.schemaName}_${field?.schemaName ?? c.column} {`,
83
+ '\r\n',
84
+ c.options.map(x => ` '${x.text.replace(`'`, `\\'`)}' = ${x.value}`).join(',\r\n'),
85
+ '\r\n',
86
+ '}'
87
+ ].join('');
88
+ }).join('\r\n\r\n'),
89
+ '\r\n',
90
+ '\r\n',
91
+ `export default new ${metadata.schemaName}();`
92
+ ].join('');
93
+ if (!fs_1.default.existsSync(path_1.default.resolve(currentPath, 'src', 'scripts', 'models'))) {
94
+ fs_1.default.mkdirSync(path_1.default.resolve(currentPath, 'src', 'scripts', 'models'));
95
+ }
96
+ fs_1.default.writeFileSync(path_1.default.resolve(currentPath, 'src', 'scripts', 'models', `${metadata.schemaName}.ts`), codeFile);
97
+ logger_1.logger.done(`Table metadata output to models/${metadata.schemaName}.ts`);
98
+ }
99
+ exports.default = generate;
package/lib/index.js CHANGED
@@ -1,44 +1,44 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const commander_1 = require("commander");
8
- const deploy_1 = __importDefault(require("./deploy"));
9
- const generate_1 = __importDefault(require("./generate"));
10
- // eslint-disable-next-line @typescript-eslint/no-var-requires
11
- const packageJson = require('../package.json');
12
- commander_1.program
13
- .version(packageJson.version)
14
- .usage('<command> [options]');
15
- // Deploy command
16
- commander_1.program
17
- .command('deploy')
18
- .description('Deploy file(s) to dataverse (webresource, assembly, pcf)')
19
- .argument('[type]', 'Type of project to deploy')
20
- .argument('[files]', 'Comma separate list of files to deploy')
21
- .action((type, files) => {
22
- (0, deploy_1.default)(type, files);
23
- });
24
- // Generate command
25
- commander_1.program
26
- .command('generate')
27
- .description('Generate early-bound TypeScript file for specified table')
28
- .argument('[table]', 'Table to generate')
29
- .action((table) => {
30
- (0, generate_1.default)(table);
31
- });
32
- // Show help on unknown command
33
- commander_1.program
34
- .arguments('<command>')
35
- .action((cmd) => {
36
- commander_1.program.outputHelp();
37
- console.log();
38
- console.log(`Unknown command ${cmd}.`);
39
- console.log();
40
- });
41
- commander_1.program.parse(process.argv);
42
- if (!process.argv.slice(2).length) {
43
- commander_1.program.outputHelp();
44
- }
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const commander_1 = require("commander");
8
+ const deploy_1 = __importDefault(require("./deploy"));
9
+ const generate_1 = __importDefault(require("./generate"));
10
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
11
+ const packageJson = require('../package.json');
12
+ commander_1.program
13
+ .version(packageJson.version)
14
+ .usage('<command> [options]');
15
+ // Deploy command
16
+ commander_1.program
17
+ .command('deploy')
18
+ .description('Deploy file(s) to dataverse (webresource, assembly, pcf)')
19
+ .argument('[type]', 'Type of project to deploy')
20
+ .argument('[files]', 'Comma separate list of files to deploy')
21
+ .action((type, files) => {
22
+ (0, deploy_1.default)(type, files);
23
+ });
24
+ // Generate command
25
+ commander_1.program
26
+ .command('generate')
27
+ .description('Generate early-bound TypeScript file for specified table')
28
+ .argument('[table]', 'Table to generate')
29
+ .action((table) => {
30
+ (0, generate_1.default)(table);
31
+ });
32
+ // Show help on unknown command
33
+ commander_1.program
34
+ .arguments('<command>')
35
+ .action((cmd) => {
36
+ commander_1.program.outputHelp();
37
+ console.log();
38
+ console.log(`Unknown command ${cmd}.`);
39
+ console.log();
40
+ });
41
+ commander_1.program.parse(process.argv);
42
+ if (!process.argv.slice(2).length) {
43
+ commander_1.program.outputHelp();
44
+ }
package/lib/logger.js CHANGED
@@ -1,37 +1,37 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.logger = exports.icons = void 0;
7
- const kleur_1 = __importDefault(require("kleur"));
8
- const figures_1 = __importDefault(require("figures"));
9
- const isTest = process.env.JEST_WORKER_ID !== undefined;
10
- exports.icons = {
11
- done: kleur_1.default.green(figures_1.default.tick),
12
- info: kleur_1.default.cyan(figures_1.default.pointer),
13
- error: kleur_1.default.red(figures_1.default.cross),
14
- warn: kleur_1.default.yellow(figures_1.default.warning)
15
- };
16
- exports.logger = {
17
- info(...args) {
18
- if (!isTest) {
19
- console.info(exports.icons.info, ...args);
20
- }
21
- },
22
- warn(...args) {
23
- if (!isTest) {
24
- console.warn(exports.icons.warn, ...args);
25
- }
26
- },
27
- error(...args) {
28
- if (!isTest) {
29
- console.error(exports.icons.error, ...args);
30
- }
31
- },
32
- done(...args) {
33
- if (!isTest) {
34
- console.info(exports.icons.done, ...args);
35
- }
36
- },
37
- };
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.logger = exports.icons = void 0;
7
+ const kleur_1 = __importDefault(require("kleur"));
8
+ const figures_1 = __importDefault(require("figures"));
9
+ const isTest = process.env.JEST_WORKER_ID !== undefined;
10
+ exports.icons = {
11
+ done: kleur_1.default.green(figures_1.default.tick),
12
+ info: kleur_1.default.cyan(figures_1.default.pointer),
13
+ error: kleur_1.default.red(figures_1.default.cross),
14
+ warn: kleur_1.default.yellow(figures_1.default.warning)
15
+ };
16
+ exports.logger = {
17
+ info(...args) {
18
+ if (!isTest) {
19
+ console.info(exports.icons.info, ...args);
20
+ }
21
+ },
22
+ warn(...args) {
23
+ if (!isTest) {
24
+ console.warn(exports.icons.warn, ...args);
25
+ }
26
+ },
27
+ error(...args) {
28
+ if (!isTest) {
29
+ console.error(exports.icons.error, ...args);
30
+ }
31
+ },
32
+ done(...args) {
33
+ if (!isTest) {
34
+ console.info(exports.icons.done, ...args);
35
+ }
36
+ },
37
+ };
@@ -1,77 +1,77 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.retrieveApi = exports.deployApi = void 0;
4
- const node_1 = require("dataverse-webapi/lib/node");
5
- const pluginType_1 = require("./pluginType");
6
- const logger_1 = require("../logger");
7
- async function deployApi(config, assemblyId, apiConfig, solution) {
8
- const api = structuredClone(config);
9
- if (!config.customapiid) {
10
- config.customapiid = await retrieveApi(api.name, apiConfig);
11
- }
12
- if (config.plugintype && !config.plugintypeid) {
13
- config.plugintypeid = await (0, pluginType_1.retrieveType)(config.plugintype, assemblyId, apiConfig);
14
- if (config.plugintypeid === '') {
15
- logger_1.logger.error(`unable to find plugin type ${api.plugintype}`);
16
- return;
17
- }
18
- delete api.plugintype;
19
- delete api.customapiid;
20
- delete api.plugintypeid;
21
- api['PluginTypeId@odata.bind'] = `plugintypes(${config.plugintypeid})`;
22
- }
23
- if (config.customapiid) {
24
- try {
25
- await updateApi(config.customapiid, api, apiConfig);
26
- }
27
- catch (error) {
28
- throw new Error(`failed to update custom api: ${error.message}`);
29
- }
30
- }
31
- else {
32
- try {
33
- config.customapiid = await createApi(api, apiConfig, solution);
34
- }
35
- catch (error) {
36
- throw new Error(`failed to create custom api: ${error.message}`);
37
- }
38
- }
39
- }
40
- exports.deployApi = deployApi;
41
- async function retrieveApi(name, apiConfig) {
42
- const options = `$select=customapiid&$filter=name eq '${name}'`;
43
- const result = await (0, node_1.retrieveMultiple)(apiConfig, 'customapis', options);
44
- return result.value.length > 0 ? result.value[0].customapiid : '';
45
- }
46
- exports.retrieveApi = retrieveApi;
47
- async function createApi(api, apiConfig, solution) {
48
- logger_1.logger.info(`create custom api ${api.name}`);
49
- const options = {};
50
- if (solution) {
51
- options.customHeaders = { 'MSCRM.SolutionUniqueName': solution };
52
- }
53
- const result = await (0, node_1.createWithReturnData)(apiConfig, 'customapis', api, '$select=customapiid', options);
54
- if (result?.error) {
55
- throw new Error(result.error.message);
56
- }
57
- return result.customapiid;
58
- }
59
- async function updateApi(id, api, apiConfig) {
60
- logger_1.logger.info(`update custom api ${api.name}`);
61
- const record = {
62
- displayname: api.displayname,
63
- description: api.description,
64
- name: api.name,
65
- executeprivilegename: api.executeprivilegename
66
- };
67
- if (api['PluginTypeId@odata.bind']) {
68
- record['PluginTypeId@odata.bind'] = api['PluginTypeId@odata.bind'];
69
- }
70
- else {
71
- record['PluginTypeId@odata.bind'] = null;
72
- }
73
- const result = await (0, node_1.update)(apiConfig, 'customapis', id, record);
74
- if (result?.error) {
75
- throw new Error(result.error.message);
76
- }
77
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.retrieveApi = exports.deployApi = void 0;
4
+ const node_1 = require("dataverse-webapi/lib/node");
5
+ const pluginType_1 = require("./pluginType");
6
+ const logger_1 = require("../logger");
7
+ async function deployApi(config, assemblyId, apiConfig, solution) {
8
+ const api = structuredClone(config);
9
+ if (!config.customapiid) {
10
+ config.customapiid = await retrieveApi(api.name, apiConfig);
11
+ }
12
+ if (config.plugintype && !config.plugintypeid) {
13
+ config.plugintypeid = await (0, pluginType_1.retrieveType)(config.plugintype, assemblyId, apiConfig);
14
+ if (config.plugintypeid === '') {
15
+ logger_1.logger.error(`unable to find plugin type ${api.plugintype}`);
16
+ return;
17
+ }
18
+ }
19
+ delete api.plugintype;
20
+ delete api.customapiid;
21
+ delete api.plugintypeid;
22
+ api['PluginTypeId@odata.bind'] = `plugintypes(${config.plugintypeid})`;
23
+ if (config.customapiid) {
24
+ try {
25
+ await updateApi(config.customapiid, api, apiConfig);
26
+ }
27
+ catch (error) {
28
+ throw new Error(`failed to update custom api: ${error.message}`);
29
+ }
30
+ }
31
+ else {
32
+ try {
33
+ config.customapiid = await createApi(api, apiConfig, solution);
34
+ }
35
+ catch (error) {
36
+ throw new Error(`failed to create custom api: ${error.message}`);
37
+ }
38
+ }
39
+ }
40
+ exports.deployApi = deployApi;
41
+ async function retrieveApi(name, apiConfig) {
42
+ const options = `$select=customapiid&$filter=name eq '${name}'`;
43
+ const result = await (0, node_1.retrieveMultiple)(apiConfig, 'customapis', options);
44
+ return result.value.length > 0 ? result.value[0].customapiid : '';
45
+ }
46
+ exports.retrieveApi = retrieveApi;
47
+ async function createApi(api, apiConfig, solution) {
48
+ logger_1.logger.info(`create custom api ${api.name}`);
49
+ const options = {};
50
+ if (solution) {
51
+ options.customHeaders = { 'MSCRM.SolutionUniqueName': solution };
52
+ }
53
+ const result = await (0, node_1.createWithReturnData)(apiConfig, 'customapis', api, '$select=customapiid', options);
54
+ if (result?.error) {
55
+ throw new Error(result.error.message);
56
+ }
57
+ return result.customapiid;
58
+ }
59
+ async function updateApi(id, api, apiConfig) {
60
+ logger_1.logger.info(`update custom api ${api.name}`);
61
+ const record = {
62
+ displayname: api.displayname,
63
+ description: api.description,
64
+ name: api.name,
65
+ executeprivilegename: api.executeprivilegename
66
+ };
67
+ if (api.plugintypeid) {
68
+ record['PluginTypeId@odata.bind'] = `plugintypes(${api.plugintypeid})`;
69
+ }
70
+ else {
71
+ record['PluginTypeId@odata.bind'] = null;
72
+ }
73
+ const result = await (0, node_1.update)(apiConfig, 'customapis', id, record);
74
+ if (result?.error) {
75
+ throw new Error(result.error.message);
76
+ }
77
+ }