Skip to content

Commit a3d83ba

Browse files
committed
app:ref: update Deepcrawl client initialization to use environment variable for API key and enhance header handling with Next.js support
1 parent ed2c626 commit a3d83ba

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

apps/app/app/actions/deepcrawl.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DeepcrawlNetworkError,
1010
DeepcrawlReadError,
1111
} from '@deepcrawl-sdk/ts';
12+
import { headers } from 'next/headers';
1213

1314
interface ApiCallInput {
1415
url: string;
@@ -23,20 +24,25 @@ interface ApiResponse {
2324
timestamp?: string;
2425
}
2526

26-
const apiKey = '123123123';
27-
28-
const dc = new DeepcrawlApp({
29-
apiKey,
30-
baseUrl:
31-
process.env.NODE_ENV === 'development'
32-
? 'http://localhost:8080'
33-
: 'https://api.deepcrawl.dev',
34-
});
27+
const apiKey =
28+
process.env.DEEP_CRAWL_API_KEY ?? 'use_header_auth_instead_api_key';
29+
30+
async function createDeepcrawlClient() {
31+
return new DeepcrawlApp({
32+
apiKey,
33+
baseUrl:
34+
process.env.NODE_ENV === 'development'
35+
? 'http://localhost:8080'
36+
: 'https://api.deepcrawl.dev',
37+
headers: await headers(), // SDK automatically extracts only auth headers
38+
});
39+
}
3540

3641
export async function getMarkdownAction({
3742
url,
3843
}: ApiCallInput): Promise<ApiResponse> {
3944
try {
45+
const dc = await createDeepcrawlClient();
4046
const data = await dc.getMarkdown(url);
4147

4248
return {
@@ -92,6 +98,7 @@ export async function readUrlAction({
9298
url,
9399
}: ApiCallInput): Promise<ApiResponse> {
94100
try {
101+
const dc = await createDeepcrawlClient();
95102
const data: ReadPOSTOutput = await dc.readUrl(url);
96103

97104
return {
@@ -147,6 +154,7 @@ export async function extractLinksAction({
147154
url,
148155
}: ApiCallInput): Promise<ApiResponse> {
149156
try {
157+
const dc = await createDeepcrawlClient();
150158
const data: LinksPOSTOutput = await dc.extractLinks(url);
151159

152160
return {

0 commit comments

Comments
 (0)