@ronas-it/rtkq-entity-api
TypeScript icon, indicating that this package has built-in type declarations

0.2.4 • Public • Published

RTK Query Entity API

Wrapper utilities for CRUD operations with REST APIs entities using RTK Query.

Usage

  1. Install the package:
npm i @ronas-it/rtkq-entity-api
  1. Create base query with your API configuration, for example using Axios:
import axios from 'axios';
import { createApiCreator, createAxiosBaseQuery } from '@ronas-it/rtkq-entity-api';

const axiosBaseQuery = createAxiosBaseQuery({
  getHttpClient: () => axios.create({ baseURL: 'https://your-api-url.com' }),
});

export const createAppApi = createApiCreator({
  baseQuery: axiosBaseQuery,
});
  1. Now you can generate your entity APIs with this creator:
import { createEntityApi } from '@ronas-it/rtkq-entity-api';
import { createAppApi } from 'your-project/utils';
import { User } from 'your-project/models';

export const usersApi = createEntityApi({
  entityName: 'user', // An entity name. Must by unique
  entityConstructor: User, // The entity model class constructor. Supports class-transformer
  baseApiCreator: createAppApi, // The api creator that shares configuration for new APIs
  baseEndpoint: '/users', // Endpoint, relative to base URL configured in the API creator
  omitEndpoints: ['create', 'update', 'delete'], // Allow only 'get' and 'search' methods
});
  1. Use the api you created as usual one created by RTK Query

TODOs

  1. Extend Readme
  2. Add code documentation and examples
  3. Remove peer-dependencies from axios-observable and luxon

Package Sidebar

Install

npm i @ronas-it/rtkq-entity-api

Weekly Downloads

17

Version

0.2.4

License

MIT

Unpacked Size

266 kB

Total Files

9

Last publish

Collaborators

  • ipakhomov
  • eleonov
  • astorozhevsky
  • opikhnenko
  • dmitryusenko