-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from blenderskool/partial-query
Partial query middleware ✂
- Loading branch information
Showing
16 changed files
with
228 additions
and
83 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as middlewareRatelimit } from './rate-limit'; | ||
export { default as middlewareRestriction } from './restriction'; | ||
export { default as middlewareCache } from './cache'; | ||
export * from './rate-limit'; | ||
export * from './restriction'; | ||
export * from './cache'; | ||
export * from './partial-query'; |
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,29 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { Readable } from 'stream'; | ||
import * as mask from 'json-mask'; | ||
import getStream from 'get-stream'; | ||
|
||
export interface PartialQueryOptions extends MiddlewareOptions {}; | ||
|
||
export function partialJsonQuery(filterString: string) { | ||
return async (req: NextApiRequest, res: NextApiResponse, next: Function) => { | ||
const resultBuffer = await getStream.buffer(req.locals.result.data); | ||
// Below code is prone to errors if the body did not contain valid JSON data | ||
try { | ||
const resultData = mask(JSON.parse(resultBuffer as any), filterString); | ||
|
||
const resultReadable = new Readable(); | ||
resultReadable.push(JSON.stringify(resultData)); | ||
resultReadable.push(null); | ||
|
||
req.locals.result = { | ||
headers: { 'content-type': 'application/json' }, | ||
data: resultReadable, | ||
}; | ||
} catch(_) { | ||
// Handle errors here if _necessary_ in the future | ||
} finally { | ||
next(); | ||
} | ||
}; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
29a30b1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: