@minimaltech/ra-infra
Version:
Minimal Technology ReactJS Infrastructure
43 lines • 1.42 kB
JavaScript
import isEmpty from 'lodash/isEmpty';
import { NetworkHelper } from '../../helpers';
import { getError } from '../../utilities';
export class BaseNetworkRequestService {
baseUrl;
networkService;
constructor(opts) {
const { name, baseUrl } = opts;
this.baseUrl = baseUrl ?? '';
this.networkService = new NetworkHelper({ name });
}
getRequestPath(opts) {
const paths = opts?.paths ?? [];
const joined = paths
.map((path) => {
if (!path.startsWith('/')) {
path = `/${path}`; // Add / to the start of url path
}
return path;
})
.join('');
return joined;
}
getRequestUrl(opts) {
let baseUrl = opts?.baseUrl ?? this.baseUrl ?? '';
const paths = opts?.paths ?? [];
if (!baseUrl || isEmpty(baseUrl)) {
throw getError({
statusCode: 500,
message: '[getRequestUrl] Invalid configuration for third party request base url!',
});
}
if (baseUrl.endsWith('/')) {
baseUrl = baseUrl.slice(0, -1); // Remove / at the end
}
const joined = this.getRequestPath({ paths });
return `${baseUrl ?? this.baseUrl}${joined}`;
}
getNetworkService() {
return this.networkService;
}
}
//# sourceMappingURL=base-network-request.service.js.map