Files
kitchen-agent/api-client.js
2025-10-23 19:02:56 -04:00

95 lines
2.2 KiB
JavaScript

const fetch = require('node-fetch');
class APIClient {
constructor(baseUrl = process.env.API_URL || 'https://api.thinklink.ai') {
this.baseUrl = baseUrl;
}
async request(endpoint, options = {}) {
const url = `${this.baseUrl}${endpoint}`;
try {
const response = await fetch(url, {
method: options.method || 'POST',
headers: {
'Content-Type': 'application/json',
...(options.headers || {})
},
body: options.body ? JSON.stringify(options.body) : undefined
});
const data = await response.json();
return data;
} catch (error) {
console.error(`API request failed: ${endpoint}`, error.message);
return {
error: true,
message: `Network error: ${error.message}`
};
}
}
async login(email, password, recaptchaToken) {
return this.request('/user/login', {
body: {
login: email,
password: password,
'g-recaptcha-response': recaptchaToken
}
});
}
async getBots(token) {
return this.request('/bot/list', {
body: {
token: token
}
});
}
async getOrders(token, botId, afterId = 0, options = {}) {
const body = {
token: token,
botId: parseInt(botId, 10),
afterId: afterId || 0,
limit: options.limit || 50,
includeCanceled: options.includeCanceled || false
};
if (options.orderStatus) {
body.orderStatus = options.orderStatus;
}
if (options.sinceTs) {
body.sinceTs = options.sinceTs;
}
return this.request('/food-order/orders', { body });
}
async modifyOrder(token, botId, orderId, action, cancellationReason = '') {
const body = {
token: token,
botId: parseInt(botId, 10),
orderId: parseInt(orderId, 10),
action: action
};
if (action === 'cancel' && cancellationReason) {
body.cancellationReason = cancellationReason;
}
return this.request('/food-order/modify', { body });
}
isTokenExpired(expirationDate) {
if (!expirationDate) return true;
const expiry = new Date(expirationDate);
const now = new Date();
return now >= expiry;
}
}
module.exports = new APIClient();