1.0.17 • Published 9 months ago

@rtkcd/mf-connect v1.0.17

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

Generate RTK Query hooks with injection to store

This is a wrapper over the @rtk-query/codegen-openapi library, with automatic injection to store

Install

npm i @npm.piece/generate-hooks-rtk -D
yarn add @npm.piece/generate-hooks-rtk -D

Run command

npm.piece-gen-rtk-hooks

Create config file openapi-config.ts near package.json:

import type { ConfigFile } from '@npm.piece/generate-hooks-rtk/types/export'

// Выбираем куда добавлять хуки
const getName = (name: string) => `./src/features/generated/hooks/${name}.ts`
const setOrderName = (name: string) => `${name}Store`
const setFilterName = (name: string) => [new RegExp(name, 'i')]

const getControllers = (names: string[]) =>
  names.reduce(
    (acc, name) => ({
      ...acc,
      [getName(name)]: {
        filterEndpoints: setFilterName(name),
        exportName: setOrderName(name),
        tag: name,
      },
    }),
    {}
  )

const controllers = ['pet', 'order', 'user']
const config: ConfigFile = {
  schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json',
  // schemaFile: './swagger.json',
  apiFile: '@app/api/RTK',
  tag: true,
  hooks: true,
  // outputFile: getName('api'),
  outputFiles: { ...getControllers(controllers) },
}

export default config

Create config file .config.openapi.js near package.json:

const path = require('path')

const createPath = (pathName) => path.resolve(__dirname, pathName)

const configFile = {
  // указываем путь к папке, в которой находятся файлы с хуками
  hooksFolder: createPath('./src/features/generated/hooks/'),
  // файл куда заберутся все импорты хуков
  exportFile: createPath('./src/features/generated/hooks.ts'),
  // приписка к импорту для файла хуков
  hooksImportFileSuffix: './hooks/',
  // путь до reducers файла
  reducersImportDestination: createPath(
    './src/app/store/generated/reducers.ts'
  ),
  // путь до middleware файла
  middlewaresImportDestination: createPath(
    './src/app/store/generated/middlewares.ts'
  ),
  // путь для импорта из файла reducers
  reducersImportFileSuffix: '@generated/hooks/',
  // путь для импорта из файла middlewares
  middlewaresImportFileSuffix: '@generated/hooks/',
  // название объекта куда вставлять импорты в файле reducers
  reducersObjectName: 'generatedReducersList',
  // название объекта куда вставлять импорты в файле middlewares
  middlewaresObjectName: 'generatedMiddlewaresList',
  // не обязательное поле, которое при наличии вызовет eslint fix в конце
  eslintCommand: 'yarn run eslint'
}

module.exports={
  configFile
}
1.0.17

9 months ago

1.0.16

9 months ago

1.0.15

9 months ago

1.0.13

9 months ago

1.0.12

9 months ago

1.0.11

9 months ago

1.0.10

10 months ago

1.0.9

10 months ago