@@ -17,7 +17,7 @@ import putHelper from '../helpers/source.js';
17
17
import { removeUEAttributes , unwrapParagraphs } from '../ue/attributes.js' ;
18
18
import { prepareHtml } from '../ue/ue.js' ;
19
19
import { getAemCtx , getAEMHtml } from '../utils/aemCtx.js' ;
20
- import { daResp } from '../responses/index.js' ;
20
+ import { daResp , get401 , get404 } from '../responses/index.js' ;
21
21
import { BRANCH_NOT_FOUND_HTML_MESSAGE , DEFAULT_HTML_TEMPLATE , UNAUTHORIZED_HTML_MESSAGE } from '../utils/constants.js' ;
22
22
import { getSiteConfig } from '../storage/config.js' ;
23
23
@@ -67,25 +67,16 @@ export async function daSourceGet({ req, env, daCtx }) {
67
67
org, site, path, ext, authToken,
68
68
} = daCtx ;
69
69
70
- const response = {
71
- status : 200 ,
72
- contentType : 'text/html; charset=utf-8' ,
73
- } ;
74
-
75
70
// check if Authorization header is present
76
71
if ( ! authToken ) {
77
- response . body = UNAUTHORIZED_HTML_MESSAGE ;
78
- response . status = 401 ;
79
- return daResp ( response ) ;
72
+ return get401 ( UNAUTHORIZED_HTML_MESSAGE ) ;
80
73
}
81
74
82
75
// get the AEM parts (head.html)
83
76
const aemCtx = getAemCtx ( env , daCtx ) ;
84
77
const headHtml = await getAEMHtml ( aemCtx , '/head.html' ) ;
85
78
if ( ! headHtml ) {
86
- response . body = BRANCH_NOT_FOUND_HTML_MESSAGE ;
87
- response . status = 404 ;
88
- return daResp ( response ) ;
79
+ return get404 ( BRANCH_NOT_FOUND_HTML_MESSAGE ) ;
89
80
}
90
81
91
82
// get the content from DA admin
@@ -102,21 +93,26 @@ export async function daSourceGet({ req, env, daCtx }) {
102
93
method : 'GET' ,
103
94
headers,
104
95
} ) ;
96
+ let body ;
105
97
const daAdminResp = await env . daadmin . fetch ( req ) ;
106
98
if ( daAdminResp && daAdminResp . status === 200 ) {
107
99
// enrich stored content with HTML header and UE attributes
108
100
const originalBodyHtml = await daAdminResp . text ( ) ;
109
101
const responseHtml = await prepareHtml ( daCtx , aemCtx , originalBodyHtml , headHtml ) ;
110
- response . body = responseHtml ;
102
+ body = responseHtml ;
111
103
} else {
112
104
// enrich default template with HTML header and UE attributes
113
105
const templateHtml = await getPageTemplate ( env , daCtx , aemCtx , headHtml ) ;
114
106
const responseHtml = await prepareHtml ( daCtx , aemCtx , templateHtml , headHtml ) ;
115
- response . body = responseHtml ;
107
+ body = responseHtml ;
116
108
}
117
109
118
- response . contentLength = response . body . length ;
119
- return daResp ( response ) ;
110
+ return daResp ( {
111
+ status : 200 ,
112
+ body,
113
+ contentLength : body . length ,
114
+ contentType : 'text/html; charset=utf-8' ,
115
+ } ) ;
120
116
}
121
117
122
118
export async function daSourcePost ( { req, env, daCtx } ) {
0 commit comments