vineanova-redux-artifacts

1.119.63 • Public • Published

VineaOpenApiReduxArtifacts

Overview

VineaOpenApiReduxArtifacts is a powerful npm library designed to simplify the integration of Swagger APIs into Redux architecture. By leveraging templates (Mustache files), this library automatically generates Redux ducks, actions, sagas, selectors, and API clients, eliminating the need for repetitive boilerplate code for each API request. This streamlined approach enhances code readability and accelerates development for New Zealand's vineyard and livestock industries.

Example Usage

import {
  VineaNovaActions,
  VineaNovaSelectors,
} from 'vineanova-redux-artifacts';

dispatchAPI(
  VineaNovaActions.api.v1.cropBlockVintage.get.request({
    queryParams: {
      VineyardBlockID: 11111,
      VintageID: 1111,
    },
  }),
);

Generated Ducks Actions

const ducksActions = createActions({
  api: {
    v1: {
      cropBlockVintage: {
        get: {
          // ... (generated actions for GET request)
        },
        post: {
          // ... (generated actions for POST request)
        },
        put: {
          // ... (generated actions for PUT request)
        },
        delete: {
          // ... (generated actions for DELETE request)
        },
      },
    },
  },
});

Generated Saga

export function* getCropBlockVintageSagas(action) {
  const {
    payload = {},
    meta: { reducerName },
  } = action;
  try {
    yield put({
      type: "FETCHING",
      name: reducerName,
    });

    const response = yield call(APIServices.getCropBlockVintage, payload);    
    yield put({
      type: "SUCCESS",
      payload: response,
      name: reducerName,
    });

    const CropBlockVintageSchema = new schema.Entity("byId");
    const CropBlockVintageListSchema = new schema.Array(CropBlockVintageSchema);
    const {entities: {byId}, result: allIds} = normalize(response, CropBlockVintageListSchema);
    yield put({
      type: "MERGE_ALL_IDS",
      payload: {byId, allIds},
      name: reducerName,
    });
    
  } catch (err) {
    logger.error(err);
    /* istanbul ignore next */
    yield put({
      type: "ERROR",
      payload: err,
      name: reducerName,
    });
  }
}

Example Usage Selector

import {
  VineaNovaActions,
  VineaNovaSelectors,
} from 'vineanova-redux-artifacts';

const cropBlockVintageEntityData = useSelector(
  VineaNovaSelectors.getCropBlockVintageEntityData,
);

Generated API Client Used by Sagas

export const getCropBlockVintage = async (payload) => {
  try {
    const { queryParams, postBody, options } = payload || {};
    let apiUrl = '/CropBlockVintage';
    const response = await serviceConfig.request({
      url: apiUrl,
      method: 'get',
      params: {
        ...queryParams,
      },
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
      ...options,
    });
    return response;
  } catch (err) {
    logger.error(err);
    throw err;
  }
};

Environment Variables

Ensure the following environment variables are set for proper library functionality:

  • SWAGGER_URL: The base URL for the Swagger API.
  • CODE: A code parameter for authenticating the Swagger API.

Installation

To install VineaOpenApiReduxArtifacts, simply run:

npm install vineanova-redux-artifacts

Get Started

There is no documentation , but download the generated code, navigate thru the generated code. These are simple functions and easy to navigate through.

To learn more about integrating this library into your project.Feel free to reach out to our support team at support@vineanova.com for any assistance or inquiries.

Happy coding!

Package Sidebar

Install

npm i vineanova-redux-artifacts

Weekly Downloads

22

Version

1.119.63

License

MIT

Unpacked Size

4.35 MB

Total Files

813

Last publish

Collaborators

  • khanzzirfan