Skip to content

Commit

Permalink
Merge branch 'release/2.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
vedrani committed Jul 22, 2017
2 parents 08cdefa + 6a0887d commit c48210c
Show file tree
Hide file tree
Showing 19 changed files with 458 additions and 82 deletions.
8 changes: 4 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"parser" : "babel-eslint",
"parser": "babel-eslint",
"extends": "airbnb",
"parserOptions": {
"ecmaVersion": 6,
Expand All @@ -8,12 +8,12 @@
"jsx": false
}
},
"plugins" : [
"plugins": [
"flow-vars"
],
"env" : {
"env": {
"browser" : true,
"mocha": true,
"mocha": true
},
"rules": {
"no-empty-label": 0,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shoutem/redux-io",
"version": "2.1.1",
"version": "2.2.0",
"description": "Action creators, reducers and middleware for simple handling of normalized json api data in state",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions src/actions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import { JSON_API_SOURCE } from './..';
import {
buildEndpoint,
resolveConfig,
buildRSAAConfig,
} from './../schemaConfig';
import thunkAction from './_thunkAction';
import { extendMetaWithResponse } from './_rsaa';
import { extendMetaWithResponse, buildRSAAConfig } from '../rsaa';

/**
* Action creator used to create item on api (POST). Tag is not needed because all collection
Expand Down
3 changes: 1 addition & 2 deletions src/actions/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import { JSON_API_SOURCE } from './..';
import {
buildEndpoint,
resolveConfig,
buildRSAAConfig,
} from './../schemaConfig';
import thunkAction from './_thunkAction';
import { extendMetaWithResponse } from './_rsaa';
import { extendMetaWithResponse, buildRSAAConfig } from '../rsaa';

/**
* If this options key is set to true, the data will be
Expand Down
3 changes: 1 addition & 2 deletions src/actions/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import { JSON_API_SOURCE } from './..';
import {
buildEndpoint,
resolveConfig,
buildRSAAConfig,
} from './../schemaConfig';
import thunkAction from './_thunkAction';
import { extendMetaWithResponse } from './_rsaa';
import { extendMetaWithResponse, buildRSAAConfig } from '../rsaa';

/**
* Action creator used to delete item on api (DELETE). Tag is not needed because all collection
Expand Down
3 changes: 1 addition & 2 deletions src/actions/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import { JSON_API_SOURCE } from './..';
import {
buildEndpoint,
resolveConfig,
buildRSAAConfig,
} from './../schemaConfig';
import thunkAction from './_thunkAction';
import { extendMetaWithResponse } from './_rsaa';
import { extendMetaWithResponse, buildRSAAConfig } from '../rsaa';

/**
* Action creator used to update item on api (POST). Tag is not needed because all collections
Expand Down
49 changes: 34 additions & 15 deletions src/reducers/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ import {
} from './../status';
import Outdated from './../outdated';

function createDefaultStatus(schema) {
return updateStatus(
createStatus(),
{
schema,
type: 'resource',
id: _.uniqueId(),
}
);
}

const actionsWithoutPayload = new Set([
LOAD_REQUEST,
CREATE_REQUEST,
Expand All @@ -46,14 +35,44 @@ const actionsWithoutPayload = new Set([
REFERENCE_CLEAR,
]);

function createNewState(state) {
/**
* @param {String} schema
* @return {Object}
*/
export function createInitialStatus(schema) {
return updateStatus(
createStatus(),
{
schema,
type: 'resource',
id: _.uniqueId(),
}
);
}

/**
* Create new state instance
* @param {Array|Object} state
* @return {Array|Object}
*/
export function createNewState(state) {
return _.isArray(state) ? [...state] : { ...state };
}

function createEmptyState(state) {
/**
* Create new empty state instance
* @param {Array|Object} state
* @return {Array|Object}
*/
export function createEmptyState(state) {
return _.isArray(state) ? [] : {};
}

/**
* @param {Object} action
* @param {String} schema
* @return {Boolean}
*/
function canHandleAction(action, schema) {
if (_.get(action, 'meta.schema') !== schema) {
return false;
Expand All @@ -72,7 +91,7 @@ function canHandleAction(action, schema) {
*/
export default function resource(schema, initialState = {}) {
// eslint-disable-next-line no-param-reassign
setStatus(initialState, createDefaultStatus(schema));
setStatus(initialState, createInitialStatus(schema));
const outdated = new Outdated();

return (state = initialState, action) => {
Expand Down Expand Up @@ -181,7 +200,7 @@ export default function resource(schema, initialState = {}) {
return state;
}
const newState = createNewState(state);
setStatus(newState, createDefaultStatus(schema));
setStatus(newState, createInitialStatus(schema));
return newState;
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/rsaa/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import _ from 'lodash';

/**
* Creates new config with data pulled out from config request,
* @param config
* @returns config
*/
export function buildRSAAConfig(config) {
const rsaaConfig = {
endpoint: config.request.endpoint,
headers: config.request.headers,
types: config.request.types,
method: config.request.method,
body: config.request.body,
};

return _.omitBy(rsaaConfig, _.isNil);
}
45 changes: 45 additions & 0 deletions src/rsaa/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import _ from 'lodash';

/**
* Check if headers object supports entities() method.
* @param {Headers} headers
*/
function isHeadersEntriesSupported(headers) {
return (_.isFunction(headers.entries));
}

/**
* Return existing or create new Headers object instance
* @param {Headers|Object} headers
*/
function getHeadersInstance(resource) {
const headers = _.get(resource, 'headers');

if (headers instanceof Headers) {
return headers;
}

return new Headers({ ...headers });
}

/**
* Extract Request/Response headers into a plain object
* @param {Object} response
*/
export function extractHeaders(resource) {
const headers = getHeadersInstance(resource);
const extractedHeaders = {};

if (isHeadersEntriesSupported(headers)) {
for (const header of headers.entries()) {
const [headerName, headerValue] = header;
extractedHeaders[headerName] = headerValue;
}
} else {
headers.forEach((headerValue, headerName) => {
extractedHeaders[headerName] = headerValue;
});
}

return extractedHeaders;
}
9 changes: 9 additions & 0 deletions src/rsaa/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { extendMetaWithResponse } from './meta';
import { extractHeaders } from './headers';
import { buildRSAAConfig } from './config';

export {
extendMetaWithResponse,
extractHeaders,
buildRSAAConfig,
};
6 changes: 5 additions & 1 deletion src/actions/_rsaa.js → src/rsaa/meta.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { extractHeaders } from './headers';

export function extendMetaWithResponse(meta) {
return (action, state, res) => {
const response = {
status: res.status,
headers: extractHeaders(res),
};
return { ... meta, response };

return { ...meta, response };
};
}
17 changes: 0 additions & 17 deletions src/schemaConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,6 @@ export function resolveConfig(schema) {
return undefined;
}

/**
* Creates new config with data pulled out from config request,
* @param config
* @returns config
*/
export function buildRSAAConfig(config) {
const rsaaConfig = {
endpoint: config.request.endpoint,
headers: config.request.headers,
types: config.request.types,
method: config.request.method,
body: config.request.body,
};

return _.omitBy(rsaaConfig, _.isNil);
}

/**
* Replace endpoint placeholders '{key}' with corresponding value of key in params dict.
* Unused params are resolved into query params as 'key=value' pairs and concatenated to endpoint
Expand Down
27 changes: 22 additions & 5 deletions test/actions/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ describe('Create action creator', () => {
};
const expectedResponseMeta = {
...expectedMeta,
response: { status: 200},
response: {
status: 200,
headers: {},
},
};
const metaResponse = [{}, {}, { status: 200 }];

Expand Down Expand Up @@ -127,7 +130,10 @@ describe('Create action creator', () => {
};
const expectedResponseMeta = {
...expectedMeta,
response: { status: 200},
response: {
status: 200,
headers: {},
},
};
const metaResponse = [{}, {}, { status: 200 }];

Expand Down Expand Up @@ -179,7 +185,10 @@ describe('Create action creator', () => {
};
const expectedResponseMeta = {
...expectedMeta,
response: { status: 200},
response: {
status: 200,
headers: {},
},
};
const metaResponse = [{}, {}, { status: 200 }];

Expand Down Expand Up @@ -238,7 +247,10 @@ describe('Create action creator', () => {
};
const expectedResponseMeta = {
...expectedMeta,
response: { status: 200},
response: {
status: 200,
headers: {},
},
};
const metaResponse = [{}, {}, { status: 200 }];

Expand Down Expand Up @@ -375,7 +387,12 @@ describe('Create action creator', () => {
};
const expectedResponseMeta = {
...expectedMeta,
response: { status: 200},
response: {
status: 200,
headers: {
"content-type": "vnd.api+json"
},
},
};

const action = create(schemaConfig, item);
Expand Down
Loading

0 comments on commit c48210c

Please sign in to comment.