@bluecopa/core 0.1.7 → 0.1.8

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 CHANGED
@@ -20,9 +20,15 @@ The core package provides essential API utilities and functions for data managem
20
20
  - [Workbook Module](#workbook-module)
21
21
  - [Workflow Module](#workflow-module)
22
22
  - [Worksheet Module](#worksheet-module)
23
+ - [WebSocket Connection](#websocket-connection)
24
+ - [WebSocket Factory](#websocket-factory)
25
+ - [WebSocket Provider Interface](#websocket-provider-interface)
26
+ - [WebSocket Usage Example](#websocket-usage-example)
27
+ - [WebSocket Requirements](#websocket-requirements)
23
28
  - [Examples](#examples)
24
- - [1. Get Input Tables](#1-get-input-tables)
25
- - [2. Get Workbooks by Type](#2-get-workbooks-by-type)
29
+ - [1. Complete Setup with User Details and WebSocket](#1-complete-setup-with-user-details-and-websocket)
30
+ - [2. Get Input Tables](#2-get-input-tables)
31
+ - [3. Get Workbooks by Type](#3-get-workbooks-by-type)
26
32
  - [Development](#development)
27
33
  - [Related Packages](#related-packages)
28
34
 
@@ -42,7 +48,7 @@ npm install @bluecopa/core
42
48
  - Dependencies:
43
49
  - axios (1.12.0) - For HTTP requests
44
50
  - lodash (4.17.21) - For utility functions
45
-
51
+ - centrifuge (5.0.0) - For WebSocket connections
46
52
 
47
53
  ## Configuration
48
54
 
@@ -51,13 +57,38 @@ The package uses a singleton-based configuration system to manage API settings.
51
57
  Import and set the config:
52
58
 
53
59
  ```typescript
54
- import { copaSetConfig } from '@bluecopa/core';
60
+ import { copaSetConfig, copaApi } from '@bluecopa/core';
55
61
 
56
62
  copaSetConfig({
57
63
  apiBaseUrl: 'https://develop.bluecopa.com', // Base URL for API endpoints
58
64
  accessToken: 'your-access-token', // Authentication token
59
- workspaceId: 'your-workspace-id' // Current workspace identifier
65
+ workspaceId: 'your-workspace-id', // Current workspace identifier
66
+ userId: 'your-user-id' // User identifier for WebSocket connections
67
+ });
68
+ ```
69
+
70
+ ### Getting User Details and Setting User ID
71
+
72
+ To automatically set the userId from the logged-in user:
73
+
74
+ ```typescript
75
+ import { copaSetConfig, copaApi } from '@bluecopa/core';
76
+
77
+ // First configure basic settings
78
+ copaSetConfig({
79
+ apiBaseUrl: 'https://develop.bluecopa.com',
80
+ accessToken: 'your-access-token',
81
+ workspaceId: 'your-workspace-id'
60
82
  });
83
+
84
+ // Get user details and set userId
85
+ try {
86
+ const userDetails = await copaApi.user.getLoggedInUserDetails();
87
+ copaSetConfig({ userId: userDetails.id });
88
+ console.log('User ID set:', userDetails.id);
89
+ } catch (error) {
90
+ console.error('Failed to get user details:', error);
91
+ }
61
92
  ```
62
93
 
63
94
  - `copaSetConfig(partialConfig: Partial<Config>)`: Updates the configuration.
@@ -71,6 +102,7 @@ export interface Config {
71
102
  apiBaseUrl: string;
72
103
  accessToken: string;
73
104
  workspaceId: string;
105
+ userId: string;
74
106
  }
75
107
  ```
76
108
 
@@ -79,6 +111,7 @@ export interface Config {
79
111
  All API functions are asynchronous and use the shared `apiClient` for HTTP requests. They handle errors by throwing objects with `message` and `status`. Access via `copaApi.moduleName.functionName()`.
80
112
 
81
113
  ### Dataset Module
114
+
82
115
  - `copaApi.dataset.getData()`: Retrieves specific dataset data by ID or parameters
83
116
  - `copaApi.dataset.getDatasets()`: Fetches a list of datasets
84
117
  - `copaApi.dataset.getSampleData()`: Gets sample data for datasets
@@ -86,6 +119,7 @@ All API functions are asynchronous and use the shared `apiClient` for HTTP reque
86
119
  See: [`dataset/getData.ts`](src/api/dataset/getData.ts:1), [`dataset/getSampleData.ts`](src/api/dataset/getSampleData.ts:1), [`dataset/getDatasets.ts`](src/api/dataset/getDatasets.ts:1)
87
120
 
88
121
  ### Definition Module
122
+
89
123
  - `copaApi.definition.runDefinition()`: Executes a custom definition
90
124
  - `copaApi.definition.runPublishedDefinition()`: Runs a published definition
91
125
  - `copaApi.definition.runSampleDefinition()`: Executes a sample definition for testing
@@ -93,11 +127,13 @@ See: [`dataset/getData.ts`](src/api/dataset/getData.ts:1), [`dataset/getSampleDa
93
127
  See: [`definition/runPublishedDefinition.ts`](src/api/definition/runPublishedDefinition.ts:1), [`definition/runSampleDefinition.ts`](src/api/definition/runSampleDefinition.ts:1), [`definition/runDefinition.ts`](src/api/definition/runDefinition.ts:1)
94
128
 
95
129
  ### File Module
130
+
96
131
  - `copaApi.files.getFileUrlByFileId(fileId: string)`: Generates a URL for a file by its ID
97
132
 
98
133
  See: [`file/getFileUrlByFileId.ts`](src/api/file/getFileUrlByFileId.ts:1)
99
134
 
100
135
  ### InputTable Module
136
+
101
137
  - `copaApi.inputTable.getData()`: Retrieves data from an input table
102
138
  - `copaApi.inputTable.getInputTables()`: Fetches all input tables
103
139
  - `copaApi.inputTable.getTableById(id: string)`: Gets a specific input table by ID
@@ -105,22 +141,26 @@ See: [`file/getFileUrlByFileId.ts`](src/api/file/getFileUrlByFileId.ts:1)
105
141
  See: [`inputTable/getData.ts`](src/api/inputTable/getData.ts:1), [`inputTable/getInputTables.ts`](src/api/inputTable/getInputTables.ts:1), [`inputTable/getTableById.ts`](src/api/inputTable/getTableById.ts:1)
106
142
 
107
143
  ### Metric Module
144
+
108
145
  - `copaApi.metric.getData()`: Fetches metric data
109
146
 
110
147
  See: [`metric/getData.ts`](src/api/metric/getData.ts:1)
111
148
 
112
149
  ### User Module
150
+
113
151
  - `copaApi.user.getLoggedInUserDetails()`: Retrieves details of the currently logged-in user
114
152
 
115
153
  See: [`user/getLoggedInUserDetails.ts`](src/api/user/getLoggedInUserDetails.ts:1)
116
154
 
117
155
  ### Workbook Module
156
+
118
157
  - `copaApi.workbook.getPublishedWorkbookById(id: string)`: Fetches a published workbook by ID
119
158
  - `copaApi.workbook.getWorkbooksByType(type: string)`: Retrieves workbooks filtered by type
120
159
 
121
160
  See: [`workbook/getPublishedWorkbookById.ts`](src/api/workbook/getPublishedWorkbookById.ts:1), [`workbook/getWorkbooksByType.ts`](src/api/workbook/getWorkbooksByType.ts:1)
122
161
 
123
162
  ### Workflow Module
163
+
124
164
  - `copaApi.workflow.getWorkflowInstanceStatusById(id: string)`: Checks the status of a workflow instance
125
165
  - `copaApi.workflow.triggerHttpWorkflowById(id: string)`: Triggers an HTTP-based workflow
126
166
  - `copaApi.workflow.triggerWorkflowById(id: string)`: Triggers a workflow by ID
@@ -128,28 +168,137 @@ See: [`workbook/getPublishedWorkbookById.ts`](src/api/workbook/getPublishedWorkb
128
168
  See: [`workflow/triggerHttpWorkflowById.ts`](src/api/workflow/triggerHttpWorkflowById.ts:1), [`workflow/triggerWorkflowById.ts`](src/api/workflow/triggerWorkflowById.ts:1), [`workflow/getWorkflowInstanceStatusById.ts`](src/api/workflow/getWorkflowInstanceStatusById.ts:1)
129
169
 
130
170
  ### Worksheet Module
171
+
131
172
  - `copaApi.worksheet.getWorksheets()`: Fetches all worksheets
132
173
  - `copaApi.worksheet.getWorksheetsByType(type: string)`: Retrieves worksheets by type
133
174
 
134
175
  See: [`worksheet/getWorksheets.ts`](src/api/worksheet/getWorksheets.ts:1), [`worksheet/getWorksheetsByType.ts`](src/api/worksheet/getWorksheetsByType.ts:1)
135
176
 
177
+ ## WebSocket Connection
178
+
179
+ The core package provides WebSocket utilities for real-time communication using Centrifugo.
180
+
181
+ ### WebSocket Factory
182
+
183
+ Access WebSocket functionality through the utilities:
184
+
185
+ ```typescript
186
+ import { copaUtils } from '@bluecopa/core';
187
+
188
+ // Create a WebSocket connection
189
+ const websocket = copaUtils.websocketUtils.WebsocketContextFactory.create('centrifugo', {
190
+ connectionUrl: 'wss://your-centrifugo-url'
191
+ });
192
+ ```
193
+
194
+ ### WebSocket Provider Interface
195
+
196
+ The `IWebsocketProvider` interface provides the following methods:
197
+
198
+ - `connect()`: Establishes the WebSocket connection
199
+ - `bind(channel: string, event: string, callback: (data: any) => void)`: Subscribe to private user-specific channels
200
+ - `bindGlobal(event: string, callback: (data: any) => void)`: Subscribe to global channels
201
+ - `unbindAll(channel: string)`: Unsubscribe from all events on a channel
202
+ - `disconnect()`: Close the WebSocket connection
203
+
204
+ ### WebSocket Usage Example
205
+
206
+ ```typescript
207
+ import { copaSetConfig, copaApi, copaUtils } from '@bluecopa/core';
208
+
209
+ // Configure with userId for WebSocket connections
210
+ copaSetConfig({
211
+ apiBaseUrl: 'https://develop.bluecopa.com',
212
+ accessToken: 'your-access-token',
213
+ workspaceId: 'your-workspace-id',
214
+ userId: 'your-user-id'
215
+ });
216
+
217
+ // Create WebSocket connection
218
+ const websocket = copaUtils.websocketUtils.WebsocketContextFactory.create('centrifugo', {
219
+ connectionUrl: 'wss://centrifugo.your-domain.com/connection/websocket'
220
+ });
221
+
222
+ // Subscribe to user-specific events
223
+ websocket.bind('notifications', 'new_message', (data) => {
224
+ console.log('New notification:', data);
225
+ });
226
+
227
+ // Subscribe to global events
228
+ websocket.bindGlobal('system_updates', (data) => {
229
+ console.log('System update:', data);
230
+ });
231
+
232
+ // Clean up when done
233
+ websocket.disconnect();
234
+ ```
235
+
236
+ ### WebSocket Requirements
237
+
238
+ - **userId**: Required for private channel subscriptions (`bind` method)
239
+ - **accessToken**: Required for authentication with Centrifugo
240
+ - **connectionUrl**: WebSocket endpoint URL
241
+
242
+ The WebSocket connection automatically uses the configured `accessToken` and `userId` from the config for authentication and channel binding.
243
+
136
244
  ## Examples
137
245
 
138
- ### 1. Get Input Tables
246
+ ### 1. Complete Setup with User Details and WebSocket
247
+
248
+ Complete example showing configuration, user details retrieval, and WebSocket setup.
249
+
250
+ ```typescript
251
+ import { copaSetConfig, copaApi, copaUtils } from '@bluecopa/core';
252
+
253
+ // Initial configuration
254
+ copaSetConfig({
255
+ apiBaseUrl: 'https://develop.bluecopa.com',
256
+ accessToken: 'your-access-token',
257
+ workspaceId: 'your-workspace-id'
258
+ });
259
+
260
+ // Get user details and set userId
261
+ try {
262
+ const userDetails = await copaApi.user.getLoggedInUserDetails();
263
+ copaSetConfig({ userId: userDetails.id });
264
+ console.log('User configured:', userDetails.name, userDetails.id);
265
+ } catch (error: any) {
266
+ console.error('Failed to get user details:', error.message, error.status);
267
+ }
268
+
269
+ // Set up WebSocket connection
270
+ const websocket = copaUtils.websocketUtils.WebsocketContextFactory.create('centrifugo', {
271
+ connectionUrl: 'wss://centrifugo.develop.bluecopa.com/connection/websocket'
272
+ });
273
+
274
+ // Subscribe to notifications
275
+ websocket.bind('notifications', 'new_message', (data) => {
276
+ console.log('New notification received:', data);
277
+ });
278
+ ```
279
+
280
+ ### 2. Get Input Tables
281
+
139
282
  Fetches all input tables from the API.
140
283
 
141
284
  ```typescript
142
285
  import { copaApi } from '@bluecopa/core';
143
286
 
144
287
  // Configure first
145
- copaSetConfig({ apiBaseUrl: 'https://api.example.com', accessToken: 'token', workspaceId: 'ws1' });
288
+ copaSetConfig({
289
+ apiBaseUrl: 'https://api.example.com',
290
+ accessToken: 'token',
291
+ workspaceId: 'ws1',
292
+ userId: 'user123'
293
+ });
146
294
 
147
295
  // Use API
148
296
  const { getInputTables } = copaApi.inputTable;
149
297
  const { getWorkbooksByType } = copaApi.workbook;
150
298
  ```
151
299
 
152
- ### 2. Get Workbooks by Type
300
+ ### 3. Get Workbooks by Type
301
+
153
302
  Retrieves workbooks filtered by a specific type.
154
303
 
155
304
  ```typescript
package/dist/config.d.ts CHANGED
@@ -2,6 +2,7 @@ export interface Config {
2
2
  apiBaseUrl: string;
3
3
  accessToken: string;
4
4
  workspaceId: string;
5
+ userId: string;
5
6
  }
6
7
  declare class ConfigSingleton {
7
8
  private static instance;