Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@codetanzania/emis-api-client

Package Overview
Dependencies
Maintainers
2
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codetanzania/emis-api-client - npm Package Compare versions

Comparing version 0.6.0 to 0.7.0

src/common.js

27

CHANGELOG.md

@@ -0,1 +1,28 @@

#### 0.7.0 (2019-02-19)
##### New Features
* **common:** export builded resource http actions ([3722ec24](https://github.com/CodeTanzania/emis-api-client/commit/3722ec24871ecd59b06df7ae34ada10875ab9c66))
* **client:**
* add put, patch & delete resurce action creator ([f8f316cc](https://github.com/CodeTanzania/emis-api-client/commit/f8f316cc0cd72914a88446cf2a44d6e60f2cb9b8))
* add create get single resource http action ([9cf67fbe](https://github.com/CodeTanzania/emis-api-client/commit/9cf67fbe8eac5799f2d16b0a2a929ecf38ca6b4e))
* add getSchema & getList action creator ([a51a59b5](https://github.com/CodeTanzania/emis-api-client/commit/a51a59b5fcd1685d0ae7884951cd0ac8afd575f1))
* add resource definition normalizer ([350dd33d](https://github.com/CodeTanzania/emis-api-client/commit/350dd33dffca22f640f75c674e998c45f4579a31))
* add cmmon & shortcut resource definition ([6aad2845](https://github.com/CodeTanzania/emis-api-client/commit/6aad284516106653b862593346ad8ea55a701c54))
* implement default options & well known enpoint names ([eb21061d](https://github.com/CodeTanzania/emis-api-client/commit/eb21061de76ea3c94f70dbb316a06e6a643aa4cc))
##### Refactors
* **client:**
* improve action creator jsdocs & source code order ([3cddfcba](https://github.com/CodeTanzania/emis-api-client/commit/3cddfcba3d4ed58c3223fac35b1864a02900b9b6))
* compose resource http actions using creators ([accd6fe6](https://github.com/CodeTanzania/emis-api-client/commit/accd6fe699a98b10deeb6e46fc3a0ce769777e29))
##### Code Style Changes
* compact create http actions source ([c1d0e84c](https://github.com/CodeTanzania/emis-api-client/commit/c1d0e84c0dcf21de3c9ee6d89cebb981dbfbe54b))
##### Tests
* **client:** correct http actions destructuring ([7d564c60](https://github.com/CodeTanzania/emis-api-client/commit/7d564c60869f6e0ffc29ff8ea4bc8e8ae5401079))
#### 0.6.0 (2019-01-29)

@@ -2,0 +29,0 @@

713

es/index.js
import axios from 'axios';
import moment from 'moment';
import { singularize, pluralize } from 'inflection';
import { merge, forEach, isEmpty, camelCase, toLower, isArray, isPlainObject, uniq, compact, first, min, max } from 'lodash';
import { merge, forEach, isEmpty, isString, camelCase, isArray, isPlainObject, toLower, uniq, compact, first, min, max, clone } from 'lodash';

@@ -379,241 +379,526 @@ // default http client

/**
* @function createHttpActionsFor
* @name createHttpActionsFor
* @description generate name http action shortcut to interact with resource
* @param {String} resource valid http resource.
* @return {Object} http actions to interact with a resource
* @since 0.1.0
* @function normalizeResource
* @name normalizeResource
* @description normalize resource for action http building
* @param {Object} resource valid http resource definition
* @return {Object} normalized http resource definition
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const normalizeResource = resource => {
// normalize & get copy
const definition = isString(resource)
? { wellknown: resource }
: merge({}, resource);
// rormalize wellknown
const { wellknown } = definition;
let singular = singularize(wellknown);
let plural = pluralize(wellknown);
definition.wellknown = { singular, plural };
// rormalize shortcut
const { shortcut } = definition;
singular = singularize(shortcut || wellknown);
plural = pluralize(shortcut || wellknown);
definition.shortcut = { singular, plural };
// return resource definition
return definition;
};
/**
* @function createGetSchemaHttpAction
* @name createGetSchemaHttpAction
* @description generate http action to obtain resource schema definition
* @param {Object} resource valid http resource definition
* @return {Object} http action to get resource schema
* @since 0.7.0
* @version 0.1.0
* @example
* import { createHttpActionsFor } from 'emis-api-client';
* import { createGetSchemaHttpAction } from 'emis-api-client';
*
* const { deleteUser } = createHttpActionsFor('user');
* const deleteUser = del('/users/5c1766243c9d520004e2b542');
* deleteUser.then(user => { ... }).catch(error => { ... });
* const resource = { wellknown: 'user' };
* const getUserSchema = createGetSchemaHttpAction(resource);
* getUserSchema().then(schema => { ... }).catch(error => { ... });
*/
const createHttpActionsFor = resource => {
const singular = singularize(resource);
const plural = pluralize(resource);
const httpActions = {
[fn('get', singular, 'Schema')]: () =>
get(`/${toLower(plural)}/schema`).then(response => response.data),
[fn('get', plural)]: params =>
get(`/${toLower(plural)}`, params).then(response => response.data),
[fn('get', singular)]: id =>
get(`/${toLower(plural)}/${id}`).then(response => response.data),
[fn('post', singular)]: data =>
post(`/${toLower(plural)}`, data).then(response => response.data),
[fn('put', singular)]: data =>
put(`/${toLower(plural)}/${idOf(data)}`, data).then(
response => response.data
),
[fn('patch', singular)]: data =>
patch(`/${toLower(plural)}/${idOf(data)}`, data).then(
response => response.data
),
[fn('delete', singular)]: id =>
del(`/${toLower(plural)}/${id}`).then(response => response.data),
const createGetSchemaHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('get', singular, 'Schema');
// build action
const action = {
[methodName]: () => {
const endpoint = `/${toLower(plural)}/schema`;
return get(endpoint).then(response => response.data);
},
};
return httpActions;
// return get schema action
return action;
};
const getSchemas = () =>
get('/schemas').then(response => {
const schemas = response.data;
if (schemas) {
schemas.Warehouse = schemas.Feature;
}
return schemas;
});
/**
* @function createGetListHttpAction
* @name createGetListHttpAction
* @description generate http action to obtain resource list
* @param {Object} resource valid http resource definition
* @return {Object} http action to get resource list
* @since 0.7.0
* @version 0.1.0
* @example
* import { createGetListHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const getUsers = createGetListHttpAction(resource);
* getUsers().then(users => { ... }).catch(error => { ... });
*/
const createGetListHttpAction = resource => {
// ensure resource
const { shortcut, wellknown } = normalizeResource(resource);
const {
getActivitySchema,
getActivities,
getActivity,
postActivity,
putActivity,
patchActivity,
deleteActivity,
} = createHttpActionsFor('activity');
// generate method name
const methodName = fn('get', shortcut.plural);
const {
getAdjustmentSchema,
getAdjustments,
getAdjustment,
postAdjustment,
putAdjustment,
patchAdjustment,
deleteAdjustment,
} = createHttpActionsFor('adjustment');
// build action
const action = {
[methodName]: options => {
// prepare params
const params = merge({}, resource.params, options);
const endpoint = `/${toLower(wellknown.plural)}`;
return get(endpoint, params).then(response => response.data);
},
};
const {
getAlertSchema,
getAlerts,
getAlert,
postAlert,
putAlert,
patchAlert,
deleteAlert,
} = createHttpActionsFor('alert');
// return get resource list action
return action;
};
const {
getAlertSourceSchema,
getAlertSources,
getAlertSource,
postAlertSource,
putAlertSource,
patchAlertSource,
deleteAlertSource,
} = createHttpActionsFor('alertSource');
/**
* @function createGetSingleHttpAction
* @name createGetSingleHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createGetSingleHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const getUser = createGetSingleHttpAction(resource);
* getUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
const createGetSingleHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
const {
getAssessmentSchema,
getAssessments,
getAssessment,
postAssessment,
putAssessment,
patchAssessment,
deleteAssessment,
} = createHttpActionsFor('assessment');
// generate method name
const methodName = fn('get', singular);
const {
getFeatureSchema,
getFeatures,
getFeature,
postFeature,
putFeature,
patchFeature,
deleteFeature,
} = createHttpActionsFor('feature');
// build action
const action = {
[methodName]: id => {
// prepare params
const params = merge({}, resource.params);
const endpoint = `/${toLower(plural)}/${id}`;
return get(endpoint, params).then(response => response.data);
},
};
const {
getIncidentSchema,
getIncidents,
getIncident,
postIncident,
putIncident,
patchIncident,
deleteIncident,
} = createHttpActionsFor('incident');
// return get single resource action
return action;
};
const {
getIncidentTypeSchema,
getIncidentTypes,
getIncidentType,
postIncidentType,
putIncidentType,
patchIncidentType,
deleteIncidentType,
} = createHttpActionsFor('incidentType');
/**
* @function createPostHttpAction
* @name createPostHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPostHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const postUser = createPostHttpAction(resource);
* postUser({ name: ... }).then(user => { ... }).catch(error => { ... });
*/
const createPostHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
const {
getIndicatorSchema,
getIndicators,
getIndicator,
postIndicator,
putIndicator,
patchIndicator,
deleteIndicator,
} = createHttpActionsFor('indicator');
// generate method name
const methodName = fn('post', singular);
const {
getItemSchema,
getItems,
getItem,
postItem,
putItem,
patchItem,
deleteItem,
} = createHttpActionsFor('item');
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = merge({}, defaults, payload);
const endpoint = `/${toLower(plural)}`;
return post(endpoint, data).then(response => response.data);
},
};
const {
getPartySchema,
getParties,
getParty,
postParty,
putParty,
patchParty,
deleteParty,
} = createHttpActionsFor('party');
// return post single resource action
return action;
};
const {
getPermissionSchema,
getPermissions,
getPermission,
postPermission,
putPermission,
patchPermission,
deletePermission,
} = createHttpActionsFor('permission');
/**
* @function createPutHttpAction
* @name createPutHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPutHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const putUser = createPutHttpAction(resource);
* putUser({ _id: ..., name: ...}).then(user => { ... }).catch(error => { ... });
*/
const createPutHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
const {
getPlanSchema,
getPlans,
getPlan,
postPlan,
putPlan,
patchPlan,
deletePlan,
} = createHttpActionsFor('plan');
// generate method name
const methodName = fn('put', singular);
const {
getProcedureSchema,
getProcedures,
getProcedure,
postProcedure,
putProcedure,
patchProcedure,
deleteProcedure,
} = createHttpActionsFor('procedure');
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = merge({}, defaults, payload);
const endpoint = `/${toLower(plural)}/${idOf(data)}`;
return put(endpoint, data).then(response => response.data);
},
};
const {
getQuestionSchema,
getQuestions,
getQuestion,
postQuestion,
putQuestion,
patchQuestion,
deleteQuestion,
} = createHttpActionsFor('question');
// return put single resource action
return action;
};
const {
getQuestionnaireSchema,
getQuestionnaires,
getQuestionnaire,
postQuestionnaire,
putQuestionnaire,
patchQuestionnaire,
deleteQuestionnaire,
} = createHttpActionsFor('questionnaire');
/**
* @function createPatchHttpAction
* @name createPatchHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPatchHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const patchUser = createPatchHttpAction(resource);
* patchUser({ _id: ..., name: ...}).then(user => { ... }).catch(error => { ... });
*/
const createPatchHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
const {
getRoleSchema,
getRoles,
getRole,
postRole,
putRole,
patchRole,
deleteRole,
} = createHttpActionsFor('role');
// generate method name
const methodName = fn('patch', singular);
const {
getStockSchema,
getStocks,
getStock,
postStock,
putStock,
patchStock,
deleteStock,
} = createHttpActionsFor('stock');
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = merge({}, defaults, payload);
const endpoint = `/${toLower(plural)}/${idOf(data)}`;
return patch(endpoint, data).then(response => response.data);
},
};
const {
getWarehouseSchema,
getWarehouses,
getWarehouse,
postWarehouse,
putWarehouse,
patchWarehouse,
deleteWarehouse,
} = createHttpActionsFor('warehouse');
// return patch single resource action
return action;
};
export { getSchemas, getActivitySchema, getActivities, getActivity, postActivity, putActivity, patchActivity, deleteActivity, getAdjustmentSchema, getAdjustments, getAdjustment, postAdjustment, putAdjustment, patchAdjustment, deleteAdjustment, getAlertSchema, getAlerts, getAlert, postAlert, putAlert, patchAlert, deleteAlert, getAlertSourceSchema, getAlertSources, getAlertSource, postAlertSource, putAlertSource, patchAlertSource, deleteAlertSource, getAssessmentSchema, getAssessments, getAssessment, postAssessment, putAssessment, patchAssessment, deleteAssessment, getFeatureSchema, getFeatures, getFeature, postFeature, putFeature, patchFeature, deleteFeature, getIncidentSchema, getIncidents, getIncident, postIncident, putIncident, patchIncident, deleteIncident, getIncidentTypeSchema, getIncidentTypes, getIncidentType, postIncidentType, putIncidentType, patchIncidentType, deleteIncidentType, getIndicatorSchema, getIndicators, getIndicator, postIndicator, putIndicator, patchIndicator, deleteIndicator, getItemSchema, getItems, getItem, postItem, putItem, patchItem, deleteItem, getPartySchema, getPartySchema as getStakeholderSchema, getParties, getParties as getStakeholders, getParty, getParty as getStakeholder, postParty, postParty as postStakeholder, putParty, putParty as putStakeholder, patchParty, patchParty as patchStakeholder, deleteParty, deleteParty as deleteStakeholder, getPermissionSchema, getPermissions, getPermission, postPermission, putPermission, patchPermission, deletePermission, getPlanSchema, getPlans, getPlan, postPlan, putPlan, patchPlan, deletePlan, getProcedureSchema, getProcedures, getProcedure, postProcedure, putProcedure, patchProcedure, deleteProcedure, getQuestionSchema, getQuestions, getQuestion, postQuestion, putQuestion, patchQuestion, deleteQuestion, getQuestionnaireSchema, getQuestionnaires, getQuestionnaire, postQuestionnaire, putQuestionnaire, patchQuestionnaire, deleteQuestionnaire, getRoleSchema, getRoles, getRole, postRole, putRole, patchRole, deleteRole, getStockSchema, getStocks, getStock, postStock, putStock, patchStock, deleteStock, getWarehouseSchema, getWarehouses, getWarehouse, postWarehouse, putWarehouse, patchWarehouse, deleteWarehouse, CONTENT_TYPE, HEADERS, prepareParams, createHttpClient, disposeHttpClient, all, spread, get, post, put, patch, del, createHttpActionsFor };
/**
* @function createDeleteHttpAction
* @name createDeleteHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createDeleteHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const deleteUser = createDeleteHttpAction(resource);
* deleteUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
const createDeleteHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('delete', singular);
// build action
const action = {
[methodName]: id => {
// prepare params
const endpoint = `/${toLower(plural)}/${id}`;
return del(endpoint).then(response => response.data);
},
};
// return delete single resource action
return action;
};
/**
* @function createHttpActionsFor
* @name createHttpActionsFor
* @description generate http actions to interact with resource
* @param {String} resource valid http resource
* @return {Object} http actions to interact with a resource
* @since 0.1.0
* @version 0.1.0
* @example
* import { createHttpActionsFor } from 'emis-api-client';
*
* const { deleteUser } = createHttpActionsFor('user');
* deleteUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
const createHttpActionsFor = resource => {
// compose resource http actions
const getSchema = createGetSchemaHttpAction(resource);
const getResources = createGetListHttpAction(resource);
const getResource = createGetSingleHttpAction(resource);
const postResource = createPostHttpAction(resource);
const putResource = createPutHttpAction(resource);
const patchResource = createPatchHttpAction(resource);
const deleteResource = createDeleteHttpAction(resource);
// return resource http actions
const httpActions = merge(
{},
getSchema,
getResources,
getResource,
postResource,
putResource,
patchResource,
deleteResource
);
return httpActions;
};
/**
* @name DEFAULT_FILTER
* @description default resource filtering options
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const DEFAULT_FILTER = { deletedAt: { $eq: null } };
/**
* @name DEFAULT_PAGINATION
* @description default resource pagination options
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const DEFAULT_PAGINATION = { limit: 10, skip: 0, page: 1 };
/**
* @name DEFAULT_SORT
* @description default resource sorting order options
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @private
*/
const DEFAULT_SORT = { updatedAt: -1 };
/**
* @constant
* @name WELL_KNOWN
* @description set of well known api endpoints. they must be one-to-one to
* naked api endpoints exposed by the server and they must presented in
* camelcase.
* @type {String[]}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const WELL_KNOWN = [
'activity',
'adjustment',
'alert',
'alertSource',
'assessment',
'feature',
'incident',
'incidentType',
'indicator',
'item',
'party',
'permission',
'plan',
'procedure',
'question',
'questionnaire',
'role',
'stock',
];
// default request params
const DEFAULT_PARAMS = {
filter: DEFAULT_FILTER,
paginate: DEFAULT_PAGINATION,
sort: DEFAULT_SORT,
};
// parties shortcuts
const PARTY_SHORTCUTS = {
focalPerson: {
shortcut: 'focalPerson',
wellknown: 'party',
params: merge({}, DEFAULT_PARAMS, { filter: { type: 'Focal Person' } }),
},
agency: {
shortcut: 'agency',
wellknown: 'party',
params: merge({}, DEFAULT_PARAMS, { filter: { type: 'Agency' } }),
},
};
// features shortcuts
const FEATURE_SHORTCUTS = {
region: {
shortcut: 'region',
wellknown: 'feature',
params: merge({}, DEFAULT_PARAMS, {
filter: {
nature: 'Boundary',
family: 'Administrative',
type: 'Region',
},
}),
},
district: {
shortcut: 'district',
wellknown: 'feature',
params: merge({}, DEFAULT_PARAMS, {
filter: {
nature: 'Boundary',
family: 'Administrative',
type: 'District',
},
}),
},
warehouse: {
shortcut: 'warehouse',
wellknown: 'feature',
params: merge({}, DEFAULT_PARAMS, {
filter: {
nature: 'Building',
family: 'Warehouse',
},
}),
},
};
/**
* @constant
* @name SHORTCUTS
* @description set of applicable api shortcuts.
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const SHORTCUTS = merge({}, PARTY_SHORTCUTS, FEATURE_SHORTCUTS);
/**
* @constant
* @name RESOURCES
* @description set of applicable api endpoints including both well-kown and
* shortcuts. they must presented in camelcase and wellknown key should point
* back to {@link WELL_KNOWN}.
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const RESOURCES = merge({}, SHORTCUTS);
// build wellknown resources
forEach([...WELL_KNOWN], wellknown => {
const name = clone(wellknown);
const shortcut = clone(wellknown);
const params = merge({}, DEFAULT_PARAMS);
const resource = { shortcut, wellknown, params };
RESOURCES[name] = resource;
});
/**
* @name httpActions
* @description resource http actions
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const httpActions = {
getSchemas: () =>
get('/schemas').then(response => {
const schemas = response.data;
// TODO expose shortcuts schema
if (schemas) {
schemas.Warehouse = schemas.Feature;
}
return schemas;
}),
};
// build resource http actions
forEach(RESOURCES, resource => {
const resourceHttpActions = createHttpActionsFor(resource);
merge(httpActions, resourceHttpActions);
});
export { CONTENT_TYPE, HEADERS, prepareParams, createHttpClient, disposeHttpClient, all, spread, get, post, put, patch, del, normalizeResource, createGetSchemaHttpAction, createGetListHttpAction, createGetSingleHttpAction, createPostHttpAction, createPutHttpAction, createPatchHttpAction, createDeleteHttpAction, createHttpActionsFor, DEFAULT_FILTER, DEFAULT_PAGINATION, DEFAULT_SORT, WELL_KNOWN, SHORTCUTS, RESOURCES, httpActions };

@@ -385,382 +385,526 @@ 'use strict';

/**
* @function createHttpActionsFor
* @name createHttpActionsFor
* @description generate name http action shortcut to interact with resource
* @param {String} resource valid http resource.
* @return {Object} http actions to interact with a resource
* @since 0.1.0
* @function normalizeResource
* @name normalizeResource
* @description normalize resource for action http building
* @param {Object} resource valid http resource definition
* @return {Object} normalized http resource definition
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const normalizeResource = resource => {
// normalize & get copy
const definition = lodash.isString(resource)
? { wellknown: resource }
: lodash.merge({}, resource);
// rormalize wellknown
const { wellknown } = definition;
let singular = inflection.singularize(wellknown);
let plural = inflection.pluralize(wellknown);
definition.wellknown = { singular, plural };
// rormalize shortcut
const { shortcut } = definition;
singular = inflection.singularize(shortcut || wellknown);
plural = inflection.pluralize(shortcut || wellknown);
definition.shortcut = { singular, plural };
// return resource definition
return definition;
};
/**
* @function createGetSchemaHttpAction
* @name createGetSchemaHttpAction
* @description generate http action to obtain resource schema definition
* @param {Object} resource valid http resource definition
* @return {Object} http action to get resource schema
* @since 0.7.0
* @version 0.1.0
* @example
* import { createHttpActionsFor } from 'emis-api-client';
* import { createGetSchemaHttpAction } from 'emis-api-client';
*
* const { deleteUser } = createHttpActionsFor('user');
* const deleteUser = del('/users/5c1766243c9d520004e2b542');
* deleteUser.then(user => { ... }).catch(error => { ... });
* const resource = { wellknown: 'user' };
* const getUserSchema = createGetSchemaHttpAction(resource);
* getUserSchema().then(schema => { ... }).catch(error => { ... });
*/
const createHttpActionsFor = resource => {
const singular = inflection.singularize(resource);
const plural = inflection.pluralize(resource);
const httpActions = {
[fn('get', singular, 'Schema')]: () =>
get(`/${lodash.toLower(plural)}/schema`).then(response => response.data),
[fn('get', plural)]: params =>
get(`/${lodash.toLower(plural)}`, params).then(response => response.data),
[fn('get', singular)]: id =>
get(`/${lodash.toLower(plural)}/${id}`).then(response => response.data),
[fn('post', singular)]: data =>
post(`/${lodash.toLower(plural)}`, data).then(response => response.data),
[fn('put', singular)]: data =>
put(`/${lodash.toLower(plural)}/${idOf(data)}`, data).then(
response => response.data
),
[fn('patch', singular)]: data =>
patch(`/${lodash.toLower(plural)}/${idOf(data)}`, data).then(
response => response.data
),
[fn('delete', singular)]: id =>
del(`/${lodash.toLower(plural)}/${id}`).then(response => response.data),
const createGetSchemaHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('get', singular, 'Schema');
// build action
const action = {
[methodName]: () => {
const endpoint = `/${lodash.toLower(plural)}/schema`;
return get(endpoint).then(response => response.data);
},
};
return httpActions;
// return get schema action
return action;
};
const getSchemas = () =>
get('/schemas').then(response => {
const schemas = response.data;
if (schemas) {
schemas.Warehouse = schemas.Feature;
}
return schemas;
});
/**
* @function createGetListHttpAction
* @name createGetListHttpAction
* @description generate http action to obtain resource list
* @param {Object} resource valid http resource definition
* @return {Object} http action to get resource list
* @since 0.7.0
* @version 0.1.0
* @example
* import { createGetListHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const getUsers = createGetListHttpAction(resource);
* getUsers().then(users => { ... }).catch(error => { ... });
*/
const createGetListHttpAction = resource => {
// ensure resource
const { shortcut, wellknown } = normalizeResource(resource);
const {
getActivitySchema,
getActivities,
getActivity,
postActivity,
putActivity,
patchActivity,
deleteActivity,
} = createHttpActionsFor('activity');
// generate method name
const methodName = fn('get', shortcut.plural);
const {
getAdjustmentSchema,
getAdjustments,
getAdjustment,
postAdjustment,
putAdjustment,
patchAdjustment,
deleteAdjustment,
} = createHttpActionsFor('adjustment');
// build action
const action = {
[methodName]: options => {
// prepare params
const params = lodash.merge({}, resource.params, options);
const endpoint = `/${lodash.toLower(wellknown.plural)}`;
return get(endpoint, params).then(response => response.data);
},
};
const {
getAlertSchema,
getAlerts,
getAlert,
postAlert,
putAlert,
patchAlert,
deleteAlert,
} = createHttpActionsFor('alert');
// return get resource list action
return action;
};
const {
getAlertSourceSchema,
getAlertSources,
getAlertSource,
postAlertSource,
putAlertSource,
patchAlertSource,
deleteAlertSource,
} = createHttpActionsFor('alertSource');
/**
* @function createGetSingleHttpAction
* @name createGetSingleHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createGetSingleHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const getUser = createGetSingleHttpAction(resource);
* getUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
const createGetSingleHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
const {
getAssessmentSchema,
getAssessments,
getAssessment,
postAssessment,
putAssessment,
patchAssessment,
deleteAssessment,
} = createHttpActionsFor('assessment');
// generate method name
const methodName = fn('get', singular);
const {
getFeatureSchema,
getFeatures,
getFeature,
postFeature,
putFeature,
patchFeature,
deleteFeature,
} = createHttpActionsFor('feature');
// build action
const action = {
[methodName]: id => {
// prepare params
const params = lodash.merge({}, resource.params);
const endpoint = `/${lodash.toLower(plural)}/${id}`;
return get(endpoint, params).then(response => response.data);
},
};
const {
getIncidentSchema,
getIncidents,
getIncident,
postIncident,
putIncident,
patchIncident,
deleteIncident,
} = createHttpActionsFor('incident');
// return get single resource action
return action;
};
const {
getIncidentTypeSchema,
getIncidentTypes,
getIncidentType,
postIncidentType,
putIncidentType,
patchIncidentType,
deleteIncidentType,
} = createHttpActionsFor('incidentType');
/**
* @function createPostHttpAction
* @name createPostHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPostHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const postUser = createPostHttpAction(resource);
* postUser({ name: ... }).then(user => { ... }).catch(error => { ... });
*/
const createPostHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
const {
getIndicatorSchema,
getIndicators,
getIndicator,
postIndicator,
putIndicator,
patchIndicator,
deleteIndicator,
} = createHttpActionsFor('indicator');
// generate method name
const methodName = fn('post', singular);
const {
getItemSchema,
getItems,
getItem,
postItem,
putItem,
patchItem,
deleteItem,
} = createHttpActionsFor('item');
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = lodash.merge({}, defaults, payload);
const endpoint = `/${lodash.toLower(plural)}`;
return post(endpoint, data).then(response => response.data);
},
};
const {
getPartySchema,
getParties,
getParty,
postParty,
putParty,
patchParty,
deleteParty,
} = createHttpActionsFor('party');
// return post single resource action
return action;
};
const {
getPermissionSchema,
getPermissions,
getPermission,
postPermission,
putPermission,
patchPermission,
deletePermission,
} = createHttpActionsFor('permission');
/**
* @function createPutHttpAction
* @name createPutHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPutHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const putUser = createPutHttpAction(resource);
* putUser({ _id: ..., name: ...}).then(user => { ... }).catch(error => { ... });
*/
const createPutHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
const {
getPlanSchema,
getPlans,
getPlan,
postPlan,
putPlan,
patchPlan,
deletePlan,
} = createHttpActionsFor('plan');
// generate method name
const methodName = fn('put', singular);
const {
getProcedureSchema,
getProcedures,
getProcedure,
postProcedure,
putProcedure,
patchProcedure,
deleteProcedure,
} = createHttpActionsFor('procedure');
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = lodash.merge({}, defaults, payload);
const endpoint = `/${lodash.toLower(plural)}/${idOf(data)}`;
return put(endpoint, data).then(response => response.data);
},
};
const {
getQuestionSchema,
getQuestions,
getQuestion,
postQuestion,
putQuestion,
patchQuestion,
deleteQuestion,
} = createHttpActionsFor('question');
// return put single resource action
return action;
};
const {
getQuestionnaireSchema,
getQuestionnaires,
getQuestionnaire,
postQuestionnaire,
putQuestionnaire,
patchQuestionnaire,
deleteQuestionnaire,
} = createHttpActionsFor('questionnaire');
/**
* @function createPatchHttpAction
* @name createPatchHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPatchHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const patchUser = createPatchHttpAction(resource);
* patchUser({ _id: ..., name: ...}).then(user => { ... }).catch(error => { ... });
*/
const createPatchHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
const {
getRoleSchema,
getRoles,
getRole,
postRole,
putRole,
patchRole,
deleteRole,
} = createHttpActionsFor('role');
// generate method name
const methodName = fn('patch', singular);
const {
getStockSchema,
getStocks,
getStock,
postStock,
putStock,
patchStock,
deleteStock,
} = createHttpActionsFor('stock');
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = lodash.merge({}, defaults, payload);
const endpoint = `/${lodash.toLower(plural)}/${idOf(data)}`;
return patch(endpoint, data).then(response => response.data);
},
};
const {
getWarehouseSchema,
getWarehouses,
getWarehouse,
postWarehouse,
putWarehouse,
patchWarehouse,
deleteWarehouse,
} = createHttpActionsFor('warehouse');
// return patch single resource action
return action;
};
exports.getSchemas = getSchemas;
exports.getActivitySchema = getActivitySchema;
exports.getActivities = getActivities;
exports.getActivity = getActivity;
exports.postActivity = postActivity;
exports.putActivity = putActivity;
exports.patchActivity = patchActivity;
exports.deleteActivity = deleteActivity;
exports.getAdjustmentSchema = getAdjustmentSchema;
exports.getAdjustments = getAdjustments;
exports.getAdjustment = getAdjustment;
exports.postAdjustment = postAdjustment;
exports.putAdjustment = putAdjustment;
exports.patchAdjustment = patchAdjustment;
exports.deleteAdjustment = deleteAdjustment;
exports.getAlertSchema = getAlertSchema;
exports.getAlerts = getAlerts;
exports.getAlert = getAlert;
exports.postAlert = postAlert;
exports.putAlert = putAlert;
exports.patchAlert = patchAlert;
exports.deleteAlert = deleteAlert;
exports.getAlertSourceSchema = getAlertSourceSchema;
exports.getAlertSources = getAlertSources;
exports.getAlertSource = getAlertSource;
exports.postAlertSource = postAlertSource;
exports.putAlertSource = putAlertSource;
exports.patchAlertSource = patchAlertSource;
exports.deleteAlertSource = deleteAlertSource;
exports.getAssessmentSchema = getAssessmentSchema;
exports.getAssessments = getAssessments;
exports.getAssessment = getAssessment;
exports.postAssessment = postAssessment;
exports.putAssessment = putAssessment;
exports.patchAssessment = patchAssessment;
exports.deleteAssessment = deleteAssessment;
exports.getFeatureSchema = getFeatureSchema;
exports.getFeatures = getFeatures;
exports.getFeature = getFeature;
exports.postFeature = postFeature;
exports.putFeature = putFeature;
exports.patchFeature = patchFeature;
exports.deleteFeature = deleteFeature;
exports.getIncidentSchema = getIncidentSchema;
exports.getIncidents = getIncidents;
exports.getIncident = getIncident;
exports.postIncident = postIncident;
exports.putIncident = putIncident;
exports.patchIncident = patchIncident;
exports.deleteIncident = deleteIncident;
exports.getIncidentTypeSchema = getIncidentTypeSchema;
exports.getIncidentTypes = getIncidentTypes;
exports.getIncidentType = getIncidentType;
exports.postIncidentType = postIncidentType;
exports.putIncidentType = putIncidentType;
exports.patchIncidentType = patchIncidentType;
exports.deleteIncidentType = deleteIncidentType;
exports.getIndicatorSchema = getIndicatorSchema;
exports.getIndicators = getIndicators;
exports.getIndicator = getIndicator;
exports.postIndicator = postIndicator;
exports.putIndicator = putIndicator;
exports.patchIndicator = patchIndicator;
exports.deleteIndicator = deleteIndicator;
exports.getItemSchema = getItemSchema;
exports.getItems = getItems;
exports.getItem = getItem;
exports.postItem = postItem;
exports.putItem = putItem;
exports.patchItem = patchItem;
exports.deleteItem = deleteItem;
exports.getPartySchema = getPartySchema;
exports.getStakeholderSchema = getPartySchema;
exports.getParties = getParties;
exports.getStakeholders = getParties;
exports.getParty = getParty;
exports.getStakeholder = getParty;
exports.postParty = postParty;
exports.postStakeholder = postParty;
exports.putParty = putParty;
exports.putStakeholder = putParty;
exports.patchParty = patchParty;
exports.patchStakeholder = patchParty;
exports.deleteParty = deleteParty;
exports.deleteStakeholder = deleteParty;
exports.getPermissionSchema = getPermissionSchema;
exports.getPermissions = getPermissions;
exports.getPermission = getPermission;
exports.postPermission = postPermission;
exports.putPermission = putPermission;
exports.patchPermission = patchPermission;
exports.deletePermission = deletePermission;
exports.getPlanSchema = getPlanSchema;
exports.getPlans = getPlans;
exports.getPlan = getPlan;
exports.postPlan = postPlan;
exports.putPlan = putPlan;
exports.patchPlan = patchPlan;
exports.deletePlan = deletePlan;
exports.getProcedureSchema = getProcedureSchema;
exports.getProcedures = getProcedures;
exports.getProcedure = getProcedure;
exports.postProcedure = postProcedure;
exports.putProcedure = putProcedure;
exports.patchProcedure = patchProcedure;
exports.deleteProcedure = deleteProcedure;
exports.getQuestionSchema = getQuestionSchema;
exports.getQuestions = getQuestions;
exports.getQuestion = getQuestion;
exports.postQuestion = postQuestion;
exports.putQuestion = putQuestion;
exports.patchQuestion = patchQuestion;
exports.deleteQuestion = deleteQuestion;
exports.getQuestionnaireSchema = getQuestionnaireSchema;
exports.getQuestionnaires = getQuestionnaires;
exports.getQuestionnaire = getQuestionnaire;
exports.postQuestionnaire = postQuestionnaire;
exports.putQuestionnaire = putQuestionnaire;
exports.patchQuestionnaire = patchQuestionnaire;
exports.deleteQuestionnaire = deleteQuestionnaire;
exports.getRoleSchema = getRoleSchema;
exports.getRoles = getRoles;
exports.getRole = getRole;
exports.postRole = postRole;
exports.putRole = putRole;
exports.patchRole = patchRole;
exports.deleteRole = deleteRole;
exports.getStockSchema = getStockSchema;
exports.getStocks = getStocks;
exports.getStock = getStock;
exports.postStock = postStock;
exports.putStock = putStock;
exports.patchStock = patchStock;
exports.deleteStock = deleteStock;
exports.getWarehouseSchema = getWarehouseSchema;
exports.getWarehouses = getWarehouses;
exports.getWarehouse = getWarehouse;
exports.postWarehouse = postWarehouse;
exports.putWarehouse = putWarehouse;
exports.patchWarehouse = patchWarehouse;
exports.deleteWarehouse = deleteWarehouse;
/**
* @function createDeleteHttpAction
* @name createDeleteHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createDeleteHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const deleteUser = createDeleteHttpAction(resource);
* deleteUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
const createDeleteHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('delete', singular);
// build action
const action = {
[methodName]: id => {
// prepare params
const endpoint = `/${lodash.toLower(plural)}/${id}`;
return del(endpoint).then(response => response.data);
},
};
// return delete single resource action
return action;
};
/**
* @function createHttpActionsFor
* @name createHttpActionsFor
* @description generate http actions to interact with resource
* @param {String} resource valid http resource
* @return {Object} http actions to interact with a resource
* @since 0.1.0
* @version 0.1.0
* @example
* import { createHttpActionsFor } from 'emis-api-client';
*
* const { deleteUser } = createHttpActionsFor('user');
* deleteUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
const createHttpActionsFor = resource => {
// compose resource http actions
const getSchema = createGetSchemaHttpAction(resource);
const getResources = createGetListHttpAction(resource);
const getResource = createGetSingleHttpAction(resource);
const postResource = createPostHttpAction(resource);
const putResource = createPutHttpAction(resource);
const patchResource = createPatchHttpAction(resource);
const deleteResource = createDeleteHttpAction(resource);
// return resource http actions
const httpActions = lodash.merge(
{},
getSchema,
getResources,
getResource,
postResource,
putResource,
patchResource,
deleteResource
);
return httpActions;
};
/**
* @name DEFAULT_FILTER
* @description default resource filtering options
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const DEFAULT_FILTER = { deletedAt: { $eq: null } };
/**
* @name DEFAULT_PAGINATION
* @description default resource pagination options
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const DEFAULT_PAGINATION = { limit: 10, skip: 0, page: 1 };
/**
* @name DEFAULT_SORT
* @description default resource sorting order options
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @private
*/
const DEFAULT_SORT = { updatedAt: -1 };
/**
* @constant
* @name WELL_KNOWN
* @description set of well known api endpoints. they must be one-to-one to
* naked api endpoints exposed by the server and they must presented in
* camelcase.
* @type {String[]}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const WELL_KNOWN = [
'activity',
'adjustment',
'alert',
'alertSource',
'assessment',
'feature',
'incident',
'incidentType',
'indicator',
'item',
'party',
'permission',
'plan',
'procedure',
'question',
'questionnaire',
'role',
'stock',
];
// default request params
const DEFAULT_PARAMS = {
filter: DEFAULT_FILTER,
paginate: DEFAULT_PAGINATION,
sort: DEFAULT_SORT,
};
// parties shortcuts
const PARTY_SHORTCUTS = {
focalPerson: {
shortcut: 'focalPerson',
wellknown: 'party',
params: lodash.merge({}, DEFAULT_PARAMS, { filter: { type: 'Focal Person' } }),
},
agency: {
shortcut: 'agency',
wellknown: 'party',
params: lodash.merge({}, DEFAULT_PARAMS, { filter: { type: 'Agency' } }),
},
};
// features shortcuts
const FEATURE_SHORTCUTS = {
region: {
shortcut: 'region',
wellknown: 'feature',
params: lodash.merge({}, DEFAULT_PARAMS, {
filter: {
nature: 'Boundary',
family: 'Administrative',
type: 'Region',
},
}),
},
district: {
shortcut: 'district',
wellknown: 'feature',
params: lodash.merge({}, DEFAULT_PARAMS, {
filter: {
nature: 'Boundary',
family: 'Administrative',
type: 'District',
},
}),
},
warehouse: {
shortcut: 'warehouse',
wellknown: 'feature',
params: lodash.merge({}, DEFAULT_PARAMS, {
filter: {
nature: 'Building',
family: 'Warehouse',
},
}),
},
};
/**
* @constant
* @name SHORTCUTS
* @description set of applicable api shortcuts.
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const SHORTCUTS = lodash.merge({}, PARTY_SHORTCUTS, FEATURE_SHORTCUTS);
/**
* @constant
* @name RESOURCES
* @description set of applicable api endpoints including both well-kown and
* shortcuts. they must presented in camelcase and wellknown key should point
* back to {@link WELL_KNOWN}.
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const RESOURCES = lodash.merge({}, SHORTCUTS);
// build wellknown resources
lodash.forEach([...WELL_KNOWN], wellknown => {
const name = lodash.clone(wellknown);
const shortcut = lodash.clone(wellknown);
const params = lodash.merge({}, DEFAULT_PARAMS);
const resource = { shortcut, wellknown, params };
RESOURCES[name] = resource;
});
/**
* @name httpActions
* @description resource http actions
* @type {Object}
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
const httpActions = {
getSchemas: () =>
get('/schemas').then(response => {
const schemas = response.data;
// TODO expose shortcuts schema
if (schemas) {
schemas.Warehouse = schemas.Feature;
}
return schemas;
}),
};
// build resource http actions
lodash.forEach(RESOURCES, resource => {
const resourceHttpActions = createHttpActionsFor(resource);
lodash.merge(httpActions, resourceHttpActions);
});
exports.CONTENT_TYPE = CONTENT_TYPE;

@@ -778,2 +922,17 @@ exports.HEADERS = HEADERS;

exports.del = del;
exports.normalizeResource = normalizeResource;
exports.createGetSchemaHttpAction = createGetSchemaHttpAction;
exports.createGetListHttpAction = createGetListHttpAction;
exports.createGetSingleHttpAction = createGetSingleHttpAction;
exports.createPostHttpAction = createPostHttpAction;
exports.createPutHttpAction = createPutHttpAction;
exports.createPatchHttpAction = createPatchHttpAction;
exports.createDeleteHttpAction = createDeleteHttpAction;
exports.createHttpActionsFor = createHttpActionsFor;
exports.DEFAULT_FILTER = DEFAULT_FILTER;
exports.DEFAULT_PAGINATION = DEFAULT_PAGINATION;
exports.DEFAULT_SORT = DEFAULT_SORT;
exports.WELL_KNOWN = WELL_KNOWN;
exports.SHORTCUTS = SHORTCUTS;
exports.RESOURCES = RESOURCES;
exports.httpActions = httpActions;
{
"name": "@codetanzania/emis-api-client",
"version": "0.6.0",
"version": "0.7.0",
"description": "http client for EMIS API.",

@@ -41,5 +41,5 @@ "main": "lib/index.js",

"devDependencies": {
"@commitlint/cli": "^7.4.0",
"@commitlint/config-conventional": "^7.3.1",
"@commitlint/travis-cli": "^7.4.0",
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"@commitlint/travis-cli": "^7.5.2",
"axios": "^0.18.0",

@@ -49,7 +49,7 @@ "chai": "^4.2.0",

"cz-conventional-changelog": "^2.1.0",
"eslint": "^5.12.1",
"eslint": "^5.14.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-mocha": "^5.2.1",
"eslint-plugin-mocha": "^5.3.0",
"eslint-plugin-prettier": "^3.0.1",

@@ -59,10 +59,10 @@ "generate-changelog": "^1.7.1",

"inflection": "^1.12.0",
"lint-staged": "^8.1.1",
"lint-staged": "^8.1.4",
"lodash": ">=4.17.11",
"mocha": "^5.2.0",
"mocha": "^6.0.0",
"moment": ">=2.24.0",
"nock": "^10.0.6",
"prettier": "^1.16.1",
"prettier": "^1.16.4",
"rimraf": "^2.6.3",
"rollup": "^1.1.2"
"rollup": "^1.2.2"
},

@@ -69,0 +69,0 @@ "peerDependencies": {

@@ -13,6 +13,7 @@ import axios from 'axios';

isEmpty,
isString,
camelCase,
max,
min,
toLower as low,
toLower,
} from 'lodash';

@@ -393,6 +394,309 @@

/**
* @function normalizeResource
* @name normalizeResource
* @description normalize resource for action http building
* @param {Object} resource valid http resource definition
* @return {Object} normalized http resource definition
* @since 0.7.0
* @version 0.1.0
* @static
* @public
*/
export const normalizeResource = resource => {
// normalize & get copy
const definition = isString(resource)
? { wellknown: resource }
: merge({}, resource);
// rormalize wellknown
const { wellknown } = definition;
let singular = singularize(wellknown);
let plural = pluralize(wellknown);
definition.wellknown = { singular, plural };
// rormalize shortcut
const { shortcut } = definition;
singular = singularize(shortcut || wellknown);
plural = pluralize(shortcut || wellknown);
definition.shortcut = { singular, plural };
// return resource definition
return definition;
};
/**
* @function createGetSchemaHttpAction
* @name createGetSchemaHttpAction
* @description generate http action to obtain resource schema definition
* @param {Object} resource valid http resource definition
* @return {Object} http action to get resource schema
* @since 0.7.0
* @version 0.1.0
* @example
* import { createGetSchemaHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const getUserSchema = createGetSchemaHttpAction(resource);
* getUserSchema().then(schema => { ... }).catch(error => { ... });
*/
export const createGetSchemaHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('get', singular, 'Schema');
// build action
const action = {
[methodName]: () => {
const endpoint = `/${toLower(plural)}/schema`;
return get(endpoint).then(response => response.data);
},
};
// return get schema action
return action;
};
/**
* @function createGetListHttpAction
* @name createGetListHttpAction
* @description generate http action to obtain resource list
* @param {Object} resource valid http resource definition
* @return {Object} http action to get resource list
* @since 0.7.0
* @version 0.1.0
* @example
* import { createGetListHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const getUsers = createGetListHttpAction(resource);
* getUsers().then(users => { ... }).catch(error => { ... });
*/
export const createGetListHttpAction = resource => {
// ensure resource
const { shortcut, wellknown } = normalizeResource(resource);
// generate method name
const methodName = fn('get', shortcut.plural);
// build action
const action = {
[methodName]: options => {
// prepare params
const params = merge({}, resource.params, options);
const endpoint = `/${toLower(wellknown.plural)}`;
return get(endpoint, params).then(response => response.data);
},
};
// return get resource list action
return action;
};
/**
* @function createGetSingleHttpAction
* @name createGetSingleHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createGetSingleHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const getUser = createGetSingleHttpAction(resource);
* getUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
export const createGetSingleHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('get', singular);
// build action
const action = {
[methodName]: id => {
// prepare params
const params = merge({}, resource.params);
const endpoint = `/${toLower(plural)}/${id}`;
return get(endpoint, params).then(response => response.data);
},
};
// return get single resource action
return action;
};
/**
* @function createPostHttpAction
* @name createPostHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPostHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const postUser = createPostHttpAction(resource);
* postUser({ name: ... }).then(user => { ... }).catch(error => { ... });
*/
export const createPostHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('post', singular);
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = merge({}, defaults, payload);
const endpoint = `/${toLower(plural)}`;
return post(endpoint, data).then(response => response.data);
},
};
// return post single resource action
return action;
};
/**
* @function createPutHttpAction
* @name createPutHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPutHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const putUser = createPutHttpAction(resource);
* putUser({ _id: ..., name: ...}).then(user => { ... }).catch(error => { ... });
*/
export const createPutHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('put', singular);
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = merge({}, defaults, payload);
const endpoint = `/${toLower(plural)}/${idOf(data)}`;
return put(endpoint, data).then(response => response.data);
},
};
// return put single resource action
return action;
};
/**
* @function createPatchHttpAction
* @name createPatchHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createPatchHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const patchUser = createPatchHttpAction(resource);
* patchUser({ _id: ..., name: ...}).then(user => { ... }).catch(error => { ... });
*/
export const createPatchHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('patch', singular);
// build action
const action = {
[methodName]: payload => {
// prepare data
const defaults = (resource.params || {}).filter;
const data = merge({}, defaults, payload);
const endpoint = `/${toLower(plural)}/${idOf(data)}`;
return patch(endpoint, data).then(response => response.data);
},
};
// return patch single resource action
return action;
};
/**
* @function createDeleteHttpAction
* @name createDeleteHttpAction
* @description generate http action to obtain single resource
* @param {Object} resource valid http resource definition
* @return {Object} http action to get single resource
* @since 0.7.0
* @version 0.1.0
* @example
* import { createDeleteHttpAction } from 'emis-api-client';
*
* const resource = { wellknown: 'user' };
* const deleteUser = createDeleteHttpAction(resource);
* deleteUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
export const createDeleteHttpAction = resource => {
// ensure resource
const {
shortcut: { singular },
wellknown: { plural },
} = normalizeResource(resource);
// generate method name
const methodName = fn('delete', singular);
// build action
const action = {
[methodName]: id => {
// prepare params
const endpoint = `/${toLower(plural)}/${id}`;
return del(endpoint).then(response => response.data);
},
};
// return delete single resource action
return action;
};
/**
* @function createHttpActionsFor
* @name createHttpActionsFor
* @description generate name http action shortcut to interact with resource
* @param {String} resource valid http resource.
* @description generate http actions to interact with resource
* @param {String} resource valid http resource
* @return {Object} http actions to interact with a resource

@@ -405,29 +709,26 @@ * @since 0.1.0

* const { deleteUser } = createHttpActionsFor('user');
* const deleteUser = del('/users/5c1766243c9d520004e2b542');
* deleteUser.then(user => { ... }).catch(error => { ... });
* deleteUser('5c176624').then(user => { ... }).catch(error => { ... });
*/
export const createHttpActionsFor = resource => {
const singular = singularize(resource);
const plural = pluralize(resource);
const httpActions = {
[fn('get', singular, 'Schema')]: () =>
get(`/${low(plural)}/schema`).then(response => response.data),
[fn('get', plural)]: params =>
get(`/${low(plural)}`, params).then(response => response.data),
[fn('get', singular)]: id =>
get(`/${low(plural)}/${id}`).then(response => response.data),
[fn('post', singular)]: data =>
post(`/${low(plural)}`, data).then(response => response.data),
[fn('put', singular)]: data =>
put(`/${low(plural)}/${idOf(data)}`, data).then(
response => response.data
),
[fn('patch', singular)]: data =>
patch(`/${low(plural)}/${idOf(data)}`, data).then(
response => response.data
),
[fn('delete', singular)]: id =>
del(`/${low(plural)}/${id}`).then(response => response.data),
};
// compose resource http actions
const getSchema = createGetSchemaHttpAction(resource);
const getResources = createGetListHttpAction(resource);
const getResource = createGetSingleHttpAction(resource);
const postResource = createPostHttpAction(resource);
const putResource = createPutHttpAction(resource);
const patchResource = createPatchHttpAction(resource);
const deleteResource = createDeleteHttpAction(resource);
// return resource http actions
const httpActions = merge(
{},
getSchema,
getResources,
getResource,
postResource,
putResource,
patchResource,
deleteResource
);
return httpActions;
};

@@ -1,212 +0,2 @@

import { get, createHttpActionsFor } from './client';
export * from './client';
export const getSchemas = () =>
get('/schemas').then(response => {
const schemas = response.data;
if (schemas) {
schemas.Warehouse = schemas.Feature;
}
return schemas;
});
export const {
getActivitySchema,
getActivities,
getActivity,
postActivity,
putActivity,
patchActivity,
deleteActivity,
} = createHttpActionsFor('activity');
export const {
getAdjustmentSchema,
getAdjustments,
getAdjustment,
postAdjustment,
putAdjustment,
patchAdjustment,
deleteAdjustment,
} = createHttpActionsFor('adjustment');
export const {
getAlertSchema,
getAlerts,
getAlert,
postAlert,
putAlert,
patchAlert,
deleteAlert,
} = createHttpActionsFor('alert');
export const {
getAlertSourceSchema,
getAlertSources,
getAlertSource,
postAlertSource,
putAlertSource,
patchAlertSource,
deleteAlertSource,
} = createHttpActionsFor('alertSource');
export const {
getAssessmentSchema,
getAssessments,
getAssessment,
postAssessment,
putAssessment,
patchAssessment,
deleteAssessment,
} = createHttpActionsFor('assessment');
export const {
getFeatureSchema,
getFeatures,
getFeature,
postFeature,
putFeature,
patchFeature,
deleteFeature,
} = createHttpActionsFor('feature');
export const {
getIncidentSchema,
getIncidents,
getIncident,
postIncident,
putIncident,
patchIncident,
deleteIncident,
} = createHttpActionsFor('incident');
export const {
getIncidentTypeSchema,
getIncidentTypes,
getIncidentType,
postIncidentType,
putIncidentType,
patchIncidentType,
deleteIncidentType,
} = createHttpActionsFor('incidentType');
export const {
getIndicatorSchema,
getIndicators,
getIndicator,
postIndicator,
putIndicator,
patchIndicator,
deleteIndicator,
} = createHttpActionsFor('indicator');
export const {
getItemSchema,
getItems,
getItem,
postItem,
putItem,
patchItem,
deleteItem,
} = createHttpActionsFor('item');
export const {
getPartySchema,
getParties,
getParty,
postParty,
putParty,
patchParty,
deleteParty,
} = createHttpActionsFor('party');
export const {
getPermissionSchema,
getPermissions,
getPermission,
postPermission,
putPermission,
patchPermission,
deletePermission,
} = createHttpActionsFor('permission');
export const {
getPlanSchema,
getPlans,
getPlan,
postPlan,
putPlan,
patchPlan,
deletePlan,
} = createHttpActionsFor('plan');
export const {
getProcedureSchema,
getProcedures,
getProcedure,
postProcedure,
putProcedure,
patchProcedure,
deleteProcedure,
} = createHttpActionsFor('procedure');
export const {
getQuestionSchema,
getQuestions,
getQuestion,
postQuestion,
putQuestion,
patchQuestion,
deleteQuestion,
} = createHttpActionsFor('question');
export const {
getQuestionnaireSchema,
getQuestionnaires,
getQuestionnaire,
postQuestionnaire,
putQuestionnaire,
patchQuestionnaire,
deleteQuestionnaire,
} = createHttpActionsFor('questionnaire');
export const {
getRoleSchema,
getRoles,
getRole,
postRole,
putRole,
patchRole,
deleteRole,
} = createHttpActionsFor('role');
export const {
getStockSchema,
getStocks,
getStock,
postStock,
putStock,
patchStock,
deleteStock,
} = createHttpActionsFor('stock');
export const {
getWarehouseSchema,
getWarehouses,
getWarehouse,
postWarehouse,
putWarehouse,
patchWarehouse,
deleteWarehouse,
} = createHttpActionsFor('warehouse');
export {
getPartySchema as getStakeholderSchema,
getParties as getStakeholders,
getParty as getStakeholder,
postParty as postStakeholder,
putParty as putStakeholder,
patchParty as patchStakeholder,
deleteParty as deleteStakeholder,
};
export * from './common';
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc