@serialsubscriptions/platform-integration 0.0.79
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 +1 -0
- package/lib/SSIProject.d.ts +343 -0
- package/lib/SSIProject.js +429 -0
- package/lib/SSIProjectApi.d.ts +384 -0
- package/lib/SSIProjectApi.js +534 -0
- package/lib/SSISubscribedFeatureApi.d.ts +387 -0
- package/lib/SSISubscribedFeatureApi.js +511 -0
- package/lib/SSISubscribedLimitApi.d.ts +384 -0
- package/lib/SSISubscribedLimitApi.js +534 -0
- package/lib/SSISubscribedPlanApi.d.ts +384 -0
- package/lib/SSISubscribedPlanApi.js +537 -0
- package/lib/SubscribedPlanManager.d.ts +380 -0
- package/lib/SubscribedPlanManager.js +288 -0
- package/lib/UsageApi.d.ts +128 -0
- package/lib/UsageApi.js +224 -0
- package/lib/auth.server.d.ts +192 -0
- package/lib/auth.server.js +579 -0
- package/lib/cache/SSICache.d.ts +40 -0
- package/lib/cache/SSICache.js +134 -0
- package/lib/cache/backends/MemoryCacheBackend.d.ts +15 -0
- package/lib/cache/backends/MemoryCacheBackend.js +46 -0
- package/lib/cache/backends/RedisCacheBackend.d.ts +27 -0
- package/lib/cache/backends/RedisCacheBackend.js +95 -0
- package/lib/cache/constants.d.ts +7 -0
- package/lib/cache/constants.js +10 -0
- package/lib/cache/types.d.ts +27 -0
- package/lib/cache/types.js +2 -0
- package/lib/frontend/index.d.ts +1 -0
- package/lib/frontend/index.js +6 -0
- package/lib/frontend/session/SessionClient.d.ts +24 -0
- package/lib/frontend/session/SessionClient.js +145 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.js +38 -0
- package/lib/lib/session/SessionClient.d.ts +11 -0
- package/lib/lib/session/SessionClient.js +47 -0
- package/lib/lib/session/index.d.ts +3 -0
- package/lib/lib/session/index.js +3 -0
- package/lib/lib/session/stores/MemoryStore.d.ts +7 -0
- package/lib/lib/session/stores/MemoryStore.js +23 -0
- package/lib/lib/session/stores/index.d.ts +1 -0
- package/lib/lib/session/stores/index.js +1 -0
- package/lib/lib/session/types.d.ts +37 -0
- package/lib/lib/session/types.js +1 -0
- package/lib/session/SessionClient.d.ts +19 -0
- package/lib/session/SessionClient.js +132 -0
- package/lib/session/SessionManager.d.ts +139 -0
- package/lib/session/SessionManager.js +443 -0
- package/lib/stateStore.d.ts +5 -0
- package/lib/stateStore.js +9 -0
- package/lib/storage/SSIStorage.d.ts +24 -0
- package/lib/storage/SSIStorage.js +117 -0
- package/lib/storage/backends/MemoryBackend.d.ts +10 -0
- package/lib/storage/backends/MemoryBackend.js +44 -0
- package/lib/storage/backends/PostgresBackend.d.ts +24 -0
- package/lib/storage/backends/PostgresBackend.js +106 -0
- package/lib/storage/backends/RedisBackend.d.ts +19 -0
- package/lib/storage/backends/RedisBackend.js +78 -0
- package/lib/storage/types.d.ts +27 -0
- package/lib/storage/types.js +2 -0
- package/package.json +71 -0
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file
|
|
4
|
+
* SSISubscribedFeatureApi - JSON:API Client for Subscribed Feature Entities
|
|
5
|
+
*
|
|
6
|
+
* A TypeScript client for interacting with Drupal JSON:API endpoints
|
|
7
|
+
* for subscribed_feature entities. This class is independent of Drupal and can be used
|
|
8
|
+
* on any platform that supports fetch.
|
|
9
|
+
*
|
|
10
|
+
* Requirements:
|
|
11
|
+
* - Node.js 18+ (for native fetch) or a fetch polyfill
|
|
12
|
+
* - TypeScript 5.0+
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Initialize the client with a domain
|
|
18
|
+
* const client = new SSISubscribedFeatureApi('https://example.com');
|
|
19
|
+
*
|
|
20
|
+
* // Set Bearer token authentication
|
|
21
|
+
* client.setBearerToken('your-oauth-or-jwt-token-here');
|
|
22
|
+
*
|
|
23
|
+
* // List subscribed features with filters and pagination
|
|
24
|
+
* const features = await client.list(
|
|
25
|
+
* { status: 'active' },
|
|
26
|
+
* ['-created'],
|
|
27
|
+
* { offset: 0, limit: 10 }
|
|
28
|
+
* );
|
|
29
|
+
*
|
|
30
|
+
* // Get a single subscribed feature by UUID
|
|
31
|
+
* const feature = await client.get('550e8400-e29b-41d4-a716-446655440000', {
|
|
32
|
+
* include: ['subscription', 'feature'],
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Create a subscribed feature
|
|
36
|
+
* const newFeature = await client.create(
|
|
37
|
+
* { status: 'active' },
|
|
38
|
+
* {
|
|
39
|
+
* subscription: { type: 'subscription--subscription', id: 'subscription-uuid' },
|
|
40
|
+
* feature: { type: 'feature--feature', id: 'feature-uuid' },
|
|
41
|
+
* }
|
|
42
|
+
* );
|
|
43
|
+
*
|
|
44
|
+
* // Update a subscribed feature by UUID
|
|
45
|
+
* await client.update('550e8400-e29b-41d4-a716-446655440000', { status: 'inactive' });
|
|
46
|
+
*
|
|
47
|
+
* // Delete a subscribed feature by UUID
|
|
48
|
+
* await client.delete('550e8400-e29b-41d4-a716-446655440000');
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
+
exports.SSISubscribedFeatureApi = void 0;
|
|
53
|
+
/**
|
|
54
|
+
* SSISubscribedFeatureApi - JSON:API Client for Subscribed Feature Entities
|
|
55
|
+
*/
|
|
56
|
+
class SSISubscribedFeatureApi {
|
|
57
|
+
/**
|
|
58
|
+
* Constructs an SSISubscribedFeatureApi client.
|
|
59
|
+
*
|
|
60
|
+
* @param domain - The full URL prefix of the Drupal site (e.g., 'https://example.com').
|
|
61
|
+
* @param options - Optional configuration:
|
|
62
|
+
* - apiBasePath: Override the default API path (default: '/jsonapi/subscribed_feature/subscribed_feature')
|
|
63
|
+
* - timeout: Request timeout in milliseconds (default: 30000)
|
|
64
|
+
*/
|
|
65
|
+
constructor(domain, options) {
|
|
66
|
+
this.apiBasePath = '/jsonapi/subscribed_feature/subscribed_feature';
|
|
67
|
+
this.bearerToken = null;
|
|
68
|
+
this.csrfToken = null;
|
|
69
|
+
this.defaultHeaders = {
|
|
70
|
+
'Accept': 'application/vnd.api+json',
|
|
71
|
+
'Content-Type': 'application/vnd.api+json',
|
|
72
|
+
};
|
|
73
|
+
this.baseUrl = domain.trim().replace(/\/+$/, ''); // Remove trailing slashes
|
|
74
|
+
if (options?.apiBasePath) {
|
|
75
|
+
this.apiBasePath = options.apiBasePath;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Sets the Bearer token for authentication.
|
|
80
|
+
*
|
|
81
|
+
* @param token - The Bearer token (OAuth/JWT).
|
|
82
|
+
*/
|
|
83
|
+
setBearerToken(token) {
|
|
84
|
+
this.bearerToken = token;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Gets the current Bearer token.
|
|
88
|
+
*
|
|
89
|
+
* @returns The Bearer token, or null if not set.
|
|
90
|
+
*/
|
|
91
|
+
getBearerToken() {
|
|
92
|
+
return this.bearerToken;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Fetches a CSRF token from the Drupal session/token endpoint.
|
|
96
|
+
* The token is cached after the first fetch to avoid unnecessary requests.
|
|
97
|
+
*
|
|
98
|
+
* @returns Promise resolving to the CSRF token string.
|
|
99
|
+
*
|
|
100
|
+
* @throws Error if the request fails.
|
|
101
|
+
*
|
|
102
|
+
* @private
|
|
103
|
+
*/
|
|
104
|
+
async getCsrfToken() {
|
|
105
|
+
if (this.csrfToken) {
|
|
106
|
+
return this.csrfToken;
|
|
107
|
+
}
|
|
108
|
+
const url = `${this.baseUrl}/session/token`;
|
|
109
|
+
const headers = {
|
|
110
|
+
'Accept': 'text/plain',
|
|
111
|
+
};
|
|
112
|
+
if (this.bearerToken) {
|
|
113
|
+
headers['Authorization'] = `Bearer ${this.bearerToken}`;
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
const response = await fetch(url, {
|
|
117
|
+
method: 'GET',
|
|
118
|
+
headers,
|
|
119
|
+
});
|
|
120
|
+
if (!response.ok) {
|
|
121
|
+
throw new Error(`Failed to fetch CSRF token: HTTP ${response.status} ${response.statusText}`);
|
|
122
|
+
}
|
|
123
|
+
const token = await response.text();
|
|
124
|
+
if (!token || !token.trim()) {
|
|
125
|
+
throw new Error('CSRF token response was empty');
|
|
126
|
+
}
|
|
127
|
+
this.csrfToken = token.trim();
|
|
128
|
+
return this.csrfToken;
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
console.error('[SSISubscribedFeatureApi] Failed to fetch CSRF token:', error);
|
|
132
|
+
if (error instanceof Error) {
|
|
133
|
+
throw new Error(`CSRF token fetch failed: ${error.message}`);
|
|
134
|
+
}
|
|
135
|
+
throw new Error(`CSRF token fetch failed: ${String(error)}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Clears the cached CSRF token, forcing a fresh fetch on the next write operation.
|
|
140
|
+
* This may be useful if the token expires or becomes invalid.
|
|
141
|
+
*/
|
|
142
|
+
clearCsrfToken() {
|
|
143
|
+
this.csrfToken = null;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Lists subscribed_feature entities with optional filtering, sorting, and pagination.
|
|
147
|
+
*
|
|
148
|
+
* @param options - List options:
|
|
149
|
+
* - filters: Filter conditions (e.g., { status: 'active' } or { created: { operator: '>', value: '2024-01-01' } })
|
|
150
|
+
* - sort: Sort fields (e.g., ['-created', 'status'] for descending created, ascending status)
|
|
151
|
+
* - pagination: Pagination options (offset, limit; max limit: 50)
|
|
152
|
+
* - include: Related resources to include (e.g., ['subscription', 'feature'])
|
|
153
|
+
* - fields: Sparse fieldsets to limit returned fields
|
|
154
|
+
*
|
|
155
|
+
* @returns Promise resolving to the JSON:API document with subscribed features.
|
|
156
|
+
*
|
|
157
|
+
* @throws Error if the request fails.
|
|
158
|
+
*/
|
|
159
|
+
async list(options = {}) {
|
|
160
|
+
const queryParams = new URLSearchParams();
|
|
161
|
+
if (options.filters) {
|
|
162
|
+
const filterParams = this.buildFilterParams(options.filters);
|
|
163
|
+
for (const [key, value] of Object.entries(filterParams)) {
|
|
164
|
+
queryParams.append(key, String(value));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (options.sort && options.sort.length > 0) {
|
|
168
|
+
queryParams.set('sort', options.sort.join(','));
|
|
169
|
+
}
|
|
170
|
+
if (options.pagination) {
|
|
171
|
+
if (options.pagination.offset !== undefined) {
|
|
172
|
+
queryParams.set('page[offset]', String(options.pagination.offset));
|
|
173
|
+
}
|
|
174
|
+
if (options.pagination.limit !== undefined) {
|
|
175
|
+
queryParams.set('page[limit]', String(Math.min(options.pagination.limit, 50)));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (options.include && options.include.length > 0) {
|
|
179
|
+
queryParams.set('include', options.include.join(','));
|
|
180
|
+
}
|
|
181
|
+
if (options.fields) {
|
|
182
|
+
for (const [resourceType, fieldList] of Object.entries(options.fields)) {
|
|
183
|
+
queryParams.set(`fields[${resourceType}]`, fieldList.join(','));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const url = `${this.baseUrl}${this.apiBasePath}${queryParams.toString() ? `?${queryParams.toString()}` : ''}`;
|
|
187
|
+
return this.request('GET', url);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Retrieves a single subscribed_feature entity by UUID.
|
|
191
|
+
*
|
|
192
|
+
* The API endpoint is constructed as: baseUrl + apiBasePath + "/" + uuid
|
|
193
|
+
* Example: https://example.com/jsonapi/subscribed_feature/subscribed_feature/550e8400-e29b-41d4-a716-446655440000
|
|
194
|
+
*
|
|
195
|
+
* @param uuid - The UUID of the subscribed_feature entity (NOT the subscribed_feature ID).
|
|
196
|
+
* Must be a valid UUID string (e.g., '550e8400-e29b-41d4-a716-446655440000').
|
|
197
|
+
* @param options - Get options:
|
|
198
|
+
* - include: Related resources to include (e.g., ['subscription', 'feature'])
|
|
199
|
+
* - fields: Sparse fieldsets to limit returned fields
|
|
200
|
+
*
|
|
201
|
+
* @returns Promise resolving to the JSON:API document with the subscribed feature.
|
|
202
|
+
*
|
|
203
|
+
* @throws Error if the request fails or entity is not found.
|
|
204
|
+
*/
|
|
205
|
+
async get(uuid, options = {}) {
|
|
206
|
+
const queryParams = new URLSearchParams();
|
|
207
|
+
if (options.include && options.include.length > 0) {
|
|
208
|
+
queryParams.set('include', options.include.join(','));
|
|
209
|
+
}
|
|
210
|
+
if (options.fields) {
|
|
211
|
+
for (const [resourceType, fieldList] of Object.entries(options.fields)) {
|
|
212
|
+
queryParams.set(`fields[${resourceType}]`, fieldList.join(','));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const url = `${this.baseUrl}${this.apiBasePath}/${uuid}${queryParams.toString() ? `?${queryParams.toString()}` : ''}`;
|
|
216
|
+
return this.request('GET', url);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Creates a new subscribed_feature entity.
|
|
220
|
+
*
|
|
221
|
+
* @param attributes - Entity attributes.
|
|
222
|
+
* @param options - Create options:
|
|
223
|
+
* - relationships: Entity relationships
|
|
224
|
+
* - resourceType: The resource type (default: 'subscribed_feature--subscribed_feature')
|
|
225
|
+
*
|
|
226
|
+
* @returns Promise resolving to the JSON:API document with the created entity.
|
|
227
|
+
*
|
|
228
|
+
* @throws Error if the request fails or validation errors occur.
|
|
229
|
+
*/
|
|
230
|
+
async create(attributes, options = {}) {
|
|
231
|
+
const resourceType = options.resourceType || 'subscribed_feature--subscribed_feature';
|
|
232
|
+
const data = {
|
|
233
|
+
type: resourceType,
|
|
234
|
+
attributes,
|
|
235
|
+
};
|
|
236
|
+
if (options.relationships) {
|
|
237
|
+
data.relationships = this.formatRelationships(options.relationships);
|
|
238
|
+
}
|
|
239
|
+
const body = { data };
|
|
240
|
+
const url = `${this.baseUrl}${this.apiBasePath}`;
|
|
241
|
+
return this.request('POST', url, body);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Updates an existing subscribed_feature entity.
|
|
245
|
+
*
|
|
246
|
+
* The API endpoint is constructed as: baseUrl + apiBasePath + "/" + uuid
|
|
247
|
+
* Example: https://example.com/jsonapi/subscribed_feature/subscribed_feature/550e8400-e29b-41d4-a716-446655440000
|
|
248
|
+
*
|
|
249
|
+
* @param uuid - The UUID of the subscribed_feature entity to update (NOT the subscribed_feature ID).
|
|
250
|
+
* Must be a valid UUID string (e.g., '550e8400-e29b-41d4-a716-446655440000').
|
|
251
|
+
* @param attributes - Attributes to update (partial updates supported).
|
|
252
|
+
* @param options - Update options:
|
|
253
|
+
* - relationships: Relationships to update (optional)
|
|
254
|
+
* - resourceType: The resource type (default: 'subscribed_feature--subscribed_feature')
|
|
255
|
+
*
|
|
256
|
+
* @returns Promise resolving to the JSON:API document with the updated entity.
|
|
257
|
+
*
|
|
258
|
+
* @throws Error if the request fails or validation errors occur.
|
|
259
|
+
*/
|
|
260
|
+
async update(uuid, attributes, options = {}) {
|
|
261
|
+
const resourceType = options.resourceType || 'subscribed_feature--subscribed_feature';
|
|
262
|
+
const data = {
|
|
263
|
+
type: resourceType,
|
|
264
|
+
id: uuid,
|
|
265
|
+
attributes,
|
|
266
|
+
};
|
|
267
|
+
if (options.relationships) {
|
|
268
|
+
data.relationships = this.formatRelationships(options.relationships);
|
|
269
|
+
}
|
|
270
|
+
const body = { data };
|
|
271
|
+
const url = `${this.baseUrl}${this.apiBasePath}/${uuid}`;
|
|
272
|
+
return this.request('PATCH', url, body);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Deletes a subscribed_feature entity.
|
|
276
|
+
*
|
|
277
|
+
* The API endpoint is constructed as: baseUrl + apiBasePath + "/" + uuid
|
|
278
|
+
* Example: https://example.com/jsonapi/subscribed_feature/subscribed_feature/550e8400-e29b-41d4-a716-446655440000
|
|
279
|
+
*
|
|
280
|
+
* @param uuid - The UUID of the subscribed_feature entity to delete (NOT the subscribed_feature ID).
|
|
281
|
+
* Must be a valid UUID string (e.g., '550e8400-e29b-41d4-a716-446655440000').
|
|
282
|
+
*
|
|
283
|
+
* @returns Promise resolving to true if deletion was successful.
|
|
284
|
+
*
|
|
285
|
+
* @throws Error if the request fails.
|
|
286
|
+
*/
|
|
287
|
+
async delete(uuid) {
|
|
288
|
+
const url = `${this.baseUrl}${this.apiBasePath}/${uuid}`;
|
|
289
|
+
const response = await this.requestRaw('DELETE', url);
|
|
290
|
+
const responseClone = response.clone();
|
|
291
|
+
const responseText = await responseClone.text();
|
|
292
|
+
if (response.status === 204) {
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
if (!response.ok) {
|
|
296
|
+
const errorData = await this.parseErrorResponse(response);
|
|
297
|
+
console.error('[SSISubscribedFeatureApi] DELETE error response:', errorData);
|
|
298
|
+
throw this.createError(errorData, response.status, response.statusText);
|
|
299
|
+
}
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Makes an HTTP request to the API and returns the parsed JSON:API document.
|
|
304
|
+
*
|
|
305
|
+
* @param method - HTTP method (GET, POST, PATCH).
|
|
306
|
+
* @param url - The full URL.
|
|
307
|
+
* @param body - Request body (for POST/PATCH).
|
|
308
|
+
*
|
|
309
|
+
* @returns Promise resolving to the parsed JSON:API document.
|
|
310
|
+
*
|
|
311
|
+
* @throws Error if the request fails.
|
|
312
|
+
*
|
|
313
|
+
* @private
|
|
314
|
+
*/
|
|
315
|
+
async request(method, url, body) {
|
|
316
|
+
const response = await this.requestRaw(method, url, body);
|
|
317
|
+
const responseText = await response.text();
|
|
318
|
+
if (!response.ok) {
|
|
319
|
+
const errorData = this.parseErrorResponseFromText(responseText);
|
|
320
|
+
console.error('[SSISubscribedFeatureApi] Error response:', errorData);
|
|
321
|
+
throw this.createError(errorData, response.status, response.statusText);
|
|
322
|
+
}
|
|
323
|
+
if (response.status === 204) {
|
|
324
|
+
return { data: [] };
|
|
325
|
+
}
|
|
326
|
+
const json = JSON.parse(responseText);
|
|
327
|
+
return json;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Makes a raw HTTP request to the API and returns the Response object.
|
|
331
|
+
*
|
|
332
|
+
* @param method - HTTP method (GET, POST, PATCH, DELETE).
|
|
333
|
+
* @param url - The full URL.
|
|
334
|
+
* @param body - Request body (for POST/PATCH).
|
|
335
|
+
*
|
|
336
|
+
* @returns Promise resolving to the Response object.
|
|
337
|
+
*
|
|
338
|
+
* @throws Error if the request fails (network errors, etc.).
|
|
339
|
+
*
|
|
340
|
+
* @private
|
|
341
|
+
*/
|
|
342
|
+
async requestRaw(method, url, body) {
|
|
343
|
+
const headers = { ...this.defaultHeaders };
|
|
344
|
+
if (this.bearerToken) {
|
|
345
|
+
headers['Authorization'] = `Bearer ${this.bearerToken}`;
|
|
346
|
+
}
|
|
347
|
+
if (['POST', 'PATCH', 'DELETE'].includes(method)) {
|
|
348
|
+
try {
|
|
349
|
+
const csrfToken = await this.getCsrfToken();
|
|
350
|
+
if (csrfToken) {
|
|
351
|
+
headers['X-CSRF-Token'] = csrfToken;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
catch (error) {
|
|
355
|
+
console.warn('[SSISubscribedFeatureApi] Failed to fetch CSRF token, proceeding without it:', error);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const fetchOptions = {
|
|
359
|
+
method,
|
|
360
|
+
headers,
|
|
361
|
+
};
|
|
362
|
+
if (body && (method === 'POST' || method === 'PATCH')) {
|
|
363
|
+
fetchOptions.body = JSON.stringify(body);
|
|
364
|
+
}
|
|
365
|
+
try {
|
|
366
|
+
const response = await fetch(url, fetchOptions);
|
|
367
|
+
return response;
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
console.error('[SSISubscribedFeatureApi] Request error:', error);
|
|
371
|
+
if (error instanceof Error) {
|
|
372
|
+
throw new Error(`Request failed: ${error.message}`);
|
|
373
|
+
}
|
|
374
|
+
throw new Error(`Request failed: ${String(error)}`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Parses error response from JSON:API.
|
|
379
|
+
*
|
|
380
|
+
* @param response - The HTTP response.
|
|
381
|
+
*
|
|
382
|
+
* @returns Promise resolving to the error data.
|
|
383
|
+
*
|
|
384
|
+
* @private
|
|
385
|
+
*/
|
|
386
|
+
async parseErrorResponse(response) {
|
|
387
|
+
try {
|
|
388
|
+
const text = await response.text();
|
|
389
|
+
return this.parseErrorResponseFromText(text);
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Parses error response from text string.
|
|
397
|
+
*
|
|
398
|
+
* @param text - The response text.
|
|
399
|
+
*
|
|
400
|
+
* @returns The error data, or null if parsing fails.
|
|
401
|
+
*
|
|
402
|
+
* @private
|
|
403
|
+
*/
|
|
404
|
+
parseErrorResponseFromText(text) {
|
|
405
|
+
try {
|
|
406
|
+
if (!text) {
|
|
407
|
+
return null;
|
|
408
|
+
}
|
|
409
|
+
return JSON.parse(text);
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Creates an error from JSON:API error response.
|
|
417
|
+
*
|
|
418
|
+
* @param errorData - The error data.
|
|
419
|
+
* @param status - HTTP status code.
|
|
420
|
+
* @param statusText - HTTP status text.
|
|
421
|
+
*
|
|
422
|
+
* @returns Error instance.
|
|
423
|
+
*
|
|
424
|
+
* @private
|
|
425
|
+
*/
|
|
426
|
+
createError(errorData, status, statusText) {
|
|
427
|
+
if (errorData && errorData.errors && errorData.errors.length > 0) {
|
|
428
|
+
const messages = errorData.errors.map((error) => {
|
|
429
|
+
const statusCode = error.status || String(status);
|
|
430
|
+
const message = error.detail || error.title || 'Unknown error';
|
|
431
|
+
return `[${statusCode}] ${message}`;
|
|
432
|
+
});
|
|
433
|
+
const error = new Error(messages.join('; '));
|
|
434
|
+
error.status = status;
|
|
435
|
+
error.errors = errorData.errors;
|
|
436
|
+
return error;
|
|
437
|
+
}
|
|
438
|
+
const error = new Error(`HTTP ${status} ${statusText}`);
|
|
439
|
+
error.status = status;
|
|
440
|
+
return error;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Builds filter query parameters from a filter object.
|
|
444
|
+
*
|
|
445
|
+
* @param filters - Filter conditions.
|
|
446
|
+
*
|
|
447
|
+
* @returns Query parameters for filters.
|
|
448
|
+
*
|
|
449
|
+
* @private
|
|
450
|
+
*/
|
|
451
|
+
buildFilterParams(filters) {
|
|
452
|
+
const params = {};
|
|
453
|
+
let index = 0;
|
|
454
|
+
for (const [field, condition] of Object.entries(filters)) {
|
|
455
|
+
if (typeof condition === 'string' || typeof condition === 'number') {
|
|
456
|
+
params[`filter[${field}]`] = String(condition);
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (condition && typeof condition === 'object' && 'operator' in condition) {
|
|
460
|
+
const filterKey = `filter[${index}]`;
|
|
461
|
+
params[`${filterKey}[condition][path]`] = field;
|
|
462
|
+
params[`${filterKey}[condition][operator]`] = condition.operator;
|
|
463
|
+
if ('value' in condition) {
|
|
464
|
+
const value = condition.value;
|
|
465
|
+
if (Array.isArray(value)) {
|
|
466
|
+
value.forEach((v, i) => {
|
|
467
|
+
params[`${filterKey}[condition][value][${i}]`] = String(v);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
params[`${filterKey}[condition][value]`] = String(value);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
index++;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return params;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Formats relationships for JSON:API format.
|
|
481
|
+
*
|
|
482
|
+
* @param relationships - Relationships input.
|
|
483
|
+
*
|
|
484
|
+
* @returns Formatted relationships.
|
|
485
|
+
*
|
|
486
|
+
* @private
|
|
487
|
+
*/
|
|
488
|
+
formatRelationships(relationships) {
|
|
489
|
+
const formatted = {};
|
|
490
|
+
for (const [fieldName, relationship] of Object.entries(relationships)) {
|
|
491
|
+
if (relationship && typeof relationship === 'object' && 'type' in relationship && 'id' in relationship) {
|
|
492
|
+
formatted[fieldName] = {
|
|
493
|
+
data: {
|
|
494
|
+
type: relationship.type,
|
|
495
|
+
id: relationship.id,
|
|
496
|
+
},
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
else if (Array.isArray(relationship)) {
|
|
500
|
+
formatted[fieldName] = {
|
|
501
|
+
data: relationship.map((item) => ({
|
|
502
|
+
type: item.type,
|
|
503
|
+
id: item.id,
|
|
504
|
+
})),
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return formatted;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
exports.SSISubscribedFeatureApi = SSISubscribedFeatureApi;
|