|
2 | 2 | <p align="center"> |
3 | 3 | <br><br> |
4 | 4 | <img src="https://leaf-docs.netlify.app/images/logo.png" height="100"/> |
5 | | - <h1 align="center">Leaf HTTP Fetch</h1> |
| 5 | + <h1 align="center">Fetch</h1> |
6 | 6 | <br><br> |
7 | 7 | </p> |
8 | 8 |
|
@@ -129,6 +129,183 @@ Fetch::delete("/todos/10"); |
129 | 129 | // ... |
130 | 130 | ``` |
131 | 131 |
|
| 132 | +### request |
| 133 | + |
| 134 | +As you've seen earlier, the fetch class also provides a `request` method which is also used under the hood by the `fetch` function. `request` allows you to manually build up your request object with whatever data you need. |
| 135 | + |
| 136 | +```php |
| 137 | +use Leaf\Fetch; |
| 138 | + |
| 139 | +$res = Fetch::request([ |
| 140 | + "method" => "GET", |
| 141 | + "url" => "https://jsonplaceholder.typicode.com/todos", |
| 142 | +]); |
| 143 | + |
| 144 | +echo json_encode($res->data); |
| 145 | +``` |
| 146 | + |
| 147 | +### Request object |
| 148 | + |
| 149 | +This is the array which is used to construct the request to be sent. The available fields are: |
| 150 | + |
| 151 | +```php |
| 152 | +[ |
| 153 | + // `url` is the server URL that will be used for the request |
| 154 | + "url" => null, |
| 155 | + |
| 156 | + // `method` is the request method to be used when making the request |
| 157 | + "method" => "GET", // default |
| 158 | + |
| 159 | + // `baseURL` will be prepended to `url` unless `url` is absolute. |
| 160 | + // It can be convenient to set `baseURL` for an instance of axios to pass relative URLs |
| 161 | + // to methods of that instance. |
| 162 | + "baseUrl" => "", |
| 163 | + |
| 164 | + // `transformRequest` allows changes to the request data before it is sent to the server |
| 165 | + // This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE' |
| 166 | + // The last function in the array must return a string or an instance of Buffer, ArrayBuffer, |
| 167 | + // FormData or Stream |
| 168 | + // You may modify the headers object. |
| 169 | + // "transformRequest" => function ($data, $headers) { |
| 170 | + // // Do whatever you want to transform the data |
| 171 | + |
| 172 | + // return $data; |
| 173 | + // }, |
| 174 | + |
| 175 | + // `transformResponse` allows changes to the response data to be made before |
| 176 | + // it is passed to then/catch |
| 177 | + // "transformResponse" => function ($data) { |
| 178 | + // // Do whatever you want to transform the data |
| 179 | + |
| 180 | + // return $data; |
| 181 | + // }, |
| 182 | + |
| 183 | + // `headers` are custom headers to be sent |
| 184 | + "headers" => [], |
| 185 | + |
| 186 | + // `params` are the URL parameters to be sent with the request |
| 187 | + // Must be a plain object or a URLSearchParams object |
| 188 | + "params" => [], |
| 189 | + |
| 190 | + // `paramsSerializer` is an optional function in charge of serializing `params` |
| 191 | + // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/) |
| 192 | + // "paramsSerializer" => function ($params) { |
| 193 | + // return Qs.stringify($params, ["arrayFormat" => "brackets"]); |
| 194 | + // }, |
| 195 | + |
| 196 | + // `data` is the data to be sent as the request body |
| 197 | + // Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH' |
| 198 | + // When no `transformRequest` is set, must be of one of the following types: |
| 199 | + // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams |
| 200 | + // - Browser "only" => FormData, File, Blob |
| 201 | + // - Node "only" => Stream, Buffer |
| 202 | + "data" => [], |
| 203 | + |
| 204 | + // `timeout` specifies the number of milliseconds before the request times out. |
| 205 | + // If the request takes longer than `timeout`, the request will be aborted. |
| 206 | + "timeout" => 0, // default is `0` (no timeout) |
| 207 | + |
| 208 | + // `withCredentials` indicates whether or not cross-site Access-Control requests |
| 209 | + // should be made using credentials |
| 210 | + "withCredentials" => false, // default |
| 211 | + |
| 212 | + // `adapter` allows custom handling of requests which makes testing easier. |
| 213 | + // Return a promise and supply a valid response (see lib/adapters/README.md). |
| 214 | + // "adapter" => function ($config) { |
| 215 | + // /* ... */ |
| 216 | + // }, |
| 217 | + |
| 218 | + // `auth` indicates that HTTP Basic auth should be used, and supplies credentials. |
| 219 | + // This will set an `Authorization` header, overwriting any existing |
| 220 | + // `Authorization` custom headers you have set using `headers`. |
| 221 | + // Please note that only HTTP Basic auth is configurable through this parameter. |
| 222 | + // For Bearer tokens and such, use `Authorization` custom headers instead. |
| 223 | + "auth" => [], |
| 224 | + |
| 225 | + // `responseType` indicates the type of data that the server will respond with |
| 226 | + // options "are" => 'arraybuffer', 'document', 'json', 'text', 'stream' |
| 227 | + // browser "only" => 'blob' |
| 228 | + "responseType" => "json", // default |
| 229 | + |
| 230 | + // `responseEncoding` indicates encoding to use for decoding responses (Node.js only) |
| 231 | + // "Note" => Ignored for `responseType` of 'stream' or client-side requests |
| 232 | + "responseEncoding" => "utf8", // default |
| 233 | + |
| 234 | + // `xsrfCookieName` is the name of the cookie to use as a value for xsrf token |
| 235 | + "xsrfCookieName" => "XSRF-TOKEN", // default |
| 236 | + |
| 237 | + // `xsrfHeaderName` is the name of the http header that carries the xsrf token value |
| 238 | + "xsrfHeaderName" => "X-XSRF-TOKEN", // default |
| 239 | + |
| 240 | + // `onUploadProgress` allows handling of progress events for uploads |
| 241 | + // browser only |
| 242 | + // "onUploadProgress" => function ($progressEvent) { |
| 243 | + // // Do whatever you want with the native progress event |
| 244 | + // }, |
| 245 | + |
| 246 | + // `onDownloadProgress` allows handling of progress events for downloads |
| 247 | + // browser only |
| 248 | + // "onDownloadProgress" => function ($progressEvent) { |
| 249 | + // // Do whatever you want with the native progress event |
| 250 | + // }, |
| 251 | + |
| 252 | + // `maxContentLength` defines the max size of the http response content in bytes allowed in node.js |
| 253 | + "maxContentLength" => 2000, |
| 254 | + |
| 255 | + // `maxBodyLength` (Node only option) defines the max size of the http request content in bytes allowed |
| 256 | + "maxBodyLength" => 2000, |
| 257 | + |
| 258 | + // `validateStatus` defines whether to resolve or reject the promise for a given |
| 259 | + // HTTP response status code. If `validateStatus` returns `true` (or is set to `null` |
| 260 | + // or `undefined`), the promise will be resolved; otherwise, the promise will be |
| 261 | + // rejected. |
| 262 | + // "validateStatus" => function ($status) { |
| 263 | + // return $status >= 200 && $status < 300; // default |
| 264 | + // }, |
| 265 | + |
| 266 | + // `maxRedirects` defines the maximum number of redirects to follow in node.js. |
| 267 | + // If set to 0, no redirects will be followed. |
| 268 | + "maxRedirects" => 5, // default |
| 269 | + |
| 270 | + // `socketPath` defines a UNIX Socket to be used in node.js. |
| 271 | + // e.g. '/var/run/docker.sock' to send requests to the docker daemon. |
| 272 | + // Only either `socketPath` or `proxy` can be specified. |
| 273 | + // If both are specified, `socketPath` is used. |
| 274 | + "socketPath" => null, // default |
| 275 | + |
| 276 | + // `proxy` defines the hostname, port, and protocol of the proxy server. |
| 277 | + // You can also define your proxy using the conventional `http_proxy` and |
| 278 | + // `https_proxy` environment variables. If you are using environment variables |
| 279 | + // for your proxy configuration, you can also define a `no_proxy` environment |
| 280 | + // variable as a comma-separated list of domains that should not be proxied. |
| 281 | + // Use `false` to disable proxies, ignoring environment variables. |
| 282 | + // `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and |
| 283 | + // supplies credentials. |
| 284 | + // This will set an `Proxy-Authorization` header, overwriting any existing |
| 285 | + // `Proxy-Authorization` custom headers you have set using `headers`. |
| 286 | + // If the proxy server uses HTTPS, then you must set the protocol to `https`. |
| 287 | + "proxy" => [], |
| 288 | + |
| 289 | + // `decompress` indicates whether or not the response body should be decompressed |
| 290 | + // automatically. If set to `true` will also remove the 'content-encoding' header |
| 291 | + // from the responses objects of all decompressed responses |
| 292 | + // - Node only (XHR cannot turn off decompression) |
| 293 | + "decompress" => true, // default |
| 294 | + |
| 295 | + // If false, fetch will try to parse json responses |
| 296 | + "rawResponse" => false, |
| 297 | + |
| 298 | + // CURLOPT_SSL_VERIFYHOST accepts only 0 (false) or 2 (true). |
| 299 | + // Future versions of libcurl will treat values 1 and 2 as equals |
| 300 | + "verifyHost" => true, // default |
| 301 | + |
| 302 | + "verifyPeer" => true, // default |
| 303 | + |
| 304 | + // Set additional options for curl. |
| 305 | + "curl" => [], |
| 306 | +]; |
| 307 | +``` |
| 308 | + |
132 | 309 | ## View Leaf's docs [here](https://leafphp.netlify.app/#/) |
133 | 310 |
|
134 | 311 | Built with ❤ by [**Mychi Darko**](https://mychi.netlify.app) |
0 commit comments