@unito/integration-sdk 1.1.3 → 1.2.0
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
|
@@ -1209,7 +1209,7 @@ class Provider {
|
|
|
1209
1209
|
});
|
|
1210
1210
|
}
|
|
1211
1211
|
async postForm(endpoint, form, options) {
|
|
1212
|
-
const { url: providerUrl, headers: providerHeaders } = this.prepareRequest(options);
|
|
1212
|
+
const { url: providerUrl, headers: providerHeaders } = await this.prepareRequest(options);
|
|
1213
1213
|
const absoluteUrl = this.generateAbsoluteUrl(providerUrl, endpoint, options.queryParams);
|
|
1214
1214
|
const headers = { ...form.getHeaders(), ...providerHeaders, ...options.additionnalheaders };
|
|
1215
1215
|
const reqOptions = {
|
|
@@ -1335,7 +1335,7 @@ class Provider {
|
|
|
1335
1335
|
return absoluteUrl;
|
|
1336
1336
|
}
|
|
1337
1337
|
async fetchWrapper(endpoint, body, options) {
|
|
1338
|
-
const { url: providerUrl, headers: providerHeaders } = this.prepareRequest(options);
|
|
1338
|
+
const { url: providerUrl, headers: providerHeaders } = await this.prepareRequest(options);
|
|
1339
1339
|
const absoluteUrl = this.generateAbsoluteUrl(providerUrl, endpoint, options.queryParams);
|
|
1340
1340
|
const headers = { ...options.defaultHeaders, ...providerHeaders, ...options.additionnalheaders };
|
|
1341
1341
|
let stringifiedBody = null;
|
|
@@ -1395,6 +1395,10 @@ class Provider {
|
|
|
1395
1395
|
// When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it.
|
|
1396
1396
|
body = response.body;
|
|
1397
1397
|
}
|
|
1398
|
+
else if (headers.Accept?.includes('text/html')) {
|
|
1399
|
+
// Accept text based content types
|
|
1400
|
+
body = (await response.text());
|
|
1401
|
+
}
|
|
1398
1402
|
else {
|
|
1399
1403
|
throw this.handleError(500, 'Unsupported Content-Type');
|
|
1400
1404
|
}
|
|
@@ -54,6 +54,10 @@ export type Response<T> = {
|
|
|
54
54
|
status: number;
|
|
55
55
|
headers: Headers;
|
|
56
56
|
};
|
|
57
|
+
export type PreparedRequest = {
|
|
58
|
+
url: string;
|
|
59
|
+
headers: Record<string, string>;
|
|
60
|
+
};
|
|
57
61
|
/**
|
|
58
62
|
* The Provider class is a wrapper around the fetch function to call a provider's HTTP API.
|
|
59
63
|
*
|
|
@@ -82,10 +86,7 @@ export declare class Provider {
|
|
|
82
86
|
protected prepareRequest: (options: {
|
|
83
87
|
credentials: Credentials;
|
|
84
88
|
logger: Logger;
|
|
85
|
-
}) =>
|
|
86
|
-
url: string;
|
|
87
|
-
headers: Record<string, string>;
|
|
88
|
-
};
|
|
89
|
+
}) => PreparedRequest | Promise<PreparedRequest>;
|
|
89
90
|
/**
|
|
90
91
|
* (Optional) Custom error handler to handle specific errors returned by the provider.
|
|
91
92
|
*
|
|
@@ -110,7 +110,7 @@ export class Provider {
|
|
|
110
110
|
});
|
|
111
111
|
}
|
|
112
112
|
async postForm(endpoint, form, options) {
|
|
113
|
-
const { url: providerUrl, headers: providerHeaders } = this.prepareRequest(options);
|
|
113
|
+
const { url: providerUrl, headers: providerHeaders } = await this.prepareRequest(options);
|
|
114
114
|
const absoluteUrl = this.generateAbsoluteUrl(providerUrl, endpoint, options.queryParams);
|
|
115
115
|
const headers = { ...form.getHeaders(), ...providerHeaders, ...options.additionnalheaders };
|
|
116
116
|
const reqOptions = {
|
|
@@ -236,7 +236,7 @@ export class Provider {
|
|
|
236
236
|
return absoluteUrl;
|
|
237
237
|
}
|
|
238
238
|
async fetchWrapper(endpoint, body, options) {
|
|
239
|
-
const { url: providerUrl, headers: providerHeaders } = this.prepareRequest(options);
|
|
239
|
+
const { url: providerUrl, headers: providerHeaders } = await this.prepareRequest(options);
|
|
240
240
|
const absoluteUrl = this.generateAbsoluteUrl(providerUrl, endpoint, options.queryParams);
|
|
241
241
|
const headers = { ...options.defaultHeaders, ...providerHeaders, ...options.additionnalheaders };
|
|
242
242
|
let stringifiedBody = null;
|
|
@@ -296,6 +296,10 @@ export class Provider {
|
|
|
296
296
|
// When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it.
|
|
297
297
|
body = response.body;
|
|
298
298
|
}
|
|
299
|
+
else if (headers.Accept?.includes('text/html')) {
|
|
300
|
+
// Accept text based content types
|
|
301
|
+
body = (await response.text());
|
|
302
|
+
}
|
|
299
303
|
else {
|
|
300
304
|
throw this.handleError(500, 'Unsupported Content-Type');
|
|
301
305
|
}
|
|
@@ -50,6 +50,35 @@ describe('Provider', () => {
|
|
|
50
50
|
]);
|
|
51
51
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
52
52
|
});
|
|
53
|
+
it('should accept text/html type response', async (context) => {
|
|
54
|
+
const response = new Response('', {
|
|
55
|
+
status: 200,
|
|
56
|
+
headers: { 'Content-Type': 'text/html; charset=UTF-8' },
|
|
57
|
+
});
|
|
58
|
+
const fetchMock = context.mock.method(global, 'fetch', () => Promise.resolve(response));
|
|
59
|
+
const actualResponse = await provider.get('/endpoint', {
|
|
60
|
+
credentials: { apiKey: 'apikey#1111' },
|
|
61
|
+
logger: logger,
|
|
62
|
+
signal: new AbortController().signal,
|
|
63
|
+
additionnalheaders: { 'X-Additional-Header': 'value1', Accept: 'text/html; charset=UTF-8' },
|
|
64
|
+
});
|
|
65
|
+
assert.equal(fetchMock.mock.calls.length, 1);
|
|
66
|
+
assert.deepEqual(fetchMock.mock.calls[0]?.arguments, [
|
|
67
|
+
'www.myApi.com/endpoint',
|
|
68
|
+
{
|
|
69
|
+
method: 'GET',
|
|
70
|
+
body: null,
|
|
71
|
+
signal: new AbortController().signal,
|
|
72
|
+
headers: {
|
|
73
|
+
Accept: 'text/html; charset=UTF-8',
|
|
74
|
+
'X-Custom-Provider-Header': 'value',
|
|
75
|
+
'X-Provider-Credential-Header': 'apikey#1111',
|
|
76
|
+
'X-Additional-Header': 'value1',
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
]);
|
|
80
|
+
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: '' });
|
|
81
|
+
});
|
|
53
82
|
it('gets an endpoint which is an absolute url', async (context) => {
|
|
54
83
|
const response = new Response('{"data": "value"}', {
|
|
55
84
|
status: 200,
|
package/package.json
CHANGED
|
@@ -57,6 +57,11 @@ export type Response<T> = {
|
|
|
57
57
|
headers: Headers;
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
+
export type PreparedRequest = {
|
|
61
|
+
url: string;
|
|
62
|
+
headers: Record<string, string>;
|
|
63
|
+
};
|
|
64
|
+
|
|
60
65
|
/**
|
|
61
66
|
* The Provider class is a wrapper around the fetch function to call a provider's HTTP API.
|
|
62
67
|
*
|
|
@@ -82,10 +87,10 @@ export class Provider {
|
|
|
82
87
|
* This is applied at large to all requests made to the provider. If you need to add specific headers to a single request,
|
|
83
88
|
* pass it through the RequestOptions object when calling the Provider's methods.
|
|
84
89
|
*/
|
|
85
|
-
protected prepareRequest: (options: {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
90
|
+
protected prepareRequest: (options: {
|
|
91
|
+
credentials: Credentials;
|
|
92
|
+
logger: Logger;
|
|
93
|
+
}) => PreparedRequest | Promise<PreparedRequest>;
|
|
89
94
|
/**
|
|
90
95
|
* (Optional) Custom error handler to handle specific errors returned by the provider.
|
|
91
96
|
*
|
|
@@ -181,7 +186,7 @@ export class Provider {
|
|
|
181
186
|
}
|
|
182
187
|
|
|
183
188
|
public async postForm<T>(endpoint: string, form: FormData, options: RequestOptions): Promise<Response<T>> {
|
|
184
|
-
const { url: providerUrl, headers: providerHeaders } = this.prepareRequest(options);
|
|
189
|
+
const { url: providerUrl, headers: providerHeaders } = await this.prepareRequest(options);
|
|
185
190
|
const absoluteUrl = this.generateAbsoluteUrl(providerUrl, endpoint, options.queryParams);
|
|
186
191
|
const headers = { ...form.getHeaders(), ...providerHeaders, ...options.additionnalheaders };
|
|
187
192
|
|
|
@@ -325,7 +330,7 @@ export class Provider {
|
|
|
325
330
|
body: Record<string, unknown> | null,
|
|
326
331
|
options: RequestOptions & { defaultHeaders: { 'Content-Type'?: string; Accept?: string }; method: string },
|
|
327
332
|
): Promise<Response<T>> {
|
|
328
|
-
const { url: providerUrl, headers: providerHeaders } = this.prepareRequest(options);
|
|
333
|
+
const { url: providerUrl, headers: providerHeaders } = await this.prepareRequest(options);
|
|
329
334
|
const absoluteUrl = this.generateAbsoluteUrl(providerUrl, endpoint, options.queryParams);
|
|
330
335
|
const headers = { ...options.defaultHeaders, ...providerHeaders, ...options.additionnalheaders };
|
|
331
336
|
|
|
@@ -395,6 +400,9 @@ export class Provider {
|
|
|
395
400
|
} else if (headers.Accept == 'application/octet-stream') {
|
|
396
401
|
// When we expect octet-stream, we accept any Content-Type the provider sends us, we just want to stream it.
|
|
397
402
|
body = response.body as T;
|
|
403
|
+
} else if (headers.Accept?.includes('text/html')) {
|
|
404
|
+
// Accept text based content types
|
|
405
|
+
body = (await response.text()) as T;
|
|
398
406
|
} else {
|
|
399
407
|
throw this.handleError(500, 'Unsupported Content-Type');
|
|
400
408
|
}
|
|
@@ -59,6 +59,41 @@ describe('Provider', () => {
|
|
|
59
59
|
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: { data: 'value' } });
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
+
it('should accept text/html type response', async context => {
|
|
63
|
+
const response = new Response('', {
|
|
64
|
+
status: 200,
|
|
65
|
+
headers: { 'Content-Type': 'text/html; charset=UTF-8' },
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const fetchMock = context.mock.method(global, 'fetch', () => Promise.resolve(response));
|
|
69
|
+
|
|
70
|
+
const actualResponse = await provider.get('/endpoint', {
|
|
71
|
+
credentials: { apiKey: 'apikey#1111' },
|
|
72
|
+
logger: logger,
|
|
73
|
+
signal: new AbortController().signal,
|
|
74
|
+
additionnalheaders: { 'X-Additional-Header': 'value1', Accept: 'text/html; charset=UTF-8' },
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
assert.equal(fetchMock.mock.calls.length, 1);
|
|
78
|
+
|
|
79
|
+
assert.deepEqual(fetchMock.mock.calls[0]?.arguments, [
|
|
80
|
+
'www.myApi.com/endpoint',
|
|
81
|
+
{
|
|
82
|
+
method: 'GET',
|
|
83
|
+
body: null,
|
|
84
|
+
signal: new AbortController().signal,
|
|
85
|
+
headers: {
|
|
86
|
+
Accept: 'text/html; charset=UTF-8',
|
|
87
|
+
'X-Custom-Provider-Header': 'value',
|
|
88
|
+
'X-Provider-Credential-Header': 'apikey#1111',
|
|
89
|
+
'X-Additional-Header': 'value1',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
assert.deepEqual(actualResponse, { status: 200, headers: response.headers, body: '' });
|
|
95
|
+
});
|
|
96
|
+
|
|
62
97
|
it('gets an endpoint which is an absolute url', async context => {
|
|
63
98
|
const response = new Response('{"data": "value"}', {
|
|
64
99
|
status: 200,
|