Last active
April 8, 2022 16:50
Revisions
-
tungd revised this gist
Apr 8, 2022 . 1 changed file with 26 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ PROXY_FN = <<-JS export async function onRequest(context) { const { request, params, env } = context; const url = new URL(request.url); url.host = env.API_HOST; url.pathname = params.path.join('') === 'json' ? `/api/json` : params.path.join('/'); if (request.method === 'POST' && !request.headers.get('Content-Type')) { const headers = Object.fromEntries(request.headers.entries()) return fetch(url, { method: request.method, headers: { ...headers, 'Content-Type': 'application/json' }, body: '{}' }); } return fetch(url, request); } JS task :default do FileUtils.makedirs 'functions/api' File.write 'functions/api/[[path]].js', PROXY_FN sh 'npm run build' end -
tungd revised this gist
Apr 8, 2022 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,11 +4,12 @@ export async function onRequest(context) { const url = new URL(request.url); url.host = env.API_HOST; url.pathname = params.path.join('') === 'json' ? `/api/json` : params.path.join('/'); if (request.method === 'POST' && !request.headers.get('Content-Type')) { const headers = Object.fromEntries(request.headers.entries()) return fetch(url, { method: request.method, headers: { ...headers, 'Content-Type': 'application/json' }, body: '{}' -
tungd revised this gist
Apr 8, 2022 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,13 +5,14 @@ export async function onRequest(context) { url.host = env.API_HOST; url.pathname = params.path.join('') === 'json' ? `/api/json` : params.path.join('/'); if (!request.headers.get('Content-Type')) { return fetch(url, { method: request.method, headers: { ...request.headers, 'Content-Type': 'application/json' }, body: '{}' }); } return fetch(url, request); } -
tungd revised this gist
Apr 8, 2022 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,13 +5,13 @@ export async function onRequest(context) { url.host = env.API_HOST; url.pathname = params.path.join('') === 'json' ? `/api/json` : params.path.join('/'); if (!request.headers.get('Content-Type')) { const fixed = new Request(request, { headers: { 'Content-Type': 'application/json' }, body: '{}' }) return fetch(url, fixed); } return fetch(url, request); } -
tungd revised this gist
Apr 8, 2022 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,15 @@ export async function onRequest(context) { const url = new URL(request.url); url.host = env.API_HOST; url.pathname = params.path.join('') === 'json' ? `/api/json` : params.path.join('/'); if (!request.headers.get('Content-Type')) { return fetch(url, { headers: { ...request.headers, 'Content-Type': 'application/json' }, body: '{}' }); } return fetch(url, request); } endef @@ -19,4 +28,3 @@ clean: rm -rf dist functions .PHONY: build clean -
tungd revised this gist
Mar 29, 2022 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ define PROXY_FN export async function onRequest(context) { const { request, params, env } = context; const url = new URL(request.url); url.host = env.API_HOST; url.pathname = params.path.join('') === 'json' ? `/api/json` : params.path.join('/'); return fetch(url, request); } endef @@ -19,3 +19,4 @@ clean: rm -rf dist functions .PHONY: build clean -
tungd created this gist
Mar 29, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ define PROXY_FN export async function onRequest(context) { const { request, params, env } = context; const url = params.path.join('') === 'json' ? `$${env.ENV_URL}/api/json` : `$${env.ENV_URL}/$${params.path.join('/')}`; return fetch(url, request); } endef export PROXY_FN build: mkdir -p functions/api echo $$PROXY_FN >> functions/api/[[path]].js npm run build clean: rm -rf dist functions .PHONY: build clean