@contrail/flexplm 1.0.5 → 1.0.7

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.
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.Federation = void 0;
13
4
  const sdk_1 = require("@contrail/sdk");
@@ -21,67 +12,61 @@ class Federation {
21
12
  constructor() {
22
13
  this.CHUNK_SIZE = 50;
23
14
  }
24
- getFederatedMappedRefId(entityType, entityId) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- const criteria = {
27
- reference: entityType + ':' + entityId,
28
- appIdentifier: FED_CONFIG.appIdentifier,
29
- federationSchema: FED_CONFIG.federationSchema
30
- };
31
- console.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
32
- const fedRecords = yield new sdk_1.Entities().get({
33
- entityName: 'federation',
34
- criteria
35
- });
36
- const federatedId = (fedRecords[0])
37
- ? fedRecords[0]['mappedReference']
38
- : '';
39
- return federatedId;
15
+ async getFederatedMappedRefId(entityType, entityId) {
16
+ const criteria = {
17
+ reference: entityType + ':' + entityId,
18
+ appIdentifier: FED_CONFIG.appIdentifier,
19
+ federationSchema: FED_CONFIG.federationSchema
20
+ };
21
+ console.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
22
+ const fedRecords = await new sdk_1.Entities().get({
23
+ entityName: 'federation',
24
+ criteria
40
25
  });
26
+ const federatedId = (fedRecords[0])
27
+ ? fedRecords[0]['mappedReference']
28
+ : '';
29
+ return federatedId;
41
30
  }
42
- createFederatedRecord(eventBody) {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- const itemResults = eventBody.payload;
45
- for (let i = 0; i < itemResults.length; i++) {
46
- if (!itemResults[i].federatedId) {
47
- continue;
48
- }
49
- const payload = {
50
- appIdentifier: FED_CONFIG.appIdentifier,
51
- reference: itemResults[i].entityReference,
52
- mappedReference: itemResults[i].federatedId,
53
- federationSchema: FED_CONFIG.federationSchema
54
- };
55
- try {
56
- const results = yield new sdk_1.Entities().create({ entityName: 'federation', object: payload });
57
- return results;
58
- }
59
- catch (error) {
60
- console.log('createFederatedRecord-error: ', error);
61
- throw new Error('Error creating federation record: ' + error.message);
62
- }
31
+ async createFederatedRecord(eventBody) {
32
+ const itemResults = eventBody.payload;
33
+ for (let i = 0; i < itemResults.length; i++) {
34
+ if (!itemResults[i].federatedId) {
35
+ continue;
63
36
  }
64
- });
65
- }
66
- getFederationRecordFromMappedRefId(mappedRefId) {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- const criteria = {
37
+ const payload = {
69
38
  appIdentifier: FED_CONFIG.appIdentifier,
70
- mappedReference: mappedRefId,
39
+ reference: itemResults[i].entityReference,
40
+ mappedReference: itemResults[i].federatedId,
71
41
  federationSchema: FED_CONFIG.federationSchema
72
42
  };
73
43
  try {
74
- const fedRecords = yield new sdk_1.Entities().get({
75
- entityName: 'federation',
76
- criteria
77
- });
78
- return (fedRecords && fedRecords[0]) ? fedRecords[0] : undefined;
44
+ const results = await new sdk_1.Entities().create({ entityName: 'federation', object: payload });
45
+ return results;
79
46
  }
80
- catch (e) {
81
- console.log('getFederationData-error: ' + e.message);
47
+ catch (error) {
48
+ console.log('createFederatedRecord-error: ', error);
49
+ throw new Error('Error creating federation record: ' + error.message);
82
50
  }
83
- return undefined;
84
- });
51
+ }
52
+ }
53
+ async getFederationRecordFromMappedRefId(mappedRefId) {
54
+ const criteria = {
55
+ appIdentifier: FED_CONFIG.appIdentifier,
56
+ mappedReference: mappedRefId,
57
+ federationSchema: FED_CONFIG.federationSchema
58
+ };
59
+ try {
60
+ const fedRecords = await new sdk_1.Entities().get({
61
+ entityName: 'federation',
62
+ criteria
63
+ });
64
+ return (fedRecords && fedRecords[0]) ? fedRecords[0] : undefined;
65
+ }
66
+ catch (e) {
67
+ console.log('getFederationData-error: ' + e.message);
68
+ }
69
+ return undefined;
85
70
  }
86
71
  static getEntityId(fedRecord) {
87
72
  const entityString = fedRecord['reference'];
@@ -90,73 +75,67 @@ class Federation {
90
75
  const entityId = entitySplit[1];
91
76
  return { entityType, entityId };
92
77
  }
93
- getEntityFromMappedRefId(mappedRefId) {
94
- return __awaiter(this, void 0, void 0, function* () {
95
- const fedRecord = yield this.getFederationRecordFromMappedRefId(mappedRefId);
96
- console.log('fedRecord: ' + JSON.stringify(fedRecord));
97
- if (!fedRecord) {
98
- console.log('Federation Record doesnt exist. Cant get entity!');
99
- return;
100
- }
101
- const { entityType, entityId } = Federation.getEntityId(fedRecord);
78
+ async getEntityFromMappedRefId(mappedRefId) {
79
+ const fedRecord = await this.getFederationRecordFromMappedRefId(mappedRefId);
80
+ console.log('fedRecord: ' + JSON.stringify(fedRecord));
81
+ if (!fedRecord) {
82
+ console.log('Federation Record doesnt exist. Cant get entity!');
83
+ return;
84
+ }
85
+ const { entityType, entityId } = Federation.getEntityId(fedRecord);
86
+ const criteria = {
87
+ id: entityId
88
+ };
89
+ const entities = await new sdk_1.Entities().get({
90
+ entityName: entityType,
91
+ criteria
92
+ });
93
+ const entity = (entities && entities[0]) ? entities[0] : undefined;
94
+ return entity;
95
+ }
96
+ async getFederationRecordsFromIds(ids) {
97
+ const fedRecords = [];
98
+ for (let i = 0; i < ids.length; i++) {
102
99
  const criteria = {
103
- id: entityId
100
+ reference: ids[i],
101
+ appIdentifier: FED_CONFIG.appIdentifier,
102
+ federationSchema: FED_CONFIG.federationSchema
104
103
  };
105
- const entities = yield new sdk_1.Entities().get({
106
- entityName: entityType,
104
+ const recs = await new sdk_1.Entities().get({
105
+ entityName: 'federation',
107
106
  criteria
108
107
  });
109
- const entity = (entities && entities[0]) ? entities[0] : undefined;
110
- return entity;
111
- });
108
+ if (recs[0]) {
109
+ fedRecords.push(recs[0]);
110
+ }
111
+ }
112
+ return fedRecords;
112
113
  }
113
- getFederationRecordsFromIds(ids) {
114
- return __awaiter(this, void 0, void 0, function* () {
115
- const fedRecords = [];
116
- for (let i = 0; i < ids.length; i++) {
114
+ async getFederationRecordsFromIdsBulk(ids) {
115
+ const chunks = this.splitIntoChunksByLen(ids, this.CHUNK_SIZE);
116
+ const fedRecords = [];
117
+ const federatedPromises = [];
118
+ const entities = new sdk_1.Entities();
119
+ for (const chunk of chunks) {
120
+ const fedPromise = limit(async () => {
117
121
  const criteria = {
118
- reference: ids[i],
122
+ references: chunk,
119
123
  appIdentifier: FED_CONFIG.appIdentifier,
120
124
  federationSchema: FED_CONFIG.federationSchema
121
125
  };
122
- const recs = yield new sdk_1.Entities().get({
126
+ const records = await entities.get({
123
127
  entityName: 'federation',
124
128
  criteria
125
129
  });
126
- if (recs[0]) {
127
- fedRecords.push(recs[0]);
128
- }
129
- }
130
- return fedRecords;
131
- });
132
- }
133
- getFederationRecordsFromIdsBulk(ids) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- const chunks = this.splitIntoChunksByLen(ids, this.CHUNK_SIZE);
136
- const fedRecords = [];
137
- const federatedPromises = [];
138
- const entities = new sdk_1.Entities();
139
- for (const chunk of chunks) {
140
- const fedPromise = limit(() => __awaiter(this, void 0, void 0, function* () {
141
- const criteria = {
142
- references: chunk,
143
- appIdentifier: FED_CONFIG.appIdentifier,
144
- federationSchema: FED_CONFIG.federationSchema
145
- };
146
- const records = yield entities.get({
147
- entityName: 'federation',
148
- criteria
149
- });
150
- fedRecords.push(...records);
151
- }));
152
- federatedPromises.push(fedPromise);
153
- }
154
- yield Promise.all(federatedPromises);
155
- return fedRecords;
156
- });
130
+ fedRecords.push(...records);
131
+ });
132
+ federatedPromises.push(fedPromise);
133
+ }
134
+ await Promise.all(federatedPromises);
135
+ return fedRecords;
157
136
  }
158
137
  splitIntoChunksByLen(arr, len) {
159
- const chunks = [], n = arr === null || arr === void 0 ? void 0 : arr.length;
138
+ const chunks = [], n = arr?.length;
160
139
  let i = 0;
161
140
  while (i < n) {
162
141
  chunks.push(arr.slice(i, i += len));
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.FlexPLMConnect = void 0;
13
4
  const app_framework_1 = require("@contrail/app-framework");
@@ -24,7 +15,6 @@ class FlexPLMConnect {
24
15
  : this.config['payloadDefaultAsArray'];
25
16
  }
26
17
  getRequestOptions(method) {
27
- var _a;
28
18
  const csrfOptions = {
29
19
  method,
30
20
  headers: {
@@ -35,117 +25,106 @@ class FlexPLMConnect {
35
25
  };
36
26
  if (app_framework_1.Logger.isInfoOn()) {
37
27
  const logOptions = JSON.parse(JSON.stringify(csrfOptions));
38
- logOptions['headers']['Authorization'] = (_a = logOptions === null || logOptions === void 0 ? void 0 : logOptions.headers) === null || _a === void 0 ? void 0 : _a.Authorization.substring(0, 9);
28
+ logOptions['headers']['Authorization'] = logOptions?.headers?.Authorization.substring(0, 9);
39
29
  console.info('csrfOptions: ' + JSON.stringify(logOptions));
40
30
  }
41
31
  return csrfOptions;
42
32
  }
43
- getCSRF() {
44
- return __awaiter(this, void 0, void 0, function* () {
45
- const urlContext = this.config.urlContext;
46
- const csrfEndpoint = this.config.csrfEndpoint;
47
- const csrfURL = this.config.apiHost + urlContext + csrfEndpoint;
48
- console.info('csrfURL: ' + csrfURL);
49
- const csrfOptions = this.getRequestOptions('GET');
50
- const response = yield fetch(csrfURL, csrfOptions);
51
- if (response.status >= 300) {
52
- const message = 'Error connecting to FlexPLM:status: ' + response.status;
53
- console.error(message);
54
- console.error(yield response.text());
55
- throw new Error(message);
56
- }
57
- try {
58
- const body = yield response.json();
59
- const nonce_key = body.items[0].attributes.nonce_key;
60
- const nonce = body.items[0].attributes.nonce;
61
- console.info('nonce_key: ' + nonce_key);
62
- console.info('nonce: ' + nonce);
63
- return {
64
- nonce_key,
65
- nonce
66
- };
67
- }
68
- catch (e) {
69
- const message = 'Error connecting to FlexPLM: ' + e.message;
70
- console.error(message);
71
- console.error(yield response.text());
72
- throw new Error(message);
73
- }
74
- });
33
+ async getCSRF() {
34
+ const urlContext = this.config.urlContext;
35
+ const csrfEndpoint = this.config.csrfEndpoint;
36
+ const csrfURL = this.config.apiHost + urlContext + csrfEndpoint;
37
+ console.info('csrfURL: ' + csrfURL);
38
+ const csrfOptions = this.getRequestOptions('GET');
39
+ const response = await fetch(csrfURL, csrfOptions);
40
+ if (response.status >= 300) {
41
+ const message = 'Error connecting to FlexPLM:status: ' + response.status;
42
+ console.error(message);
43
+ console.error(await response.text());
44
+ throw new Error(message);
45
+ }
46
+ try {
47
+ const body = await response.json();
48
+ const nonce_key = body.items[0].attributes.nonce_key;
49
+ const nonce = body.items[0].attributes.nonce;
50
+ console.info('nonce_key: ' + nonce_key);
51
+ console.info('nonce: ' + nonce);
52
+ return {
53
+ nonce_key,
54
+ nonce
55
+ };
56
+ }
57
+ catch (e) {
58
+ const message = 'Error connecting to FlexPLM: ' + e.message;
59
+ console.error(message);
60
+ console.error(await response.text());
61
+ throw new Error(message);
62
+ }
75
63
  }
76
- sendRequest(csrf, payload) {
77
- var _a;
78
- return __awaiter(this, void 0, void 0, function* () {
79
- if (this.payloadSendAsArray && !Array.isArray(payload)) {
80
- payload = [payload];
81
- }
82
- const xferOptions = this.getRequestOptions('POST');
83
- xferOptions['body'] = JSON.stringify(payload);
84
- xferOptions.headers[csrf.nonce_key] = csrf.nonce;
85
- const urlContext = this.config.urlContext;
86
- const vibeEventsURL = this.config.apiHost + urlContext + '/servlet/rest' + this.vibeEventEndpoint;
87
- if (app_framework_1.Logger.isInfoOn()) {
88
- console.info('Request:');
89
- console.info('vibeEventsURL: ' + vibeEventsURL);
90
- const logOptions = JSON.parse(JSON.stringify(xferOptions));
91
- logOptions['headers']['Authorization'] = (_a = logOptions === null || logOptions === void 0 ? void 0 : logOptions.headers) === null || _a === void 0 ? void 0 : _a.Authorization.substring(0, 9);
92
- console.info('csrfOptions: ' + JSON.stringify(logOptions));
93
- console.info('Making call to xfer data to FlexPLM');
94
- }
95
- const eventResponse = yield fetch(vibeEventsURL, xferOptions);
96
- return eventResponse;
97
- });
64
+ async sendRequest(csrf, payload) {
65
+ if (this.payloadSendAsArray && !Array.isArray(payload)) {
66
+ payload = [payload];
67
+ }
68
+ const xferOptions = this.getRequestOptions('POST');
69
+ xferOptions['body'] = JSON.stringify(payload);
70
+ xferOptions.headers[csrf.nonce_key] = csrf.nonce;
71
+ const urlContext = this.config.urlContext;
72
+ const vibeEventsURL = this.config.apiHost + urlContext + '/servlet/rest' + this.vibeEventEndpoint;
73
+ if (app_framework_1.Logger.isInfoOn()) {
74
+ console.info('Request:');
75
+ console.info('vibeEventsURL: ' + vibeEventsURL);
76
+ const logOptions = JSON.parse(JSON.stringify(xferOptions));
77
+ logOptions['headers']['Authorization'] = logOptions?.headers?.Authorization.substring(0, 9);
78
+ console.info('csrfOptions: ' + JSON.stringify(logOptions));
79
+ console.info('Making call to xfer data to FlexPLM');
80
+ }
81
+ const eventResponse = await fetch(vibeEventsURL, xferOptions);
82
+ return eventResponse;
98
83
  }
99
- processRequest(payload) {
100
- return __awaiter(this, void 0, void 0, function* () {
101
- if (!payload) {
102
- const message = 'No payload to send to FlexPLM';
103
- console.error(message);
104
- throw new Error(message);
105
- }
106
- if (app_framework_1.Logger.isInfoOn()) {
107
- console.info('payload: ' + JSON.stringify(payload));
108
- }
109
- const csrf = yield this.getCSRF();
110
- if (!csrf) {
111
- const message = 'Failed to get CSRF nonce';
112
- console.error(message);
113
- throw new Error(message);
114
- }
115
- const response = yield this.sendRequest(csrf, payload);
116
- const status = response.status;
117
- if (status >= 300) {
118
- const message = 'Error sending data to FlexPLM:status: ' + response.status;
119
- console.error(message);
120
- console.error(yield response.text());
121
- throw new Error(message);
122
- }
123
- try {
124
- const data = yield response.json();
125
- const res = {
126
- status,
127
- data
128
- };
129
- console.log('eventResponse.status: ' + status);
130
- return res;
131
- }
132
- catch (e) {
133
- const message = 'Error getting json data from FlexPLM: ' + e.message;
134
- console.error(message);
135
- console.error(yield response.text());
136
- throw new Error(message);
137
- }
138
- });
84
+ async processRequest(payload) {
85
+ if (!payload) {
86
+ const message = 'No payload to send to FlexPLM';
87
+ console.error(message);
88
+ throw new Error(message);
89
+ }
90
+ if (app_framework_1.Logger.isInfoOn()) {
91
+ console.info('payload: ' + JSON.stringify(payload));
92
+ }
93
+ const csrf = await this.getCSRF();
94
+ if (!csrf) {
95
+ const message = 'Failed to get CSRF nonce';
96
+ console.error(message);
97
+ throw new Error(message);
98
+ }
99
+ const response = await this.sendRequest(csrf, payload);
100
+ const status = response.status;
101
+ if (status >= 300) {
102
+ const message = 'Error sending data to FlexPLM:status: ' + response.status;
103
+ console.error(message);
104
+ console.error(await response.text());
105
+ throw new Error(message);
106
+ }
107
+ try {
108
+ const data = await response.json();
109
+ const res = {
110
+ status,
111
+ data
112
+ };
113
+ console.log('eventResponse.status: ' + status);
114
+ return res;
115
+ }
116
+ catch (e) {
117
+ const message = 'Error getting json data from FlexPLM: ' + e.message;
118
+ console.error(message);
119
+ console.error(await response.text());
120
+ throw new Error(message);
121
+ }
139
122
  }
140
- sendToFlexPLM(payload) {
141
- return __awaiter(this, void 0, void 0, function* () {
142
- return yield this.processRequest(payload);
143
- });
123
+ async sendToFlexPLM(payload) {
124
+ return await this.processRequest(payload);
144
125
  }
145
- sendMultipleToFlexPLM(payload) {
146
- return __awaiter(this, void 0, void 0, function* () {
147
- return yield this.processRequest(payload);
148
- });
126
+ async sendMultipleToFlexPLM(payload) {
127
+ return await this.processRequest(payload);
149
128
  }
150
129
  }
151
130
  exports.FlexPLMConnect = FlexPLMConnect;
@@ -1,37 +1,26 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.setLoggerConfig = void 0;
13
4
  const app_framework_1 = require("@contrail/app-framework");
14
- function setLoggerConfig(appConfig) {
15
- return __awaiter(this, void 0, void 0, function* () {
16
- let logLevel = app_framework_1.LogLevel.INFO;
17
- if (!appConfig.logLevel) {
18
- }
19
- else if (appConfig.logLevel === 'error') {
20
- logLevel = app_framework_1.LogLevel.ERROR;
21
- }
22
- else if (appConfig.logLevel === 'warn') {
23
- logLevel = app_framework_1.LogLevel.WARN;
24
- }
25
- else if (appConfig.logLevel === 'info') {
26
- logLevel = app_framework_1.LogLevel.INFO;
27
- }
28
- else if (appConfig.logLevel === 'debug') {
29
- logLevel = app_framework_1.LogLevel.DEBUG;
30
- }
31
- else {
32
- console.log('could not parse log level:', appConfig);
33
- }
34
- app_framework_1.Logger.setConfig({ logLevel: logLevel });
35
- });
5
+ async function setLoggerConfig(appConfig) {
6
+ let logLevel = app_framework_1.LogLevel.INFO;
7
+ if (!appConfig.logLevel) {
8
+ }
9
+ else if (appConfig.logLevel === 'error') {
10
+ logLevel = app_framework_1.LogLevel.ERROR;
11
+ }
12
+ else if (appConfig.logLevel === 'warn') {
13
+ logLevel = app_framework_1.LogLevel.WARN;
14
+ }
15
+ else if (appConfig.logLevel === 'info') {
16
+ logLevel = app_framework_1.LogLevel.INFO;
17
+ }
18
+ else if (appConfig.logLevel === 'debug') {
19
+ logLevel = app_framework_1.LogLevel.DEBUG;
20
+ }
21
+ else {
22
+ console.log('could not parse log level:', appConfig);
23
+ }
24
+ app_framework_1.Logger.setConfig({ logLevel: logLevel });
36
25
  }
37
26
  exports.setLoggerConfig = setLoggerConfig;