Skip to content

Commit e0a6f9b

Browse files
author
Vincent Dubois
committed
Add default multipart json fetch client
1 parent fae038e commit e0a6f9b

5 files changed

Lines changed: 35 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ You can use `createMultipartHttpFetchRequest` to upload a file or a list of file
322322
export default class ApiHttpClient {
323323
// ...
324324
multipartRequest<T>(method: HttpMethod, path: string): MultipartHttpRequest<HttpPromise<T>> {
325-
return createMultipartHttpFetchRequest<T>(baseUrl, method, path, multipartHttpFetchClient);
325+
return createMultipartHttpFetchRequest<T>(baseUrl, method, path, defaultJsonMultipartFetchClient);
326326
}
327327
}
328328
```

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export {
5151
} from './lib/client/HttpResponse';
5252
// custom fetch clients
5353
export {
54-
jsonContentTypeValidator,
5554
defaultJsonFetchClient,
5655
} from './lib/client/JsonFetchClient';
5756
export {
@@ -76,3 +75,4 @@ export { SynchronizedHttpPromise } from './lib/promise/SynchronizedHttpPromise';
7675

7776
// TODO to remove in the next future major release 3.x.x
7877
export { validateContentType as contentTypeValidator } from './lib/handler/ValidateContentTypeHandler';
78+
export { jsonContentTypeValidator } from './lib/handler/ValidateContentTypeHandler';

src/lib/client/JsonFetchClient.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
import { HttpRequest } from 'simple-http-request-builder';
2-
import { FetchResponseHandler } from '../handler/FetchResponseHandlers';
32
import { toJsonResponse } from '../handler/ResponseJsonHandler';
4-
import { validateContentType } from '../handler/ValidateContentTypeHandler';
5-
import { fetchClient } from './FetchClient';
63
import { validateBasicStatusCodes } from '../handler/ValidateBasicStatusCodeHandler';
4+
import { jsonContentTypeValidator } from '../handler/ValidateContentTypeHandler';
5+
import { fetchClient } from './FetchClient';
76
import { HttpResponse } from './HttpResponse';
87

9-
/**
10-
* A {@link FetchResponseHandler} that verify that the content type of a {@link Response} is JSON
11-
* using the `content-type` response HTTP header.
12-
*
13-
* See {@link validateContentType} for the content type validation.
14-
*
15-
* @param response The {@link Response} to validate
16-
*/
17-
export const jsonContentTypeValidator: FetchResponseHandler = (
18-
response: Response,
19-
) => validateContentType(response, 'json');
20-
218
/**
229
* A {@link HttpClient} that executes an {@link HttpRequest} that returns JSON responses.
2310
* It uses {@link fetchClient} to executes the {@link HttpRequest}, and then it uses the following handlers:
2411
* 1. {@link validateBasicStatusCodes}
2512
* 2. {@link jsonContentTypeValidator}
2613
* 3. {@link toJsonResponse}
2714
*/
15+
// eslint-disable-next-line import/prefer-default-export
2816
export const defaultJsonFetchClient = <T>(httpRequest: HttpRequest<unknown>)
2917
: Promise<HttpResponse<T>> => fetchClient(
3018
httpRequest, validateBasicStatusCodes, jsonContentTypeValidator, toJsonResponse,

src/lib/handler/ValidateContentTypeHandler.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Logger } from 'simple-logging-system';
22
import { genericError, toErrorResponsePromise } from '../client/HttpResponse';
3+
import { FetchResponseHandler } from './FetchResponseHandlers';
34

45
const logger = new Logger('ValidateContentTypeHandler');
56

@@ -14,7 +15,6 @@ const logger = new Logger('ValidateContentTypeHandler');
1415
* Note that the `jsonContentType` can be a partial type. For example if `searchContentType = 'json'`,
1516
* then a response with the header `application/vnd.myapp.type.v1+json` will be valid.
1617
*/
17-
// eslint-disable-next-line import/prefer-default-export
1818
export const validateContentType = (
1919
response: Response,
2020
searchContentType: string,
@@ -28,3 +28,15 @@ export const validateContentType = (
2828

2929
return undefined;
3030
};
31+
32+
/**
33+
* A {@link FetchResponseHandler} that verify that the content type of a {@link Response} is JSON
34+
* using the `content-type` response HTTP header.
35+
*
36+
* See {@link validateContentType} for the content type validation.
37+
*
38+
* @param response The {@link Response} to validate
39+
*/
40+
export const jsonContentTypeValidator: FetchResponseHandler = (
41+
response: Response,
42+
) => validateContentType(response, 'json');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { MultipartHttpRequest } from 'simple-http-request-builder';
2+
import { HttpResponse } from '../client/HttpResponse';
3+
import { toJsonResponse } from '../handler/ResponseJsonHandler';
4+
import { validateBasicStatusCodes } from '../handler/ValidateBasicStatusCodeHandler';
5+
import { jsonContentTypeValidator } from '../handler/ValidateContentTypeHandler';
6+
import { multipartHttpFetchClient } from './MultipartHttpClient';
7+
8+
export default function defaultJsonMultipartFetchClient<T>(
9+
httpRequest: MultipartHttpRequest<unknown>,
10+
): Promise<HttpResponse<T>> {
11+
return multipartHttpFetchClient(
12+
httpRequest,
13+
validateBasicStatusCodes,
14+
jsonContentTypeValidator,
15+
toJsonResponse,
16+
);
17+
}

0 commit comments

Comments
 (0)