@@ -35,17 +35,109 @@ export type RequestUriParams = Params;
35
35
export type RequestSearchParams = URLSearchParams | Params ;
36
36
37
37
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
+ */
41
42
baseURL : string ;
42
43
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
+ */
43
104
responseDataType : ResponseDataType ;
44
105
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
+ */
45
116
uriParams : RequestUriParams ;
46
117
47
118
headers : RequestHeaders ;
48
119
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
+ */
49
141
params : RequestSearchParams ;
50
142
}
51
143
0 commit comments