Skip to content

progres: migrate from deprecated common-http to common-request #1171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/cold-lions-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@openfn/language-progres': major
---

- Migrate from `common.http` to `common.request`.
- Remove `axios` and `nock` from progres.
- Remove exportation of `http` from `common`, and the exportation of `axios`.
15 changes: 13 additions & 2 deletions packages/progres/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@
"operation"
],
"docs": {
"description": "Scopes an array of data based on a JSONPath.\nUseful when the source data has `n` items you would like to map to\nan operation.\nThe operation will receive a slice of the data based of each item\nof the JSONPath provided.\n\nIt also ensures the results of an operation make their way back into\nthe state's references.",
"description": "Iterates over an array of items and invokes an operation upon each one, where the state\nobject is _scoped_ so that state.data is the item under iteration.\nThe rest of the state object is untouched and can be referenced as usual.\nYou can pass an array directly, or use lazy state or a JSONPath string to\nreference a slice of state.",
"tags": [
{
"title": "public",
Expand All @@ -345,7 +345,18 @@
},
{
"title": "example",
"description": "each(\"$.[*]\",\n create(\"SObject\",\n field(\"FirstName\", sourceValue(\"$.firstName\"))\n )\n)"
"description": "each(\n $.data,\n // Inside the callback operation, `$.data` is scoped to the item under iteration\n insert(\"patient\", {\n patient_name: $.data.properties.case_name,\n patient_id: $.data.case_id,\n })\n);",
"caption": "Using lazy state ($) to iterate over items in state.data and pass each into an \"insert\" operation"
},
{
"title": "example",
"description": "each(\n $.data,\n insert(\"patient\", (state) => ({\n patient_id: state.data.case_id,\n ...state.data\n }))\n);",
"caption": "Iterate over items in state.data and pass each one into an \"insert\" operation"
},
{
"title": "example",
"description": "each(\n \"$.data[*]\",\n insert(\"patient\", (state) => ({\n patient_name: state.data.properties.case_name,\n patient_id: state.data.case_id,\n }))\n);",
"caption": "Using JSON path to iterate over items in state.data and pass each one into an \"insert\" operation"
},
{
"title": "param",
Expand Down
6 changes: 2 additions & 4 deletions packages/progres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "workspace:*",
"axios": "^1.7.7"
"@openfn/language-common": "workspace:*"
},
"devDependencies": {
"assertion-error": "^1.0.1",
"chai": "^4.2.0",
"deep-eql": "^4.0.0",
"esno": "^0.16.3",
"nock": "^13.0.5",
"rimraf": "^3.0.2",
"sinon": "^9.2.3"
},
Expand All @@ -47,4 +45,4 @@
"./package.json": "./package.json"
},
"types": "types/index.d.ts"
}
}
41 changes: 18 additions & 23 deletions packages/progres/src/Adaptor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {
execute as commonExecute,
composeNextState,
http, // Important: this is the OLD axios-based http
} from '@openfn/language-common';
import { expandReferences } from '@openfn/language-common/util';
import { expandReferences, request } from '@openfn/language-common/util';

/**
* Execute a sequence of operations.
Expand All @@ -12,7 +11,7 @@ import { expandReferences } from '@openfn/language-common/util';
* execute(
* create('foo'),
* delete('bar')
* )(state)
* )
* @private
* @param {Operations} operations - Operations to be performed.
* @returns {Operation}
Expand Down Expand Up @@ -45,36 +44,33 @@ export function execute(...operations) {
* key,
* cert,
* },
* }, callback)(state)
* }, callback)
* @function
* @param {object} params - Url, Headers and Body parameters
* @param {function} callback - (Optional) A callback function
* @returns {Operation}
*/
export function postData(params, callback) {
return state => {
export function postData(params, callback = s => s) {
return async state => {
const [resolvedParams] = expandReferences(state, params);
const { url, body, headers, agentOptions } = resolvedParams;
return http
.post(url, body,{
headers,
agentOptions,
})(state)
.then(response => {
console.log('POST succeeded.');

const nextState = composeNextState(state, response);
if (callback) return callback(nextState);
return nextState;
})
.catch(error => {
console.log(error);
return error;
});
const result = await request('POST', url, {
body,
headers,
agentOptions,
});
console.log('POST succeeded.');

const { body: responseBody, ...responseWithoutBody } = result;

return callback({
...composeNextState(state, responseBody),
response: responseWithoutBody,
});
};
}

// What functions do you want from the common adaptor?
export {
fn,
fnIf,
Expand All @@ -84,7 +80,6 @@ export {
each,
field,
fields,
http, // Important: this is the OLD axios-based http. Public docs will be incorrect.
lastReferenceValue,
merge,
sourceValue,
Expand Down
28 changes: 15 additions & 13 deletions packages/progres/test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect } from 'chai';

import nock from 'nock';
import ClientFixtures, { fixtures } from './ClientFixtures';

import { enableMockClient } from '@openfn/language-common/util';
import Adaptor from '../src';

const { execute, postData } = Adaptor;
const testServer = enableMockClient('https://fake.server.com');

describe('execute', () => {
it('executes each operation in sequence', done => {
Expand Down Expand Up @@ -41,30 +40,33 @@ describe('execute', () => {
});

describe('post', () => {
before(() => {
nock('https://fake.server.com').post('/api').reply(200, { foo: 'bar' });
});

it('calls the callback', () => {
it('calls the callback', async () => {
let state = {
configuration: {
username: 'hello',
password: 'there',
},
};

return execute(
testServer
.intercept({
method: 'POST',
path: '/api',
})
.reply(200, { foo: 'bar' });

await execute(
postData({
url: 'https://fake.server.com/api',
headers: null,
body: { a: 1 },
})
)(state).then(state => {
let status = state.data.status;
let responseBody = state.data.data;
// Check that the post made it's way to the request as a string.
let status = state.response.statusCode;
let responseBody = state.data;
expect(status).to.eq(200);
expect(responseBody).to.eql({ foo: 'bar' });
expect(JSON.parse(responseBody)).to.eql({ foo: 'bar' });
});
});
});
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.