Skip to content

Commit cf51571

Browse files
committed
use a server action to check for api key, define it in env.ts
1 parent 93885e3 commit cf51571

File tree

4 files changed

+12
-38
lines changed

4 files changed

+12
-38
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use server';
2+
3+
import { resendApiKey } from '../app/env';
4+
5+
export async function hasResendApiKey() {
6+
return (resendApiKey ?? '').trim().length > 0;
7+
}

packages/preview-server/src/app/api/has-resend-api-key/route.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/preview-server/src/app/env.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export const previewServerLocation = process.env.PREVIEW_SERVER_LOCATION!;
88
export const emailsDirectoryAbsolutePath =
99
process.env.EMAILS_DIR_ABSOLUTE_PATH!;
1010

11+
/** ONLY ACCESSIBLE ON THE SERVER */
12+
export const resendApiKey = process.env.RESEND_API_KEY;
13+
1114
export const isBuilding = process.env.NEXT_PUBLIC_IS_BUILDING === 'true';
1215

1316
export const isPreviewDevelopment =

packages/preview-server/src/components/toolbar/resend.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { IconCloudAlert } from '../icons/icon-cloud-alert';
1111
import { IconCloudCheck } from '../icons/icon-cloud-check';
1212
import { IconLoader } from '../icons/icon-loader';
1313
import { Results } from './results';
14+
import { hasResendApiKey } from '../../actions/has-resend-api-key';
1415

1516
export interface ResendStatus {
1617
hasApiKey: boolean;
@@ -32,27 +33,11 @@ export const useResend = ({
3233
setLoading(true);
3334

3435
try {
35-
const response = await fetch('/api/has-resend-api-key', {
36-
method: 'GET',
37-
headers: { 'Content-Type': 'application/json' },
38-
});
39-
40-
const body = (await response.json().catch(() => ({}))) as
41-
| { ok: boolean; error: string | null }
42-
| undefined;
43-
44-
if (response.ok && body?.ok) {
36+
if (await hasResendApiKey()) {
4537
const result = { hasApiKey: true, error: null };
4638
setStatus(result);
4739
return result;
4840
}
49-
50-
const result = {
51-
hasApiKey: false,
52-
error: body?.error ?? 'Unknown error',
53-
};
54-
setStatus(result);
55-
return result;
5641
} catch (exception) {
5742
console.error('Error checking Resend API key', exception);
5843
} finally {

0 commit comments

Comments
 (0)