2.0.2 • Published 5 months ago

react-courier v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

React-courier

This package made your requests much easier by using Tanstack-query and Axios inside.

Installation

Install react-courier with npm

  npm install react-courier

or Install react-courier with yarn

  yarn add react-courier

Usage/Examples

At first, we should use a provider for our global config on the entire project.

CourierProvider

import { CourierProvider } from 'react-courier'

function App() {
  return (
    <CourierProvider defaultBaseUrl={API_BASE_URL}>
      <Component />
    </CourierProvider>
  )
}

CourierProvider Props

Our CourierProvider takes some props for better usage and your custom config.

propstypeDescription
defaultBaseUrlstringdefault base URL for your request *
otherBaseUrlobjectkey values for others baseUrls in case you need
defaultOptionsobjectglobal config for requests, that takes an object with different properties that you can see here
middlewarefunctionbefore running DTO on the requests this callback runs

defaultOptions properties

  • axiosAgentConfig: all the axios defaults configs except headers timeout and baseUrls, for more explanation check out here
  • errorDto: This function gets the request errors and customize it all over the project by using react-courier.
  • headers: Headers are key-value pairs that provide metadata about the request being made. They can include information such as the content type of the request, authentication credentials, and more. here you can wrtie all of the common headers.

  • mutations: All the configs exist in the tanstack-query defaultOption for the mutations hooks part can be used in this object.

  • queries: All the configs exist in the tanstack-query defaultOption for the query hooks part can be used in this object. for more info take a look at tanstack documantation here
  • timeout: Timeouts can be set in Axios using the timeout configuration option. This option specifies the number of seconds before the request times out.

CreateApi

CreateApi is a function for making requests inside components.

Explanation:

for making requests inside components. we should use the CreateApi function that takes one argument as an object and return a React-Hooks. for a different request method that we give inside CreateApi parameters, the react-hook could be useMutation or useQuery from Tanstack-query.

Code Example:

import { CreateApi } from 'react-courier'

const useGETPosts = CreateApi({
  endPoint: () => `/YOUR_END_POINT`,
  name: () => ['YOUR_REQUEST_NAME'],
  method: 'GET',
})

function Component() {
  const {data,isLoading,isError,error} = useGETPosts()
  if(isError) return <p>{error.message}</p>
  if(isLoading) return <p>Loading...</p>
  return <OtherComponent data={data} />
}

CreateApi parameter object properties:

  • endPoint: EndPoitn of your request that is a string or function if the URL includes a dynamic parameter.
  • name:The name of your request that gives the ability for caching your request. This property can be an array or a function that gets dynamicEndpoint values and dynamic queryParameters as argument. (for dynamic caching discussion) and it returns an array.
  • method: All of the rest api Methods: GET, POST, PUT, DELETE,PATCH.
  • queryParams: ...
  • requestData: ...
  • dto: ...
  • axiosAgentConfig: ...
  • baseUrl: the key of otherBaseUrl config in courierProvider. as a default it sets to defaultBaseUrl in courierProvider.
  • headers: Headers are key-value pairs that provide metadata about the request being made. They can include information such as the content type of the request, authentication credentials, and more. here you can wrtie all of the specific request headers.
  • options: ...
  • timeout: Timeouts can be set in Axios using the timeout configuration option. This option specifies the number of seconds before the request times out. and its only for this request.
2.0.2

5 months ago

2.0.1

5 months ago

2.0.0

6 months ago

1.2.8

9 months ago

1.2.7

9 months ago

1.2.6

9 months ago

1.2.5

11 months ago

1.3.0

9 months ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago