Skip to content

Commit

Permalink
refactor: deprecate utils with `use* prefix (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 1, 2022
1 parent 02f90f5 commit 56bfe0a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
12 changes: 9 additions & 3 deletions src/utils/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const PayloadMethods: HTTPMethod[] = ['PATCH', 'POST', 'PUT', 'DELETE']
*
* @return {String|Buffer} Encoded raw string or raw Buffer of the body
*/
export function useRawBody (event: CompatibilityEvent, encoding: Encoding = 'utf-8'): Encoding extends false ? Buffer : Promise<string | Buffer> {
export function readRawBody (event: CompatibilityEvent, encoding: Encoding = 'utf-8'): Encoding extends false ? Buffer : Promise<string | Buffer> {
// Ensure using correct HTTP method before attempt to read payload
assertMethod(event, PayloadMethods)

Expand All @@ -40,6 +40,9 @@ export function useRawBody (event: CompatibilityEvent, encoding: Encoding = 'utf
return encoding ? promise.then(buff => buff.toString(encoding)) : promise
}

/** @deprecated Use `h3.readRawBody` */
export const useRawBody = readRawBody

/**
* Reads request body and try to safely parse using [destr](https://github.com/unjs/destr)
* @param event {CompatibilityEvent} H3 event or req passed by h3 handler
Expand All @@ -51,13 +54,13 @@ export function useRawBody (event: CompatibilityEvent, encoding: Encoding = 'utf
* const body = await useBody(req)
* ```
*/
export async function useBody<T=any> (event: CompatibilityEvent): Promise<T> {
export async function readBody<T=any> (event: CompatibilityEvent): Promise<T> {
if (ParsedBodySymbol in event.req) {
return (event.req as any)[ParsedBodySymbol]
}

// TODO: Handle buffer
const body = await useRawBody(event) as string
const body = await readRawBody(event) as string

if (event.req.headers['content-type'] === 'application/x-www-form-urlencoded') {
const parsedForm = Object.fromEntries(new URLSearchParams(body))
Expand All @@ -68,3 +71,6 @@ export async function useBody<T=any> (event: CompatibilityEvent): Promise<T> {
(event.req as any)[ParsedBodySymbol] = json
return json
}

/** @deprecated Use `h3.readBody` */
export const useBody = readBody
14 changes: 10 additions & 4 deletions src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import { appendHeader } from './response'
* @param event {CompatibilityEvent} H3 event or req passed by h3 handler
* @returns Object of cookie name-value pairs
* ```ts
* const cookies = useCookies(req)
* const cookies = parseCookies(event)
* ```
*/
export function useCookies (event: CompatibilityEvent): Record<string, string> {
export function parseCookies (event: CompatibilityEvent): Record<string, string> {
return parse(event.req.headers.cookie || '')
}

/** @deprecated Use `h3.parseCookies` */
export const useCookies = parseCookies

/**
* Get a cookie value by name.
* @param event {CompatibilityEvent} H3 event or req passed by h3 handler
Expand All @@ -24,10 +27,13 @@ export function useCookies (event: CompatibilityEvent): Record<string, string> {
* const authorization = useCookie(request, 'Authorization')
* ```
*/
export function useCookie (event: CompatibilityEvent, name: string): string | undefined {
return useCookies(event)[name]
export function getCookie (event: CompatibilityEvent, name: string): string | undefined {
return parseCookies(event)[name]
}

/** @deprecated Use `h3.getCookie` */
export const useCookie = getCookie

/**
* Set a cookie value by name.
* @param event {CompatibilityEvent} H3 event or res passed by h3 handler
Expand Down
16 changes: 11 additions & 5 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { getQuery } from 'ufo'
import { getQuery as _getQuery } from 'ufo'
import { createError } from '../error'
import type { HTTPMethod } from '../types'
import type { CompatibilityEvent } from '../event'

export function useQuery (event: CompatibilityEvent) {
return getQuery(event.req.url || '')
export function getQuery (event: CompatibilityEvent) {
return _getQuery(event.req.url || '')
}

export function useMethod (event: CompatibilityEvent, defaultMethod: HTTPMethod = 'GET'): HTTPMethod {
/** @deprecated Use `h3.getQuery` */
export const useQuery = getQuery

export function getMethod (event: CompatibilityEvent, defaultMethod: HTTPMethod = 'GET'): HTTPMethod {
return (event.req.method || defaultMethod).toUpperCase() as HTTPMethod
}

/** @deprecated Use `h3.getMethod` */
export const useMethod = getMethod

export function isMethod (event: CompatibilityEvent, expected: HTTPMethod | HTTPMethod[], allowHead?: boolean) {
const method = useMethod(event)
const method = getMethod(event)

if (allowHead && method === 'HEAD') {
return true
Expand Down

0 comments on commit 56bfe0a

Please sign in to comment.