docpouch-client 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +23 -0
- package/dist/index.js +87 -0
- package/dist/types.d.ts +61 -0
- package/dist/types.js +3 -0
- package/package.json +19 -0
- package/src/index.ts +120 -0
- package/src/types.ts +77 -0
- package/tsconfig.json +18 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { I_UserEntry, I_UserLogin, I_UserCreation, I_UserUpdate, I_UserDisplay, I_DocumentEntry, I_DataStructure, I_LoginResponse } from "./types.js";
|
|
2
|
+
export default class Index {
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
private token;
|
|
5
|
+
constructor(baseUrl: string);
|
|
6
|
+
private request;
|
|
7
|
+
login(credentials: I_UserLogin): Promise<I_LoginResponse | null>;
|
|
8
|
+
listUsers(): Promise<I_UserEntry[]>;
|
|
9
|
+
updateUser(userID: string, userData: I_UserUpdate): Promise<void>;
|
|
10
|
+
createUser(userData: I_UserCreation): Promise<I_UserDisplay>;
|
|
11
|
+
removeUser(userID: string): Promise<void>;
|
|
12
|
+
createDocument(document: I_DocumentEntry): Promise<I_DocumentEntry>;
|
|
13
|
+
listDocuments(): Promise<I_DocumentEntry[]>;
|
|
14
|
+
fetchDocument(documentID: string): Promise<I_DocumentEntry>;
|
|
15
|
+
updateDocument(documentID: string, documentData: I_DocumentEntry): Promise<void>;
|
|
16
|
+
removeDocument(documentID: string): Promise<void>;
|
|
17
|
+
createStructure(structure: I_DataStructure): Promise<I_DataStructure>;
|
|
18
|
+
getStructures(): Promise<I_DataStructure[]>;
|
|
19
|
+
updateStructure(structureID: string, structureData: I_DataStructure): Promise<void>;
|
|
20
|
+
removeStructure(structureID: string): Promise<void>;
|
|
21
|
+
setToken(token: string | null): void;
|
|
22
|
+
getToken(): string | null;
|
|
23
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class Index {
|
|
4
|
+
constructor(baseUrl) {
|
|
5
|
+
this.token = null;
|
|
6
|
+
this.baseUrl = baseUrl;
|
|
7
|
+
}
|
|
8
|
+
async request(endpoint, method, body, requiresAuth = true) {
|
|
9
|
+
const headers = {
|
|
10
|
+
'Content-Type': 'application/json'
|
|
11
|
+
};
|
|
12
|
+
if (requiresAuth && this.token) {
|
|
13
|
+
headers['Authorization'] = `Bearer ${this.token}`;
|
|
14
|
+
}
|
|
15
|
+
const options = {
|
|
16
|
+
method,
|
|
17
|
+
headers,
|
|
18
|
+
body: body ? JSON.stringify(body) : undefined
|
|
19
|
+
};
|
|
20
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, options);
|
|
21
|
+
if (!response.ok) {
|
|
22
|
+
if (response.status === 401 || response.status === 403) {
|
|
23
|
+
this.token = null;
|
|
24
|
+
}
|
|
25
|
+
throw new Error(`API error: ${response.status} ${response.statusText}`);
|
|
26
|
+
}
|
|
27
|
+
return await response.json();
|
|
28
|
+
}
|
|
29
|
+
// User Administration Endpoints
|
|
30
|
+
async login(credentials) {
|
|
31
|
+
const response = await this.request('/users/login', 'POST', credentials, false);
|
|
32
|
+
if (response.token) {
|
|
33
|
+
this.token = response.token;
|
|
34
|
+
return { token: response.token, isAdmin: response.isAdmin };
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
async listUsers() {
|
|
39
|
+
return await this.request('/users/list', 'GET');
|
|
40
|
+
}
|
|
41
|
+
async updateUser(userID, userData) {
|
|
42
|
+
await this.request(`/users/update/${userID}`, 'PATCH', userData);
|
|
43
|
+
}
|
|
44
|
+
async createUser(userData) {
|
|
45
|
+
return await this.request('/users/create', 'POST', userData);
|
|
46
|
+
}
|
|
47
|
+
async removeUser(userID) {
|
|
48
|
+
await this.request(`/users/remove/${userID}`, 'DELETE');
|
|
49
|
+
}
|
|
50
|
+
// Document Management Endpoints
|
|
51
|
+
async createDocument(document) {
|
|
52
|
+
return await this.request('/docs/create', 'POST', document);
|
|
53
|
+
}
|
|
54
|
+
async listDocuments() {
|
|
55
|
+
return await this.request('/docs/list', 'GET');
|
|
56
|
+
}
|
|
57
|
+
async fetchDocument(documentID) {
|
|
58
|
+
return await this.request(`/docs/fetch/${documentID}`, 'GET');
|
|
59
|
+
}
|
|
60
|
+
async updateDocument(documentID, documentData) {
|
|
61
|
+
await this.request(`/docs/update/${documentID}`, 'PATCH', documentData);
|
|
62
|
+
}
|
|
63
|
+
async removeDocument(documentID) {
|
|
64
|
+
await this.request(`/docs/remove/${documentID}`, 'DELETE');
|
|
65
|
+
}
|
|
66
|
+
// Data Structure Endpoints
|
|
67
|
+
async createStructure(structure) {
|
|
68
|
+
return await this.request('/structures/create', 'POST', structure);
|
|
69
|
+
}
|
|
70
|
+
async getStructures() {
|
|
71
|
+
return await this.request('/structures/list', 'GET');
|
|
72
|
+
}
|
|
73
|
+
async updateStructure(structureID, structureData) {
|
|
74
|
+
await this.request(`/structures/update/${structureID}`, 'PATCH', structureData);
|
|
75
|
+
}
|
|
76
|
+
async removeStructure(structureID) {
|
|
77
|
+
await this.request(`/structures/remove/${structureID}`, 'DELETE');
|
|
78
|
+
}
|
|
79
|
+
setToken(token) {
|
|
80
|
+
this.token = token;
|
|
81
|
+
}
|
|
82
|
+
getToken() {
|
|
83
|
+
return this.token;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.default = Index;
|
|
87
|
+
module.exports = Index;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export interface I_UserEntry extends I_UserCreation {
|
|
2
|
+
_id: string;
|
|
3
|
+
}
|
|
4
|
+
export interface I_UserLogin {
|
|
5
|
+
name: string;
|
|
6
|
+
password: string;
|
|
7
|
+
}
|
|
8
|
+
export interface I_UserCreation {
|
|
9
|
+
name: string;
|
|
10
|
+
password: string;
|
|
11
|
+
email?: string;
|
|
12
|
+
isAdmin: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface I_UserUpdate {
|
|
15
|
+
name?: string;
|
|
16
|
+
password?: string;
|
|
17
|
+
email?: string;
|
|
18
|
+
isAdmin?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface I_UserDisplay {
|
|
21
|
+
_id: string;
|
|
22
|
+
username: string;
|
|
23
|
+
email?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface I_LoginResponse {
|
|
26
|
+
token: string;
|
|
27
|
+
isAdmin: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface I_DocumentEntry extends I_DocumentCreationOwned {
|
|
30
|
+
_id: string;
|
|
31
|
+
}
|
|
32
|
+
export interface I_DocumentCreation {
|
|
33
|
+
title: string;
|
|
34
|
+
description?: string;
|
|
35
|
+
type: number;
|
|
36
|
+
subType: number;
|
|
37
|
+
content: any;
|
|
38
|
+
}
|
|
39
|
+
export interface I_DocumentCreationOwned extends I_DocumentCreation {
|
|
40
|
+
owner: string;
|
|
41
|
+
}
|
|
42
|
+
export interface I_DataStructure {
|
|
43
|
+
_id?: string | undefined;
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
reference?: any;
|
|
47
|
+
fields: any[];
|
|
48
|
+
}
|
|
49
|
+
export interface I_StructureEntry {
|
|
50
|
+
_id?: string;
|
|
51
|
+
name: string;
|
|
52
|
+
description: string;
|
|
53
|
+
reference?: any;
|
|
54
|
+
fields: any[];
|
|
55
|
+
}
|
|
56
|
+
export interface I_StructureCreation {
|
|
57
|
+
name: string;
|
|
58
|
+
description?: string;
|
|
59
|
+
reference?: any;
|
|
60
|
+
fields: any[];
|
|
61
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "docpouch-client",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["docPouch", "API"],
|
|
10
|
+
"author": "Jan Frecè",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"description": "A small class to more easily access the docPouch API",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/node": "^22.15.17"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"typescript": "^5.8.3"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
I_UserEntry,
|
|
3
|
+
I_UserLogin,
|
|
4
|
+
I_UserCreation,
|
|
5
|
+
I_UserUpdate,
|
|
6
|
+
I_UserDisplay,
|
|
7
|
+
I_DocumentEntry,
|
|
8
|
+
I_DataStructure,
|
|
9
|
+
I_LoginResponse
|
|
10
|
+
} from "./types.js";
|
|
11
|
+
|
|
12
|
+
export default class Index {
|
|
13
|
+
baseUrl: string;
|
|
14
|
+
private token: string | null = null;
|
|
15
|
+
|
|
16
|
+
constructor(baseUrl: string) {
|
|
17
|
+
this.baseUrl = baseUrl;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private async request<T>(endpoint: string, method: string, body?: any, requiresAuth: boolean = true): Promise<T> {
|
|
21
|
+
const headers: HeadersInit = {
|
|
22
|
+
'Content-Type': 'application/json'
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (requiresAuth && this.token) {
|
|
26
|
+
headers['Authorization'] = `Bearer ${this.token}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const options: RequestInit = {
|
|
30
|
+
method,
|
|
31
|
+
headers,
|
|
32
|
+
body: body ? JSON.stringify(body) : undefined
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, options);
|
|
36
|
+
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
if (response.status === 401 || response.status === 403) {
|
|
39
|
+
this.token = null;
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`API error: ${response.status} ${response.statusText}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return await response.json() as T;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// User Administration Endpoints
|
|
48
|
+
async login(credentials: I_UserLogin): Promise<I_LoginResponse | null> {
|
|
49
|
+
const response = await this.request<I_LoginResponse>('/users/login', 'POST', credentials, false);
|
|
50
|
+
if (response.token) {
|
|
51
|
+
this.token = response.token;
|
|
52
|
+
return {token: response.token, isAdmin: response.isAdmin};
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async listUsers(): Promise<I_UserEntry[]> {
|
|
58
|
+
return await this.request<I_UserEntry[]>('/users/list', 'GET');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async updateUser(userID: string, userData: I_UserUpdate): Promise<void> {
|
|
62
|
+
await this.request<void>(`/users/update/${userID}`, 'PATCH', userData);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async createUser(userData: I_UserCreation): Promise<I_UserDisplay> {
|
|
66
|
+
return await this.request<I_UserDisplay>('/users/create', 'POST', userData);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async removeUser(userID: string): Promise<void> {
|
|
70
|
+
await this.request<void>(`/users/remove/${userID}`, 'DELETE');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Document Management Endpoints
|
|
74
|
+
async createDocument(document: I_DocumentEntry): Promise<I_DocumentEntry> {
|
|
75
|
+
return await this.request<I_DocumentEntry>('/docs/create', 'POST', document);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async listDocuments(): Promise<I_DocumentEntry[]> {
|
|
79
|
+
return await this.request<I_DocumentEntry[]>('/docs/list', 'GET');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async fetchDocument(documentID: string): Promise<I_DocumentEntry> {
|
|
83
|
+
return await this.request<I_DocumentEntry>(`/docs/fetch/${documentID}`, 'GET');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async updateDocument(documentID: string, documentData: I_DocumentEntry): Promise<void> {
|
|
87
|
+
await this.request<void>(`/docs/update/${documentID}`, 'PATCH', documentData);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async removeDocument(documentID: string): Promise<void> {
|
|
91
|
+
await this.request<void>(`/docs/remove/${documentID}`, 'DELETE');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Data Structure Endpoints
|
|
95
|
+
async createStructure(structure: I_DataStructure): Promise<I_DataStructure> {
|
|
96
|
+
return await this.request<I_DataStructure>('/structures/create', 'POST', structure);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async getStructures(): Promise<I_DataStructure[]> {
|
|
100
|
+
return await this.request<I_DataStructure[]>('/structures/list', 'GET');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async updateStructure(structureID: string, structureData: I_DataStructure): Promise<void> {
|
|
104
|
+
await this.request<void>(`/structures/update/${structureID}`, 'PATCH', structureData);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async removeStructure(structureID: string): Promise<void> {
|
|
108
|
+
await this.request<void>(`/structures/remove/${structureID}`, 'DELETE');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
setToken(token: string | null): void {
|
|
112
|
+
this.token = token;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
getToken() {
|
|
116
|
+
return this.token;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = Index;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Common type definitions for both frontend and backend
|
|
2
|
+
|
|
3
|
+
// User related types
|
|
4
|
+
export interface I_UserEntry extends I_UserCreation{
|
|
5
|
+
_id: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface I_UserLogin {
|
|
9
|
+
name: string;
|
|
10
|
+
password: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface I_UserCreation {
|
|
14
|
+
name: string;
|
|
15
|
+
password: string;
|
|
16
|
+
email?: string;
|
|
17
|
+
isAdmin: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface I_UserUpdate {
|
|
21
|
+
name?: string;
|
|
22
|
+
password?: string;
|
|
23
|
+
email?: string;
|
|
24
|
+
isAdmin?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface I_UserDisplay {
|
|
28
|
+
_id: string;
|
|
29
|
+
username: string;
|
|
30
|
+
email?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface I_LoginResponse {
|
|
34
|
+
token: string;
|
|
35
|
+
isAdmin: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Document related types
|
|
39
|
+
export interface I_DocumentEntry extends I_DocumentCreationOwned{
|
|
40
|
+
_id: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface I_DocumentCreation {
|
|
44
|
+
title: string;
|
|
45
|
+
description?: string;
|
|
46
|
+
type: number;
|
|
47
|
+
subType: number;
|
|
48
|
+
content: any;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface I_DocumentCreationOwned extends I_DocumentCreation {
|
|
52
|
+
owner: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Structure related types
|
|
56
|
+
export interface I_DataStructure {
|
|
57
|
+
_id?: string | undefined;
|
|
58
|
+
name: string;
|
|
59
|
+
description: string;
|
|
60
|
+
reference?: any;
|
|
61
|
+
fields: any[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface I_StructureEntry {
|
|
65
|
+
_id?: string;
|
|
66
|
+
name: string;
|
|
67
|
+
description: string;
|
|
68
|
+
reference?: any;
|
|
69
|
+
fields: any[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface I_StructureCreation {
|
|
73
|
+
name: string;
|
|
74
|
+
description?: string;
|
|
75
|
+
reference?: any;
|
|
76
|
+
fields: any[];
|
|
77
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "Node16",
|
|
5
|
+
"lib": ["ES6", "DOM"],
|
|
6
|
+
"moduleResolution": "Node16",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"outDir": "./dist",
|
|
12
|
+
"rootDir": "./src",
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"emitDeclarationOnly": false,
|
|
15
|
+
},
|
|
16
|
+
"include": ["**/*.ts", "**/*.tsx"],
|
|
17
|
+
"exclude": ["node_modules", "**/*.test.ts", "**/*.types.ts"]
|
|
18
|
+
}
|