Skip to content

Commit

Permalink
chore: config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
apotdevin committed Jun 18, 2024
1 parent ce58c28 commit 6b441ca
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 38 deletions.
9 changes: 1 addition & 8 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
// async rewrites() {
// return [
// {
// source: '/api/graphql',
// destination: 'http://localhost:5000/api/graphql',
// },
// ];
// },

webpack: (config, { isServer, webpack }) => {
const wasmRegex = /argon2.*\.wasm$/;

Expand Down
8 changes: 7 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default function RootLayout({
}>) {
const cookieStore = cookies();

const serverUrl = `${process.env.URL}/api/graphql`;

const accessToken = cookieStore.get('amboss_banco_access_token')?.value;
const refreshToken = cookieStore.get('amboss_banco_refresh_token')?.value;

Expand All @@ -36,7 +38,11 @@ export default function RootLayout({
enableSystem
disableTransitionOnChange
>
<ApolloWrapper accessToken={accessToken} refreshToken={refreshToken}>
<ApolloWrapper
serverUrl={serverUrl}
accessToken={accessToken}
refreshToken={refreshToken}
>
{children}
<Toaster />
</ApolloWrapper>
Expand Down
18 changes: 0 additions & 18 deletions src/lib/apollo/client.ts

This file was deleted.

29 changes: 18 additions & 11 deletions src/lib/apollo/wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { from, GraphQLRequest, HttpLink } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
import { onError } from '@apollo/client/link/error';
import {
ApolloClient,
ApolloNextAppProvider,
NextSSRApolloClient,
NextSSRInMemoryCache,
} from '@apollo/experimental-nextjs-app-support/ssr';
InMemoryCache,
} from '@apollo/experimental-nextjs-app-support';

import {
RefreshTokenDocument,
Expand All @@ -30,10 +30,12 @@ const returnTokenDependingOnOperation = (
};

const refreshTokens = async (
serverUrl: string,
accessToken: string = '',
refreshToken: string = ''
) => {
const result = await makeClient(
serverUrl,
accessToken,
refreshToken
).mutate<RefreshTokenMutation>({
Expand All @@ -44,6 +46,7 @@ const refreshTokens = async (
};

const makeClient = (
serverUrl: string,
accessToken: string | undefined,
refreshToken: string | undefined
) => {
Expand Down Expand Up @@ -79,9 +82,11 @@ const makeClient = (
}

return promiseToObservable(
refreshTokens(accessToken, refreshToken).catch(() => {
window.location.href = ROUTES.home;
})
refreshTokens(serverUrl, accessToken, refreshToken).catch(
() => {
window.location.href = ROUTES.home;
}
)
).flatMap(accessToken => {
const oldHeaders = operation.getContext().headers;

Expand All @@ -107,8 +112,8 @@ const makeClient = (

const httpLink = new HttpLink({
// this needs to be an absolute url, as relative urls cannot be used in SSR
uri: 'https://api.mibanco.app/api/graphql',
// uri: 'http://localhost:3000/api/graphql',
// uri: 'https://api.mibanco.app/api/graphql',
uri: serverUrl, //'http://localhost:3000/api/graphql',
credentials: 'include',
// credentials: 'same-origin',
// you can disable result caching here if you want to
Expand All @@ -122,24 +127,26 @@ const makeClient = (

const link = from([authLink, errorLink, httpLink]);

return new NextSSRApolloClient({
cache: new NextSSRInMemoryCache(),
return new ApolloClient({
cache: new InMemoryCache(),
link,
connectToDevTools: true,
});
};

export function ApolloWrapper({
serverUrl,
accessToken,
refreshToken,
children,
}: React.PropsWithChildren<{
serverUrl: string;
accessToken: string | undefined;
refreshToken: string | undefined;
}>) {
return (
<ApolloNextAppProvider
makeClient={() => makeClient(accessToken, refreshToken)}
makeClient={() => makeClient(serverUrl, accessToken, refreshToken)}
>
{children}
</ApolloNextAppProvider>
Expand Down
28 changes: 28 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith('/api/graphql')) {
return NextResponse.rewrite(
new URL(
`${process.env.SERVER_URL}/api/graphql` ||
'http://localhost:5000/api/graphql'
)
);
}
if (request.nextUrl.pathname.startsWith('/.well-known')) {
return NextResponse.rewrite(
new URL(
`${process.env.SERVER_URL}/.well-known` ||
'http://localhost:5000/.well-known'
)
);
}
if (request.nextUrl.pathname.startsWith('/lnurlp')) {
return NextResponse.rewrite(
new URL(
`${process.env.SERVER_URL}/lnurlp` || 'http://localhost:5000/lnurlp'
)
);
}
}

0 comments on commit 6b441ca

Please sign in to comment.