Skip to content

Commit c5f25c3

Browse files
committed
fix: object key type and getHeadersObject
1 parent 8b8ccc9 commit c5f25c3

3 files changed

Lines changed: 16 additions & 22 deletions

File tree

packages/core/src/context.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Context<TContext extends Record<string, unknown> = Record<string, u
1414
this.db = ctx.db as any
1515
}
1616

17-
get<TName extends keyof TContext>(name: TName): TContext[TName] {
17+
get<TName extends Exclude<keyof TContext, 'db'>>(name: TName): TContext[TName] {
1818
const value = this.ctx[name]
1919
if (value === undefined) {
2020
throw new Error(`Key "${String(name)}" not found in context.`)
@@ -28,12 +28,6 @@ export class Context<TContext extends Record<string, unknown> = Record<string, u
2828
): RequestContext<TContext> {
2929
return new RequestContext<TContext>(ctx.ctx, ctx.authContext, headers)
3030
}
31-
32-
static getCtx<TContext extends Record<string, unknown> = Record<string, unknown>>(
33-
ctx: Context<TContext>
34-
): TContext {
35-
return ctx.ctx
36-
}
3731
}
3832

3933
export class RequestContext<
@@ -67,19 +61,18 @@ export class RequestContext<
6761
return this._headers
6862
}
6963

70-
override get<
71-
TKey extends string | number | symbol,
72-
TValue extends TKey extends keyof TContext ? TContext[TKey] : any,
73-
>(name: TKey): TValue {
64+
override get<TKey extends Exclude<keyof TContext, 'db'>>(name: TKey): TContext[TKey]
65+
override get(name: string): unknown
66+
override get(name: string): unknown {
7467
try {
75-
const value = this.ctx[name as keyof TContext]
76-
return value as TValue
68+
const value = super.get(name as any)
69+
return value
7770
} catch {
7871
const value = this._state[name as string]
7972
if (value === undefined) {
8073
throw new Error(`Key "${String(name)}" not found in context.`)
8174
}
82-
return value as TValue
75+
return value
8376
}
8477
}
8578

packages/next/src/utils/get-user.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ import { cache } from 'react'
22

33
import { headers } from 'next/headers'
44

5-
import type { ServerFunction } from '../server-function'
5+
import { getHeadersObject } from './headers'
66

7-
function getHeadersObject(headers: Headers) {
8-
const headersObject: Record<string, string> = {}
9-
headers.forEach((value, key) => {
10-
headersObject[key] = value
11-
})
12-
return headersObject
13-
}
7+
import type { ServerFunction } from '../server-function'
148

159
// TODO: Add type for user
1610
async function _getUser(serverFunction: ServerFunction): Promise<any | null> {

packages/next/src/utils/headers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const getHeadersObject = (headers: Headers): Record<string, string> => {
2+
const headersRecord: Record<string, string> = {}
3+
headers.forEach((value, key) => {
4+
headersRecord[key] = value
5+
})
6+
return headersRecord
7+
}

0 commit comments

Comments
 (0)