-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
useMethod
/isMethod
/assertMethod
- Loading branch information
Showing
6 changed files
with
1,468 additions
and
1,543 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,6 +1,41 @@ | ||
import type { IncomingMessage } from 'http' | ||
import { getQuery } from 'ufo' | ||
import { createError } from '../error' | ||
|
||
export function useQuery (req: IncomingMessage) { | ||
return getQuery(req.url || '') | ||
} | ||
|
||
// https://www.rfc-editor.org/rfc/rfc7231#section-4.1 | ||
export type HTTPMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | ||
|
||
export function useMethod (req: IncomingMessage, defaultMethod: HTTPMethod = 'GET'): HTTPMethod { | ||
return (req.method || defaultMethod).toUpperCase() as HTTPMethod | ||
} | ||
|
||
export function isMethod (req: IncomingMessage, expected: HTTPMethod | HTTPMethod[], allowHead?: boolean) { | ||
const method = useMethod(req) | ||
|
||
if (allowHead && method === 'HEAD') { | ||
return true | ||
} | ||
|
||
if (typeof expected === 'string') { | ||
if (method === expected) { | ||
return true | ||
} | ||
} else if (expected.includes(method)) { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
export function assertMethod (req: IncomingMessage, expected: HTTPMethod | HTTPMethod[], allowHead?: boolean) { | ||
if (!isMethod(req, expected, allowHead)) { | ||
throw createError({ | ||
statusCode: 405, | ||
statusMessage: 'HTTP method is not allowed.' | ||
}) | ||
} | ||
} |
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.