Skip to content

Commit

Permalink
fix: return undefined from json() if no body is present in ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed May 8, 2023
1 parent 58998ca commit 89e6437
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
13 changes: 12 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,14 @@ 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()) : {};
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 89e6437

Please sign in to comment.