paperless-ngx
Version:
174 lines (173 loc) • 5.29 kB
JavaScript
// src/index.ts
class PaperlessClient {
baseUrl;
token;
constructor(baseUrl, token) {
this.baseUrl = baseUrl;
this.token = token;
}
async request(method, path, body) {
const response = await fetch(`${this.baseUrl}${path}`, {
method,
headers: {
Authorization: `Token ${this.token}`,
"Content-Type": "application/json"
},
body: body ? JSON.stringify(body) : undefined
});
if (!response.ok) {
throw new Error(`Request failed: ${response.status} ${response.statusText}`);
}
return response.json();
}
async getDocuments() {
return this.request("GET", "/api/documents/");
}
async getDocument(id) {
return this.request("GET", `/api/documents/${id}/`);
}
async createDocument(document) {
return this.request("POST", "/api/documents/", document);
}
async updateDocument(id, document) {
return this.request("PATCH", `/api/documents/${id}/`, document);
}
async deleteDocument(id) {
await this.request("DELETE", `/api/documents/${id}/`);
}
async getTags() {
return this.request("GET", "/api/tags/");
}
async getTag(id) {
return this.request("GET", `/api/tags/${id}/`);
}
async createTag(tag) {
return this.request("POST", "/api/tags/", tag);
}
async updateTag(id, tag) {
return this.request("PATCH", `/api/tags/${id}/`, tag);
}
async deleteTag(id) {
await this.request("DELETE", `/api/tags/${id}/`);
}
async getCorrespondents() {
return this.request("GET", "/api/correspondents/");
}
async getCorrespondent(id) {
return this.request("GET", `/api/correspondents/${id}/`);
}
async createCorrespondent(correspondent) {
return this.request("POST", "/api/correspondents/", correspondent);
}
async updateCorrespondent(id, correspondent) {
return this.request("PATCH", `/api/correspondents/${id}/`, correspondent);
}
async deleteCorrespondent(id) {
await this.request("DELETE", `/api/correspondents/${id}/`);
}
async getDocumentTypes() {
return this.request("GET", "/api/document_types/");
}
async getDocumentType(id) {
return this.request("GET", `/api/document_types/${id}/`);
}
async createDocumentType(documentType) {
return this.request("POST", "/api/document_types/", documentType);
}
async updateDocumentType(id, documentType) {
return this.request("PATCH", `/api/document_types/${id}/`, documentType);
}
async deleteDocumentType(id) {
await this.request("DELETE", `/api/document_types/${id}/`);
}
async getCustomFields() {
return this.request("GET", "/api/custom_fields/");
}
async getCustomField(id) {
return this.request("GET", `/api/custom_fields/${id}/`);
}
async createCustomField(customField) {
return this.request("POST", "/api/custom_fields/", customField);
}
async updateCustomField(id, customField) {
return this.request("PATCH", `/api/custom_fields/${id}/`, customField);
}
async deleteCustomField(id) {
await this.request("DELETE", `/api/custom_fields/${id}/`);
}
async getUsers() {
return this.request("GET", "/api/users/");
}
async getUser(id) {
return this.request("GET", `/api/users/${id}/`);
}
async createUser(user) {
return this.request("POST", "/api/users/", user);
}
async updateUser(id, user) {
return this.request("PATCH", `/api/users/${id}/`, user);
}
async deleteUser(id) {
await this.request("DELETE", `/api/users/${id}/`);
}
async getGroups() {
return this.request("GET", "/api/groups/");
}
async getGroup(id) {
return this.request("GET", `/api/groups/${id}/`);
}
async createGroup(group) {
return this.request("POST", "/api/groups/", group);
}
async updateGroup(id, group) {
return this.request("PATCH", `/api/groups/${id}/`, group);
}
async deleteGroup(id) {
await this.request("DELETE", `/api/groups/${id}/`);
}
async getShareLinks() {
return this.request("GET", "/api/share_links/");
}
async getShareLink(id) {
return this.request("GET", `/api/share_links/${id}/`);
}
async createShareLink(shareLink) {
return this.request("POST", "/api/share_links/", shareLink);
}
async deleteShareLink(id) {
await this.request("DELETE", `/api/share_links/${id}/`);
}
async getStoragePaths() {
return this.request("GET", "/api/storage_paths/");
}
async getStoragePath(id) {
return this.request("GET", `/api/storage_paths/${id}/`);
}
async createStoragePath(storagePath) {
return this.request("POST", "/api/storage_paths/", storagePath);
}
async updateStoragePath(id, storagePath) {
return this.request("PATCH", `/api/storage_paths/${id}/`, storagePath);
}
async deleteStoragePath(id) {
await this.request("DELETE", `/api/storage_paths/${id}/`);
}
async getWorkflows() {
return this.request("GET", "/api/workflows/");
}
async getWorkflow(id) {
return this.request("GET", `/api/workflows/${id}/`);
}
async createWorkflow(workflow) {
return this.request("POST", "/api/workflows/", workflow);
}
async updateWorkflow(id, workflow) {
return this.request("PATCH", `/api/workflows/${id}/`, workflow);
}
async deleteWorkflow(id) {
await this.request("DELETE", `/api/workflows/${id}/`);
}
}
export {
PaperlessClient
};