@@ -3,7 +3,6 @@ import type {
3
3
Context ,
4
4
HttpRequest ,
5
5
HttpRequestHeaders ,
6
- HttpRequestQuery ,
7
6
} from '@azure/functions' ;
8
7
import {
9
8
ApolloServer ,
@@ -55,7 +54,7 @@ export function startServerAndCreateHandler<TContext extends BaseContext>(
55
54
}
56
55
57
56
return {
58
- statusCode : status || 200 ,
57
+ status : status || 200 ,
59
58
headers : {
60
59
...Object . fromEntries ( headers ) ,
61
60
'content-length' : Buffer . byteLength ( body . string ) . toString ( ) ,
@@ -65,7 +64,7 @@ export function startServerAndCreateHandler<TContext extends BaseContext>(
65
64
} catch ( e ) {
66
65
context . log . error ( 'Failure processing GraphQL request' , e ) ;
67
66
return {
68
- statusCode : 400 ,
67
+ status : 400 ,
69
68
body : ( e as Error ) . message ,
70
69
} ;
71
70
}
@@ -80,28 +79,30 @@ function normalizeRequest(req: HttpRequest): HTTPGraphQLRequest {
80
79
return {
81
80
method : req . method ,
82
81
headers : normalizeHeaders ( req . headers ) ,
83
- search : normalizeQueryStringParams ( req . query ) ,
84
- body : req . body ,
82
+ search : new URL ( req . url ) . search ,
83
+ body : parseBody ( req . body , req . headers [ 'content-type' ] ) ,
85
84
} ;
86
85
}
87
86
87
+ function parseBody (
88
+ body : string | null | undefined ,
89
+ contentType : string | undefined ,
90
+ ) : object | string {
91
+ if ( body ) {
92
+ if ( contentType === 'application/json' && typeof body === 'string' ) {
93
+ return JSON . parse ( body ) ;
94
+ }
95
+ if ( contentType === 'text/plain' ) {
96
+ return body ;
97
+ }
98
+ }
99
+ return '' ;
100
+ }
101
+
88
102
function normalizeHeaders ( headers : HttpRequestHeaders ) : HeaderMap {
89
103
const headerMap = new HeaderMap ( ) ;
90
104
for ( const [ key , value ] of Object . entries ( headers ) ) {
91
105
headerMap . set ( key , value ?? '' ) ;
92
106
}
93
107
return headerMap ;
94
108
}
95
-
96
- function normalizeQueryStringParams (
97
- queryStringParams : HttpRequestQuery | null ,
98
- ) : string {
99
- const queryStringRecord : Record < string , string > = { } ;
100
- for ( const [ key , value ] of Object . entries ( queryStringParams ?? { } ) ) {
101
- queryStringRecord [ key ] = value ?? '' ;
102
- }
103
- return Object . keys ( queryStringRecord ) . reduce (
104
- ( acc , key ) => `${ acc } &${ key } =${ queryStringRecord [ key ] } ` ,
105
- '' ,
106
- ) ;
107
- }
0 commit comments