HttpOptions
- second argument of the http-pro request is an object which can have following properties.
All properties of second parameter of Fetch API.
baseUrlType :
string | URLbaseUrlwill be prepended tourlunlessurlis absolute.tip
It can be convenient to set
baseUrlfor an instance of http-pro and then pass relative URLs to methods of that instance.
- You can find detailed example Here.
searchParams- Type :
string | object<string, string | number | boolean> | Array<Array<string | number | boolean>> | URLSearchParams | undefined - it will append all the searchParams to url we passed.
- it will accept all the values supported by URLSearchParams.
- Type :
const res = await hp.get('https://www.x.com', {
searchParams: { name: 'pranshu', age: 25 },
}); // url : https://www.x.com/?name=pranshu&age=25
const res = await hp.get('https://www.x.com?lastname=shah', {
searchParams: { name: 'pranshu', age: 25 },
}); // url : https://www.x.com/?lastname=shah&name=pranshu&age=25
timeout- Type :
number|undefined timeoutindicates the number of milliseconds before the request times out. default is no timeout.
- Type :
validateStatus- Type :
(status: number) => boolean validateStatusdetermines whether to throw HttpProError for given status code.- You can find detailed examples Here.
- Type :
data- Type : any value accepted by
JSON.stringify(). - if you are working with json data instead of using
{body:JSON.stringify({name:"value"})}just use{data:{name:"value"}}which will set json object inbodywith appropriate headers.
- Type : any value accepted by
interceptorsinterceptorsallows you to run custom logic before sending the request, before throwing error or after you get the response. interceptors functions can be sync or async.
interceptors.beforeRequestinterceptors.beforeError- Type :
beforeError : (error:unkown,request:Request) => void | Promise<void> - The
beforeErrorfunction is called if there a any error while executing a request, the validateStatus function returns false, or if the validateStatus function does not exist and the status code is not in the range of 200-299. The beforeError function receives the error and Request object as an argument.
- Type :
interceptors.afterResponseType :
afterResponse : ( response: Response, request: Request) => Response | Promise<Response>.afterResponsewill run just after Response is resolved. function will receive Response and Request object as an arguements.note
Here,
afterResponsewon't be called if the validateStatus function returns false, or if the validateStatus function does not exist and the status code is not in the range of 200-299.
- You can detailed examples Here.
responseType- Type :
'arrayBuffer' | 'blob' | 'json' | 'text' | 'formData' | undefined - default :
json. responseTypeindicates that what will you from server Response and based on that it will decide what to put indataproperty of HpResponse.
note
If Error occurs while parsing the response-data,
datawill have empty object.- Type :
example
const res = await hp.get('someUrl.com', { responseType: 'blob' });
console.log(res.data); // will be blob.const res = await hp.get('someUrl.com', { responseType: 'arrayBuffer' });
console.log(res.data); // will be arrayBuffer.const res = await hp.get('someUrl.com');
console.log(res.data); // will be parsed json
validationSchema- schema you want to validate your response data against.
- Type :
Record<string, any>
validationOptions- options for validation function if you are using one of the @http-pro/validator function for validation.
- options for validation function if you are using one of the @http-pro/validator function for validation.
validationFunction- Type
<ResponseType extends any = any>(
data: ResponseType,
options?: HPValidationOptions,
schema?: Record<string, any>
) => Promise<ResponseType>;- function you want to validate your response data against. it will recive
data,optionsandschemaas arguments.