Commit 86d5822 1 parent 2bc3ba4 commit 86d5822 Copy full SHA for 86d5822
File tree 8 files changed +50
-10
lines changed
8 files changed +50
-10
lines changed Original file line number Diff line number Diff line change 1
1
FUEL_PROVIDER = https://beta-5.fuel.network/graphql
2
+ # Explorer API URL
3
+ FUEL_EXPLORER_API =
4
+ FUEL_EXPLORER_TOKEN =
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
import { createExecutor , createSchema } from '@fuel-explorer/graphql' ;
2
2
import { ContextDomain } from '@fuel-explorer/graphql/src/domains/Context' ;
3
3
import { createYoga } from 'graphql-yoga' ;
4
+ import { NextResponse } from 'next/server' ;
4
5
import { requireEnv } from '~/systems/utils/requireEnv' ;
5
6
6
7
const { FUEL_PROVIDER : url } = requireEnv ( [ 'FUEL_PROVIDER' ] ) ;
@@ -29,8 +30,17 @@ const { handleRequest } = createYoga({
29
30
} ,
30
31
} ) ;
31
32
33
+ const authenticatiohMiddleware = async ( request : Request , ctx : Object ) => {
34
+ if ( process . env . FUEL_EXPLORER_API ) {
35
+ return new NextResponse ( 'Unauthorized' , {
36
+ status : 401 ,
37
+ } ) ;
38
+ }
39
+ return handleRequest ( request , ctx ) ;
40
+ } ;
41
+
32
42
export {
33
- handleRequest as GET ,
34
- handleRequest as POST ,
35
- handleRequest as OPTIONS ,
43
+ authenticatiohMiddleware as GET ,
44
+ authenticatiohMiddleware as POST ,
45
+ authenticatiohMiddleware as OPTIONS ,
36
46
} ;
Original file line number Diff line number Diff line change 1
- import { resolve } from 'url' ;
2
- import { getSdk } from '@fuel-explorer/graphql' ;
1
+ import { getSdk } from '@fuel-explorer/graphql/src/sdk' ;
3
2
import { GraphQLClient } from 'graphql-request' ;
4
3
4
+ const { FUEL_EXPLORER_API , FUEL_EXPLORER_API_KEY } = process . env ;
5
5
const VERCEL_URL = process . env . VERCEL_URL || process . env . NEXT_PUBLIC_VERCEL_URL ;
6
6
const VERCEL_ENV =
7
7
process . env . VERCEL_ENV || process . env . NEXT_PUBLIC_VERCEL_ENV || 'development' ;
8
8
9
9
const getBaseUrl = ( ) => {
10
+ if ( FUEL_EXPLORER_API && FUEL_EXPLORER_API_KEY ) return FUEL_EXPLORER_API ;
10
11
if ( VERCEL_ENV !== 'development' ) return `https://${ VERCEL_URL } ` ;
11
- return 'http://localhost:3000' ;
12
+ return 'http://localhost:3000/api/graphql' ;
13
+ } ;
14
+ const getHeaders = ( ) => {
15
+ if ( FUEL_EXPLORER_API_KEY ) {
16
+ return { Authorization : `Bearer ${ FUEL_EXPLORER_API_KEY } ` } ;
17
+ }
18
+ return undefined ;
12
19
} ;
13
20
14
- const API_URL = resolve ( getBaseUrl ( ) , '/api/graphql' ) ;
15
- const client = new GraphQLClient ( API_URL , { fetch } ) ;
21
+ const client = new GraphQLClient ( getBaseUrl ( ) , {
22
+ fetch,
23
+ headers : getHeaders ( ) ,
24
+ } ) ;
16
25
export const sdk = getSdk ( client ) ;
Original file line number Diff line number Diff line change 1
1
node_moduels
2
2
.turbo
3
3
dist
4
+ .env
Original file line number Diff line number Diff line change @@ -3,7 +3,13 @@ import type { CodegenConfig } from '@graphql-codegen/cli';
3
3
const config : CodegenConfig = {
4
4
overwrite : true ,
5
5
documents : [ './src/queries/**.graphql' ] ,
6
- schema : `http://localhost:${ process . env . SERVER_PORT } /graphql` ,
6
+ schema : {
7
+ [ `http://localhost:${ process . env . SERVER_PORT } /graphql` ] : {
8
+ headers : {
9
+ Authorization : `Bearer ${ process . env . FUEL_EXPLORER_API_KEY } ` ,
10
+ } ,
11
+ } ,
12
+ } ,
7
13
generates : {
8
14
'src/generated/types.ts' : {
9
15
plugins : [
Original file line number Diff line number Diff line change
1
+ export * from './generated/types' ;
Original file line number Diff line number Diff line change @@ -11,13 +11,24 @@ import { requireEnv } from './utils/requireEnv';
11
11
const { FUEL_PROVIDER } = requireEnv ( [
12
12
[ 'FUEL_PROVIDER' , 'https://beta-5.fuel.network/graphql' ] ,
13
13
] ) ;
14
+ const { FUEL_EXPLORER_API_KEY } = process . env ;
14
15
15
16
// Create a server:
16
17
const app = express ( ) ;
17
18
18
19
app . use ( cors < cors . CorsRequest > ( ) ) ;
19
20
app . use ( express . json ( ) ) ;
20
21
22
+ if ( FUEL_EXPLORER_API_KEY ) {
23
+ app . use ( ( req , res , next ) => {
24
+ if ( req . headers . authorization !== `Bearer ${ FUEL_EXPLORER_API_KEY } ` ) {
25
+ res . status ( 401 ) . send ( 'Unauthorized' ) ;
26
+ return ;
27
+ }
28
+ next ( ) ;
29
+ } ) ;
30
+ }
31
+
21
32
app . get (
22
33
'/graphql' ,
23
34
expressPlayground ( {
You can’t perform that action at this time.
0 commit comments