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

discover(...) fails, also may be irrelevant/unnecessary #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 0 additions & 54 deletions ast.json
Expand Up @@ -360,60 +360,6 @@
},
"valid": true
},
{
"name": "discover",
"params": [
"httpMethod",
"endpoint"
],
"docs": {
"description": "Discover `DHIS2` `api` `endpoint` `query parameters` and allowed `operators` for a given resource's endpoint.",
"tags": [
{
"title": "public",
"description": null,
"type": null
},
{
"title": "function",
"description": null,
"name": null
},
{
"title": "param",
"description": "The HTTP to inspect parameter usage for a given endpoint, e.g., `get`, `post`,`put`,`patch`,`delete`",
"type": {
"type": "NameExpression",
"name": "string"
},
"name": "httpMethod"
},
{
"title": "param",
"description": "The path for a given endpoint. E.g. `/trackedEntityInstances` or `/dataValueSets`",
"type": {
"type": "NameExpression",
"name": "string"
},
"name": "endpoint"
},
{
"title": "returns",
"description": null,
"type": {
"type": "NameExpression",
"name": "Operation"
}
},
{
"title": "example",
"description": "discover('post', '/trackedEntityInstances')",
"caption": "a list of parameters allowed on a given endpoint for specific http method"
}
]
},
"valid": true
},
{
"name": "patch",
"params": [
Expand Down
58 changes: 0 additions & 58 deletions lib/Adaptor.js
Expand Up @@ -7,7 +7,6 @@ exports.execute = execute;
exports.create = create;
exports.update = update;
exports.get = get;
exports.discover = discover;
exports.patch = patch;
exports.destroy = destroy;
exports.findAttributeValue = findAttributeValue;
Expand Down Expand Up @@ -586,63 +585,6 @@ function get(resourceType, query, options = {}, callback = false) {
// };
// }

/**
* Discover `DHIS2` `api` `endpoint` `query parameters` and allowed `operators` for a given resource's endpoint.
* @public
* @function
* @param {string} httpMethod - The HTTP to inspect parameter usage for a given endpoint, e.g., `get`, `post`,`put`,`patch`,`delete`
* @param {string} endpoint - The path for a given endpoint. E.g. `/trackedEntityInstances` or `/dataValueSets`
* @returns {Operation}
* @example <caption>a list of parameters allowed on a given endpoint for specific http method</caption>
* discover('post', '/trackedEntityInstances')
*/


function discover(httpMethod, endpoint) {
return state => {
console.log(`Discovering query/import parameters for ${httpMethod} on ${endpoint}`);
return _axios.default.get('https://dhis2.github.io/dhis2-api-specification/spec/metadata_openapi.json', {
transformResponse: [data => {
let tempData = JSON.parse(data);
let filteredData = tempData.paths[endpoint][httpMethod];
return { ...filteredData,
parameters: filteredData.parameters.reduce((acc, currentValue) => {
let index = currentValue['$ref'].lastIndexOf('/') + 1;
let paramRef = currentValue['$ref'].slice(index);
let param = tempData.components.parameters[paramRef];

if (param.schema['$ref']) {
let schemaRefIndex = param.schema['$ref'].lastIndexOf('/') + 1;
let schemaRef = param.schema['$ref'].slice(schemaRefIndex);
param.schema = tempData.components.schemas[schemaRef];
}

param.schema = JSON.stringify(param.schema);
let descIndex;
if ((0, _lodash.indexOf)(param.description, ',') === -1 && (0, _lodash.indexOf)(param.description, '.') > -1) descIndex = (0, _lodash.indexOf)(param.description, '.');else if ((0, _lodash.indexOf)(param.description, ',') > -1 && (0, _lodash.indexOf)(param.description, '.') > -1) {
descIndex = (0, _lodash.indexOf)(param.description, '.') < (0, _lodash.indexOf)(param.description, ',') ? (0, _lodash.indexOf)(param.description, '.') : (0, _lodash.indexOf)(param.description, ',');
} else {
descIndex = param.description.length;
}
param.description = param.description.slice(0, descIndex);
acc[paramRef] = param;
return acc;
}, {})
};
}]
}).then(result => {
var _result$data$descript;

console.log(`\t=======================================================================================\n\tQuery Parameters for ${httpMethod} on ${endpoint} [${(_result$data$descript = result.data.description) !== null && _result$data$descript !== void 0 ? _result$data$descript : '<description_missing>'}]\n\t=======================================================================================`);
console.table(result.data.parameters, ['in', 'required', 'description']);
console.table(result.data.parameters, ['schema']);
console.log(`=========================================Responses===============================\n${(0, _Utils.prettyJson)(result.data.responses)}\n=======================================================================================`);
return { ...state,
data: result.data
};
});
};
}
/**
* Patch a record. A generic helper function to send partial updates on one or more object properties.
* - You are not required to send the full body of object properties.
Expand Down
95 changes: 0 additions & 95 deletions src/Adaptor.js
Expand Up @@ -503,101 +503,6 @@ export function get(resourceType, query, options = {}, callback = false) {
// };
// }

/**
* Discover `DHIS2` `api` `endpoint` `query parameters` and allowed `operators` for a given resource's endpoint.
* @public
* @function
* @param {string} httpMethod - The HTTP to inspect parameter usage for a given endpoint, e.g., `get`, `post`,`put`,`patch`,`delete`
* @param {string} endpoint - The path for a given endpoint. E.g. `/trackedEntityInstances` or `/dataValueSets`
* @returns {Operation}
* @example <caption>a list of parameters allowed on a given endpoint for specific http method</caption>
* discover('post', '/trackedEntityInstances')
*/
export function discover(httpMethod, endpoint) {
return state => {
console.log(
`Discovering query/import parameters for ${httpMethod} on ${endpoint}`
);
return axios
.get(
'https://dhis2.github.io/dhis2-api-specification/spec/metadata_openapi.json',
{
transformResponse: [
data => {
let tempData = JSON.parse(data);
let filteredData = tempData.paths[endpoint][httpMethod];
return {
...filteredData,
parameters: filteredData.parameters.reduce(
(acc, currentValue) => {
let index = currentValue['$ref'].lastIndexOf('/') + 1;
let paramRef = currentValue['$ref'].slice(index);
let param = tempData.components.parameters[paramRef];

if (param.schema['$ref']) {
let schemaRefIndex =
param.schema['$ref'].lastIndexOf('/') + 1;
let schemaRef = param.schema['$ref'].slice(
schemaRefIndex
);
param.schema = tempData.components.schemas[schemaRef];
}

param.schema = JSON.stringify(param.schema);

let descIndex;
if (
indexOf(param.description, ',') === -1 &&
indexOf(param.description, '.') > -1
)
descIndex = indexOf(param.description, '.');
else if (
indexOf(param.description, ',') > -1 &&
indexOf(param.description, '.') > -1
) {
descIndex =
indexOf(param.description, '.') <
indexOf(param.description, ',')
? indexOf(param.description, '.')
: indexOf(param.description, ',');
} else {
descIndex = param.description.length;
}

param.description = param.description.slice(0, descIndex);

acc[paramRef] = param;
return acc;
},
{}
),
};
},
],
}
)
.then(result => {
console.log(
`\t=======================================================================================\n\tQuery Parameters for ${httpMethod} on ${endpoint} [${
result.data.description ?? '<description_missing>'
}]\n\t=======================================================================================`
);
console.table(result.data.parameters, [
'in',
'required',
'description',
]);
console.table(result.data.parameters, ['schema']);
console.log(
`=========================================Responses===============================\n${prettyJson(
result.data.responses
)}\n=======================================================================================`
);
return { ...state, data: result.data };
});
};
}

/**
* Patch a record. A generic helper function to send partial updates on one or more object properties.
* - You are not required to send the full body of object properties.
Expand Down