Skip to content

Commit 415b75e

Browse files
Merge pull request #526 from gridaco/canary
Refactor API hooks to use dynamic base URL for hosted environments
2 parents 54cbd23 + f38c638 commit 415b75e

File tree

2 files changed

+9
-7
lines changed
  • editor/app/(api)/(public)/v1/submit/[id]

2 files changed

+9
-7
lines changed

editor/app/(api)/(public)/v1/submit/[id]/hooks.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { Env } from "@/env";
44
import { resend } from "@/clients/resend";
55
import EmailTemplate from "@/theme/templates-email/formcomplete/default";
66

7-
const GRIDA_S2S_PRIVATE_API_KEY = process.env.GRIDA_S2S_PRIVATE_API_KEY;
7+
// In hosted env, avoid calling the deployment domain (`*.vercel.app`) since it
8+
// can be protected upstream (401) even when our app routes would allow it.
9+
const HOOK_BASE_URL = Env.server.IS_HOSTED ? Env.web.HOST : Env.server.HOST;
10+
const GRIDA_S2S_PRIVATE_API_KEY = process.env.GRIDA_S2S_PRIVATE_API_KEY ?? null;
811

912
const bird = new Bird(
1013
process.env.BIRD_WORKSPACE_ID as string,
@@ -24,7 +27,7 @@ export namespace OnSubmit {
2427
response_id: string;
2528
session_id: string;
2629
}) {
27-
return fetch(`${Env.server.HOST}/v1/submit/${form_id}/hooks/clearsession`, {
30+
return fetch(`${HOOK_BASE_URL}/v1/submit/${form_id}/hooks/clearsession`, {
2831
headers: {
2932
"Content-Type": "application/json",
3033
},
@@ -43,7 +46,7 @@ export namespace OnSubmit {
4346
form_id: string;
4447
response_id: string;
4548
}) {
46-
return fetch(`${Env.server.HOST}/v1/submit/${form_id}/hooks/postindexing`, {
49+
return fetch(`${HOOK_BASE_URL}/v1/submit/${form_id}/hooks/postindexing`, {
4750
headers: {
4851
"Content-Type": "application/json",
4952
},
@@ -62,7 +65,7 @@ export namespace OnSubmit {
6265
response_id: string;
6366
}) {
6467
return fetch(
65-
`${Env.server.HOST}/v1/submit/${form_id}/hooks/notification-respondent-email`,
68+
`${HOOK_BASE_URL}/v1/submit/${form_id}/hooks/notification-respondent-email`,
6669
{
6770
headers: {
6871
"Content-Type": "application/json",

editor/app/(api)/(public)/v1/submit/[id]/hooks/notification-respondent-email/route.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ type Params = { id: string };
1414
* This route uses `service_role` and can send emails, so it must not be
1515
* callable by arbitrary third-parties.
1616
*/
17-
const GRIDA_S2S_PRIVATE_API_KEY = process.env.GRIDA_S2S_PRIVATE_API_KEY;
17+
const GRIDA_S2S_PRIVATE_API_KEY = process.env.GRIDA_S2S_PRIVATE_API_KEY ?? null;
1818

1919
export async function POST(
2020
req: NextRequest,
2121
context: {
2222
params: Promise<Params>;
2323
}
2424
) {
25-
const provided =
26-
req.headers.get("x-grida-s2s-key") ?? req.headers.get("x-hook-secret");
25+
const provided = req.headers.get("x-grida-s2s-key");
2726
if (!GRIDA_S2S_PRIVATE_API_KEY) {
2827
console.error(
2928
"notification-respondent-email/err/misconfigured: GRIDA_S2S_PRIVATE_API_KEY missing"

0 commit comments

Comments
 (0)