@unito/integration-sdk 1.5.0 → 1.6.1
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/src/index.cjs
CHANGED
|
@@ -1352,7 +1352,8 @@ class Provider {
|
|
|
1352
1352
|
if (headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
|
1353
1353
|
stringifiedBody = new URLSearchParams(body).toString();
|
|
1354
1354
|
}
|
|
1355
|
-
else if (headers['Content-Type'] === 'application/json'
|
|
1355
|
+
else if (headers['Content-Type'] === 'application/json' ||
|
|
1356
|
+
headers['Content-Type'] === 'application/json-patch+json') {
|
|
1356
1357
|
stringifiedBody = JSON.stringify(body);
|
|
1357
1358
|
}
|
|
1358
1359
|
}
|
|
@@ -60,6 +60,7 @@ export type PreparedRequest = {
|
|
|
60
60
|
url: string;
|
|
61
61
|
headers: Record<string, string>;
|
|
62
62
|
};
|
|
63
|
+
export type RequestBody = Record<string, unknown> | RequestBody[];
|
|
63
64
|
/**
|
|
64
65
|
* The Provider class is a wrapper around the fetch function to call a provider's HTTP API.
|
|
65
66
|
*
|
|
@@ -149,7 +150,7 @@ export declare class Provider {
|
|
|
149
150
|
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
150
151
|
* @returns The {@link Response} extracted from the provider.
|
|
151
152
|
*/
|
|
152
|
-
post<T>(endpoint: string, body:
|
|
153
|
+
post<T>(endpoint: string, body: RequestBody, options: RequestOptions): Promise<Response<T>>;
|
|
153
154
|
postForm<T>(endpoint: string, form: FormData, options: RequestOptions): Promise<Response<T>>;
|
|
154
155
|
/**
|
|
155
156
|
* Performs a PUT request to the provider.
|
|
@@ -163,7 +164,7 @@ export declare class Provider {
|
|
|
163
164
|
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
164
165
|
* @returns The {@link Response} extracted from the provider.
|
|
165
166
|
*/
|
|
166
|
-
put<T>(endpoint: string, body:
|
|
167
|
+
put<T>(endpoint: string, body: RequestBody, options: RequestOptions): Promise<Response<T>>;
|
|
167
168
|
/**
|
|
168
169
|
* Performs a PATCH request to the provider.
|
|
169
170
|
*
|
|
@@ -176,7 +177,7 @@ export declare class Provider {
|
|
|
176
177
|
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
177
178
|
* @returns The {@link Response} extracted from the provider.
|
|
178
179
|
*/
|
|
179
|
-
patch<T>(endpoint: string, body:
|
|
180
|
+
patch<T>(endpoint: string, body: RequestBody, options: RequestOptions): Promise<Response<T>>;
|
|
180
181
|
/**
|
|
181
182
|
* Performs a DELETE request to the provider.
|
|
182
183
|
*
|
|
@@ -245,7 +245,8 @@ export class Provider {
|
|
|
245
245
|
if (headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
|
246
246
|
stringifiedBody = new URLSearchParams(body).toString();
|
|
247
247
|
}
|
|
248
|
-
else if (headers['Content-Type'] === 'application/json'
|
|
248
|
+
else if (headers['Content-Type'] === 'application/json' ||
|
|
249
|
+
headers['Content-Type'] === 'application/json-patch+json') {
|
|
249
250
|
stringifiedBody = JSON.stringify(body);
|
|
250
251
|
}
|
|
251
252
|
}
|
|
@@ -157,6 +157,39 @@ describe('Provider', () => {
|
|
|
157
157
|
]);
|
|
158
158
|
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
159
159
|
});
|
|
160
|
+
it('accept an array as body for post request', async (context) => {
|
|
161
|
+
const response = new Response('{"data": "value"}', {
|
|
162
|
+
status: 201,
|
|
163
|
+
headers: { 'Content-Type': 'application/json' },
|
|
164
|
+
});
|
|
165
|
+
const fetchMock = context.mock.method(global, 'fetch', () => Promise.resolve(response));
|
|
166
|
+
const actualResponse = await provider.post('/endpoint', [
|
|
167
|
+
{ data: '1', data2: '2' },
|
|
168
|
+
{ data: '3', data2: '4' },
|
|
169
|
+
], {
|
|
170
|
+
credentials: { apiKey: 'apikey#1111' },
|
|
171
|
+
logger: logger,
|
|
172
|
+
signal: new AbortController().signal,
|
|
173
|
+
additionnalheaders: { 'Content-Type': 'application/json-patch+json', 'X-Additional-Header': 'value1' },
|
|
174
|
+
});
|
|
175
|
+
assert.equal(fetchMock.mock.calls.length, 1);
|
|
176
|
+
assert.deepEqual(fetchMock.mock.calls[0]?.arguments, [
|
|
177
|
+
'www.myApi.com/endpoint',
|
|
178
|
+
{
|
|
179
|
+
method: 'POST',
|
|
180
|
+
body: '[{"data":"1","data2":"2"},{"data":"3","data2":"4"}]',
|
|
181
|
+
signal: new AbortController().signal,
|
|
182
|
+
headers: {
|
|
183
|
+
'Content-Type': 'application/json-patch+json',
|
|
184
|
+
Accept: 'application/json',
|
|
185
|
+
'X-Custom-Provider-Header': 'value',
|
|
186
|
+
'X-Provider-Credential-Header': 'apikey#1111',
|
|
187
|
+
'X-Additional-Header': 'value1',
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
]);
|
|
191
|
+
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
192
|
+
});
|
|
160
193
|
it('put with json body', async (context) => {
|
|
161
194
|
const response = new Response('{"data": "value"}', {
|
|
162
195
|
status: 201,
|
package/package.json
CHANGED
|
@@ -64,6 +64,8 @@ export type PreparedRequest = {
|
|
|
64
64
|
headers: Record<string, string>;
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
+
export type RequestBody = Record<string, unknown> | RequestBody[];
|
|
68
|
+
|
|
67
69
|
/**
|
|
68
70
|
* The Provider class is a wrapper around the fetch function to call a provider's HTTP API.
|
|
69
71
|
*
|
|
@@ -184,7 +186,7 @@ export class Provider {
|
|
|
184
186
|
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
185
187
|
* @returns The {@link Response} extracted from the provider.
|
|
186
188
|
*/
|
|
187
|
-
public async post<T>(endpoint: string, body:
|
|
189
|
+
public async post<T>(endpoint: string, body: RequestBody, options: RequestOptions): Promise<Response<T>> {
|
|
188
190
|
return this.fetchWrapper<T>(endpoint, body, {
|
|
189
191
|
...options,
|
|
190
192
|
method: 'POST',
|
|
@@ -261,7 +263,7 @@ export class Provider {
|
|
|
261
263
|
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
262
264
|
* @returns The {@link Response} extracted from the provider.
|
|
263
265
|
*/
|
|
264
|
-
public async put<T>(endpoint: string, body:
|
|
266
|
+
public async put<T>(endpoint: string, body: RequestBody, options: RequestOptions): Promise<Response<T>> {
|
|
265
267
|
return this.fetchWrapper<T>(endpoint, body, {
|
|
266
268
|
...options,
|
|
267
269
|
method: 'PUT',
|
|
@@ -284,11 +286,7 @@ export class Provider {
|
|
|
284
286
|
* @param options RequestOptions used to adjust the call made to the provider (use to override default headers).
|
|
285
287
|
* @returns The {@link Response} extracted from the provider.
|
|
286
288
|
*/
|
|
287
|
-
public async patch<T>(
|
|
288
|
-
endpoint: string,
|
|
289
|
-
body: Record<string, unknown>,
|
|
290
|
-
options: RequestOptions,
|
|
291
|
-
): Promise<Response<T>> {
|
|
289
|
+
public async patch<T>(endpoint: string, body: RequestBody, options: RequestOptions): Promise<Response<T>> {
|
|
292
290
|
return this.fetchWrapper<T>(endpoint, body, {
|
|
293
291
|
...options,
|
|
294
292
|
method: 'PATCH',
|
|
@@ -337,7 +335,7 @@ export class Provider {
|
|
|
337
335
|
|
|
338
336
|
private async fetchWrapper<T>(
|
|
339
337
|
endpoint: string,
|
|
340
|
-
body:
|
|
338
|
+
body: RequestBody | null,
|
|
341
339
|
options: RequestOptions & { defaultHeaders: { 'Content-Type'?: string; Accept?: string }; method: string },
|
|
342
340
|
): Promise<Response<T>> {
|
|
343
341
|
const { url: providerUrl, headers: providerHeaders } = await this.prepareRequest(options);
|
|
@@ -349,7 +347,10 @@ export class Provider {
|
|
|
349
347
|
if (body) {
|
|
350
348
|
if (headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
|
351
349
|
stringifiedBody = new URLSearchParams(body as Record<string, string>).toString();
|
|
352
|
-
} else if (
|
|
350
|
+
} else if (
|
|
351
|
+
headers['Content-Type'] === 'application/json' ||
|
|
352
|
+
headers['Content-Type'] === 'application/json-patch+json'
|
|
353
|
+
) {
|
|
353
354
|
stringifiedBody = JSON.stringify(body);
|
|
354
355
|
}
|
|
355
356
|
}
|
|
@@ -188,6 +188,47 @@ describe('Provider', () => {
|
|
|
188
188
|
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
189
189
|
});
|
|
190
190
|
|
|
191
|
+
it('accept an array as body for post request', async context => {
|
|
192
|
+
const response = new Response('{"data": "value"}', {
|
|
193
|
+
status: 201,
|
|
194
|
+
headers: { 'Content-Type': 'application/json' },
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const fetchMock = context.mock.method(global, 'fetch', () => Promise.resolve(response));
|
|
198
|
+
|
|
199
|
+
const actualResponse = await provider.post(
|
|
200
|
+
'/endpoint',
|
|
201
|
+
[
|
|
202
|
+
{ data: '1', data2: '2' },
|
|
203
|
+
{ data: '3', data2: '4' },
|
|
204
|
+
],
|
|
205
|
+
{
|
|
206
|
+
credentials: { apiKey: 'apikey#1111' },
|
|
207
|
+
logger: logger,
|
|
208
|
+
signal: new AbortController().signal,
|
|
209
|
+
additionnalheaders: { 'Content-Type': 'application/json-patch+json', 'X-Additional-Header': 'value1' },
|
|
210
|
+
},
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
assert.equal(fetchMock.mock.calls.length, 1);
|
|
214
|
+
assert.deepEqual(fetchMock.mock.calls[0]?.arguments, [
|
|
215
|
+
'www.myApi.com/endpoint',
|
|
216
|
+
{
|
|
217
|
+
method: 'POST',
|
|
218
|
+
body: '[{"data":"1","data2":"2"},{"data":"3","data2":"4"}]',
|
|
219
|
+
signal: new AbortController().signal,
|
|
220
|
+
headers: {
|
|
221
|
+
'Content-Type': 'application/json-patch+json',
|
|
222
|
+
Accept: 'application/json',
|
|
223
|
+
'X-Custom-Provider-Header': 'value',
|
|
224
|
+
'X-Provider-Credential-Header': 'apikey#1111',
|
|
225
|
+
'X-Additional-Header': 'value1',
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
]);
|
|
229
|
+
assert.deepEqual(actualResponse, { status: 201, headers: response.headers, body: { data: 'value' } });
|
|
230
|
+
});
|
|
231
|
+
|
|
191
232
|
it('put with json body', async context => {
|
|
192
233
|
const response = new Response('{"data": "value"}', {
|
|
193
234
|
status: 201,
|