@mojaloop/scheduling-bc-client-lib
Version:
scheduling BC - Clients
147 lines (141 loc) • 6.07 kB
JavaScript
/*****
License
--------------
Copyright © 2020-2025 Mojaloop Foundation
The Mojaloop files are made available by the Mojaloop Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Mojaloop Foundation for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Mojaloop Foundation
- Name Surname <name.surname@mojaloop.io>
* Crosslake
- Pedro Sousa Barreto <pedrob@crosslaketech.com>
* Gonçalo Garcia <goncalogarcia99@gmail.com>
*****/
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchedulingClient = void 0;
const utils_1 = require("./utils");
const errors_1 = require("./errors");
const DEFAULT_TIMEOUT_MS = 10000;
class SchedulingClient {
// Properties received through the constructor.
logger;
// Other properties.
_baseUrlHttpService;
_requestTimeoutMs;
_remindersPath = "reminders";
constructor(logger, baseUrlHttpService, requestTimeoutMs = DEFAULT_TIMEOUT_MS) {
this.logger = logger;
this._baseUrlHttpService = baseUrlHttpService;
this._requestTimeoutMs = requestTimeoutMs;
}
async createReminder(reminder) {
try {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const res = await (0, utils_1.fetchWithTimeOut)(`${this._baseUrlHttpService}/${this._remindersPath}/`, {
method: "POST",
headers: headers,
body: JSON.stringify(reminder)
}, this._requestTimeoutMs);
const data = await res.json();
// istanbul ignore if
if (!data.reminderId) {
throw new errors_1.UnableToCreateReminderError(data.message);
}
return data.reminderId;
}
catch (e) {
const serverMessage = e.message;
if (e instanceof errors_1.UnableToCreateReminderError) {
this.logger.error(e);
throw new errors_1.UnableToCreateReminderError(serverMessage);
}
else {
this.logger.error(e);
throw new errors_1.UnableToReachServerError();
}
}
}
async createSingleReminder(reminder) {
try {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const res = await (0, utils_1.fetchWithTimeOut)(`${this._baseUrlHttpService}/${this._remindersPath}/single`, {
method: "POST",
headers: headers,
body: JSON.stringify(reminder)
}, this._requestTimeoutMs);
const data = await res.json();
// istanbul ignore if
if (!data.reminderId) {
throw new errors_1.UnableToCreateReminderError(data.message);
}
return data.reminderId;
}
catch (e) {
const serverErrorMessage = e.message;
if (e instanceof errors_1.UnableToCreateReminderError) {
this.logger.error(e);
throw new errors_1.UnableToCreateReminderError(serverErrorMessage);
}
else {
this.logger.error(e);
throw new errors_1.UnableToReachServerError(serverErrorMessage);
}
}
}
async getReminder(reminderId) {
try {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const res = await (0, utils_1.fetchWithTimeOut)(`${this._baseUrlHttpService}/${this._remindersPath}/${reminderId}`, {
method: "GET",
headers: headers,
}, this._requestTimeoutMs);
if (res.status === 404) {
return null;
}
const data = await res.json();
return data.reminder;
}
catch (e) {
const serverErrorMessage = e.message;
throw new errors_1.UnableToGetReminderError(serverErrorMessage); // TODO: receive a string?
}
}
async deleteReminder(reminderId) {
try {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const res = await (0, utils_1.fetchWithTimeOut)(`${this._baseUrlHttpService}/${this._remindersPath}/${reminderId}`, {
method: "DELETE",
headers: headers,
}, this._requestTimeoutMs);
const data = await res.json();
if (res.status != 200) {
throw new errors_1.UnableToDeleteReminderError(data.message);
}
}
catch (e) {
const serverErrorMessage = e.message;
if (e instanceof errors_1.UnableToDeleteReminderError) {
this.logger.error(e);
throw new errors_1.UnableToDeleteReminderError(serverErrorMessage);
}
throw new errors_1.UnableToReachServerError(serverErrorMessage); // TODO: receive a string?
}
}
}
exports.SchedulingClient = SchedulingClient;
//# sourceMappingURL=scheduling_client.js.map