gatsby-source-tilda
Version:
Gatsby source plugin for building websites using the tilda.cc CMS as a data source
50 lines (49 loc) • 1.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const API = 'http://api.tildacdn.info/v1/';
class TildaApi {
publicKey;
secret;
instance;
constructor(publicKey, secret) {
this.publicKey = publicKey;
this.secret = secret;
this.instance = axios_1.default.create({
baseURL: API,
params: {
publickey: this.publicKey,
secretkey: this.secret,
},
});
}
async fetchProjects() {
const response = await this.instance.get('/getprojectslist');
if (response.data.status !== 'FOUND') {
throw new Error(`Tilda Projects not found. ${JSON.stringify(response.data)}`);
}
return response.data.result || [];
}
async fetchProjectPages(projectId) {
const response = await this.instance.get('/getpageslist', {
params: { projectid: projectId },
});
if (response.data.status !== 'FOUND') {
throw new Error(`Tilda Project #${projectId} pages not found. ${JSON.stringify(response.data)}`);
}
return response.data.result.filter((page) => page.published !== '') || [];
}
async fetchPage(pageId) {
const response = await this.instance.get('/getpageexport', {
params: { pageid: pageId },
});
if (response.data.status !== 'FOUND') {
throw new Error(`Tilda Page not found #${pageId} ${JSON.stringify(response.data)}`);
}
return response.data.result || [];
}
}
exports.default = TildaApi;