@mulutime/plugin-sdk
Version:
SDK for developing MuluTime booking platform plugins
111 lines • 3.92 kB
JavaScript
;
// =============================================================================
// PLUGIN IMPLEMENTATION CLIENTS
// =============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginLoggerClient = exports.PluginStorageClient = exports.PluginAPIClient = void 0;
class PluginAPIClient {
constructor(baseURL, pluginSecret) {
this.baseURL = baseURL;
this.pluginSecret = pluginSecret;
}
getHeaders() {
return {
'Content-Type': 'application/json',
'X-Plugin-Secret': this.pluginSecret,
};
}
async get(url, options = {}) {
return fetch(`${this.baseURL}${url}`, {
method: 'GET',
headers: { ...this.getHeaders(), ...options.headers },
...options,
});
}
async post(url, data, options = {}) {
return fetch(`${this.baseURL}${url}`, {
method: 'POST',
headers: { ...this.getHeaders(), ...options.headers },
body: data ? JSON.stringify(data) : undefined,
...options,
});
}
async put(url, data, options = {}) {
return fetch(`${this.baseURL}${url}`, {
method: 'PUT',
headers: { ...this.getHeaders(), ...options.headers },
body: data ? JSON.stringify(data) : undefined,
...options,
});
}
async delete(url, options = {}) {
return fetch(`${this.baseURL}${url}`, {
method: 'DELETE',
headers: { ...this.getHeaders(), ...options.headers },
...options,
});
}
}
exports.PluginAPIClient = PluginAPIClient;
class PluginStorageClient {
constructor(api) {
this.api = api;
}
async get(key) {
const response = await this.api.get(`/api/plugin/v1/storage/${key}`);
if (response.status === 404)
return null;
if (!response.ok)
throw new Error(`Storage get failed: ${response.statusText}`);
const data = await response.json();
return data.value;
}
async set(key, value) {
const response = await this.api.put(`/api/plugin/v1/storage/${key}`, { value });
if (!response.ok)
throw new Error(`Storage set failed: ${response.statusText}`);
}
async delete(key) {
const response = await this.api.delete(`/api/plugin/v1/storage/${key}`);
if (!response.ok)
throw new Error(`Storage delete failed: ${response.statusText}`);
}
async list(prefix) {
const params = prefix ? `?prefix=${encodeURIComponent(prefix)}` : '';
const response = await this.api.get(`/api/plugin/v1/storage${params}`);
if (!response.ok)
throw new Error(`Storage list failed: ${response.statusText}`);
const data = await response.json();
return data.keys || [];
}
}
exports.PluginStorageClient = PluginStorageClient;
class PluginLoggerClient {
constructor(api) {
this.api = api;
}
async debug(message, meta) {
await this.log('debug', message, meta);
}
async info(message, meta) {
await this.log('info', message, meta);
}
async warn(message, meta) {
await this.log('warn', message, meta);
}
async error(message, error, meta) {
const errorMeta = error ? { ...meta, error: error.message, stack: error.stack } : meta;
await this.log('error', message, errorMeta);
}
async log(level, message, meta) {
try {
await this.api.post('/api/plugin/v1/sdk/log', { level, message, meta });
}
catch (err) {
// Fallback to console if API fails
console.log(`[${level.toUpperCase()}]`, message, meta);
}
}
}
exports.PluginLoggerClient = PluginLoggerClient;
//# sourceMappingURL=pluginClients.js.map