@elasticapi/wpengine-typescript-sdk
Version:
Unofficial TypeScript SDK for the WP Engine API
101 lines • 4.39 kB
JavaScript
import { Configuration } from './generated/configuration';
import { AccountApi, AccountUserApi, BackupApi, CacheApi, DomainApi, InstallApi, SiteApi, SshKeyApi, StatusApi, UserApi } from './generated/api';
import { ConfigurationManager } from './config';
import { ValidatedApiWrapper } from './validation/api-wrapper';
import { validators } from './validation/validators';
import { RateLimiter, RateLimitError } from './rate-limiter';
import axios from 'axios';
export class WPEngineSDK {
constructor(credentials, configPath, profile = 'Default', options = {}) {
// Initialize rate limiter
this.rateLimiter = new RateLimiter(options.maxRequestsPerSecond);
if (credentials) {
// Validate provided credentials
validators.credentials(credentials.username, credentials.password);
// Use provided credentials directly
this.config = {
credentials,
baseURL: 'https://api.wpengineapi.com/v1'
};
}
else {
// Load from config file or environment variables
const configManager = new ConfigurationManager(configPath);
this.config = configManager.getConfig(profile);
// Validate loaded credentials
validators.credentials(this.config.credentials.username, this.config.credentials.password);
}
const params = {
baseOptions: {
auth: {
username: this.config.credentials.username,
password: this.config.credentials.password,
},
},
basePath: this.config.baseURL,
};
this.axiosConfig = new Configuration(params);
// Initialize API clients with validation wrappers
this.accounts = new AccountApi(this.axiosConfig);
this.accountUsers = ValidatedApiWrapper.wrapAccountUserApi(new AccountUserApi(this.axiosConfig));
this.backups = new BackupApi(this.axiosConfig);
this.cache = new CacheApi(this.axiosConfig);
this.domains = ValidatedApiWrapper.wrapDomainApi(new DomainApi(this.axiosConfig));
this.installs = ValidatedApiWrapper.wrapInstallApi(new InstallApi(this.axiosConfig));
this.sites = ValidatedApiWrapper.wrapSiteApi(new SiteApi(this.axiosConfig));
this.sshKeys = new SshKeyApi(this.axiosConfig);
this.status = new StatusApi(this.axiosConfig);
this.users = new UserApi(this.axiosConfig);
// Add request interceptor for rate limiting and validation
axios.interceptors.request.use(async (config) => {
// Apply rate limiting
try {
await this.rateLimiter.acquireToken();
}
catch (error) {
throw new RateLimitError('Rate limit exceeded');
}
// Validate URLs
if (config.url) {
validators.url(config.url);
}
return config;
});
// Add response interceptor for error handling
axios.interceptors.response.use((response) => response, (error) => {
var _a, _b, _c;
if (error.response) {
// Clean sensitive data from error responses
if ((_a = error.response.config) === null || _a === void 0 ? void 0 : _a.auth) {
delete error.response.config.auth;
}
if ((_c = (_b = error.response.config) === null || _b === void 0 ? void 0 : _b.headers) === null || _c === void 0 ? void 0 : _c.Authorization) {
delete error.response.config.headers.Authorization;
}
}
throw error;
});
}
/**
* Get the current configuration
*/
getConfig() {
// Return a copy to prevent modification
return { ...this.config };
}
/**
* Get rate limiter statistics
*/
getRateLimiterStats() {
return {
availableTokens: this.rateLimiter.getAvailableTokens(),
waitTime: this.rateLimiter.getWaitTime()
};
}
}
// Export types and utilities
export * from './generated/api';
export { ConfigurationManager } from './config';
export { ValidationError } from './validation/validators';
export { RateLimitError } from './rate-limiter';
//# sourceMappingURL=index.js.map