@@ -26,47 +26,54 @@ export default async function render_route(request, route) {
2626 /** @type {import('types/endpoint').RequestHandler } */
2727 const handler = mod [ request . method . toLowerCase ( ) . replace ( 'delete' , 'del' ) ] ; // 'delete' is a reserved word
2828
29- if ( handler ) {
30- const match = route . pattern . exec ( request . path ) ;
31- const params = route . params ( match ) ;
32-
33- const response = await handler ( { ...request , params } ) ;
34- const preface = `Invalid response from route ${ request . path } ` ;
35-
36- if ( response ) {
37- if ( typeof response !== 'object' ) {
38- return error ( `${ preface } : expected an object, got ${ typeof response } ` ) ;
39- }
40-
41- let { status = 200 , body, headers = { } } = response ;
42-
43- headers = lowercase_keys ( headers ) ;
44- const type = headers [ 'content-type' ] ;
45-
46- const is_type_textual = isContentTypeTextual ( type ) ;
47-
48- if ( ! is_type_textual && ! ( body instanceof Uint8Array || is_string ( body ) ) ) {
49- return error (
50- `${ preface } : body must be an instance of string or Uint8Array if content-type is not a supported textual content-type`
51- ) ;
52- }
53-
54- /** @type {import('types/hooks').StrictBody } */
55- let normalized_body ;
56-
57- // ensure the body is an object
58- if (
59- ( typeof body === 'object' || typeof body === 'undefined' ) &&
60- ! ( body instanceof Uint8Array ) &&
61- ( ! type || type . startsWith ( 'application/json' ) )
62- ) {
63- headers = { ...headers , 'content-type' : 'application/json; charset=utf-8' } ;
64- normalized_body = JSON . stringify ( typeof body === 'undefined' ? { } : body ) ;
65- } else {
66- normalized_body = /** @type {import('types/hooks').StrictBody } */ ( body ) ;
67- }
68-
69- return { status, body : normalized_body , headers } ;
70- }
29+ if ( ! handler ) {
30+ return error ( 'no handler' ) ;
7131 }
32+
33+ const match = route . pattern . exec ( request . path ) ;
34+ if ( ! match ) {
35+ return error ( 'could not parse parameters from request path' ) ;
36+ }
37+
38+ const params = route . params ( match ) ;
39+
40+ const response = await handler ( { ...request , params } ) ;
41+ const preface = `Invalid response from route ${ request . path } ` ;
42+
43+ if ( ! response ) {
44+ return error ( 'no response' ) ;
45+ }
46+ if ( typeof response !== 'object' ) {
47+ return error ( `${ preface } : expected an object, got ${ typeof response } ` ) ;
48+ }
49+
50+ let { status = 200 , body, headers = { } } = response ;
51+
52+ headers = lowercase_keys ( headers ) ;
53+ const type = headers [ 'content-type' ] ;
54+
55+ const is_type_textual = isContentTypeTextual ( type ) ;
56+
57+ if ( ! is_type_textual && ! ( body instanceof Uint8Array || is_string ( body ) ) ) {
58+ return error (
59+ `${ preface } : body must be an instance of string or Uint8Array if content-type is not a supported textual content-type`
60+ ) ;
61+ }
62+
63+ /** @type {import('types/hooks').StrictBody } */
64+ let normalized_body ;
65+
66+ // ensure the body is an object
67+ if (
68+ ( typeof body === 'object' || typeof body === 'undefined' ) &&
69+ ! ( body instanceof Uint8Array ) &&
70+ ( ! type || type . startsWith ( 'application/json' ) )
71+ ) {
72+ headers = { ...headers , 'content-type' : 'application/json; charset=utf-8' } ;
73+ normalized_body = JSON . stringify ( typeof body === 'undefined' ? { } : body ) ;
74+ } else {
75+ normalized_body = /** @type {import('types/hooks').StrictBody } */ ( body ) ;
76+ }
77+
78+ return { status, body : normalized_body , headers } ;
7279}
0 commit comments