-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
34 additions
and
52 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
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,59 +1,32 @@ | ||
import { IncomingMessage, ServerResponse } from 'http' | ||
|
||
const OK = 200 | ||
const METHOD_NOT_ALLOWED = 405 | ||
const BED_REQUEST = 400 | ||
const BAD_REQUEST = 400 | ||
|
||
export const readRequestBody = (req: { | ||
on: ( | ||
arg0: string, | ||
arg1: (chunk: any) => number | ||
) => { | ||
(): any | ||
new (): any | ||
on: { | ||
(arg0: string, arg1: (error: any) => void): { | ||
(): any | ||
new (): any | ||
on: { (arg0: string, arg1: () => void): void; new (): any } | ||
} | ||
new (): any | ||
} | ||
} | ||
}) => | ||
export const readRequestBody = (req: IncomingMessage) => | ||
new Promise((resolve, reject) => { | ||
const body: any[] = [] | ||
req.on('data', (chunk: any) => body.push(chunk)) | ||
.on('error', (error: Error) => reject(error)) | ||
.on('error', (error: any) => reject(error)) | ||
.on('end', () => resolve(JSON.parse(Buffer.concat(body).toString()))) | ||
}) | ||
|
||
export const methodNotAllowed = ( | ||
res: { writeHead: (arg0: number) => void; end: (arg0: string) => void }, | ||
message = 'Method Not Allowed' | ||
) => { | ||
export const methodNotAllowed = (res: ServerResponse, message = 'Method Not Allowed') => { | ||
res.writeHead(METHOD_NOT_ALLOWED) | ||
res.end(message) | ||
} | ||
|
||
export const badRequest = ( | ||
res: { writeHead: (arg0: number) => void; end: (arg0: string) => void }, | ||
message = 'Bad Request' | ||
) => { | ||
res.writeHead(BED_REQUEST) | ||
export const badRequest = (res: ServerResponse, message = 'Bad Request') => { | ||
res.writeHead(BAD_REQUEST) | ||
res.end(message) | ||
} | ||
|
||
export const json = ( | ||
res: { | ||
writeHead: (arg0: number, arg1: { 'Content-Type': string }) => void | ||
end: (arg0: string) => void | ||
}, | ||
result: any, | ||
statusCode = OK | ||
) => { | ||
export const json = (res: ServerResponse, result: any, statusCode = OK) => { | ||
res.writeHead(statusCode, { | ||
'Content-Type': 'application/json', | ||
}) | ||
res.end(JSON.stringify(result)) | ||
} | ||
|
||
export const badRequestJson = (res: any, result: any) => json(res, result, BED_REQUEST) | ||
export const badRequestJson = (res: any, result: any) => json(res, result, BAD_REQUEST) |