vineanova-redux-artifacts
Version:
## Overview
157 lines (126 loc) • 3.77 kB
Markdown
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
```javascript
import {
VineaNovaActions,
VineaNovaSelectors,
} from 'vineanova-redux-artifacts';
dispatchAPI(
VineaNovaActions.api.v1.cropBlockVintage.get.request({
queryParams: {
VineyardBlockID: 11111,
VintageID: 1111,
},
}),
);
```
```javascript
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)
},
},
},
},
});
```
```javascript
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,
});
}
}
```
```javascript
import {
VineaNovaActions,
VineaNovaSelectors,
} from 'vineanova-redux-artifacts';
const cropBlockVintageEntityData = useSelector(
VineaNovaSelectors.getCropBlockVintageEntityData,
);
```
```javascript
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;
}
};
```
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.
To install VineaOpenApiReduxArtifacts, simply run:
```bash
npm install vineanova-redux-artifacts
```
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](mailto:support@vineanova.com) for any assistance or inquiries.
Happy coding!