Skip to content

Commit

Permalink
common: request throws if parseAs is json and the response body is em…
Browse files Browse the repository at this point in the history
…pty (#906)

* fix: remove parse for undefined body response

Signed-off-by: Hunter Achieng <[email protected]>

* feat: add changeset

Signed-off-by: Hunter Achieng <[email protected]>

* fix: switch to patch changeset

Signed-off-by: Hunter Achieng <[email protected]>

* update tests and readResponse

Signed-off-by: Hunter Achieng <[email protected]>

* add await to response json

Signed-off-by: Hunter Achieng <[email protected]>

* fix tests

Signed-off-by: Hunter Achieng <[email protected]>

* fix patch statement and add test

Signed-off-by: Hunter Achieng <[email protected]>

* bump version and update their changelog

---------

Signed-off-by: Hunter Achieng <[email protected]>
Co-authored-by: Emmanuel Evance <[email protected]>
  • Loading branch information
hunterachieng and mtuchi authored Jan 16, 2025
1 parent 9411de5 commit a4e8f74
Show file tree
Hide file tree
Showing 104 changed files with 479 additions and 69 deletions.
7 changes: 7 additions & 0 deletions packages/asana/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-asana

## 4.0.7

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 4.0.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/asana/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-asana",
"version": "4.0.6",
"version": "4.0.7",
"description": "An adaptor to access objects in Asana",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
7 changes: 7 additions & 0 deletions packages/azure-storage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-azure-storage

## 2.0.7

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 2.0.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/azure-storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-azure-storage",
"version": "2.0.6",
"version": "2.0.7",
"description": "OpenFn adaptor for Azure Storage",
"type": "module",
"exports": {
Expand Down
7 changes: 7 additions & 0 deletions packages/beyonic/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-beyonic

## 0.3.8

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 0.3.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/beyonic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-beyonic",
"version": "0.3.7",
"version": "0.3.8",
"description": "beyonic Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
7 changes: 7 additions & 0 deletions packages/bigquery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-bigquery

## 3.0.8

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 3.0.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/bigquery/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-bigquery",
"version": "3.0.7",
"version": "3.0.8",
"description": "A Google BigQuery language package for use with Open Function",
"main": "dist/index.cjs",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions packages/cartodb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-cartodb

## 0.4.9

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 0.4.8

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cartodb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-cartodb",
"version": "0.4.8",
"version": "0.4.9",
"description": "cartodb Language Pack for OpenFn",
"main": "dist/index.cjs",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions packages/cht/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-cht

## 1.0.7

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 1.0.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cht/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-cht",
"version": "1.0.6",
"version": "1.0.7",
"description": "OpenFn CHT adaptor",
"type": "module",
"exports": {
Expand Down
7 changes: 7 additions & 0 deletions packages/collections/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-collections

## 0.7.3

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 0.7.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/collections/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-collections",
"version": "0.7.2",
"version": "0.7.3",
"description": "OpenFn collections adaptor",
"type": "module",
"exports": {
Expand Down
7 changes: 7 additions & 0 deletions packages/commcare/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-commcare

## 3.2.7

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 3.2.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/commcare/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-commcare",
"version": "3.2.6",
"version": "3.2.7",
"description": "Commcare Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
7 changes: 7 additions & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## 2.0.0

## 2.2.1

### Patch Changes

- 6dffdbd: Fixed an issue in the HTTP helpers where responses without a body can
cause an error to be thrown.

## 2.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-common",
"version": "2.2.0",
"version": "2.2.1",
"description": "Common Expressions for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
40 changes: 27 additions & 13 deletions packages/common/src/util/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getReasonPhrase } from 'http-status-codes';
import { Readable } from 'node:stream';
import querystring from 'node:querystring';
import path from 'node:path';
import throwError from './throw-error';

const clients = new Map();

Expand Down Expand Up @@ -238,19 +239,32 @@ function encodeRequestBody(body) {
}

async function readResponseBody(response, parseAs) {
const contentType = response.headers['content-type'];

switch (parseAs) {
case 'json':
return response.body.json();
case 'text':
return response.body.text();
case 'stream':
return response.body;
default:
return contentType && contentType.includes('application/json')
? response.body.json()
: response.body.text();
try {
const contentType = response.headers['content-type'];
if (+response.headers['content-length'] === 0) {
return undefined;
}

switch (parseAs) {
case 'json':
return await response.body.json();
case 'text':
return response.body.text();
case 'stream':
return response.body;
default:
return contentType && contentType.includes('application/json')
? await response.body.json()
: response.body.text();
}
} catch (error) {
throwError(response.statusCode, {
description: 'Error parsing the response body',
parseAs,
contentType: response.headers['content-type'],
bodyLength: +response.headers['content-length'],
error: error.message,
});
}
}

Expand Down
53 changes: 53 additions & 0 deletions packages/common/test/util/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,59 @@ describe('request function', () => {
expect(response.url).to.eql('https://www.example.com/api');
});

it('should return undefined if response body is empty and parseAs is json', async () => {
client
.intercept({
path: '/api',
method: 'PUT',
})
.reply(200, undefined, {
headers: { 'Content-Length': '0' },
});

const response = await request('PUT', 'https://www.example.com/api', {
parseAs: 'json',
body: { id: 2 },
});

expect(response.statusCode).to.eql(200);
expect(response.body).to.eql(undefined);
});

it('should throw an error if there is no content-length header and an empty response body', async () => {
client
.intercept({
path: '/api',
method: 'PUT',
})
.reply(200);

await request('PUT', 'https://www.example.com/api', {
parseAs: 'json',
body: { id: 2 },
}).catch(error => {
expect(error.message).to.eql('200: Error parsing the response body');
});
});

it('should display the body length in numbers if a request throws an error', async () => {
client
.intercept({
path: '/api',
method: 'PUT',
})
.reply(200, undefined, {
headers: { 'Content-Length': '0' },
});

await request('PUT', 'https://www.example.com/api', {
parseAs: 'json',
body: { id: 2 },
}).catch(error => {
expect(error.bodyLength).to.eql(0);
});
});

it('should send data', async () => {
const data = {
hello: 'world',
Expand Down
7 changes: 7 additions & 0 deletions packages/dhis2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-dhis2

## 6.0.3

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 6.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/dhis2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-dhis2",
"version": "6.0.2",
"version": "6.0.3",
"description": "DHIS2 Language Pack for OpenFn",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
7 changes: 7 additions & 0 deletions packages/dynamics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-dynamics

## 0.5.11

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 0.5.10

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/dynamics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-dynamics",
"version": "0.5.10",
"version": "0.5.11",
"description": "A Microsoft Dynamics Language Pack for OpenFn",
"main": "dist/index.cjs",
"scripts": {
Expand All @@ -20,7 +20,7 @@
"configuration-schema.json"
],
"dependencies": {
"@openfn/language-common": "2.2.0",
"@openfn/language-common": "2.2.1",
"request": "^2.72.0"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions packages/facebook/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-facebook

## 0.4.9

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 0.4.8

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/facebook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-facebook",
"version": "0.4.8",
"version": "0.4.9",
"description": "An Language Package for Facebook Messenger API",
"main": "dist/index.cjs",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions packages/fhir-fr/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/language-fhir-fr

## 1.0.6

### Patch Changes

- Updated dependencies [6dffdbd]
- @openfn/language-common@2.2.1

## 1.0.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/fhir-fr/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-fhir-fr",
"version": "1.0.5",
"version": "1.0.6",
"description": "OpenFn fhir-fr adaptor",
"scripts": {
"build": "pnpm clean && build-adaptor fhir-fr src ast docs",
Expand Down
Loading

0 comments on commit a4e8f74

Please sign in to comment.