Skip to content

Commit 657c154

Browse files
authored
Merge pull request #250 from mapswipe/fix/data-fetch-script
Improve documentation and data fetch script
2 parents 7c9480e + 0d4399e commit 657c154

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ NextJs application for [Mapswipe community website](https://mapswipe.org).
44

55
## Development
66

7+
Get all the submodules
8+
9+
```bash
10+
git submodule update --init --recursive
11+
```
12+
713
Before you start, create `.env.local` file:
814

915
```bash
@@ -13,24 +19,31 @@ touch .env.local
1319
Set these environment variables:
1420

1521
```env
16-
APP_ENVIRONMENT=DEV
22+
APP_ENVIRONMENT=PROD # Use DEV if you are running your own server instance
1723
MAPSWIPE_API_ENDPOINT=https://backend.mapswipe.org/
18-
MAPSWIPE_REFERRER_ENDPOINT=https://website.mapswipe.org/
1924
NEXT_PUBLIC_POSTHOG_KEY=<posthog-key>
2025
NEXT_PUBLIC_POSTHOG_HOST_API=<posthog-host-api>
2126
```
2227

2328
### Running
2429

2530
```bash
31+
# Install dependencies
2632
yarn install
27-
# This fetches latest data from MapSwipe database for projects
33+
34+
# Generate typescript types from graphql schema
35+
yarn generate:type
36+
37+
# Fetch latest data from MapSwipe database for projects
2838
yarn fetch-data:local
2939

30-
> NOTE: Currently the platform runs smoothly in node 16, so developers might have to switch to node 16 for development.
40+
# Run
3141
yarn dev
3242
```
3343

44+
> [!NOTE]
45+
> Currently the platform runs smoothly in node 16, so developers might have to switch to node 16 for development.
46+
3447
Whenever new texts for translation are added, translation files need to be generated.
3548

3649
```bash

scripts/fetchData.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,19 @@ async function getCsrfTokenValue() {
195195
try {
196196
const healthcheckData = await fetch(healthcheckUrl, { credentials: 'include' });
197197

198-
const setCookies = 'getSetCookie' in healthcheckData.headers && typeof healthcheckData.headers.getSetCookie === 'function'
199-
? healthcheckData.headers.getSetCookie()
200-
: undefined;
198+
const cookiesToSet = (
199+
healthcheckData.headers as (Headers & { getSetCookie: () => string[] })
200+
).getSetCookie();
201201

202-
// FIXME: do a proper parsing
203-
const token = setCookies?.[0].split('; ')[0].split('=')[1];
202+
const parsedCookiesToSet = cookiesToSet
203+
.flatMap((item: string) => item.split('; '))
204+
.map((item: string) => {
205+
const [key, value] = item.split('=');
206+
return { key, value } as { key: string, value: string };
207+
});
204208

205-
return token;
209+
const csrfToken = parsedCookiesToSet.find((item) => item.key === COOKIE_NAME);
210+
return csrfToken?.value;
206211
} catch (err) {
207212
// eslint-disable-next-line no-console
208213
console.error('failed to do the healthcheck', healthcheckUrl);
@@ -223,9 +228,14 @@ async function fetchAndWriteData() {
223228
console.error('Could not fetch crsf token');
224229
return;
225230
}
231+
const referer = process.env.MAPSWIPE_REFERER_ENDPOINT ?? baseUrl;
232+
233+
console.log('CSRF Token exists:', !!csrfTokenValue);
234+
console.log('Referer exists:', !!referer);
235+
226236
graphQLClient.setHeader('X-CSRFToken', csrfTokenValue);
227237
graphQLClient.setHeader('Cookie', `${COOKIE_NAME}=${csrfTokenValue}`);
228-
graphQLClient.setHeader('Referer', process.env.MAPSWIPE_REFERER_ENDPOINT ?? '');
238+
graphQLClient.setHeader('Referer', referer);
229239
data = (await graphQLClient.request(query)) as AllDataQuery;
230240
}
231241

0 commit comments

Comments
 (0)