eightcard / openapi-to-normalizr

Open API specification to normalizr
17 stars 3 forks source link

CircleCI

openapi-to-normalizr

schemas generator for normalizr

日本語

Install

$ npm install git+ssh://git@github.com/eightcard/openapi-to-normalizr.git

Usage

generate code

import { createEntitiesReducer, createOpenApiMiddleware } from 'openapi-to-normalizr';
import * as Models from '{your models dir}/index';
import Spec from '{your output dir}/spec';
import { createStore, applyMiddleware, combineReducers } from 'redux';

const reducers = combineReducers({
  entities: createEntitiesReducer(Models),
});

createOpenApiMiddleware(Spec)
  .then((middleware) => {
    return createStore(reducers, applyMiddleware(middleware));
  })
  .then((store) => {
    // React Application code.
  });

API

createEntitiesReducer(Models, {additionalReducer, initialState})

create reducer that output normalized state as model. (normalized by normalizr)

Models

Type: {[key: string]: Model}

additionalReducer

Type: Function

additional reducer that support no automatic merge reduce.

Example:

import * as ActionTypes from 'actions/actionTypes';
function additionalReducer(state = Map(), action = {}) {
  const id = action.payload.id;
  switch (action.type) {
    case ActionTypes.DELETE_PETS__ID_:
      return state.removeIn(['Pet', id.toString()]);
    default:
      return state;
  }
}

initialState

Type: Immutable.Map

reducer's initial state object.

createOpenApiMiddleware(spec, [httpOptions])

create redux middleware support swagger-js.

spec

Type: Object

OpenAPI Spec

httpOptions

Type: Object

Same as httpOptions of swagger-js.

Release

Contributors

Made with contributors-img.