@skbhati199/ai-img-gen-js
Version:
JavaScript/TypeScript SDK for AI Image Generator API
189 lines (188 loc) • 6.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AIImageGeneratorClient = void 0;
const axios_1 = __importDefault(require("axios"));
/**
* AI Image Generator SDK Client
*
* A client for interacting with the AI Image Generator API
*/
class AIImageGeneratorClient {
/**
* Create a new AI Image Generator client
*
* @param config - Configuration options for the client
*/
constructor(config) {
this.baseUrl = config.baseUrl.endsWith('/')
? config.baseUrl.slice(0, -1)
: config.baseUrl;
const axiosConfig = {
baseURL: this.baseUrl,
timeout: config.timeout || 30000,
headers: {
'Content-Type': 'application/json',
},
};
if (config.apiKey) {
axiosConfig.headers = {
...axiosConfig.headers,
Authorization: `Bearer ${config.apiKey}`,
};
}
this.client = axios_1.default.create(axiosConfig);
}
/**
* Generate an image using AI
*
* @param options - Image generation options
* @returns URL of the generated image
*/
async generateImage(options) {
const params = new URLSearchParams();
// Add required parameters
params.append('width', options.width.toString());
params.append('height', options.height.toString());
params.append('prompt', options.prompt);
// Add optional parameters if provided
if (options.model)
params.append('model', options.model);
if (options.format)
params.append('format', options.format);
if (options.quality)
params.append('quality', options.quality.toString());
if (options.optimize !== undefined)
params.append('optimize', options.optimize.toString());
let endpoint = '/images/image-gen';
try {
const response = await this.client.get(endpoint, { params });
return response.request.res.responseUrl || response.headers.location;
}
catch (error) {
// If the first endpoint fails, try the alternative endpoint
endpoint = '/images/image-gen';
const response = await this.client.get(endpoint, { params });
return response.request.res.responseUrl || response.headers.location;
}
}
/**
* Get a list of supported AI models
*
* @returns List of supported models
*/
async getSupportedModels() {
const response = await this.client.get('/images/supported-models');
console.log(response.data);
return {
data: response.data,
status: response.status,
statusText: response.statusText,
};
}
/**
* Get a list of supported image sizes
*
* @returns List of supported image sizes
*/
async getSupportedSizes() {
const response = await this.client.get('/images/supported-sizes');
return {
data: response.data,
status: response.status,
statusText: response.statusText,
};
}
/**
* Resize an existing image
*
* @param id - ID of the image to resize
* @param options - Resize options
* @returns URL of the resized image
*/
async resizeImage(id, options) {
const params = new URLSearchParams();
params.append('width', options.width.toString());
params.append('height', options.height.toString());
params.append('format', options.format);
params.append('quality', options.quality.toString());
const response = await this.client.get(`/images/process/resize/${id}`, {
params,
});
return response.request.res.responseUrl || response.headers.location;
}
/**
* Convert an image to a different format
*
* @param id - ID of the image to convert
* @param options - Conversion options
* @returns URL of the converted image
*/
async convertImage(id, options) {
const params = new URLSearchParams();
params.append('format', options.format);
params.append('quality', options.quality.toString());
const response = await this.client.get(`/images/process/convert/${id}`, {
params,
});
return response.request.res.responseUrl || response.headers.location;
}
/**
* Optimize an image for web delivery
*
* @param id - ID of the image to optimize
* @param options - Optimization options
* @returns URL of the optimized image
*/
async optimizeImage(id, options) {
const params = new URLSearchParams();
params.append('format', options.format);
params.append('quality', options.quality.toString());
const response = await this.client.get(`/images/process/optimize/${id}`, {
params,
});
return response.request.res.responseUrl || response.headers.location;
}
/**
* Get a list of supported image formats
*
* @returns List of supported image formats
*/
async getSupportedFormats() {
const response = await this.client.get('/images/process/formats');
return {
data: response.data,
status: response.status,
statusText: response.statusText,
};
}
/**
* Get API metrics
*
* @returns API metrics
*/
async getMetrics() {
const response = await this.client.get('/monitoring/metrics');
return {
data: response.data,
status: response.status,
statusText: response.statusText,
};
}
/**
* Check API health
*
* @returns Health status
*/
async checkHealth() {
const response = await this.client.get('/monitoring/health');
return {
data: response.data,
status: response.status,
statusText: response.statusText,
};
}
}
exports.AIImageGeneratorClient = AIImageGeneratorClient;