-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add origin passthrough to other endpoints
- Loading branch information
1 parent
8a78ee7
commit 178ce8d
Showing
6 changed files
with
104 additions
and
67 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
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,34 @@ | ||
// Pass the request to an origin server if USE_ORIGIN is set to 'true' | ||
import fetchWithTimeout from '../utils/fetch-with-timeout.mjs'; | ||
|
||
export default function useGraphQLOrigin(env) { | ||
return { | ||
async onParams({params, request, setParams, setResult, fetchAPI}) { | ||
if (env.USE_ORIGIN !== 'true') { | ||
return; | ||
} | ||
try { | ||
const originUrl = new URL(request.url); | ||
if (env.ORIGIN_OVERRIDE) { | ||
originUrl.host = env.ORIGIN_OVERRIDE; | ||
} | ||
const queryResult = await fetchWithTimeout(originUrl, { | ||
method: request.method, | ||
body: JSON.stringify(params), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
timeout: 20000 | ||
}); | ||
if (queryResult.status !== 200) { | ||
throw new Error(`${queryResult.status} ${queryResult.statusText}: ${await queryResult.text()}`); | ||
} | ||
console.log('Request served from origin server'); | ||
setResult(await queryResult.json()); | ||
request.cached = true; | ||
} catch (error) { | ||
console.error(`Error getting response from origin server: ${error}`); | ||
} | ||
}, | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.