Skip to content

Commit

Permalink
fix: return undefined when request body is empty (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed May 9, 2023
2 parents 096041b + fc64dcb commit d9851a9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
</p>

<p align="center">
Build <a href="https://nitric.io">nitric</a> applications with Node.js
Build <a href="https://nitric.io">Nitric</a> applications with Node.js
</p>

<p align="center">
<img alt="Tests" src="https://github.com/nitrictech/node-sdk/actions/workflows/test.yaml/badge.svg?branch=main">
<a href="https://codecov.io/gh/nitrictech/node-sdk">
<img alt="codecov" src="https://codecov.io/gh/nitrictech/node-sdk/branch/main/graph/badge.svg?token=N46TTGPE4G">
<img alt="codecov" src="https://img.shields.io/codecov/c/github/nitrictech/node-sdk?style=for-the-badge">
</a>
<a href="https://npmjs.org/package/@nitric/sdk">
<img alt="Version" src="https://img.shields.io/npm/v/@nitric/sdk.svg">
<img alt="Version" src="https://img.shields.io/npm/v/@nitric/sdk.svg?style=for-the-badge">
</a>
<a href="https://npmjs.org/package/@nitric/sdk">
<img alt="Downloads/week" src="https://img.shields.io/npm/dw/@nitric/sdk.svg">
<img alt="Downloads/week" src="https://img.shields.io/npm/dw/@nitric/sdk.svg?style=for-the-badge">
</a>
<a href="https://discord.gg/Webemece5C"><img alt="Discord" src="https://img.shields.io/discord/955259353043173427?label=discord"></a>
<a href="https://discord.gg/Webemece5C"><img alt="Discord" src="https://img.shields.io/discord/955259353043173427?label=discord&style=for-the-badge"></a>
</p>

The NodeJS SDK supports the use of the Nitric framework with NodeJS 12+. For more information, check out the main [Nitric repo](https://github.com/nitrictech/nitric).
Expand Down
19 changes: 8 additions & 11 deletions assets/nitric-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion src/faas/v0/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export abstract class AbstractRequest<
this.traceContext = traceContext;
}

/**
* Return the request payload as a string.
* If the request was a byte array it will converted using UTF-8 text decoding.
*
* @returns the request payload as a string
*/
text(): string {
const stringPayload =
typeof this.data === 'string'
Expand All @@ -111,9 +117,15 @@ export abstract class AbstractRequest<
return stringPayload;
}

/**
* Deserialize the request payload from JSON
*
* @returns JSON parsed request body
*/
json(): JSONT {
// attempt to deserialize as a JSON object
return this.text() ? JSON.parse(this.text()) : {};
const textBody = this.text();
return textBody ? JSON.parse(textBody) : undefined;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/resources/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
Resource,
ResourceDeclareRequest,
ResourceDeclareResponse,
ResourceDetailsRequest,
ResourceDetailsResponse,
ResourceType,
ResourceTypeMap,
Expand Down
1 change: 0 additions & 1 deletion src/resources/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
ResourceDeclareRequest,
ResourceType,
Action,
ActionMap,
ResourceDeclareResponse,
} from '@nitric/api/proto/resource/v1/resource_pb';
import resourceClient from './client';
Expand Down

0 comments on commit d9851a9

Please sign in to comment.