Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): specify detailed return type for parseBody #2771

Merged
merged 13 commits into from
May 24, 2024
Prev Previous commit
Next Next commit
fix(utils): Accept parseBody<T extends BodyData>()
usualoma committed May 23, 2024
commit 215be5a7da91b9bfa962c0d5666632fe9d04e296
23 changes: 15 additions & 8 deletions src/utils/body.ts
Original file line number Diff line number Diff line change
@@ -60,13 +60,20 @@ export type ParseBodyOptions = {
* @param {Partial<ParseBodyOptions>} [options] - Options for parsing the body.
* @returns {Promise<T>} The parsed body data.
*/
export const parseBody = async <
Options extends Partial<ParseBodyOptions>,
T extends BodyData<Options> = BodyData<Options>
>(
interface ParseBody {
<Options extends Partial<ParseBodyOptions>, T extends BodyData<Options>>(
request: HonoRequest | Request,
options?: Options
): Promise<T>
<T extends BodyData>(
request: HonoRequest | Request,
options?: Partial<ParseBodyOptions>
): Promise<T>
}
export const parseBody: ParseBody = async (
request: HonoRequest | Request,
options: Options = Object.create(null)
): Promise<T> => {
options = Object.create(null)
) => {
const { all = false, dot = false } = options

const headers = request instanceof HonoRequest ? request.raw.headers : request.headers
@@ -76,10 +83,10 @@ export const parseBody = async <
(contentType !== null && contentType.startsWith('multipart/form-data')) ||
(contentType !== null && contentType.startsWith('application/x-www-form-urlencoded'))
) {
return parseFormData<T>(request, { all, dot })
return parseFormData(request, { all, dot })
}

return {} as T
return {}
}

/**