@encatch/api-sdk 0.0.9 → 0.0.10
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 +366 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
# @encatch/api-sdk
|
|
2
|
+
|
|
3
|
+
JavaScript/TypeScript SDK for interacting with the Encatch API. This package provides a simple and type-safe way to fetch feedback configurations, submit user feedback, and utilize Encatch services.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
Use this package when you need direct programmatic access to Encatch API endpoints for:
|
|
8
|
+
- Fetching feedback form configurations
|
|
9
|
+
- Submitting and viewing feedback
|
|
10
|
+
- Refining text using Encatch AI services
|
|
11
|
+
|
|
12
|
+
## Resources
|
|
13
|
+
|
|
14
|
+
- **Website**: [https://encatch.com](https://encatch.com)
|
|
15
|
+
- **Documentation**: [https://encatch.com/docs](https://encatch.com/docs)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @encatch/api-sdk
|
|
21
|
+
# or
|
|
22
|
+
pnpm add @encatch/api-sdk
|
|
23
|
+
# or
|
|
24
|
+
yarn add @encatch/api-sdk
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { EncatchApiSDK } from '@encatch/api-sdk';
|
|
31
|
+
|
|
32
|
+
// Initialize the SDK
|
|
33
|
+
const sdk = new EncatchApiSDK({
|
|
34
|
+
apiKey: 'your-api-key',
|
|
35
|
+
appPackageName: 'com.example.app',
|
|
36
|
+
hostUrl: 'https://app.dev.encatch.com', // optional
|
|
37
|
+
enableLogging: true, // optional, defaults to false
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Fetch configurations
|
|
41
|
+
const configs = await sdk.fetchConfiguration({
|
|
42
|
+
deviceDetails: {
|
|
43
|
+
deviceId: 'unique-device-id',
|
|
44
|
+
platform: 'web',
|
|
45
|
+
},
|
|
46
|
+
identifier: 'user-identifier',
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Features
|
|
51
|
+
|
|
52
|
+
- **TypeScript Support**: Full type safety with TypeScript definitions
|
|
53
|
+
- **Automatic Case Conversion**: Handles snake_case/camelCase transformations
|
|
54
|
+
- **Gzip Compression**: Automatic compression for feedback submissions
|
|
55
|
+
- **Error Handling**: Built-in error handling with state management
|
|
56
|
+
- **Logging**: Optional logging for debugging
|
|
57
|
+
- **State Management**: Track loading and error states
|
|
58
|
+
|
|
59
|
+
## Configuration
|
|
60
|
+
|
|
61
|
+
### SDK Constructor Options
|
|
62
|
+
|
|
63
|
+
| Option | Type | Required | Default | Description |
|
|
64
|
+
|--------|------|----------|---------|-------------|
|
|
65
|
+
| `apiKey` | `string` | Yes | - | Your Encatch API key |
|
|
66
|
+
| `appPackageName` | `string` | Yes | - | Your application package name (used as Referer) |
|
|
67
|
+
| `hostUrl` | `string` | No | `https://app.dev.encatch.com` | Base URL for API endpoints |
|
|
68
|
+
| `enableLogging` | `boolean` | No | `false` | Enable console logging for debugging |
|
|
69
|
+
|
|
70
|
+
## API Methods
|
|
71
|
+
|
|
72
|
+
### fetchConfiguration
|
|
73
|
+
|
|
74
|
+
Fetches the list of available feedback configurations for a device.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const response = await sdk.fetchConfiguration({
|
|
78
|
+
deviceDetails: {
|
|
79
|
+
deviceId: 'unique-device-id',
|
|
80
|
+
platform: 'web',
|
|
81
|
+
osVersion: '10.0',
|
|
82
|
+
appVersion: '1.0.0',
|
|
83
|
+
},
|
|
84
|
+
identifier: 'user@example.com',
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### fetchNewDeviceConfiguration
|
|
89
|
+
|
|
90
|
+
Fetches configurations specifically for new devices.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
const response = await sdk.fetchNewDeviceConfiguration({
|
|
94
|
+
deviceDetails: {
|
|
95
|
+
deviceId: 'new-device-id',
|
|
96
|
+
platform: 'web',
|
|
97
|
+
},
|
|
98
|
+
identifier: 'user@example.com',
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### fetchConfigurationDetails
|
|
103
|
+
|
|
104
|
+
Fetches detailed information about a specific feedback configuration.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
const details = await sdk.fetchConfigurationDetails({
|
|
108
|
+
formConfig: {
|
|
109
|
+
feedbackConfigurationId: 'config-uuid',
|
|
110
|
+
userAction: 'view',
|
|
111
|
+
},
|
|
112
|
+
deviceDetails: {
|
|
113
|
+
deviceId: 'unique-device-id',
|
|
114
|
+
platform: 'web',
|
|
115
|
+
},
|
|
116
|
+
identifier: 'user@example.com',
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### submitFeedback
|
|
121
|
+
|
|
122
|
+
Submits user feedback data. The payload is automatically compressed using gzip.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
const result = await sdk.submitFeedback({
|
|
126
|
+
formConfig: {
|
|
127
|
+
feedbackConfigurationId: 'config-uuid',
|
|
128
|
+
userAction: 'submit',
|
|
129
|
+
},
|
|
130
|
+
deviceDetails: {
|
|
131
|
+
deviceId: 'unique-device-id',
|
|
132
|
+
platform: 'web',
|
|
133
|
+
},
|
|
134
|
+
identifier: 'user@example.com',
|
|
135
|
+
responses: [
|
|
136
|
+
{
|
|
137
|
+
questionId: 'q1',
|
|
138
|
+
answer: 'User response here',
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
if (result.success) {
|
|
144
|
+
console.log('Feedback submitted successfully');
|
|
145
|
+
} else {
|
|
146
|
+
console.error('Error:', result.error);
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### viewFeedback
|
|
151
|
+
|
|
152
|
+
Tracks when a user views a feedback form. The payload is automatically compressed using gzip.
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
const result = await sdk.viewFeedback({
|
|
156
|
+
formConfig: {
|
|
157
|
+
feedbackConfigurationId: 'config-uuid',
|
|
158
|
+
userAction: 'view',
|
|
159
|
+
},
|
|
160
|
+
deviceDetails: {
|
|
161
|
+
deviceId: 'unique-device-id',
|
|
162
|
+
platform: 'web',
|
|
163
|
+
},
|
|
164
|
+
identifier: 'user@example.com',
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### refineText
|
|
169
|
+
|
|
170
|
+
Uses Encatch AI services to refine or improve text.
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
const refinedText = await sdk.refineText({
|
|
174
|
+
text: 'Original text to refine',
|
|
175
|
+
context: 'feedback',
|
|
176
|
+
language: 'en',
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
console.log('Refined text:', refinedText.refinedText);
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## State Management
|
|
183
|
+
|
|
184
|
+
The SDK provides getters to track the current state:
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
// Check if a request is in progress
|
|
188
|
+
if (sdk.loading) {
|
|
189
|
+
console.log('Loading...');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Check for errors
|
|
193
|
+
if (sdk.error) {
|
|
194
|
+
console.error('Error occurred:', sdk.error);
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Error Handling
|
|
199
|
+
|
|
200
|
+
All methods throw errors on failure. Use try-catch blocks for error handling:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
try {
|
|
204
|
+
const configs = await sdk.fetchConfiguration(params);
|
|
205
|
+
// Handle success
|
|
206
|
+
} catch (error) {
|
|
207
|
+
console.error('Failed to fetch configurations:', error);
|
|
208
|
+
// The error message is also available via sdk.error getter
|
|
209
|
+
console.log('SDK error state:', sdk.error);
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The `submitFeedback` and `viewFeedback` methods return a result object instead of throwing:
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
const result = await sdk.submitFeedback(params);
|
|
217
|
+
if (!result.success) {
|
|
218
|
+
console.error('Submission failed:', result.error);
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## TypeScript Support
|
|
223
|
+
|
|
224
|
+
The package exports all TypeScript types from the `@encatch/schema` package:
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import type {
|
|
228
|
+
FetchConfigurationListRequest,
|
|
229
|
+
FetchConfigurationListResponse,
|
|
230
|
+
FetchFeedbackDetailsRequest,
|
|
231
|
+
FetchFeedbackDetailsResponse,
|
|
232
|
+
SubmitFeedback,
|
|
233
|
+
ViewFeedback,
|
|
234
|
+
RefineTextParams,
|
|
235
|
+
RefineTextResponse,
|
|
236
|
+
} from '@encatch/api-sdk';
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Advanced Usage
|
|
240
|
+
|
|
241
|
+
### Custom Host URL
|
|
242
|
+
|
|
243
|
+
For different environments (development, staging, production):
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
const sdk = new EncatchApiSDK({
|
|
247
|
+
apiKey: process.env.ENCATCH_API_KEY,
|
|
248
|
+
appPackageName: 'com.example.app',
|
|
249
|
+
hostUrl: process.env.ENCATCH_HOST_URL || 'https://app.encatch.com',
|
|
250
|
+
enableLogging: process.env.NODE_ENV === 'development',
|
|
251
|
+
});
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Debugging with Logging
|
|
255
|
+
|
|
256
|
+
Enable logging to see detailed request/response information:
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
const sdk = new EncatchApiSDK({
|
|
260
|
+
apiKey: 'your-api-key',
|
|
261
|
+
appPackageName: 'com.example.app',
|
|
262
|
+
enableLogging: true,
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// All API calls will now log to console:
|
|
266
|
+
// [EncatchApiSDK] Fetching configuration from server
|
|
267
|
+
// [EncatchApiSDK] Request body: {...}
|
|
268
|
+
// [EncatchApiSDK] Configuration fetched successfully {...}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Dependencies
|
|
272
|
+
|
|
273
|
+
- `@encatch/schema` - Type definitions and utilities (workspace)
|
|
274
|
+
- `pako` >= 2.1.0 - Gzip compression for feedback submissions
|
|
275
|
+
- `ts-case-convert` >= 2.1.0 - Automatic case conversion
|
|
276
|
+
- `zod` >= 4.1.8 - Runtime type validation
|
|
277
|
+
|
|
278
|
+
## Best Practices
|
|
279
|
+
|
|
280
|
+
1. **Store API keys securely**: Never commit API keys to version control. Use environment variables.
|
|
281
|
+
|
|
282
|
+
2. **Reuse SDK instances**: Create a single SDK instance and reuse it throughout your application.
|
|
283
|
+
|
|
284
|
+
3. **Error handling**: Always wrap SDK calls in try-catch blocks or handle returned error objects.
|
|
285
|
+
|
|
286
|
+
4. **Enable logging in development**: Use `enableLogging: true` during development for easier debugging.
|
|
287
|
+
|
|
288
|
+
5. **Monitor loading state**: Use the `loading` getter to show loading indicators in your UI.
|
|
289
|
+
|
|
290
|
+
## Example: Complete Integration
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
import { EncatchApiSDK } from '@encatch/api-sdk';
|
|
294
|
+
|
|
295
|
+
class FeedbackService {
|
|
296
|
+
private sdk: EncatchApiSDK;
|
|
297
|
+
|
|
298
|
+
constructor() {
|
|
299
|
+
this.sdk = new EncatchApiSDK({
|
|
300
|
+
apiKey: process.env.ENCATCH_API_KEY!,
|
|
301
|
+
appPackageName: 'com.example.app',
|
|
302
|
+
hostUrl: 'https://app.encatch.com',
|
|
303
|
+
enableLogging: process.env.NODE_ENV === 'development',
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async loadFeedbackForms(deviceId: string, userId: string) {
|
|
308
|
+
try {
|
|
309
|
+
const response = await this.sdk.fetchConfiguration({
|
|
310
|
+
deviceDetails: {
|
|
311
|
+
deviceId,
|
|
312
|
+
platform: 'web',
|
|
313
|
+
},
|
|
314
|
+
identifier: userId,
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
return response.configurations;
|
|
318
|
+
} catch (error) {
|
|
319
|
+
console.error('Failed to load feedback forms:', error);
|
|
320
|
+
throw error;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
async submitUserFeedback(
|
|
325
|
+
configId: string,
|
|
326
|
+
deviceId: string,
|
|
327
|
+
userId: string,
|
|
328
|
+
responses: any[]
|
|
329
|
+
) {
|
|
330
|
+
const result = await this.sdk.submitFeedback({
|
|
331
|
+
formConfig: {
|
|
332
|
+
feedbackConfigurationId: configId,
|
|
333
|
+
userAction: 'submit',
|
|
334
|
+
},
|
|
335
|
+
deviceDetails: {
|
|
336
|
+
deviceId,
|
|
337
|
+
platform: 'web',
|
|
338
|
+
},
|
|
339
|
+
identifier: userId,
|
|
340
|
+
responses,
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
if (!result.success) {
|
|
344
|
+
throw new Error(result.error || 'Failed to submit feedback');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
isLoading(): boolean {
|
|
351
|
+
return this.sdk.loading;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
getError(): string | null {
|
|
355
|
+
return this.sdk.error;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Usage
|
|
360
|
+
const feedbackService = new FeedbackService();
|
|
361
|
+
const forms = await feedbackService.loadFeedbackForms('device-123', 'user@example.com');
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
## License
|
|
365
|
+
|
|
366
|
+
See the main repository for license information.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@encatch/api-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"pako": "^2.1.0",
|
|
21
21
|
"ts-case-convert": "^2.1.0",
|
|
22
22
|
"zod": "^4.1.8",
|
|
23
|
-
"@encatch/schema": "0.1.
|
|
23
|
+
"@encatch/schema": "0.1.33"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"dev": "vite",
|