All files / src/lib/api/admin social-studio-templates.ts

100% Statements 12/12
100% Branches 6/6
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                95x         4x 4x 4x 1x 4x 4x 4x             3x                       3x                 1x                
import { request, uploadFile } from "../base";
import type { SocialStudioTemplate } from "../social-drafts";
 
export type AdminSocialStudioTemplate = SocialStudioTemplate & {
	isActive: boolean;
	dateUpdated: string;
};
 
export const adminSocialStudioTemplatesApi = {
	async listTemplates(params?: {
		category?: string;
		includeInactive?: boolean;
	}) {
		const searchParams = new URLSearchParams();
		if (params?.category) searchParams.set("category", params.category);
		if (params?.includeInactive)
			searchParams.set("includeInactive", "true");
		const query = searchParams.toString();
		const url = `/api/admin/social-studio/templates${query ? `?${query}` : ""}`;
		return request<{ templates: AdminSocialStudioTemplate[] }>(url);
	},
 
	async createTemplate(
		file: File | null,
		fields: Record<string, string>,
	) {
		return uploadFile<{ template: AdminSocialStudioTemplate }>(
			"/api/admin/social-studio/templates",
			file,
			fields,
		);
	},
 
	async updateTemplate(
		id: string,
		file: File | null,
		fields: Record<string, string>,
	) {
		return uploadFile<{ template: AdminSocialStudioTemplate }>(
			`/api/admin/social-studio/templates/${id}`,
			file,
			fields,
			{ method: "PUT" },
		);
	},
 
	async deleteTemplate(id: string) {
		return request<{ ok: boolean }>(
			`/api/admin/social-studio/templates/${id}`,
			{
				method: "DELETE",
			},
		);
	},
};