kidshift-skills/lambda/api.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-07-16 22:25:27 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const BASE_URL = 'https://kidshift-beta.nem.one';
const TIMEOUT = 5000;
class ApiClient {
constructor() {
2024-07-17 00:19:40 +00:00
this.token = null;
2024-07-16 22:25:27 +00:00
this.client = axios_1.default.create({
baseURL: BASE_URL,
timeout: TIMEOUT,
});
}
2024-07-17 00:19:40 +00:00
setToken(token) {
this.token = token;
}
2024-07-16 22:25:27 +00:00
getHeaders(includeToken) {
const headers = {
'Content-Type': 'application/json',
};
if (includeToken) {
2024-07-17 00:19:40 +00:00
const token = this.token;
2024-07-16 22:25:27 +00:00
headers['Authorization'] = `Bearer ${token}`;
}
return headers;
}
async get(url, params, includeToken = true) {
const headers = this.getHeaders(includeToken);
const config = {
headers,
params
};
const response = await this.client.get(url, config);
return response.data;
}
2024-07-17 05:06:37 +00:00
async post(url, data, params, includeToken = true) {
2024-07-16 22:25:27 +00:00
const headers = this.getHeaders(includeToken);
2024-07-17 05:06:37 +00:00
const response = await this.client.post(url, data, { headers, params });
2024-07-16 22:25:27 +00:00
return response.data;
}
}
exports.default = new ApiClient();
//# sourceMappingURL=api.js.map