@@ -4,12 +4,12 @@ import * as cookie from 'cookie'
4
4
import { UserDetails } from 'prisma-client'
5
5
import { PrismaClientKnownRequestError } from 'prisma-client/runtime/library.js'
6
6
import invariant from 'tiny-invariant'
7
- import { ServerEnvironment } from '../env.server '
7
+ import { ALLOW } from '../handlers/validators '
8
8
import { getUserFromSession } from '../models/session.server'
9
9
import { ApiError } from './errors'
10
10
import { Method } from './methods.server'
11
11
import { Status } from './statusCodes'
12
- import { ALLOW } from '../handlers/validators '
12
+ import { ServerEnvironment } from '../env.server '
13
13
14
14
interface ErrorResponse {
15
15
error : string
@@ -19,13 +19,15 @@ type EmptyResponse = Record<string, never>
19
19
20
20
export type ApiResponse < T > = TypedResponse < T | ErrorResponse | EmptyResponse >
21
21
22
- const defaultResponseHeaders = new Headers ( {
23
- 'Access-Control-Allow-Origin' : ServerEnvironment . CORSOrigin ,
24
- 'Access-Control-Allow-Credentials' : 'true' ,
25
- 'Access-Control-Allow-Headers' : 'content-type, origin, cookie' ,
26
- 'Access-Control-Allow-Methods' : 'GET,POST,PUT,DELETE,OPTIONS' ,
27
- 'Cache-control' : 'no-cache' ,
28
- } )
22
+ function defaultResponseHeaders ( ) : Headers {
23
+ return new Headers ( {
24
+ 'Access-Control-Allow-Origin' : ServerEnvironment . CORSOrigin ,
25
+ 'Access-Control-Allow-Credentials' : 'true' ,
26
+ 'Access-Control-Allow-Headers' : 'content-type, origin, cookie' ,
27
+ 'Access-Control-Allow-Methods' : 'GET,POST,PUT,DELETE,OPTIONS' ,
28
+ 'Cache-control' : 'no-cache' ,
29
+ } )
30
+ }
29
31
30
32
export function alwaysAllow (
31
33
handler : ( request : Request , params : Params < string > ) => Promise < unknown > ,
@@ -37,7 +39,7 @@ export function alwaysAllow(
37
39
}
38
40
39
41
async function optionsHandler ( ) : Promise < TypedResponse < EmptyResponse > > {
40
- return json ( { } , { headers : defaultResponseHeaders } )
42
+ return json ( { } , { headers : defaultResponseHeaders ( ) } )
41
43
}
42
44
export const handleOptions = alwaysAllow ( optionsHandler )
43
45
@@ -79,26 +81,27 @@ async function handleMethod<T>(
79
81
if ( validator != null ) {
80
82
await validator ( request , params )
81
83
}
84
+
82
85
const resp = await fn ( request , params )
83
86
if ( resp instanceof Response ) {
84
- const mergedHeaders = new Headers ( defaultResponseHeaders )
87
+ let headers = new Headers ( )
85
88
resp . headers . forEach ( ( value , key ) => {
86
- mergedHeaders . set ( key , value )
89
+ headers . set ( key , value )
87
90
} )
88
91
return new Response ( resp . body , {
89
92
status : resp . status ,
90
- headers : mergedHeaders ,
93
+ headers : headers ,
91
94
} )
92
95
}
93
- return json ( resp , { headers : defaultResponseHeaders } )
96
+ return json ( resp , { headers : { ... defaultResponseHeaders ( ) } } )
94
97
} catch ( err ) {
95
98
const { message, status, name } = getErrorData ( err )
96
99
97
100
console . error ( `${ request . method } ${ request . url } : ${ message } ` )
98
101
99
102
return json (
100
103
{ error : name , status : status , message : message } ,
101
- { headers : defaultResponseHeaders , status : status } ,
104
+ { headers : defaultResponseHeaders ( ) , status : status } ,
102
105
)
103
106
}
104
107
}
@@ -145,6 +148,7 @@ export async function proxiedResponse(response: Response): Promise<unknown> {
145
148
}
146
149
147
150
export const SESSION_COOKIE_NAME = 'JSESSIONID'
151
+
148
152
function getSessionId ( request : Request ) : string | null {
149
153
const cookieHeader = request . headers . get ( 'cookie' ) ?? ''
150
154
const cookies = cookie . parse ( cookieHeader )
0 commit comments