UNPKG

@druid-sh/sdk

Version:

Druid.sh SDK for rendering blog content with SSR support

137 lines (136 loc) 4.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DruidClient = void 0; const client_1 = require("hono/client"); // END OF MAGIC const API_URL = "https://api.druid.sh"; class DruidClient { constructor(config) { this.config = config; this.config = config; this.siteName = config.siteName; this.client = (0, client_1.hc)(API_URL, { headers: { "x-api-key": this.config.apiKey, }, }); } async fetchTag(tag) { const res = await this.client.api.blog[":projectId"].tags[":tagId"].$get({ param: { projectId: this.config.projectId, tagId: tag }, }); const tagData = await res.json(); if ("error" in tagData) { throw new Error(`Error fetching tag: ${tagData.error}`); } return tagData; } async fetchPosts(projectId, page, limit) { const res = await this.client.api.blog[":projectId"].posts.$get({ query: { page: page.toString(), limit: limit.toString(), }, param: { projectId: projectId }, }); const data = await res.json(); if ("error" in data) { throw new Error(data.error); } return data; } async fetchPostsByTag(projectId, tag, page, limit) { const res = await this.client.api.blog[":projectId"].tags[":tagId"].posts.$get({ query: { page: page.toString(), limit: limit.toString(), }, param: { projectId: projectId, tagId: tag }, }); const data = await res.json(); if ("error" in data) { throw new Error(data.error); } return data; } async fetchTags(projectId) { const res = await this.client.api.blog[":projectId"].tags.$get({ param: { projectId: projectId }, }); const data = await res.json(); if ("error" in data) { throw new Error(data.error); } return data; } async fetchSlugs(projectId) { const res = await this.client.api.blog[":projectId"].slugs.$get({ param: { projectId: projectId }, }); const data = await res.json(); if ("error" in data) { throw new Error(data.error); } return data.slugs; } async fetchPost(projectId, slug) { const res = await this.client.api.blog[":projectId"].posts[":postId"].$get({ param: { projectId: projectId, postId: slug }, }); const data = await res.json(); if ("error" in data) { throw new Error(data.error); } return data; } async getPosts(page = 1, limit = 10) { const { posts, pagination } = await this.fetchPosts(this.config.projectId, page, limit); const allTags = await this.getTags(); return { posts, pagination: { page: pagination.page, limit: pagination.limit, total: pagination.total, }, allTags, basePath: this.config.basePath, currentTag: undefined, }; } async getPost(slug) { const post = await this.fetchPost(this.config.projectId, slug); return { post, basePath: this.config.basePath, }; } async getPostsByTag(tag, page = 1, limit = 10) { const { posts, pagination } = await this.fetchPostsByTag(this.config.projectId, tag, page, limit); const allTags = await this.getTags(); return { posts, pagination: { page: pagination.page, limit: pagination.limit, total: pagination.total, }, allTags, basePath: this.config.basePath, currentTag: tag, }; } async getTags() { const tags = await this.fetchTags(this.config.projectId); return tags; } async getSlugs() { const slugs = await this.fetchSlugs(this.config.projectId); return slugs; } async getTag(tag) { const tagData = await this.fetchTag(tag); return tagData; } } exports.DruidClient = DruidClient;