smart-track
Version:
A TypeScript SDK for integrating with tracking services using the Beckn protocol. Provides adapters for package tracking with built-in error handling, retry logic, and type safety.
49 lines • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.APIAdapter = void 0;
const endpoint_const_1 = require("../constants/endpoint.const");
const base_adapter_1 = require("./base.adapter");
const error_1 = require("./utils/error");
class APIAdapter extends base_adapter_1.BaseAdapter {
constructor(config) {
super(config);
this.authToken = config.authToken || "";
this.apiVersion = config.apiVersion || "v1";
}
async request(method, endpoint, data, options = {}) {
const authHeaders = this.getAuthHeaders();
const versionHeaders = this.getVersionHeaders();
const mergedOptions = {
...options,
headers: {
...authHeaders,
...versionHeaders,
...options.headers,
},
};
try {
return await super.request(method, endpoint, data, mergedOptions);
}
catch (error) {
if (error instanceof error_1.AdapterError && error.status === 401) {
throw new error_1.AuthenticationError("Invalid or expired token");
}
throw error;
}
}
getAuthHeaders() {
if (!this.authToken)
return {};
return { Authorization: `Bearer ${this.authToken}` };
}
getVersionHeaders() {
if (!this.apiVersion)
return {};
return { "X-API-Version": this.apiVersion };
}
async getPackageDetails(payload) {
return this.request('GET', endpoint_const_1.ENDPOINTS.TRACKING, payload);
}
}
exports.APIAdapter = APIAdapter;
//# sourceMappingURL=api.adapter.js.map