@elasticapi/wpengine-typescript-sdk
Version:
Unofficial TypeScript SDK for the WP Engine API
125 lines • 5.95 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimitError = exports.ValidationError = exports.ConfigurationManager = exports.WPEngineSDK = void 0;
const configuration_1 = require("./generated/configuration");
const api_1 = require("./generated/api");
const config_1 = require("./config");
const api_wrapper_1 = require("./validation/api-wrapper");
const validators_1 = require("./validation/validators");
const rate_limiter_1 = require("./rate-limiter");
const axios_1 = __importDefault(require("axios"));
class WPEngineSDK {
constructor(credentials, configPath, profile = 'Default', options = {}) {
// Initialize rate limiter
this.rateLimiter = new rate_limiter_1.RateLimiter(options.maxRequestsPerSecond);
if (credentials) {
// Validate provided credentials
validators_1.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 config_1.ConfigurationManager(configPath);
this.config = configManager.getConfig(profile);
// Validate loaded credentials
validators_1.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_1.Configuration(params);
// Initialize API clients with validation wrappers
this.accounts = new api_1.AccountApi(this.axiosConfig);
this.accountUsers = api_wrapper_1.ValidatedApiWrapper.wrapAccountUserApi(new api_1.AccountUserApi(this.axiosConfig));
this.backups = new api_1.BackupApi(this.axiosConfig);
this.cache = new api_1.CacheApi(this.axiosConfig);
this.domains = api_wrapper_1.ValidatedApiWrapper.wrapDomainApi(new api_1.DomainApi(this.axiosConfig));
this.installs = api_wrapper_1.ValidatedApiWrapper.wrapInstallApi(new api_1.InstallApi(this.axiosConfig));
this.sites = api_wrapper_1.ValidatedApiWrapper.wrapSiteApi(new api_1.SiteApi(this.axiosConfig));
this.sshKeys = new api_1.SshKeyApi(this.axiosConfig);
this.status = new api_1.StatusApi(this.axiosConfig);
this.users = new api_1.UserApi(this.axiosConfig);
// Add request interceptor for rate limiting and validation
axios_1.default.interceptors.request.use(async (config) => {
// Apply rate limiting
try {
await this.rateLimiter.acquireToken();
}
catch (error) {
throw new rate_limiter_1.RateLimitError('Rate limit exceeded');
}
// Validate URLs
if (config.url) {
validators_1.validators.url(config.url);
}
return config;
});
// Add response interceptor for error handling
axios_1.default.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()
};
}
}
exports.WPEngineSDK = WPEngineSDK;
// Export types and utilities
__exportStar(require("./generated/api"), exports);
var config_2 = require("./config");
Object.defineProperty(exports, "ConfigurationManager", { enumerable: true, get: function () { return config_2.ConfigurationManager; } });
var validators_2 = require("./validation/validators");
Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return validators_2.ValidationError; } });
var rate_limiter_2 = require("./rate-limiter");
Object.defineProperty(exports, "RateLimitError", { enumerable: true, get: function () { return rate_limiter_2.RateLimitError; } });
//# sourceMappingURL=index.js.map