Skip to content

Commit 56bfe0a

Browse files
authored
refactor: deprecate utils with `use* prefix (#156)
1 parent 02f90f5 commit 56bfe0a

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

src/utils/body.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const PayloadMethods: HTTPMethod[] = ['PATCH', 'POST', 'PUT', 'DELETE']
1515
*
1616
* @return {String|Buffer} Encoded raw string or raw Buffer of the body
1717
*/
18-
export function useRawBody (event: CompatibilityEvent, encoding: Encoding = 'utf-8'): Encoding extends false ? Buffer : Promise<string | Buffer> {
18+
export function readRawBody (event: CompatibilityEvent, encoding: Encoding = 'utf-8'): Encoding extends false ? Buffer : Promise<string | Buffer> {
1919
// Ensure using correct HTTP method before attempt to read payload
2020
assertMethod(event, PayloadMethods)
2121

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

43+
/** @deprecated Use `h3.readRawBody` */
44+
export const useRawBody = readRawBody
45+
4346
/**
4447
* Reads request body and try to safely parse using [destr](https://github.com/unjs/destr)
4548
* @param event {CompatibilityEvent} H3 event or req passed by h3 handler
@@ -51,13 +54,13 @@ export function useRawBody (event: CompatibilityEvent, encoding: Encoding = 'utf
5154
* const body = await useBody(req)
5255
* ```
5356
*/
54-
export async function useBody<T=any> (event: CompatibilityEvent): Promise<T> {
57+
export async function readBody<T=any> (event: CompatibilityEvent): Promise<T> {
5558
if (ParsedBodySymbol in event.req) {
5659
return (event.req as any)[ParsedBodySymbol]
5760
}
5861

5962
// TODO: Handle buffer
60-
const body = await useRawBody(event) as string
63+
const body = await readRawBody(event) as string
6164

6265
if (event.req.headers['content-type'] === 'application/x-www-form-urlencoded') {
6366
const parsedForm = Object.fromEntries(new URLSearchParams(body))
@@ -68,3 +71,6 @@ export async function useBody<T=any> (event: CompatibilityEvent): Promise<T> {
6871
(event.req as any)[ParsedBodySymbol] = json
6972
return json
7073
}
74+
75+
/** @deprecated Use `h3.readBody` */
76+
export const useBody = readBody

src/utils/cookie.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import { appendHeader } from './response'
88
* @param event {CompatibilityEvent} H3 event or req passed by h3 handler
99
* @returns Object of cookie name-value pairs
1010
* ```ts
11-
* const cookies = useCookies(req)
11+
* const cookies = parseCookies(event)
1212
* ```
1313
*/
14-
export function useCookies (event: CompatibilityEvent): Record<string, string> {
14+
export function parseCookies (event: CompatibilityEvent): Record<string, string> {
1515
return parse(event.req.headers.cookie || '')
1616
}
1717

18+
/** @deprecated Use `h3.parseCookies` */
19+
export const useCookies = parseCookies
20+
1821
/**
1922
* Get a cookie value by name.
2023
* @param event {CompatibilityEvent} H3 event or req passed by h3 handler
@@ -24,10 +27,13 @@ export function useCookies (event: CompatibilityEvent): Record<string, string> {
2427
* const authorization = useCookie(request, 'Authorization')
2528
* ```
2629
*/
27-
export function useCookie (event: CompatibilityEvent, name: string): string | undefined {
28-
return useCookies(event)[name]
30+
export function getCookie (event: CompatibilityEvent, name: string): string | undefined {
31+
return parseCookies(event)[name]
2932
}
3033

34+
/** @deprecated Use `h3.getCookie` */
35+
export const useCookie = getCookie
36+
3137
/**
3238
* Set a cookie value by name.
3339
* @param event {CompatibilityEvent} H3 event or res passed by h3 handler

src/utils/request.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
import { getQuery } from 'ufo'
1+
import { getQuery as _getQuery } from 'ufo'
22
import { createError } from '../error'
33
import type { HTTPMethod } from '../types'
44
import type { CompatibilityEvent } from '../event'
55

6-
export function useQuery (event: CompatibilityEvent) {
7-
return getQuery(event.req.url || '')
6+
export function getQuery (event: CompatibilityEvent) {
7+
return _getQuery(event.req.url || '')
88
}
99

10-
export function useMethod (event: CompatibilityEvent, defaultMethod: HTTPMethod = 'GET'): HTTPMethod {
10+
/** @deprecated Use `h3.getQuery` */
11+
export const useQuery = getQuery
12+
13+
export function getMethod (event: CompatibilityEvent, defaultMethod: HTTPMethod = 'GET'): HTTPMethod {
1114
return (event.req.method || defaultMethod).toUpperCase() as HTTPMethod
1215
}
1316

17+
/** @deprecated Use `h3.getMethod` */
18+
export const useMethod = getMethod
19+
1420
export function isMethod (event: CompatibilityEvent, expected: HTTPMethod | HTTPMethod[], allowHead?: boolean) {
15-
const method = useMethod(event)
21+
const method = getMethod(event)
1622

1723
if (allowHead && method === 'HEAD') {
1824
return true

0 commit comments

Comments
 (0)