-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle requests with body already parsed (#23)
- Loading branch information
1 parent
896e943
commit c0fade7
Showing
4 changed files
with
102 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module.exports = getPayload | ||
|
||
function getPayload (request) { | ||
// If request.body already exists we can stop here | ||
// See https://github.com/octokit/webhooks.js/pull/23 | ||
if (request.body) { | ||
return Promise.resolve(request.body) | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const dataChunks = [] | ||
|
||
request.on('error', reject) | ||
request.on('data', (chunk) => dataChunks.push(chunk)) | ||
request.on('end', () => { | ||
const data = Buffer.concat(dataChunks).toString() | ||
try { | ||
resolve(JSON.parse(data)) | ||
} catch (error) { | ||
error.message = 'Invalid JSON' | ||
error.status = 400 | ||
reject(error) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters