2.1.0 • Published 1 year ago

@personio/request v2.1.0

Weekly downloads
3,425
License
MIT
Repository
-
Last release
1 year ago

@personio/request

A helper library used in Personio UI to perform HTTP requests. It also takes care to include the X-CSRF-Token on every request. Written in TypeScript, uses axios in the background.

Installation

Run

yarn add @personio/request

Usage

In the root of your project, import configureRequest and execute it to provide a configuration to subsequent requests performed using the library:

import { configureRequest } from '@personio/request';

configureRequest({ baseURL: API_BASE_URL, 
                   timeout: REQUEST_TIMEOUT,
                // Default is false. When enabled, returns the data content instead of an object with data as prop.
                // This is useful to avoid something like data.data when mapping the response. 
                   retrieveDataFromResponse: true,
                // Default is false, when enabled, dispatches the SESSION:EXPIRED event.
                // This makes the login modal from the monolith be triggered.  
                   dispatchSessionExpiredEvent: true,
                // Number in milliseconds, default is 0. Adds a delay to the session expired event.
                // Useful for finishing some animations like a closing a modal before it shows the login modal.
                   sessionExpiredDelay: 300 
                });

Perform an actual HTTP request anywhere in the code:

import request from '@personio/request';
 
const response = await request({ method: "GET", url: 'example' }); 

Behind the scenes, request passes the arguments as is to axios. See more about possible options on https://github.com/axios/axios.

With useRequest hook

It's possible to perform HTTP requests directly in React components or create custom React hooks using the useRequest hook provided by the library:

import { useRequest, wasRequestSuccessful } from '@personio/request'; 
import { useCallback, useEffect } from "react";

type DataType = { id: number, value: string };
type MetaType = { example: string };

const useRequestExample = () => {  
  const [
      {
        data = [],
        statusCode,
        hasRequested,
        cancelSource,
        meta,
        isLoading,
        error,
      },
      makeRequest,
      { resetData, setHasNotRequested }
    ] = useRequest<DataType[], MetaType>();
  
  const fetch = useCallback((id: number) =>
    makeRequest(
      {
        method: 'GET',
        url: `example/${id}`,
      }      
    ), [makeRequest]);

  useEffect(() => {
    if (!statusCode) {
      return;
    }

    if (wasRequestSuccessful(statusCode)) {
      // do some action on success
    } else {
      // do some action on fail
    }
  }, [statusCode]);

  return [
   { data, meta, isLoading, error, statusCode, hasRequested, cancelSource },
   { fetch, resetData, setHasNotRequested }
  ] as const;
};

export default useRequestExample

The makeRequest function can also accept a second argument, an object that defines request options:

{
  // prop to indicate if we should clear or persist the data between requests.
  // default: false
   persistData: boolean
}

Testing

Use createMock and mockResponse

import request, { createMock, mockResponse, getRequestAmount } from '@personio/request';
import { render, screen } from '@testing-library/react';

type Response = {
  data: { id: number; text: string }[];
};

const mockAdapter = createMock(request);

const exampleMock = mockResponse({ method: 'GET', url: /example/ });

describe('<Example/>', () => {
  beforeEach(() => {
      exampleMock.reply<Response>(200, { data: [{id: 1, text: 'example'}] });
    });
  afterEach(() => {
      mockAdapter.reset();
  });
 
  it('should render with text "example"', async () => {    
      render(<Example />);
        
      expect(await screen.findByText('example')).toBeInTheDocument();      
  });
  
  it('should call the example API only once"', async () => {
      render(<Example />);
  
      await screen.findByText('example')

      expect(getRequestAmount(mockAdapter.history.get, /example/)).toBe(1);      
  });  
  
});

The axios-mock-adapter with a modified API is used to mock HTTP requests. For more info, check https://github.com/ctimmerm/axios-mock-adapter.

2.1.0

1 year ago

2.0.0

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.39.6

5 years ago

0.39.4

5 years ago

0.39.1

5 years ago

0.39.0

5 years ago

0.38.3

5 years ago

0.37.5

5 years ago

0.37.4

5 years ago

0.37.3

5 years ago

0.37.2

5 years ago

0.37.1

5 years ago

0.32.1-alpha.0

5 years ago

0.36.5

5 years ago

0.36.4

5 years ago

0.36.3

5 years ago

0.36.1

5 years ago

0.36.0

5 years ago

0.35.0

5 years ago

0.31.1

5 years ago

0.33.0

5 years ago

0.31.3-alpha.0

5 years ago

0.31.2-alpha.0

5 years ago

0.30.0

5 years ago

0.29.0

5 years ago

0.27.8-alpha.0

5 years ago

0.27.7-alpha.0

5 years ago

0.27.6-alpha.0

5 years ago

0.27.5-alpha.0

5 years ago

0.27.4-alpha.0

5 years ago

0.27.3-alpha.0

5 years ago

0.27.2-alpha.0

5 years ago

0.27.1-alpha.0

5 years ago

0.25.2

5 years ago

0.25.0

5 years ago

0.24.1

5 years ago

0.24.0

5 years ago

0.23.0

5 years ago

0.22.1

5 years ago

0.22.0

5 years ago

0.21.4

5 years ago

0.21.0

5 years ago

0.20.0

5 years ago

0.19.0

5 years ago

0.18.0

5 years ago

0.16.0

5 years ago

0.15.0

5 years ago

0.14.0

5 years ago

0.13.0

5 years ago

0.12.0

5 years ago

0.9.9

5 years ago

0.9.0

5 years ago

0.8.15

5 years ago

0.8.13

5 years ago

0.8.10

5 years ago

0.8.9

5 years ago

0.8.8

5 years ago

0.8.7

5 years ago

0.8.6

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.13

5 years ago

0.6.13-alpha.0

5 years ago

0.6.2

5 years ago

0.6.2-alpha.0

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.11

5 years ago

0.5.9

6 years ago

0.5.8

6 years ago

0.5.7

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago