Skip to main content

HttpOptions

  • second argument of the http-pro request is an object which can have following properties.
  1. All properties of second parameter of Fetch API.

  2. baseUrl

    • Type : string | URL

    • baseUrl will be prepended to url unless url is absolute.

      tip

      It can be convenient to set baseUrl for an instance of http-pro and then pass relative URLs to methods of that instance.

      note

      If you pass Request or URL object in url, baseUrl property will be ignored.

  • You can find detailed example Here.

  1. 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.
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
  1. timeout

    • Type : number|undefined
    • timeout indicates the number of milliseconds before the request times out. default is no timeout.

  2. validateStatus

    • Type : (status: number) => boolean
    • validateStatus determines whether to throw HttpProError for given status code.
    • You can find detailed examples Here.

  3. 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 in body with appropriate headers.

  4. interceptors

    • interceptors allows 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.

    1. interceptors.beforeRequest

      • Type : beforeRequest?: (request: Request) => Request | Promise<Request>
      • beforeRequest will run just before sending the request. it will receive Request object as an argument and it should return Request object.

    2. interceptors.beforeError

      • Type : beforeError : (error:unkown,request:Request) => void | Promise<void>
      • The beforeError function 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.

    3. interceptors.afterResponse

      • Type : afterResponse : ( response: Response, request: Request) => Response | Promise<Response>.

      • afterResponse will run just after Response is resolved. function will receive Response and Request object as an arguements.

        note

        Here, afterResponse won'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.

  1. responseType

    • Type : 'arrayBuffer' | 'blob' | 'json' | 'text' | 'formData' | undefined
    • default : json.
    • responseType indicates that what will you from server Response and based on that it will decide what to put in data property of HpResponse.

    note

    If Error occurs while parsing the response-data, data will have empty object.

  • 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
  1. validationSchema

    • schema you want to validate your response data against.
    • Type : Record<string, any>

  2. validationOptions

    • options for validation function if you are using one of the @http-pro/validator function for validation.

    1. mode

      • Type : 'sync' | 'async'
      • default : 'async'
      • A string indicating the mode of the validation function.
    2. raw

      • Type : boolean
      • default : true
      • boolean indicating whether the validation function should return the raw data instead of the parsed data.

  3. 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, options and schema as arguments.