tsonik
Version:
A TypeScript client library for the Iconik API based on Swagger documentation
145 lines • 5.76 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tsonik = void 0;
const axios_1 = __importDefault(require("axios"));
const errors_1 = require("./errors");
const resources_1 = require("./resources");
const retry_1 = require("./retry");
/**
* Main client class for interacting with the Iconik API
*/
class Tsonik {
constructor(config) {
this.config = config;
this.retryConfig = (0, retry_1.mergeRetryConfig)(config.retry);
this.httpClient = axios_1.default.create({
baseURL: config.baseUrl,
timeout: config.timeout || 30000,
headers: {
'App-ID': config.appId,
'Auth-Token': config.authToken,
'Content-Type': 'application/json',
'Accept': 'application/json',
...config.headers,
},
});
// Add request interceptor for logging
this.httpClient.interceptors.request.use((config) => {
if (this.config.debug) {
console.log(`[Iconik API] ${config.method?.toUpperCase()} ${config.url}`);
}
return config;
}, (error) => Promise.reject(error));
// Add response interceptor for error handling
this.httpClient.interceptors.response.use((response) => response, (error) => {
// Log detailed error information for debugging
console.error('Iconik API Error Details:', {
status: error.response?.status,
statusText: error.response?.statusText,
url: error.config?.url,
method: error.config?.method,
requestData: error.config?.data,
responseData: error.response?.data,
message: error.message
});
// Log specific validation errors if available
if (error.response?.data?.errors) {
console.error('API Validation Errors:', error.response.data.errors);
}
if (error.response?.data?.error_description) {
console.error('API Error Description:', error.response.data.error_description);
}
if (error.response?.status === 401) {
throw new errors_1.IconikAuthError('Invalid API key or unauthorized access');
}
if (error.response?.status >= 400) {
const errorMessage = error.response.data?.message ||
error.response.data?.error_description ||
error.response.statusText ||
'API request failed';
throw new errors_1.IconikAPIError(errorMessage, error.response.status, error.response.data);
}
throw new errors_1.IconikError(`Network error: ${error.message}`);
});
// Initialize ORM-like resources
this.assets = new resources_1.AssetResource(this);
this.jobs = new resources_1.JobResource(this);
this.collections = new resources_1.CollectionResource(this);
this.filesets = new resources_1.FileSetResource(this);
this.files = new resources_1.FileResource(this);
this.formats = new resources_1.FormatResource(this);
this.metadata = new resources_1.MetadataResource(this);
}
/**
* Make a GET request to the API
*/
async get(path, config) {
const response = await (0, retry_1.createRetryWrapper)(() => this.httpClient.get(path, config), this.retryConfig);
return {
data: response.data,
status: response.status,
headers: response.headers,
};
}
/**
* Make a POST request to the API
*/
async post(path, data, config) {
const response = await (0, retry_1.createRetryWrapper)(() => this.httpClient.post(path, data, config), this.retryConfig);
return {
data: response.data,
status: response.status,
headers: response.headers,
};
}
/**
* Make a PUT request to the API
*/
async put(path, data, config) {
const response = await (0, retry_1.createRetryWrapper)(() => this.httpClient.put(path, data, config), this.retryConfig);
return {
data: response.data,
status: response.status,
headers: response.headers,
};
}
/**
* Make a DELETE request to the API
*/
async delete(path, config) {
const response = await (0, retry_1.createRetryWrapper)(() => this.httpClient.delete(path, config), this.retryConfig);
return {
data: response.data,
status: response.status,
headers: response.headers,
};
}
/**
* Make a PATCH request to the API
*/
async patch(path, data, config) {
const response = await (0, retry_1.createRetryWrapper)(() => this.httpClient.patch(path, data, config), this.retryConfig);
return {
data: response.data,
status: response.status,
headers: response.headers,
};
}
/**
* Get client information including version and configuration
* Useful for debugging and support purposes
*/
getClientInfo() {
return {
name: 'tsonik',
version: '1.1.0', // This will be updated by semantic-release
baseUrl: this.config.baseUrl || 'https://app.iconik.io',
userAgent: this.httpClient.defaults.headers.common['User-Agent'] || 'tsonik-client'
};
}
}
exports.Tsonik = Tsonik;
//# sourceMappingURL=client.js.map