Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
tests, interceptor, remove upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Dec 21, 2021
1 parent 446f881 commit 220dee8
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 253 deletions.
121 changes: 0 additions & 121 deletions ast.json
Expand Up @@ -355,127 +355,6 @@
},
"valid": true
},
{
"name": "upsert",
"params": [
"resourceType",
"data",
"options",
"callback"
],
"docs": {
"description": "Upsert a record. A generic helper function used to atomically either insert a row, or on the basis of the row already existing, UPDATE that existing row instead.",
"tags": [
{
"title": "public",
"description": null,
"type": null
},
{
"title": "function",
"description": null,
"name": null
},
{
"title": "param",
"description": "The type of a resource to `insert` or `update`. E.g. `trackedEntityInstances`",
"type": {
"type": "NameExpression",
"name": "string"
},
"name": "resourceType"
},
{
"title": "param",
"description": "The update data containing new values",
"type": {
"type": "NameExpression",
"name": "Object"
},
"name": "data"
},
{
"title": "param",
"description": "`Optional` options for `upsertTEI` operation. Defaults to `{replace: false, apiVersion: state.configuration.apiVersion,strict: true,responseType: 'json'}`.",
"type": {
"type": "OptionalType",
"expression": {
"type": "RecordType",
"fields": [
{
"type": "FieldType",
"key": "replace",
"value": {
"type": "NameExpression",
"name": "boolean"
}
},
{
"type": "FieldType",
"key": "apiVersion",
"value": {
"type": "NameExpression",
"name": "number"
}
},
{
"type": "FieldType",
"key": "strict",
"value": {
"type": "NameExpression",
"name": "boolean"
}
},
{
"type": "FieldType",
"key": "responseType",
"value": {
"type": "NameExpression",
"name": "string"
}
}
]
}
},
"name": "options"
},
{
"title": "param",
"description": "Optional callback to handle the response",
"type": {
"type": "OptionalType",
"expression": {
"type": "NameExpression",
"name": "function"
}
},
"name": "callback"
},
{
"title": "throws",
"description": "Throws range error",
"type": {
"type": "NameExpression",
"name": "RangeError"
}
},
{
"title": "returns",
"description": null,
"type": {
"type": "NameExpression",
"name": "Operation"
}
},
{
"title": "example",
"description": "upsert(\n 'trackedEntityInstances',\n {\n attributeId: 'lZGmxYbs97q',\n attributeValue: state =>\n state.data.attributes.find(obj => obj.attribute === 'lZGmxYbs97q')\n .value,\n },\n state.data,\n { ou: 'TSyzvBiovKh' }\n);",
"caption": "Example `expression.js` of upsert"
}
]
},
"valid": true
},
{
"name": "discover",
"params": [
Expand Down
120 changes: 59 additions & 61 deletions lib/Adaptor.js
Expand Up @@ -7,7 +7,6 @@ exports.execute = execute;
exports.create = create;
exports.update = update;
exports.get = get;
exports.upsert = upsert;
exports.discover = discover;
exports.patch = patch;
exports.del = del;
Expand Down Expand Up @@ -178,26 +177,20 @@ _axios.default.interceptors.response.use(function (response) {

return response;
}, function (error) {
try {
var _details$config, _details$config2;
var _error$config, _error$config2, _error$response, _error$response$data, _error$response2;

const details = error.toJSON();
if (details === null || details === void 0 ? void 0 : (_details$config = details.config) === null || _details$config === void 0 ? void 0 : _details$config.auth) details.config.auth = '--REDACTED--';
if (details === null || details === void 0 ? void 0 : (_details$config2 = details.config) === null || _details$config2 === void 0 ? void 0 : _details$config2.data) details.config.data = '--REDACTED--';
if ((_error$config = error.config) === null || _error$config === void 0 ? void 0 : _error$config.auth) error.config.auth = '--REDACTED--';
if ((_error$config2 = error.config) === null || _error$config2 === void 0 ? void 0 : _error$config2.data) error.config.data = '--REDACTED--';
const details = (_error$response = error.response) === null || _error$response === void 0 ? void 0 : (_error$response$data = _error$response.data) === null || _error$response$data === void 0 ? void 0 : _error$response$data.response;

_Utils.Log.error(JSON.stringify(details, null, 2));
_Utils.Log.error(error.message || "That didn't work.");

return Promise.reject({
error: error.message,
data: error.response.data
});
} catch (e) {
// TODO: @Elias, why does this error sometimes already appear to be JSONified?
// console.log(e) // "not JSONABLE TypeError: error.toJSON is not a function"
_Utils.Log.error(error.message);

return Promise.reject(error);
}
if (details) console.log(JSON.stringify(details, null, 2));
return Promise.reject({
request: error.config,
error: error.message,
response: (_error$response2 = error.response) === null || _error$response2 === void 0 ? void 0 : _error$response2.data
});
});
/**
* Create a record
Expand Down Expand Up @@ -538,50 +531,55 @@ function get(resourceType, filters, options, callback) {
return (0, _Utils.handleResponse)(result, state, callback);
});
};
}
/**
* Upsert a record. A generic helper function used to atomically either insert a row, or on the basis of the row already existing, UPDATE that existing row instead.
* @public
* @function
* @param {string} resourceType - The type of a resource to `insert` or `update`. E.g. `trackedEntityInstances`
* @param {Object} data - The update data containing new values
* @param {{replace:boolean, apiVersion: number,strict: boolean,responseType: string}} [options] - `Optional` options for `upsertTEI` operation. Defaults to `{replace: false, apiVersion: state.configuration.apiVersion,strict: true,responseType: 'json'}`.
* @param {function} [callback] - Optional callback to handle the response
* @throws {RangeError} - Throws range error
* @returns {Operation}
* @example <caption>Example `expression.js` of upsert</caption>
* upsert(
* 'trackedEntityInstances',
* {
* attributeId: 'lZGmxYbs97q',
* attributeValue: state =>
* state.data.attributes.find(obj => obj.attribute === 'lZGmxYbs97q')
* .value,
* },
* state.data,
* { ou: 'TSyzvBiovKh' }
* );
*/
} // /**
// * Upsert a record. A generic helper function used to atomically either insert a row, or on the basis of the row already existing, UPDATE that existing row instead.
// * @public
// * @function
// * @param {string} resourceType - The type of a resource to `insert` or `update`. E.g. `trackedEntityInstances`
// * @param {Object} data - The update data containing new values
// * @param {{replace:boolean, apiVersion: number,strict: boolean,responseType: string}} [options] - `Optional` options for `upsertTEI` operation. Defaults to `{replace: false, apiVersion: state.configuration.apiVersion,strict: true,responseType: 'json'}`.
// * @param {function} [callback] - Optional callback to handle the response
// * @throws {RangeError} - Throws range error
// * @returns {Operation}
// * @example <caption>Example `expression.js` of upsert</caption>
// * upsert(
// * 'trackedEntityInstances',
// * {
// * attributeId: 'lZGmxYbs97q',
// * attributeValue: state =>
// * state.data.attributes.find(obj => obj.attribute === 'lZGmxYbs97q')
// * .value,
// * },
// * state.data,
// * { ou: 'TSyzvBiovKh' }
// * );
// */
// export function upsert(resourceType, data, options, callback) {
// return state => {
// console.log(`Preparing upsert via 'get' then 'create' OR 'update'...`);
// return get(
// resourceType,
// data
// )(state).then(resp => {
// const resources = resp.data[resourceType];
// if (resources.length > 1) {
// throw new RangeError(
// `Cannot upsert on Non-unique attribute. The operation found more than one records for your request.`
// );
// } else if (resources.length <= 0) {
// return create(resourceType, data, options, callback)(state);
// } else {
// const pathName =
// resourceType === 'trackedEntityInstances'
// ? 'trackedEntityInstance'
// : 'id';
// const path = resources[0][pathName];
// return update(resourceType, path, data, options, callback)(state);
// }
// });
// };
// }


function upsert(resourceType, data, options, callback) {
return state => {
console.log(`Preparing upsert via 'get' then 'create' OR 'update'...`);
return get(resourceType, data)(state).then(resp => {
const resources = resp.data[resourceType];

if (resources.length > 1) {
throw new RangeError(`Cannot upsert on Non-unique attribute. The operation found more than one records for your request.`);
} else if (resources.length <= 0) {
return create(resourceType, data, options, callback)(state);
} else {
const pathName = resourceType === 'trackedEntityInstances' ? 'trackedEntityInstance' : 'id';
const path = resources[0][pathName];
return update(resourceType, path, data, options, callback)(state);
}
});
};
}
/**
* Discover `DHIS2` `api` `endpoint` `query parameters` and allowed `operators` for a given resource's endpoint.
* @public
Expand Down
6 changes: 3 additions & 3 deletions lib/Utils.js
Expand Up @@ -24,15 +24,15 @@ exports.CONTENT_TYPES = CONTENT_TYPES;

class Log {
static success(message) {
return console.info(`✓ ${message} @ ${new Date()}`);
return console.info(`✓ Success at ${new Date()}:\n`, message);
}

static warn(message) {
return console.warn(`⚠ Warning: ${message} @ ${new Date()}`);
return console.warn(`⚠ Warning at ${new Date()}:\n`, message);
}

static error(message) {
return console.error(`✗ Error: ${message} @ ${new Date()}`);
return console.error(`✗ Error at ${new Date()}:\n`, message);
}

}
Expand Down

0 comments on commit 220dee8

Please sign in to comment.