dynamicsmobile
Version:
Allows development of off-line mobile and web business apps over the Dynamics Mobile platform. More info on https://www.dynamicsmobile.com
74 lines • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackendDmsApplicationService = exports.universalFetch = void 0;
const tslib_1 = require("tslib");
const injectable_1 = require("../ioc/injectable");
const $ = require("jquery");
function universalFetch(url, options) {
var _a;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (typeof globalThis.process !== 'undefined' && !!((_a = globalThis.process.versions) === null || _a === void 0 ? void 0 : _a.node)) {
//get via https
const https = yield Promise.resolve().then(() => tslib_1.__importStar(require('https')));
return new Promise((resolve, reject) => {
const requestOptions = typeof url === 'string' ? Object.assign(Object.assign({}, options), new URL(url)) : options;
const req = https.request(requestOptions, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
})
.on('error', (e) => {
reject(e);
});
req.setTimeout(10000, () => {
req.abort();
reject(new Error('Request timed out'));
});
req.end();
});
}
else {
return fetch(url, options);
}
});
}
exports.universalFetch = universalFetch;
function ajaxRequest(options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
//ansure the method checks if window is defined and calls different methods
let response;
if (typeof window === 'undefined') {
//if we are in a server-side environment, we can use node-fetch or similar
response = yield universalFetch(options.url, options);
}
else {
response = yield $.ajax(options).promise();
}
return { data: response };
});
}
/**
* The class contains "globally" accessesed variables.
* They are meant to be accessible across the components (views, datamodels) of the app
*/
let BackendDmsApplicationService = exports.BackendDmsApplicationService = class BackendDmsApplicationService {
constructor(appCode, version, currentTask) {
this.appCode = appCode;
this.version = version;
this.currentTask = currentTask;
this.app = {
data: {},
views: [],
request: { promise: ajaxRequest }
};
}
};
exports.BackendDmsApplicationService = BackendDmsApplicationService = tslib_1.__decorate([
(0, injectable_1.Injectable)({ singleton: true }),
tslib_1.__metadata("design:paramtypes", [String, String, String])
], BackendDmsApplicationService);
//# sourceMappingURL=backend-applicationcontext-service.js.map