Skip to content

Instantly share code, notes, and snippets.

@tungd
Last active April 8, 2022 16:50

Revisions

  1. tungd revised this gist Apr 8, 2022. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions Rakefile
    Original 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
  2. tungd revised this gist Apr 8, 2022. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions Makefile
    Original 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.headers.get('Content-Type')) {
    if (request.method === 'POST' && !request.headers.get('Content-Type')) {
    const headers = Object.fromEntries(request.headers.entries())
    return fetch(url, {
    method: request.method,
    headers: {
    ...request.headers,
    ...headers,
    'Content-Type': 'application/json'
    },
    body: '{}'
  3. tungd revised this gist Apr 8, 2022. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions Makefile
    Original 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')) {
    const fixed = new Request(request, {
    return fetch(url, {
    method: request.method,
    headers: {
    ...request.headers,
    'Content-Type': 'application/json'
    },
    body: '{}'
    })
    return fetch(url, fixed);
    });
    }
    return fetch(url, request);
    }
  4. tungd revised this gist Apr 8, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Makefile
    Original 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')) {
    return fetch(url, {
    const fixed = new Request(request, {
    headers: {
    ...request.headers,
    'Content-Type': 'application/json'
    },
    body: '{}'
    });
    })
    return fetch(url, fixed);
    }
    return fetch(url, request);
    }
  5. tungd revised this gist Apr 8, 2022. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion Makefile
    Original 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

  6. tungd revised this gist Mar 29, 2022. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions Makefile
    Original 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 = params.path.join('') === 'json'
    ? `$${env.ENV_URL}/api/json`
    : `$${env.ENV_URL}/$${params.path.join('/')}`;
    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

  7. tungd created this gist Mar 29, 2022.
    21 changes: 21 additions & 0 deletions Makefile
    Original 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