UNPKG

nurekit

Version:

Node.js client for Mindenit Schedule API

223 lines (214 loc) 6.79 kB
//#region src/errors.ts var NurekitError = class extends Error { status; constructor(message = "Something went wrong", status = 500) { super(`NurekitError: ${status} ${message}`); this.name = "NurekitError"; this.message = message; this.status = status; } }; //#endregion //#region src/helpers/searchParams.ts const getScheduleParams = ({ start, end, filters }) => { const params = new URLSearchParams({ startedAt: start.toString(), endedAt: end.toString() }); if (filters) Object.entries(filters).forEach(([key, value]) => { if (value !== void 0 && Array.isArray(value)) params.append(`filters[${key}]`, value.join(",")); }); return params; }; //#endregion //#region src/modules/BaseModule.ts var BaseModuleImpl = class { baseUrl; constructor(baseUrl) { this.baseUrl = baseUrl; } async findMany() { const response = await fetch(this.baseUrl); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } async getSchedule({ id, startedAt, endedAt, filters }) { const params = getScheduleParams({ start: startedAt, end: endedAt, filters }); const url = new URL(`${this.baseUrl}/${id}/schedule`); url.search = params.toString(); const response = await fetch(url.toString()); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } }; //#endregion //#region src/modules/AuditoriumsModule.ts var AuditoriumsModuleImpl = class extends BaseModuleImpl { constructor(baseUrl) { super(`${baseUrl}/auditoriums`); } async getGroups(auditoriumId) { const url = `${this.baseUrl}/${auditoriumId}/groups`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } async getTeachers(auditoriumId) { const url = `${this.baseUrl}/${auditoriumId}/teachers`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } async getSubjects(auditoriumId) { const url = `${this.baseUrl}/${auditoriumId}/subjects`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } }; //#endregion //#region src/modules/GroupsModule.ts var GroupsModuleImpl = class extends BaseModuleImpl { constructor(baseUrl) { super(`${baseUrl}/groups`); } async getAuditoriums(groupId) { const url = `${this.baseUrl}/${groupId}/auditoriums`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } async getTeachers(groupId) { const url = `${this.baseUrl}/${groupId}/teachers`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } async getSubjects(groupId) { const url = `${this.baseUrl}/${groupId}/subjects`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } }; //#endregion //#region src/modules/LinksModule.ts var LinksModuleImpl = class { baseUrl; constructor(baseUrl) { this.baseUrl = baseUrl; } async getUserLinks() { const response = await fetch(`${this.baseUrl}/links`, { credentials: "include" }); if (!response.ok) throw new NurekitError("Failed to fetch user links", response.status); return (await response.json()).data; } async createLink(data) { const response = await fetch(`${this.baseUrl}/links`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, credentials: "include" }); if (!response.ok) throw new NurekitError("Failed to create link", response.status); return (await response.json()).data; } async updateLink(linkId, data) { const response = await fetch(`${this.baseUrl}/links/${linkId}`, { method: "PUT", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, credentials: "include" }); if (!response.ok) throw new NurekitError("Failed to update link", response.status); return (await response.json()).data; } async deleteLink(linkId) { const response = await fetch(`${this.baseUrl}/links/${linkId}`, { method: "DELETE", body: JSON.stringify({}), headers: { "Content-Type": "application/json" }, credentials: "include" }); if (!response.ok) throw new NurekitError("Failed to delete link", response.status); return (await response.json()).data; } }; //#endregion //#region src/modules/SharableLinksModule.ts var SharableLinksModuleImpl = class { baseUrl; constructor(baseUrl) { this.baseUrl = baseUrl; } async getLink(linkId) { const response = await fetch(`${this.baseUrl}/sharable-links/${linkId}`); if (!response.ok) throw new NurekitError("Failed to fetch sharable link", response.status); return (await response.json()).data; } async createLink(data) { const response = await fetch(`${this.baseUrl}/sharable-links`, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, credentials: "include" }); if (!response.ok) throw new NurekitError("Failed to create sharable link", response.status); return (await response.json()).data; } async acceptLink(linkId) { const response = await fetch(`${this.baseUrl}/sharable-links/${linkId}/accept`, { method: "PUT", headers: { "Content-Type": "application/json" }, credentials: "include" }); if (!response.ok) throw new NurekitError("Failed to delete link", response.status); } }; //#endregion //#region src/modules/TeachersModule.ts var TeachersModuleImpl = class extends BaseModuleImpl { constructor(baseUrl) { super(`${baseUrl}/teachers`); } async getAuditoriums(teacherId) { const url = `${this.baseUrl}/${teacherId}/auditoriums`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } async getGroups(teacherId) { const url = `${this.baseUrl}/${teacherId}/groups`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } async getSubjects(teacherId) { const url = `${this.baseUrl}/${teacherId}/subjects`; const response = await fetch(url); if (!response.ok) throw new NurekitError(); return (await response.json()).data; } }; //#endregion //#region src/client.ts var Nurekit = class { auditoriums; groups; teachers; links; sharableLinks; constructor(baseUrl = "https://sh.mindenit.org/api") { this.auditoriums = new AuditoriumsModuleImpl(baseUrl); this.groups = new GroupsModuleImpl(baseUrl); this.teachers = new TeachersModuleImpl(baseUrl); this.links = new LinksModuleImpl(baseUrl); this.sharableLinks = new SharableLinksModuleImpl(baseUrl); } }; //#endregion export { Nurekit, NurekitError };