From d04453ecef4da4f064f88206fff18a6acea04675 Mon Sep 17 00:00:00 2001 From: Hunter Achieng Date: Mon, 3 Feb 2025 19:39:23 +0300 Subject: [PATCH] fix: default format json in utils Signed-off-by: Hunter Achieng --- packages/kobotoolbox/src/Utils.js | 5 +++- packages/kobotoolbox/src/http.js | 34 +++++++++++------------- packages/kobotoolbox/test/index.js | 42 ++++++++++++++++++++---------- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/packages/kobotoolbox/src/Utils.js b/packages/kobotoolbox/src/Utils.js index a81ca7cef..b6ab3ac5a 100644 --- a/packages/kobotoolbox/src/Utils.js +++ b/packages/kobotoolbox/src/Utils.js @@ -28,7 +28,10 @@ export async function request(state, method, path, opts) { ...authHeaders, ...headers, }, - query, + query: { + format: 'json', + ...query, + }, parseAs, baseUrl: `${baseURL}/api/${apiVersion}`, }; diff --git a/packages/kobotoolbox/src/http.js b/packages/kobotoolbox/src/http.js index 8f2526bcd..ccdeabd2f 100644 --- a/packages/kobotoolbox/src/http.js +++ b/packages/kobotoolbox/src/http.js @@ -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 @@ -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 GET forms resource + * @example GET assets resource * 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 = {}) { @@ -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 Create an asset resource @@ -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 = {}) { @@ -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 Update an asset resource @@ -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 = {}) { diff --git a/packages/kobotoolbox/test/index.js b/packages/kobotoolbox/test/index.js index 94ad097f1..e612a217c 100644 --- a/packages/kobotoolbox/test/index.js +++ b/packages/kobotoolbox/test/index.js @@ -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/', @@ -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'); }); @@ -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'); });