Skip to content

Commit e0abefa

Browse files
committed
💡 Add comments & examples to properties in FullRequestConfig type
1 parent 05f69c2 commit e0abefa

File tree

1 file changed

+95
-3
lines changed
  • packages/@ackee/antonio-core/src

1 file changed

+95
-3
lines changed

packages/@ackee/antonio-core/src/types.ts

+95-3
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,109 @@ export type RequestUriParams = Params;
3535
export type RequestSearchParams = URLSearchParams | Params;
3636

3737
export interface FullRequestConfig extends Omit<RequestInit, 'body' | 'headers' | 'method'> {
38-
// `baseURL` will be prepended to `url` unless `url` is absolute.
39-
// It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
40-
// to methods of that instance.
38+
/**
39+
* `baseURL` will be prepended to `url` unless `url` is absolute.
40+
* It can be convenient to set `baseURL` for an instance of antonio to pass relative URLs.
41+
*/
4142
baseURL: string;
4243

44+
/**
45+
* It defines the format of the returned `data` property in the request result object (`RequestResult`):
46+
* - `json` -> `data === await response.json()`
47+
* - `text` -> `data === await response.text()`
48+
* - ...
49+
*
50+
* #### Default behavior based on `Content-Type` header
51+
*
52+
* Antonio selects the `responseDataType` by default based on the response `Content-Type` header (its mime type).
53+
* Based on that it chooses method for formatting the body:
54+
* - `Content-Type: application/json` refers to `response.json()` method.
55+
* - `text/*` -> `response.text()` method.
56+
* - `image/*`, `audio/*`, `video/*`, `application/octet-stream` -> `response.arrayBuffer()`.
57+
* - `multipart/form-data` -> `response.formData()`
58+
*
59+
* Without `Content-Type` or without explicitly defined `responseDataType`, it returns `data` as `null`.
60+
*
61+
* #### Exception with `Content-Length` header
62+
*
63+
* Note that if the `Content-Length` header is `'0'`, then formatting body is skip completely and `data` are `null`.
64+
*
65+
* #### Streaming the response
66+
* Selecting `iterableStream` response data type fetches data with `ReadableStream`.
67+
* The `data` is going to be async generator that yields out slices of data.
68+
* If the `Content-Type` response header has `application/json` mime type,
69+
* then the received chunk of string from the stream is going to be parsed as json
70+
* and the `slice` and once a valid json is created, it's yielded out as the `slice`.
71+
*
72+
* __Note that both examples are functionally identical.__
73+
*
74+
* _#1 Using async generators:_
75+
* @example
76+
* ```js
77+
* async function* fetchPosts() {
78+
* const { data } = yield* api.get('/posts', {
79+
* responseDataType: 'iterableStream',
80+
* });
81+
*
82+
* for await (const slice of data) {
83+
* console.log(slice) // -> [{ title: '1' }, { title: '2' }, ...]
84+
* }
85+
* }
86+
* ```
87+
*
88+
* _#2 Using sync generators:_
89+
* @example
90+
* ```js
91+
* import { runIterableStream } from `@ackee/antonio-core`;
92+
*
93+
* function* fetchPosts() {
94+
* const { data } = yield* api.get('/posts', {
95+
* responseDataType: 'iterableStream',
96+
* });
97+
*
98+
* yield runIterableStream(data, function*(slice) {
99+
* console.log(slice) // -> [{ title: '1' }, { title: '2' }, ...]
100+
* });
101+
* }
102+
* ```
103+
*/
43104
responseDataType: ResponseDataType;
44105

106+
/**
107+
* @example
108+
* ```
109+
* const { data } = yield* api.get('/user/:id', {
110+
* uriParams: { id: '1' }
111+
* });
112+
*
113+
* console.assert(data.id === '1');
114+
* ```
115+
*/
45116
uriParams: RequestUriParams;
46117

47118
headers: RequestHeaders;
48119

120+
/**
121+
* An instace of `URLSearchParams` or a plain object.
122+
* @example
123+
* ```
124+
* api.get('/posts', {
125+
* params: new URLSearchParams({
126+
* page: 1,
127+
* limit: 20,
128+
* })
129+
* });
130+
* ```
131+
* @example
132+
* ```
133+
* api.get('/posts', {
134+
* params: {
135+
* page: 1,
136+
* limit: 20,
137+
* }
138+
* });
139+
* ```
140+
*/
49141
params: RequestSearchParams;
50142
}
51143

0 commit comments

Comments
 (0)