Skip to content

Commit aff35d2

Browse files
committed
docs: adding sample clients
1 parent 173cb12 commit aff35d2

File tree

7 files changed

+159
-6
lines changed

7 files changed

+159
-6
lines changed

docs/usage/clients.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Clients Reference
22

3-
TODO: provide pre-made HTTP client factories
3+
Different projects have slightly different requirements regarding the error handling, request and response processing and other logic.
4+
These requirements should be implemented as an `HTTPClient` instance, which in turn is provided to the generated controllers.
45

5-
For developing a custom `HTTPClient`, please refer to [Developers Guide - HTTP clients](../development/clients.md).
6+
Resources:
7+
- For developing a custom `HTTPClient`, please refer to [Developers Guide - HTTP clients](../development/clients.md);
8+
- The repository contains a few [examples](../../examples/clients) of HTTPClient implementation for common libraries;

examples/clients/fetch.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { HTTPClient1 } from '../out/petstore.yaml/client/client';
2+
import { MonadThrow1 } from 'fp-ts/MonadThrow';
3+
import { pipe } from 'fp-ts/function';
4+
import { record } from 'fp-ts';
5+
6+
export const URI = 'Promise' as const;
7+
export type URI = typeof URI;
8+
9+
declare module 'fp-ts/HKT' {
10+
interface URItoKind<A> {
11+
readonly [URI]: Promise<A>;
12+
}
13+
}
14+
15+
export const monadPromise: MonadThrow1<URI> = {
16+
URI,
17+
of: Promise.resolve,
18+
throwError: Promise.reject,
19+
map: (fa, f) => fa.then(a => Promise.resolve(f(a))),
20+
ap: (fab, fa) => fab.then(fn => fa.then(a => fn(a))),
21+
chain: (fa, f) => fa.then(f),
22+
};
23+
24+
export const fetchClient: HTTPClient1<URI> = {
25+
...monadPromise,
26+
request: ({ body, headers, method, query, url }) =>
27+
fetch(query ? `${url}?${query}` : url, {
28+
body: body && JSON.stringify(body),
29+
headers:
30+
headers &&
31+
pipe(
32+
headers,
33+
record.map(val => {
34+
if (Array.isArray(val)) {
35+
return val.join(',');
36+
} else {
37+
return String(val);
38+
}
39+
}),
40+
),
41+
method,
42+
}),
43+
};

examples/clients/live-data.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { HTTPClient2 } from '../out/petstore.yaml/client/client';
2+
import { Monad } from 'fp-ts-rxjs/Observable';
3+
import { Observable, of } from 'rxjs';
4+
import { ajax } from 'rxjs/ajax';
5+
import { catchError, map, startWith, switchMap, take } from 'rxjs/operators';
6+
import { failure, pending, RemoteData, success } from '@devexperts/remote-data-ts';
7+
import { getRemoteDataM } from '@devexperts/remote-data-ts/dist/remote-data-t';
8+
import { MonadThrow2 } from 'fp-ts/MonadThrow';
9+
10+
export const URI = 'LiveData' as const;
11+
export type URI = typeof URI;
12+
export type LiveData<E, A> = Observable<RemoteData<E, A>>;
13+
14+
declare module 'fp-ts/HKT' {
15+
interface URItoKind2<E, A> {
16+
readonly [URI]: LiveData<E, A>;
17+
}
18+
}
19+
20+
const remoteDataMonad: MonadThrow2<URI> = {
21+
...getRemoteDataM(Monad),
22+
URI,
23+
throwError: e => of(failure(e)),
24+
};
25+
26+
export const liveDataClient: HTTPClient2<URI> = {
27+
...remoteDataMonad,
28+
request: ({ body, headers, method, query, url }) =>
29+
ajax({
30+
body: body && JSON.stringify(body),
31+
url: query ? `${url}?${query}` : url,
32+
headers,
33+
method,
34+
}).pipe(
35+
map(response => success(response.response)),
36+
catchError(remoteDataMonad.throwError),
37+
startWith(pending),
38+
),
39+
};
40+
41+
export const liveDataClientWithAuth = (token$: Observable<string>): HTTPClient2<URI> => ({
42+
...liveDataClient,
43+
request: req =>
44+
token$.pipe(
45+
take(1),
46+
switchMap(token =>
47+
liveDataClient.request({
48+
...req,
49+
headers: {
50+
...req.headers,
51+
Authorization: `Bearer ${token}`,
52+
},
53+
}),
54+
),
55+
),
56+
});

examples/clients/observable.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { HTTPClient1 } from '../out/petstore.yaml/client/client';
2+
import { URI, Monad } from 'fp-ts-rxjs/Observable';
3+
import { throwError } from 'rxjs';
4+
import { ajax } from 'rxjs/ajax';
5+
import { map } from 'rxjs/operators';
6+
7+
export const rxjsClient: HTTPClient1<URI> = {
8+
...Monad,
9+
throwError,
10+
request: ({ body, headers, method, query, url }) =>
11+
ajax({
12+
body: body && JSON.stringify(body),
13+
url: query ? `${url}?${query}` : url,
14+
headers,
15+
method,
16+
}).pipe(map(response => response.response)),
17+
};

examples/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
"license": "MPL-2.0",
77
"private": true,
88
"dependencies": {
9+
"@devexperts/remote-data-ts": "^2.0.5",
910
"fp-ts": "^2.10.5",
11+
"fp-ts-rxjs": "^0.6.15",
1012
"io-ts": "^2.2.16",
1113
"io-ts-types": "^0.5.16",
14+
"rxjs": "^6.6.7",
1215
"typescript": "^4.2.4"
1316
},
1417
"devDependencies": {
1518
"@devexperts/swagger-codegen-ts": "^2.0.0-alpha.24",
1619
"fs-extra": "^10.0.0",
1720
"ts-node": "^9.1.1"
21+
},
22+
"scripts": {
23+
"generate": "ts-node generate/01-single-spec.ts"
1824
}
1925
}

examples/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"lib": ["ES6", "DOM"]
5+
}
6+
}

examples/yarn.lock

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
# yarn lockfile v1
33

44

5+
"@devexperts/remote-data-ts@^2.0.5":
6+
version "2.0.5"
7+
resolved "https://registry.yarnpkg.com/@devexperts/remote-data-ts/-/remote-data-ts-2.0.5.tgz#7a8ec88587ca132e8d608e23abf453423026f63b"
8+
integrity sha512-Eb84KGBFyhGFIK0WCyl7pT0MSOaj+4ZWJLBd7gaW6/GyeMBzpOyZyJls8QIYcpef3hUPx4rH0x2Dy3xq9mUFaw==
9+
dependencies:
10+
fp-ts "^2.0.0"
11+
io-ts "^2.0.0"
12+
io-ts-types "^0.5.7"
13+
tslib "^1.9.3"
14+
515
"@devexperts/swagger-codegen-ts@^2.0.0-alpha.24":
616
version "2.0.0-alpha.24"
717
resolved "https://registry.yarnpkg.com/@devexperts/swagger-codegen-ts/-/swagger-codegen-ts-2.0.0-alpha.24.tgz#47bc13bfa24678d382a3ee8539938200374ad214"
@@ -157,7 +167,12 @@ fast-diff@^1.1.2:
157167
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"
158168
integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==
159169

160-
fp-ts@^2.10.5:
170+
fp-ts-rxjs@^0.6.15:
171+
version "0.6.15"
172+
resolved "https://registry.yarnpkg.com/fp-ts-rxjs/-/fp-ts-rxjs-0.6.15.tgz#4c53ecaf5e18a82c94e4a43a4cf8148978ef9e6c"
173+
integrity sha512-5fD4a/439l0aUkSkPq7xnznXYsD7YwGyYANO3v6dkkZJTta36et3bNJ2tzq28UcVR6bS9nTDbP7kMjgQqFFxYQ==
174+
175+
fp-ts@^2.0.0, fp-ts@^2.10.5:
161176
version "2.10.5"
162177
resolved "https://registry.yarnpkg.com/fp-ts/-/fp-ts-2.10.5.tgz#7c77868fe8bd9b229743303c1bec505b959f631b"
163178
integrity sha512-X2KfTIV0cxIk3d7/2Pvp/pxL/xr2MV1WooyEzKtTWYSc1+52VF4YzjBTXqeOlSiZsPCxIBpDGfT9Dyo7WEY0DQ==
@@ -185,12 +200,12 @@ graceful-fs@^4.1.6, graceful-fs@^4.2.0:
185200
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
186201
integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==
187202

188-
io-ts-types@^0.5.16:
203+
io-ts-types@^0.5.16, io-ts-types@^0.5.7:
189204
version "0.5.16"
190205
resolved "https://registry.yarnpkg.com/io-ts-types/-/io-ts-types-0.5.16.tgz#e9eed75371e217c97050cc507915e8eedc250946"
191206
integrity sha512-h9noYVfY9rlbmKI902SJdnV/06jgiT2chxG6lYDxaYNp88HscPi+SBCtmcU+m0E7WT5QSwt7sIMj93+qu0FEwQ==
192207

193-
io-ts@^2.2.16:
208+
io-ts@^2.0.0, io-ts@^2.2.16:
194209
version "2.2.16"
195210
resolved "https://registry.yarnpkg.com/io-ts/-/io-ts-2.2.16.tgz#597dffa03db1913fc318c9c6df6931cb4ed808b2"
196211
integrity sha512-y5TTSa6VP6le0hhmIyN0dqEXkrZeJLeC5KApJq6VLci3UEKF80lZ+KuoUs02RhBxNWlrqSNxzfI7otLX1Euv8Q==
@@ -255,6 +270,13 @@ prettier@^1.19.1:
255270
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
256271
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==
257272

273+
rxjs@^6.6.7:
274+
version "6.6.7"
275+
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.6.7.tgz#90ac018acabf491bf65044235d5863c4dab804c9"
276+
integrity sha512-hTdwr+7yYNIT5n4AMYp85KA6yw2Va0FLa3Rguvbpa4W3I5xynaBZo41cM3XM+4Q6fRMj3sBYIR1VAmZMXYJvRQ==
277+
dependencies:
278+
tslib "^1.9.0"
279+
258280
simple-swizzle@^0.2.2:
259281
version "0.2.2"
260282
resolved "https://registry.yarnpkg.com/simple-swizzle/-/simple-swizzle-0.2.2.tgz#a4da6b635ffcccca33f70d17cb92592de95e557a"
@@ -292,7 +314,7 @@ ts-node@^9.1.1:
292314
source-map-support "^0.5.17"
293315
yn "3.1.1"
294316

295-
tslib@^1.10.0:
317+
tslib@^1.10.0, tslib@^1.9.0, tslib@^1.9.3:
296318
version "1.14.1"
297319
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
298320
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

0 commit comments

Comments
 (0)