Skip to content

Commit 1b5ff91

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

5 files changed

Lines changed: 39 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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export {
1515
export type {
1616
MultipartHttpFetchClient,
1717
} from './lib/multipart/MultipartHttpClient';
18+
export {
19+
defaultJsonMultipartFetchClient,
20+
} from './lib/multipart/JsonMultipartHttpClient';
1821
// handlers
1922
export {
2023
validateBasicStatusCodes,
@@ -51,7 +54,6 @@ export {
5154
} from './lib/client/HttpResponse';
5255
// custom fetch clients
5356
export {
54-
jsonContentTypeValidator,
5557
defaultJsonFetchClient,
5658
} from './lib/client/JsonFetchClient';
5759
export {
@@ -76,3 +78,4 @@ export { SynchronizedHttpPromise } from './lib/promise/SynchronizedHttpPromise';
7678

7779
// TODO to remove in the next future major release 3.x.x
7880
export { validateContentType as contentTypeValidator } from './lib/handler/ValidateContentTypeHandler';
81+
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
// eslint-disable-next-line import/prefer-default-export
9+
export function defaultJsonMultipartFetchClient<T>(
10+
httpRequest: MultipartHttpRequest<unknown>,
11+
): Promise<HttpResponse<T>> {
12+
return multipartHttpFetchClient(
13+
httpRequest,
14+
validateBasicStatusCodes,
15+
jsonContentTypeValidator,
16+
toJsonResponse,
17+
);
18+
}

0 commit comments

Comments
 (0)