@@ -11,6 +11,7 @@ export type GraphTokenResponse = {
11
11
}
12
12
13
13
const TOKEN_HEADER = 'X-Nf-Graph-Token'
14
+ const TOKEN_HEADER_NORMALIZED = 'x-nf-graph-token'
14
15
15
16
// Matches Web API Headers type (https://developer.mozilla.org/en-US/docs/Web/API/Headers)
16
17
interface RequestHeaders {
@@ -33,21 +34,37 @@ const hasRequestStyleHeaders = function (headers: RequestHeaders | IncomingHttpH
33
34
const graphTokenFromIncomingHttpStyleHeaders = function (
34
35
headers : RequestHeaders | IncomingHttpHeaders ,
35
36
) : string | null | undefined {
36
- if ( TOKEN_HEADER in headers ) {
37
- const header = headers [ TOKEN_HEADER ]
38
- if ( header == null || typeof header === 'string' ) {
39
- return header
37
+ if ( TOKEN_HEADER in headers || TOKEN_HEADER_NORMALIZED in headers ) {
38
+ const header = headers [ TOKEN_HEADER ] || headers [ TOKEN_HEADER_NORMALIZED ]
39
+ if ( Array . isArray ( header ) ) {
40
+ return header [ 0 ]
40
41
}
41
- return header [ 0 ]
42
+ return header
42
43
}
43
44
}
44
45
45
- // Backwards compatibility with older version of cli that doesn't inject header
46
- const authlifyTokenFallback = function ( event : HasHeaders ) : GraphTokenResponse {
47
- const token = ( event as { authlifyToken ?: string | null } ) ?. authlifyToken
46
+ const graphTokenFromEnv = function ( ) : GraphTokenResponse {
47
+ // _NETLIFY_GRAPH_TOKEN injected by next plugin
48
+ // eslint-disable-next-line no-underscore-dangle
49
+ const token = env . _NETLIFY_GRAPH_TOKEN || env . NETLIFY_GRAPH_TOKEN
48
50
return { token }
49
51
}
50
52
53
+ const tokenFallback = function ( event : HasHeaders ) : GraphTokenResponse {
54
+ // Backwards compatibility with older version of cli that doesn't inject header
55
+ const token = ( event as { authlifyToken ?: string | null } ) ?. authlifyToken
56
+ if ( token ) {
57
+ return { token }
58
+ }
59
+
60
+ // If we're in dev-mode with next.js, the plugin won't be there to inject
61
+ // secrets, so we need to get the token from the environment
62
+ if ( env . NETLIFY_DEV === 'true' ) {
63
+ return graphTokenFromEnv ( )
64
+ }
65
+ return { token : null }
66
+ }
67
+
51
68
const graphTokenFromEvent = function ( event : HasHeaders ) : GraphTokenResponse {
52
69
const { headers } = event
53
70
// Check if object first in case there is a header with key `get`
@@ -60,14 +77,7 @@ const graphTokenFromEvent = function (event: HasHeaders): GraphTokenResponse {
60
77
return { token : headers . get ( TOKEN_HEADER ) }
61
78
}
62
79
63
- return authlifyTokenFallback ( event )
64
- }
65
-
66
- const graphTokenFromEnv = function ( ) : GraphTokenResponse {
67
- // _NETLIFY_GRAPH_TOKEN injected by next plugin
68
- // eslint-disable-next-line no-underscore-dangle
69
- const token = env . _NETLIFY_GRAPH_TOKEN || env . NETLIFY_GRAPH_TOKEN
70
- return { token }
80
+ return tokenFallback ( event )
71
81
}
72
82
73
83
const isEventRequired = function ( ) : boolean {
@@ -125,3 +135,7 @@ export const getNetlifyGraphToken = function (
125
135
126
136
return event ? graphTokenFromEvent ( event ) : graphTokenFromEnv ( )
127
137
}
138
+
139
+ export const getNetlifyGraphTokenForBuild = function ( ) : GraphTokenResponse {
140
+ return graphTokenFromEnv ( )
141
+ }
0 commit comments