Skip to content

Commit c731eac

Browse files
committed
Implemented JSearch api route for jobs. Refactored OpenAI api route
1 parent 68721fb commit c731eac

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

app/api/openai/route.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
import { NextResponse } from "next/server";
22

3+
const config = {
4+
apiKey: process.env.OPENAI_API_KEY as string,
5+
apiHost: "https://api.openai.com/v1/chat/completions",
6+
systemContent:
7+
"You are a knowlegeable assistant that provides quality information.",
8+
userContent: (question: string) => `Tell me ${question}`,
9+
};
10+
311
export const POST = async (request: Request) => {
412
const { question } = await request.json();
513

614
try {
7-
const response = await fetch("https://api.openai.com/v1/chat/completions", {
15+
const response = await fetch(config.apiHost, {
816
method: "POST",
917
headers: {
1018
"Content-Type": "application/json",
11-
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
19+
Authorization: `Bearer ${config.apiKey}}`,
1220
},
1321
body: JSON.stringify({
1422
model: "gpt-3.5-turbo",
1523
messages: [
1624
{
1725
role: "system",
18-
content:
19-
"You are a knowlegeable assistant that provides quality information.",
26+
content: config.systemContent,
2027
},
2128
{
2229
role: "user",
23-
content: `Tell me ${question}`,
30+
content: config.userContent(question),
2431
},
2532
],
2633
}),

app/api/rapidapi/route.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { NextResponse } from "next/server";
2+
3+
const config = {
4+
apiKey: process.env.RAPIDAPI_API_KEY as string,
5+
apiHost: "https://jsearch.p.rapidapi.com",
6+
};
7+
8+
export const POST = async (request: Request) => {
9+
const {
10+
page = 1,
11+
pageSize = 1,
12+
filter = "us",
13+
searchQuery = "Software Engineer",
14+
} = await request.json();
15+
16+
try {
17+
const response = await fetch(
18+
`${config.apiHost}/search?query=${searchQuery}&location=${filter}&page=${page}&num_pages=${pageSize}`,
19+
{
20+
method: "GET",
21+
headers: {
22+
"X-RapidAPI-Key": config.apiKey,
23+
"X-RapidAPI-Host": config.apiHost,
24+
},
25+
}
26+
);
27+
28+
const responseData = await response.json();
29+
const result = responseData.result;
30+
31+
return NextResponse.json({ result });
32+
} catch (error: any) {
33+
return NextResponse.json({ error: error.message });
34+
}
35+
};

0 commit comments

Comments
 (0)