This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
const reducerJediInitialState = { | |
loading: false, | |
data: [], | |
error: undefined, | |
} | |
const reducerJediMap = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createReducer } from 'redux-create-reducer' | |
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
const reducerJediInitialState = { | |
loading: false, | |
data: [], | |
error: undefined, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
class ReducerJedi { | |
initialState = { | |
loading: false, | |
data: [], | |
error: undefined, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const METADATA_KEY_ACTION = 'reducer-class-action-metadata' | |
export const Action = (...actionTypes) => (target, propertyKey, descriptor) => { | |
Reflect.defineMetadata(METADATA_KEY_ACTION, actionTypes, target, propertyKey) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getReducerClassMethodsWthActionTypes = (instance) => { | |
// Get method names from class' prototype | |
const proto = Object.getPrototypeOf(instance) | |
const methodNames = Object.getOwnPropertyNames(proto).filter( | |
(name) => name !== 'constructor', | |
) | |
// We want to get back a collection with action types and corresponding reducers | |
const res = [] | |
methodNames.forEach((methodName) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getReducerMap = (methodsWithActionTypes) => | |
methodsWithActionTypes.reduce((reducerMap, { method, actionType }) => { | |
reducerMap[actionType] = method | |
return reducerMap | |
}, {}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createReducer } from 'redux-create-reducer' | |
const createClassReducer = (ReducerClass) => { | |
const reducerClass = new ReducerClass() | |
const methodsWithActionTypes = getReducerClassMethodsWthActionTypes( | |
reducerClass, | |
) | |
const reducerMap = getReducerMap(methodsWithActionTypes) | |
const initialState = reducerClass.initialState | |
const reducer = createReducer(initialState, reducerMap) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducerJedi = createClassReducer(ReducerJedi) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We move that generic code to a dedicated module | |
import { Action, createClassReducer } from 'utils/reducer-class' | |
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
class ReducerJedi { | |
// Take a look at "Class field delcaratrions" proposal, which is now at Stage 3. | |
// https://github.com/tc39/proposal-class-fields |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cls = require('cls-hooked') | |
const uuidv4 = require('uuid/v4') | |
const clsNamespace = cls.createNamespace('app') | |
const clsMiddleware = (req, res, next) => { | |
// req and res are event emitters. We want to access CLS context inside of their event callbacks | |
clsNamespace.bind(req) | |
clsNamespace.bind(res) |