@@ -5,36 +5,17 @@ import type { Handler } from 'express';
5
5
const isStringTuple = ( _ : [ string , unknown ] ) : _ is [ string , string ] =>
6
6
typeof _ [ 1 ] === 'string' ;
7
7
8
- const allowedOrigins = [
9
- 'https://www.theguardian.com' ,
10
- 'https://m.code.dev-theguardian.com' ,
11
- 'https://m.thegulocal.com' ,
12
- 'http://localhost:9000' ,
13
- ] ;
14
-
15
8
/**
16
9
* Get DCR content from a `theguardian.com` URL.
17
10
* Takes in optional `X-Gu-*` headers to send.
18
- * Returns the parsed JSON config and any cookies set by the request.
19
11
*/
20
12
async function getContentFromURL (
21
13
url : URL ,
22
14
_headers : IncomingHttpHeaders ,
23
- ) : Promise < { config : unknown ; cookies : string [ ] } > {
15
+ ) : Promise < unknown > {
24
16
// searchParams will only work for the first set of query params because 'url' is already a query param itself
25
17
const searchparams = url . searchParams . toString ( ) ;
26
18
27
- // Prevent requests to unknown origins
28
- if ( ! allowedOrigins . includes ( url . origin ) ) {
29
- throw new Error (
30
- `Origin ${
31
- url . origin
32
- } is not allowed. Allowed origins are: ${ allowedOrigins . join (
33
- ', ' ,
34
- ) } `,
35
- ) ;
36
- }
37
-
38
19
// Reconstruct the parsed url adding .json?dcr which we need to force dcr to return json
39
20
const jsonUrl = `${ url . origin } ${ url . pathname } .json?dcr=true&${ searchparams } ` ;
40
21
@@ -49,13 +30,10 @@ async function getContentFromURL(
49
30
. filter ( isStringTuple ) ,
50
31
) ;
51
32
52
- const { cookies, json } = await fetch ( jsonUrl , { headers } )
53
- . then ( async ( response ) => {
54
- return {
55
- json : ( await response . json ( ) ) as unknown ,
56
- cookies : response . headers . getSetCookie ( ) ,
57
- } ;
58
- } )
33
+ // pick all the keys from the JSON except `html`
34
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars -- we don't want `html` in the config
35
+ const { html, ...config } = await fetch ( jsonUrl , { headers } )
36
+ . then ( ( response ) => response . json ( ) )
59
37
. catch ( ( error ) => {
60
38
if ( isObject ( error ) && error . type === 'invalid-json' ) {
61
39
throw new Error (
@@ -65,11 +43,7 @@ async function getContentFromURL(
65
43
throw error ;
66
44
} ) ;
67
45
68
- // pick all the keys from the JSON except `html`
69
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- we don't want `html` in the config
70
- const { html, ...config } = json as Record < string , unknown > ;
71
-
72
- return { config, cookies } ;
46
+ return config ;
73
47
}
74
48
75
49
/**
@@ -91,14 +65,8 @@ export const getContentFromURLMiddleware: Handler = async (req, res, next) => {
91
65
const url = new URL (
92
66
'https://www.theguardian.com/crosswords/digital-edition' ,
93
67
) ;
94
- const { config, cookies } = await getContentFromURL (
95
- url ,
96
- req . headers ,
97
- ) ;
98
- req . body = config ;
99
- for ( const cookie of cookies ) {
100
- res . append ( 'Set-Cookie' , cookie ) ;
101
- }
68
+ const content = await getContentFromURL ( url , req . headers ) ;
69
+ req . body = content ;
102
70
next ( ) ;
103
71
} catch ( error ) {
104
72
console . error ( error ) ;
@@ -120,14 +88,7 @@ export const getContentFromURLMiddleware: Handler = async (req, res, next) => {
120
88
}
121
89
122
90
try {
123
- const { config, cookies } = await getContentFromURL (
124
- sourceURL ,
125
- req . headers ,
126
- ) ;
127
- req . body = config ;
128
- for ( const cookie of cookies ) {
129
- res . append ( 'Set-Cookie' , cookie ) ;
130
- }
91
+ req . body = await getContentFromURL ( sourceURL , req . headers ) ;
131
92
} catch ( error ) {
132
93
console . error ( error ) ;
133
94
next ( error ) ;
0 commit comments