n8n-nodes-zid-beta
Version:
BETA; n8n custom nodes for integrating with the Zid API (orders, products, customers, etc.)
95 lines (94 loc) • 4.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericZidApi = void 0;
const axios_1 = __importDefault(require("axios"));
class GenericZidApi {
constructor(credentials) {
this.client = axios_1.default.create({
baseURL: 'https://api.zid.sa/v1',
headers: {
'Authorization': `Bearer ${credentials.authorizationToken}`,
'X-Manager-Token': credentials.accessToken,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
});
}
async request(config) {
try {
const response = await this.client.request(config);
return response.data;
}
catch (error) {
if (error.response) {
const status = error.response.status;
const data = error.response.data;
// Handle specific Zid API error responses with enhanced messages
if (status === 401) {
throw new Error(`Zid API Authentication Failed: Please check your Client ID and Client Secret in the Partner Dashboard. ${JSON.stringify(data)}`);
}
else if (status === 403) {
throw new Error(`Zid API Authorization Failed: Please verify your app permissions and scopes in the Partner Dashboard. ${JSON.stringify(data)}`);
}
else if (status === 429) {
throw new Error(`Zid API Rate Limit Exceeded: Please reduce request frequency or contact Zid support. ${JSON.stringify(data)}`);
}
else if (status === 404) {
throw new Error(`Zid API Resource Not Found: The requested resource does not exist or you don't have access to it. ${JSON.stringify(data)}`);
}
else if (status === 422) {
throw new Error(`Zid API Validation Error: Please check your request parameters. ${JSON.stringify(data)}`);
}
else if (status >= 500) {
throw new Error(`Zid API Server Error: Please try again later or contact Zid support. ${JSON.stringify(data)}`);
}
else {
throw new Error(`Zid API Error: ${status} ${JSON.stringify(data)}`);
}
}
// Handle network and other errors
if (error.code === 'ECONNREFUSED') {
throw new Error('Zid API Connection Refused: Please check your internet connection and Zid API status');
}
else if (error.code === 'ETIMEDOUT') {
throw new Error('Zid API Timeout: Request timed out, please try again');
}
else if (error.code === 'ENOTFOUND') {
throw new Error('Zid API DNS Error: Cannot resolve Zid API hostname');
}
throw new Error(`Zid API Network Error: ${error.message}`);
}
}
// GET with pagination support
async getPaginated(url, params = {}) {
let results = [];
let page = 1;
let hasMore = true;
while (hasMore) {
const response = await this.request({
method: 'GET',
url,
params: { ...params, page },
});
const data = response.data || response;
results = results.concat(Array.isArray(data) ? data : [data]);
// Check pagination metadata
if (response.meta && response.meta.current_page < response.meta.last_page) {
page++;
}
else {
hasMore = false;
}
}
return results;
}
// Helper method to update tokens if needed
updateCredentials(credentials) {
this.client.defaults.headers['Authorization'] = `Bearer ${credentials.authorizationToken}`;
this.client.defaults.headers['X-Manager-Token'] = credentials.accessToken;
}
}
exports.GenericZidApi = GenericZidApi;