Skip to content

Commit

Permalink
fix: default format json in utils
Browse files Browse the repository at this point in the history
Signed-off-by: Hunter Achieng <[email protected]>
  • Loading branch information
hunterachieng committed Feb 3, 2025
1 parent 18a6094 commit d04453e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
5 changes: 4 additions & 1 deletion packages/kobotoolbox/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export async function request(state, method, path, opts) {
...authHeaders,
...headers,
},
query,
query: {
format: 'json',
...query,
},
parseAs,
baseUrl: `${baseURL}/api/${apiVersion}`,
};
Expand Down
34 changes: 15 additions & 19 deletions packages/kobotoolbox/src/http.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { expandReferences } from '@openfn/language-common/util';
import * as util from './Utils';

/**
* State object
* @typedef {Object} KoboToolboxHttpState
* @property data - The response body (as JSON)
* @property response - The HTTP response from the KoboToolbox server (excluding the body). Responses will be returned in JSON format
* @property references - An array of all previous data objects used in the Job
*/

/**
* Options object
* @typedef {Object} RequestOptions
Expand All @@ -10,20 +18,16 @@ import * as util from './Utils';
*/

/**
* Make a GET request to any Kobotoolbox endpoint.
* Make a GET request to any KoboToolbox endpoint.
* @public
* @function
* @example <caption>GET forms resource</caption>
* @example <caption>GET assets resource</caption>
* http.get(
* "/assets/",
* {
* query: {
* format: json
* }
* }
* )
* @param {string} path - path to resource
* @param {RequestOptions} [options={}] - An object containing query params and headers for the request
* @state {KoboToolboxHttpState}
* @returns {operation}
*/
export function get(path, options = {}) {
Expand All @@ -46,7 +50,7 @@ export function get(path, options = {}) {
}

/**
* Make a POST request to a Kobotoolbox endpoint
* Make a POST request to a KoboToolbox endpoint
* @public
* @function
* @example <caption>Create an asset resource</caption>
Expand All @@ -56,15 +60,11 @@ export function get(path, options = {}) {
* name: 'Feedback Survey Test',
* asset_type: 'survey',
* },
* {
* query: {
* format: 'json',
* },
* }
* );
* @param {string} path - path to resource
* @param {any} data - the body data in JSON format
* @param {RequestOptions} [options={}] - An object containing query params and headers for the request
* @state {KoboToolboxHttpState}
* @returns {operation}
*/
export function post(path, data, options = {}) {
Expand Down Expand Up @@ -92,7 +92,7 @@ export function post(path, data, options = {}) {
}

/**
* Make a PUT request to a Kobotoolbox endpoint
* Make a PUT request to a KoboToolbox endpoint
* @public
* @function
* @example <caption>Update an asset resource</caption>
Expand All @@ -102,15 +102,11 @@ export function post(path, data, options = {}) {
* name: 'Feedback Survey Test',
* asset_type: 'survey',
* },
* {
* query: {
* format: 'json',
* },
* }
* );
* @param {string} path - path to resource
* @param {any} data - the body data in JSON format
* @param {RequestOptions} [options={}] - An object containing query params and headers for the request
* @state {KoboToolboxHttpState}
* @returns {operation}
*/
export function put(path, data, options = {}) {
Expand Down
42 changes: 28 additions & 14 deletions packages/kobotoolbox/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,30 @@ describe('execute', () => {
});
});

describe('http', () => {
it('should return responses in JSON format', async () => {
testServer
.intercept({
path: '/api/v2/assets/',
query: { format: 'json' },
method: 'GET',
})
.reply(
200,
{ results: [{ name: 'Feedback Survey Test', asset_type: 'survey' }] },
{ ...jsonHeaders }
);
const state = { configuration };

const response = await http.get('/assets/')(state);
expect(response.response.headers['content-type']).to.eql(
'application/json'
);
});
});

describe('http.get', () => {
it('should GET with a query', async () => {
it('should make a GET request', async () => {
testServer
.intercept({
path: '/api/v2/assets/',
Expand All @@ -72,9 +94,7 @@ describe('http.get', () => {
);
const state = { configuration };

const { data } = await http.get('/assets/', {
query: { format: 'json' },
})(state);
const { data } = await http.get('/assets/')(state);

expect(data.results[0].name).to.eql('Feedback Survey Test');
});
Expand All @@ -99,16 +119,10 @@ describe('http.post', () => {
);
const state = { configuration };

const { data } = await http.post(
'/assets/',
{
name: 'Feedback Survey Test',
asset_type: 'survey',
},
{
query: { format: 'json' },
}
)(state);
const { data } = await http.post('/assets/', {
name: 'Feedback Survey Test',
asset_type: 'survey',
})(state);

expect(data.name).to.eql('Feedback Survey Test');
});
Expand Down

0 comments on commit d04453e

Please sign in to comment.