-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch Paths data and display settings bar
- Loading branch information
Showing
30 changed files
with
6,014 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
require('dotenv').config(); | ||
|
||
const { pathsGqlUrl } = require('./common/environment'); | ||
|
||
const JS = '*.{js,jsx,ts,tsx,mjs}'; | ||
|
||
module.exports = { | ||
client: { | ||
includes: [ | ||
'common/paths/**/*.{js,ts,tsx}', | ||
'components/paths/**/*.{js,ts,tsx}', | ||
'context/paths/**/*.{js,ts,tsx}', | ||
'queries/paths/**/*.{js,ts,tsx}', | ||
], | ||
excludes: ['components/paths/contentblocks/**/*.{js,ts,tsx}'], | ||
service: { | ||
name: 'kausal-paths', | ||
url: `${pathsGqlUrl}`, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { headers } from 'next/headers'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { captureException } from '@sentry/nextjs'; | ||
|
||
import { forwardSetCookie, getClientCookieAsHeader } from '@/common/cookies'; | ||
import { pathsGqlUrl } from '@/common/environment'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
const PASS_HEADERS = [ | ||
'x-paths-instance-identifier', | ||
'x-paths-instance-hostname', | ||
'x-wildcard-domains', | ||
'user-agent', | ||
'authorization', | ||
'accept-language', | ||
'dnt', | ||
'referer', | ||
]; | ||
|
||
const PATHS_COOKIE_PREFIX = 'paths_api_'; | ||
|
||
export async function POST(request: NextRequest) { | ||
const headersList = headers(); | ||
const requestData = await request.json(); | ||
const backendCookieHeader = getClientCookieAsHeader(request, { | ||
prefix: PATHS_COOKIE_PREFIX, | ||
}); | ||
|
||
// Determine headers to send to the backend | ||
const backendHeaders: Record<string, string> = {}; | ||
PASS_HEADERS.forEach((header) => { | ||
const value = headersList.get(header); | ||
if (value) backendHeaders[header] = value; | ||
}); | ||
backendHeaders['Content-Type'] = 'application/json'; | ||
if (backendCookieHeader) { | ||
backendHeaders['Cookie'] = backendCookieHeader; | ||
} | ||
|
||
// Do the fetch from the backend | ||
const backendResponse = await fetch(pathsGqlUrl, { | ||
method: 'POST', | ||
headers: backendHeaders, | ||
body: JSON.stringify(requestData), | ||
next: { revalidate: 0 }, | ||
}); | ||
|
||
// Set response headers | ||
const responseHeaders: Record<string, string> = { | ||
'Content-Type': | ||
backendResponse.headers.get('Content-Type') ?? 'application/json', | ||
'Content-Language': backendResponse.headers.get('Content-Language') ?? '', | ||
'Cache-Control': 'no-store', | ||
}; | ||
|
||
if (!backendResponse.ok) { | ||
console.error('Backend responded with ', backendResponse.status); | ||
let data: object | undefined, errorMessage: string | undefined; | ||
try { | ||
if (backendResponse.headers.get('content-type') === 'application/json') { | ||
data = await backendResponse.json(); | ||
} | ||
} catch (error) { | ||
captureException(error); | ||
} | ||
if (!data) { | ||
errorMessage = await backendResponse.text(); | ||
data = { errors: [{ message: errorMessage }] }; | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.log(data); | ||
} | ||
return NextResponse.json(data, { | ||
status: backendResponse.status, | ||
headers: responseHeaders, | ||
}); | ||
} | ||
|
||
try { | ||
const data = await backendResponse.json(); | ||
forwardSetCookie(request, backendResponse, { prefix: PATHS_COOKIE_PREFIX }); | ||
return NextResponse.json(data, { status: 200, headers: responseHeaders }); | ||
} catch (error) { | ||
return NextResponse.json( | ||
{ errors: [{ message: `Response is invalid JSON: ${error?.message}` }] }, | ||
{ status: 500, headers: responseHeaders } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.