docpouch-client 0.8.4 → 0.8.6

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/README.md ADDED
@@ -0,0 +1,305 @@
1
+ # docpouch-client
2
+
3
+ A TypeScript client library for interacting with the [docPouch](https://github.com/BFH-JTF/doc-pouch) database API.
4
+
5
+ ## Description
6
+
7
+ docpouch-client provides a simple and intuitive interface to access the docPouch API, allowing you to manage users,
8
+ documents, data structures, and document types. It also supports real-time synchronization via WebSockets.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install docpouch-client
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### Initializing the Client
19
+
20
+ ```typescript
21
+ import docPouchClient from 'docpouch-client';
22
+
23
+ // Initialize the client with the server URL
24
+ const client = new docPouchClient('https://your-docpouch-server.com', 80);
25
+
26
+ // Initialize with a callback for real-time updates
27
+ const client = new docPouchClient('https://your-docpouch-server.com', 80,
28
+ (event, data) => {
29
+ console.log(`Received event: ${event}`, data);
30
+ }
31
+ );
32
+ ```
33
+
34
+ ### Authentication
35
+
36
+ ```typescript
37
+ // Login to get an authentication token
38
+ const loginResponse = await client.login({
39
+ name: 'username',
40
+ password: 'password'
41
+ });
42
+
43
+ if (loginResponse) {
44
+ console.log('Login successful!');
45
+ console.log(`Token: ${loginResponse.token}`);
46
+ } else {
47
+ console.log('Login failed');
48
+ }
49
+
50
+ // Set an existing token
51
+ client.setToken('your-auth-token');
52
+ ```
53
+
54
+ ### User Management
55
+
56
+ ```typescript
57
+ // List all users
58
+ const users = await client.listUsers();
59
+
60
+ // Create a new user
61
+ const newUser = await client.createUser({
62
+ name: 'newuser',
63
+ password: 'password',
64
+ department: 'IT',
65
+ group: 'Developers',
66
+ isAdmin: false
67
+ });
68
+
69
+ // Update a user
70
+ await client.updateUser('user-id', {
71
+ department: 'Engineering',
72
+ group: 'Frontend'
73
+ });
74
+
75
+ // Remove a user
76
+ await client.removeUser('user-id');
77
+ ```
78
+
79
+ ### Document Management
80
+
81
+ ```typescript
82
+ // Create a new document
83
+ const newDocument = await client.createDocument({
84
+ type: 17,
85
+ subType: 11,
86
+ title: "Mission Statement",
87
+ content: "{\"msg\": \"We strive for a world that...\"}",
88
+ shareWithGroup: false,
89
+ shareWithDepartment: false
90
+ });
91
+
92
+ // Update a document
93
+ await client.updateDocument('document-id', {
94
+ title: 'Updated Mission Statement',
95
+ content: '{"msg": "Our updated mission..."}'
96
+ });
97
+
98
+ // Delete a document
99
+ await client.deleteDocument('document-id');
100
+
101
+ // Fetch documents by query
102
+ const documents = await client.fetchDocuments([
103
+ {type: 17, subType: 11}
104
+ ]);
105
+ ```
106
+
107
+ ### Data Structure Management
108
+
109
+ ```typescript
110
+ // Create a new data structure
111
+ const newStructure = await client.createDataStructure({
112
+ title: "Data resulting from Vision-Mission-Value-Canvas",
113
+ fields: [
114
+ {
115
+ name: "Mission value statement",
116
+ type: "string"
117
+ },
118
+ // Add more fields as needed
119
+ ]
120
+ });
121
+
122
+ // Update a data structure
123
+ await client.updateDataStructure('structure-id', {
124
+ title: 'Updated Structure Title',
125
+ fields: [
126
+ {name: 'New Field Name', type: 'number'}
127
+ ]
128
+ });
129
+
130
+ // Delete a data structure
131
+ await client.deleteDataStructure('structure-id');
132
+
133
+ // List all data structures
134
+ const structures = await client.listDataStructures();
135
+ ```
136
+
137
+ ### Document Type Management
138
+
139
+ ```typescript
140
+ // List all document types
141
+ const docTypes = await client.listDocumentTypes();
142
+
143
+ // Create or update a document type
144
+ const newDocType = await client.createOrUpdateDocumentType({
145
+ name: "HR Wages",
146
+ description: "HR Wage information Document listing wages per personnel ID",
147
+ type: 14,
148
+ subType: 2,
149
+ defaultStructureID: 'structure-id'
150
+ });
151
+
152
+ // Delete a document type
153
+ await client.deleteDocumentType('doc-type-id');
154
+ ```
155
+
156
+ ## API Reference
157
+
158
+ ### Client Class
159
+
160
+ #### Constructor
161
+
162
+ - `new docPouchClient(baseUrl: string, port: number, callback?: (event: string, data: any) => void)`
163
+
164
+ #### Methods
165
+
166
+ - `login(credentials: { name: string, password: string }): Promise<{ token: string } | null>`
167
+ - `setToken(token: string): void`
168
+ - `listUsers(): Promise<UserDisplay[]>`
169
+ - `createUser(userData: UserCreation): Promise<UserDisplay>`
170
+ - `updateUser(id: string, userData: UserUpdate): Promise<void>`
171
+ - `removeUser(id: string): Promise<void>`
172
+ - `createDocument(docData: NewDocument): Promise<Document>`
173
+ - `updateDocument(id: string, docData: Partial<Document>): Promise<void>`
174
+ - `deleteDocument(id: string): Promise<void>`
175
+ - `fetchDocuments(query: DocumentQuery[]): Promise<Document[]>`
176
+ - `createDataStructure(structureData: DataStructureCreation): Promise<DataStructure>`
177
+ - `updateDataStructure(id: string, structureData: Partial<DataStructure>): Promise<void>`
178
+ - `deleteDataStructure(id: string): Promise<void>`
179
+ - `listDataStructures(): Promise<DataStructure[]>`
180
+ - `listDocumentTypes(): Promise<DocumentTypeEdit[]>`
181
+ - `createOrUpdateDocumentType(typeData: DocumentTypeEdit): Promise<void>`
182
+ - `deleteDocumentType(id: string): Promise<void>`
183
+
184
+ ## Types
185
+
186
+ ### UserDisplay
187
+
188
+ ```typescript
189
+ {
190
+ _id: number;
191
+ name: string;
192
+ email ? : string;
193
+ department: string;
194
+ group: string;
195
+ }
196
+ ```
197
+
198
+ ### UserCreation
199
+
200
+ ```typescript
201
+ {
202
+ name: string;
203
+ password: string;
204
+ email ? : string;
205
+ department: string;
206
+ group: string;
207
+ isAdmin: boolean;
208
+ }
209
+ ```
210
+
211
+ ### UserUpdate
212
+
213
+ ```typescript
214
+ {
215
+ name ? : string;
216
+ password ? : string;
217
+ email ? : string;
218
+ isAdmin ? : boolean;
219
+ department ? : string;
220
+ group ? : string;
221
+ }
222
+ ```
223
+
224
+ ### Document
225
+
226
+ ```typescript
227
+ {
228
+ _id: string;
229
+ type: number;
230
+ subType: number;
231
+ title: string;
232
+ content: string;
233
+ shareWithGroup: boolean;
234
+ shareWithDepartment: boolean;
235
+ }
236
+ ```
237
+
238
+ ### NewDocument
239
+
240
+ ```typescript
241
+ {
242
+ type: number;
243
+ subType: number;
244
+ title: string;
245
+ content: string;
246
+ shareWithGroup: boolean;
247
+ shareWithDepartment: boolean;
248
+ }
249
+ ```
250
+
251
+ ### DocumentQuery
252
+
253
+ ```typescript
254
+ {
255
+ _id ? : string;
256
+ type ? : number;
257
+ subType ? : number;
258
+ title ? : string;
259
+ description ? : string;
260
+ shareWithGroup ? : boolean;
261
+ shareWithDepartment ? : boolean;
262
+ }
263
+ ```
264
+
265
+ ### DataStructure
266
+
267
+ ```typescript
268
+ {
269
+ _id: number;
270
+ title: string;
271
+ fields: DataField[];
272
+ }
273
+ ```
274
+
275
+ ### DataStructureCreation
276
+
277
+ ```typescript
278
+ {
279
+ title: string;
280
+ fields: DataField[];
281
+ }
282
+ ```
283
+
284
+ ### DataField
285
+
286
+ ```typescript
287
+ {
288
+ name: string;
289
+ type: 'number' | 'string' | 'boolean' | 'array' | 'structure';
290
+ items ? : string; // For array and structure types
291
+ }
292
+ ```
293
+
294
+ ### DocumentTypeEdit
295
+
296
+ ```typescript
297
+ {
298
+ _id ? : string;
299
+ name: string;
300
+ description ? : string;
301
+ type: number;
302
+ subType: number;
303
+ defaultStructureID ? : string;
304
+ }
305
+ ```
package/dist/index.d.ts CHANGED
@@ -1,17 +1,61 @@
1
1
  import type { I_UserEntry, I_UserLogin, I_UserCreation, I_UserUpdate, I_UserDisplay, I_DocumentEntry, I_DataStructure, I_LoginResponse, I_DocumentQuery, I_StructureCreation, I_WsMessage, I_EventString, I_DocumentType } from "./types.js";
2
2
  import { Socket } from "socket.io-client";
3
- export default class dbPouchClient {
3
+ /**
4
+ * Client for interacting with docPouch API.
5
+ */
6
+ export default class docPouchClient {
7
+ /**
8
+ * The base URL of the server.
9
+ *
10
+ * @type {string}
11
+ */
4
12
  baseUrl: string;
5
- private authToken;
13
+ /**
14
+ * Socket.IO socket instance for real-time communication with the server.
15
+ *
16
+ * @type {Socket}
17
+ */
6
18
  socket: Socket;
19
+ /**
20
+ * Callback function to handle socket events.
21
+ *
22
+ * @type {(event: I_EventString, data: I_WsMessage) => void}
23
+ */
7
24
  callbackFunction: ((event: I_EventString, data: I_WsMessage) => void) | undefined;
25
+ /**
26
+ * Flag indicating whether real-time synchronization is enabled.
27
+ *
28
+ * @type {boolean}
29
+ */
8
30
  realTimeSync: boolean;
31
+ /**
32
+ * Authentication token used to authorize requests.
33
+ *
34
+ * @private
35
+ * @type {string | null}
36
+ */
37
+ private authToken;
38
+ /**
39
+ * Flag indicating whether a connection attempt is in progress.
40
+ *
41
+ * @private
42
+ * @type {boolean}
43
+ */
9
44
  private connectionInProgress;
45
+ /**
46
+ * Creates an instance of docPouchClient.
47
+ *
48
+ * @param {string} host - The base URL for the server.
49
+ * @param {number} [port=80] - The port number to connect to (default is 80).
50
+ * @param {(event: I_EventString, data: I_WsMessage) => void} [callback] - Optional callback function for socket events.
51
+ */
10
52
  constructor(host: string, port?: number, callback?: (event: I_EventString, data: I_WsMessage) => void);
11
- private setupPermanentSocketListeners;
12
- setRealTimeSync: (newRealTimeSync: boolean) => void;
13
- private initWebSocket;
14
- private request;
53
+ /**
54
+ * Sets the real-time synchronization status.
55
+ *
56
+ * @param {boolean} newRealTimeSync - The new real-time sync setting (true/false).
57
+ */
58
+ setRealTimeSync(newRealTimeSync: boolean): void;
15
59
  login(credentials: I_UserLogin): Promise<I_LoginResponse | null>;
16
60
  listUsers(): Promise<I_UserEntry[]>;
17
61
  updateUser(userID: string, userData: I_UserUpdate): Promise<void>;
@@ -29,7 +73,21 @@ export default class dbPouchClient {
29
73
  createType(type: I_DocumentType): Promise<I_DocumentType>;
30
74
  removeType(typeID: string): Promise<void>;
31
75
  getTypes(): Promise<I_DocumentType[]>;
76
+ updateType(updatedType: I_DocumentType): Promise<void>;
32
77
  setToken(token: string | null): void;
33
78
  getVersion(): string;
34
79
  debugSocketConnection(): void;
80
+ /**
81
+ * Sets up permanent socket listeners for the client.
82
+ *
83
+ * @private
84
+ */
85
+ private setupPermanentSocketListeners;
86
+ /**
87
+ * Initializes the WebSocket connection with the server.
88
+ *
89
+ * @private
90
+ */
91
+ private initWebSocket;
92
+ private request;
35
93
  }