umbraco-management-api-client
Version:
TypeScript client for the Umbraco Management API (auto-generated by OpenAPI Generator)
52 lines (51 loc) • 1.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfig = getConfig;
const runtime_1 = require("../runtime");
const node_fetch_1 = __importDefault(require("node-fetch"));
// Disable SSL verification for development
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const host = 'https://localhost:44372'; // Change to your host
class ApiClient {
constructor() {
this.token = null;
this.tokenExpiryTime = null;
}
async getToken() {
if (this.token && this.tokenExpiryTime && new Date() < this.tokenExpiryTime) {
return this.token;
}
const response = await (0, node_fetch_1.default)(`${host}/umbraco/management/api/v1/security/back-office/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'client_id': 'umbraco-back-office-testkey',
'client_secret': 'wKtd*gyIa!oHOw#ZqPwzwMCIqe2*1NG$L',
'grant_type': 'client_credentials'
}).toString()
});
if (!response.ok) {
throw new Error('Failed to obtain token');
}
const data = await response.json();
if (!data.access_token) {
throw new Error('No access token in response');
}
this.token = data.access_token;
this.tokenExpiryTime = new Date(new Date().getTime() + data.expires_in * 1000);
return this.token;
}
}
async function getConfig() {
const apiClient = new ApiClient();
return new runtime_1.Configuration({
basePath: host,
accessToken: async () => `Bearer ${await apiClient.getToken()}`,
fetchApi: node_fetch_1.default
});
}