Skip to content

Commit 6b74d67

Browse files
authored
Merge pull request #1 from Coreoz/create-http-fetch-request
Create http fetch request
2 parents 4bd3cf3 + 6c827cb commit 6b74d67

File tree

6 files changed

+897
-295
lines changed

6 files changed

+897
-295
lines changed

.yarnrc.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ plugins:
22
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
33
spec: "@yarnpkg/plugin-interactive-tools"
44

5+
packageExtensions:
6+
chalk@5.0.1:
7+
dependencies:
8+
"#ansi-styles": npm:ansi-styles@6.1.0
9+
"#supports-color": npm:supports-color@9.2.2
10+
511
yarnPath: .yarn/releases/yarn-3.2.0.cjs

README.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,11 @@ A request builder will be returned, when executed, this request will return a [H
3636

3737
```typescript
3838
restRequest<T>(method: HttpMethod, path: string): HttpRequest<HttpPromise<T>> {
39-
return new HttpRequest<HttpPromise<T>>(
40-
// the base API client
41-
(httpRequest) => new HttpPromise<T>(
42-
// unwrapHttpPromise enables to transforme a Promise<HttpResponse<T>> result to a HttpPromise<T> result
43-
// while defaultJsonFetchClient will connect take care of HTTP exchange and try to parse the result to a JSON T object (the generic parameter that represents the type we are waiting for)
44-
unwrapHttpPromise(defaultJsonFetchClient(httpRequest)),
45-
httpRequest,
46-
),
47-
// the base URL, e.g. https://google.fr/api
48-
baseUrl,
49-
// the method, e.g. HttpMethod.GET
50-
method,
51-
// the path, e.g. /users/123/addresses
52-
path,
39+
return createHttpFetchRequest(
40+
baseUrl, // the base URL, e.g. https://google.fr/api
41+
method, // the method, e.g. HttpMethod.GET
42+
path, // the path, e.g. /users/123/addresses
43+
defaultJsonFetchClient // the base API client
5344
);
5445
}
5546
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-http-rest-client",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A lightweight framework for creating reliable REST HTTP clients in TypeScript",
55
"author": "Aurélien Manteaux <amanteaux@coreoz.com> (https://coreoz.com)",
66
"repository": "https://github.com/coreoz/simple-http-rest-client",
@@ -55,7 +55,7 @@
5555
"jest": "^27.5.1",
5656
"jest-config": "^27.5.1",
5757
"node-fetch": "2",
58-
"release-it": "^14.13.1",
58+
"release-it": "^15.1.0",
5959
"source-map-support": "^0.5.21",
6060
"ts-jest": "^27.1.4",
6161
"ts-node": "^10.7.0",

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// content type validator
22
export { contentTypeValidator } from './lib/client/ContentTypeValidator';
33
// fetch client
4-
export { fetchClientExecutor, networkErrorCatcher, fetchClient } from './lib/client/FetchClient';
4+
export {
5+
fetchClientExecutor,
6+
networkErrorCatcher,
7+
fetchClient,
8+
createHttpFetchRequest,
9+
} from './lib/client/FetchClient';
510
export type {
611
FetchResponseHandler,
12+
HttpFetchClient,
713
} from './lib/client/FetchClient';
814
// fetch status validators
915
export { validateBasicStatusCodes } from './lib/client/FetchStatusValidators';

src/lib/client/FetchClient.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { Logger } from 'simple-logging-system';
2-
import { HttpRequest, HttpClient } from 'simple-http-request-builder';
2+
import {
3+
HttpRequest,
4+
HttpClient,
5+
HttpMethod,
6+
} from 'simple-http-request-builder';
37
import {
48
genericError,
59
HttpResponse,
610
toErrorResponsePromise,
711
networkError,
812
timeoutError,
913
} from './HttpResponse';
14+
import { HttpPromise, unwrapHttpPromise } from '../promise/HttpPromise';
1015

1116
const logger = new Logger('FetchClient');
1217

@@ -113,3 +118,36 @@ export const fetchClient = <T = Response>(httpRequest: HttpRequest<unknown>, ...
113118
return { response };
114119
})
115120
.catch(networkErrorCatcher);
121+
122+
/**
123+
* A {@link HttpClient} that uses {@link HttpRequest} and returns a `Promise<HttpResponse<T>>`.
124+
*
125+
* This is used by {@link createHttpFetchRequest} to make fetch requests.
126+
*
127+
* Common clients are:
128+
* - {@link defaultJsonFetchClient} for REST JSON API
129+
* - raw {@link fetchClient} for non-JSON API (so often just for binary content)
130+
*/
131+
export type HttpFetchClient = <T>(httpRequest: HttpRequest<unknown>) => Promise<HttpResponse<T>>;
132+
133+
/**
134+
* Factory function to create fetch {@link HttpRequest}.
135+
*
136+
* @param baseUrl The base URL. It should not contain an ending slash. A valid base URL is: http://hostname/api
137+
* @param method The HTTP method used for the request, see {@link HttpMethod}
138+
* @param path The path of the endpoint to call, it should be composed with a leading slash
139+
* and will be appended to the {@link HttpRequest#baseUrl}. A valid path is: /users
140+
* @param httpClient The fetch client that uses {@link HttpRequest} and returns a `Promise<HttpResponse<T>>`
141+
*/
142+
export const createHttpFetchRequest = <T>(
143+
baseUrl: string, method: HttpMethod, path: string, httpClient: HttpFetchClient,
144+
)
145+
: HttpRequest<HttpPromise<T>> => new HttpRequest<HttpPromise<T>>(
146+
(httpRequest) => new HttpPromise<T>(
147+
unwrapHttpPromise(httpClient(httpRequest)),
148+
httpRequest,
149+
),
150+
baseUrl,
151+
method,
152+
path,
153+
);

0 commit comments

Comments
 (0)