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

Commit

Permalink
Merge branch 'new_pattern_for_helpers' of github.com:OpenFn/language-…
Browse files Browse the repository at this point in the history
…dhis2 into new_pattern_for_helpers
  • Loading branch information
elias-ba committed Dec 21, 2021
2 parents 7ec7508 + f5fb817 commit e9c3cfa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
12 changes: 11 additions & 1 deletion ast.json
Expand Up @@ -274,6 +274,7 @@
"name": "get",
"params": [
"resourceType",
"filters",
"options",
"callback"
],
Expand Down Expand Up @@ -301,7 +302,16 @@
},
{
"title": "param",
"description": "Optional `options` to define URL parameters (E.g. `filters`, `dimensions` and `import parameters`), axios configurations (E.g. `auth`) and DHIS 2 api version to use.",
"description": "Filters to limit what resources are retrieved.",
"type": {
"type": "NameExpression",
"name": "Object"
},
"name": "filters"
},
{
"title": "param",
"description": "Optional `options` to define URL parameters beyond filters, request configuration (e.g. `auth`) and DHIS2 api version to use.",
"type": {
"type": "OptionalType",
"expression": {
Expand Down
12 changes: 8 additions & 4 deletions lib/Adaptor.js
Expand Up @@ -487,7 +487,8 @@ function update(resourceType, path, data, options, callback) {
* @public
* @function
* @param {string} resourceType - The type of resource to get(use its `plural` name). E.g. `dataElements`, `trackedEntityInstances`,`organisationUnits`, etc.
* @param {Object} [options] - Optional `options` to define URL parameters (E.g. `filters`, `dimensions` and `import parameters`), axios configurations (E.g. `auth`) and DHIS 2 api version to use.
* @param {Object} filters - Filters to limit what resources are retrieved.
* @param {Object} [options] - Optional `options` to define URL parameters beyond filters, request configuration (e.g. `auth`) and DHIS2 api version to use.
* @param {function} [callback] - Optional callback to handle the response
* @returns {Operation} state
* @example <caption>Example getting one `trackedEntityInstance` with `Id` 'dNpxRu1mWG5' for a given `orgUnit(DiszpKrYNg8)`</caption>
Expand All @@ -500,10 +501,11 @@ function update(resourceType, path, data, options, callback) {
*/


function get(resourceType, options, callback) {
function get(resourceType, filters, options, callback) {
return state => {
console.log(`Preparing get operation...`);
resourceType = (0, _languageCommon.expandReferences)(resourceType)(state);
filters = (0, _languageCommon.expandReferences)(filters)(state);
options = (0, _languageCommon.expandReferences)(options)(state);
const {
params,
Expand All @@ -515,7 +517,9 @@ function get(resourceType, options, callback) {
return (0, _Client.request)(configuration, {
method: 'get',
url: (0, _Utils.generateUrl)(configuration, options, resourceType),
params: params && (0, _Utils.buildUrlParams)(params),
params: (0, _Utils.buildUrlParams)({ ...filters,
...params
}),
responseType: 'json',
...requestConfig
}).then(result => {
Expand Down Expand Up @@ -553,7 +557,7 @@ function get(resourceType, options, callback) {
function upsert(resourceType, data, options, callback) {
return state => {
console.log(`Preparing upsert via 'get' then 'create' OR 'update'...`);
return get(resourceType, options)(state).then(resp => {
return get(resourceType, data)(state).then(resp => {
const resources = resp.data[resourceType];

if (resources.length > 1) {
Expand Down
15 changes: 8 additions & 7 deletions src/Adaptor.js
Expand Up @@ -406,7 +406,8 @@ export function update(resourceType, path, data, options, callback) {
* @public
* @function
* @param {string} resourceType - The type of resource to get(use its `plural` name). E.g. `dataElements`, `trackedEntityInstances`,`organisationUnits`, etc.
* @param {Object} [options] - Optional `options` to define URL parameters (E.g. `filters`, `dimensions` and `import parameters`), axios configurations (E.g. `auth`) and DHIS 2 api version to use.
* @param {Object} filters - Filters to limit what resources are retrieved.
* @param {Object} [options] - Optional `options` to define URL parameters beyond filters, request configuration (e.g. `auth`) and DHIS2 api version to use.
* @param {function} [callback] - Optional callback to handle the response
* @returns {Operation} state
* @example <caption>Example getting one `trackedEntityInstance` with `Id` 'dNpxRu1mWG5' for a given `orgUnit(DiszpKrYNg8)`</caption>
Expand All @@ -417,11 +418,12 @@ export function update(resourceType, path, data, options, callback) {
* trackedEntityInstance: 'dNpxRu1mWG5',
* });
*/
export function get(resourceType, options, callback) {
export function get(resourceType, filters, options, callback) {
return state => {
console.log(`Preparing get operation...`);

resourceType = expandReferences(resourceType)(state);
filters = expandReferences(filters)(state);
options = expandReferences(options)(state);

const { params, requestConfig } = options || {};
Expand All @@ -430,7 +432,7 @@ export function get(resourceType, options, callback) {
return request(configuration, {
method: 'get',
url: generateUrl(configuration, options, resourceType),
params: params && buildUrlParams(params),
params: buildUrlParams({ ...filters, ...params }),
responseType: 'json',
...requestConfig,
}).then(result => {
Expand Down Expand Up @@ -471,7 +473,7 @@ export function upsert(resourceType, data, options, callback) {

return get(
resourceType,
options
data
)(state).then(resp => {
const resources = resp.data[resourceType];
if (resources.length > 1) {
Expand Down Expand Up @@ -526,9 +528,8 @@ export function discover(httpMethod, endpoint) {
if (param.schema['$ref']) {
let schemaRefIndex =
param.schema['$ref'].lastIndexOf('/') + 1;
let schemaRef = param.schema['$ref'].slice(
schemaRefIndex
);
let schemaRef =
param.schema['$ref'].slice(schemaRefIndex);
param.schema = tempData.components.schemas[schemaRef];
}

Expand Down

0 comments on commit e9c3cfa

Please sign in to comment.