Last active
October 10, 2023 22:17
-
-
Save ntotten/a61c0f11b07428370d163ec18123f0e3 to your computer and use it in GitHub Desktop.
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
import { ZuploContext, ZuploRequest } from "@zuplo/runtime"; | |
export default async function ( | |
request: ZuploRequest, | |
context: ZuploContext, | |
options: any, | |
policyName: string | |
) { | |
const url = new URL(request.url); | |
if (url.pathname.startsWith('/sandbox')) { | |
const newRequest = new Request(url.pathname.replace('/sandbox/', '/'), request); | |
newRequest.headers.set('authorization', `Bearer ${environment.SUPABASE_STG_KEY}`); | |
context.custom.forwardUrl = environment.SUPABASE_STAGING; | |
return newRequest; | |
} else if (url.pathname.startsWith('/v1')) { | |
const newRequest = new Request(url.pathname.replace('/v1/', '/'), request); | |
newRequest.headers.set('authorization', `Bearer ${environment.SUPABASE_PROD_KEY}`); | |
context.custom.forwardUrl = environment.SUPABASE_PRODUCTION; | |
return newRequest; | |
} else if (url.pathname.startsWith("/api")) { | |
// This assumes that the 'default' environment is production so use the productio info | |
const newRequest = new Request(url.pathname.replace('/v1/', '/'), request); | |
newRequest.headers.set('authorization', `Bearer ${environment.SUPABASE_PROD_KEY}`); | |
context.custom.forwardUrl = environment.SUPABASE_PRODUCTION; | |
return newRequest; | |
} | |
return new Response(JSON.stringify({ | |
message: 'Inexistent environment, please use a valid base URL.' | |
}), { | |
status: 404, | |
statusText: 'PXT-AUTH000' | |
}) | |
} |
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
import { ZuploContext, ZuploRequest } from "@zuplo/runtime"; | |
export default async function ( | |
request: ZuploRequest, | |
context: ZuploContext, | |
options: any, | |
policyName: string | |
) { | |
context.custom.forwardUrl = getForwardUrl(request); | |
return request; | |
} | |
function getForwardUrl(request: ZuploRequest) { | |
// Logic to determine forwarded url here: | |
return "https://my-environment.supabase.com" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment