Skip to content

Instantly share code, notes, and snippets.

@ibare
Last active February 10, 2017 08:14
Show Gist options
  • Save ibare/d6bceb1bb9df2b412afaf4302e053537 to your computer and use it in GitHub Desktop.
Save ibare/d6bceb1bb9df2b412afaf4302e053537 to your computer and use it in GitHub Desktop.
woowahanjs 리듀서 ajax 호출 전처리, 후처리 미들웨어 사용하기
export function beforeRequest(context, method, url, options = {}) {
method = method.toLowerCase();
let defaultOptions = {
timeout: 1000*10, // 10sec
headers: {
'Authorization': 'Bearer xxx-token-xxx',
'Content-Type':'application/json'
}
};
context[`${method.toLowerCase()}Data`](url, Object.assign(defaultOptions, options));
}
export function successHandler(err, type, xhr) {
// do something
}
export function failHandler(error) {
if (error.status === 401) {
this.addError(401);
} else {
// do something
}
this.finish(error, null);
}
import Woowahan from 'woowahan';
import { beforeRequest, successHandler, failHandler } from './reducer-utils';
import { USERS } from './actions';
export const users = Woowahan.Reducer.create(USERS, function(options) {
this.use(Woowahan.Reducer.SUCCESS, successHandler);
this.use(Woowahan.Reducer.FAIL, failHandler);
this.onSuccess = function(resp) {
this.finish(null, resp);
};
beforeRequest(this, 'get', '/api/users');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment