Skip to content

Commit

Permalink
fix fetch api
Browse files Browse the repository at this point in the history
  • Loading branch information
vaayne committed Apr 28, 2023
1 parent fb564c6 commit 6e3fef0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const Index = () => {
document.activeElement.nodeName.toUpperCase() === "TEXTAREA" ||
document.activeElement.nodeName.toUpperCase() === "INPUT")
) {
console.log("select text from input")
// console.log("select text from input")
// Set as original for later
setSelectedElement(document.activeElement as HTMLElement)
}
Expand Down
12 changes: 8 additions & 4 deletions src/lib/api/chatgpt-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ofetch } from "ofetch"

import type { PlasmoMessaging } from "@plasmohq/messaging"

import { parseSSEResponse } from "~lib/utils/sse"
Expand All @@ -21,14 +19,21 @@ async function chat(
{ role: "user", content: prompt }
]
}
const resp = await ofetch(url, {
const resp = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${api_key}`
},
body: JSON.stringify(data)
})

if (!resp.ok) {
const errMsg = `ChatGPTAPI return error, status: ${resp.status}`
console.error(errMsg)
throw new Error(errMsg)
}

let content: string = ""

await parseSSEResponse(resp, (message) => {
Expand Down Expand Up @@ -60,7 +65,6 @@ async function ChatGPTApiChat(
api_key: string,
res: PlasmoMessaging.Response<any>
) {
console.log(`ChatStream: ${url}, ${instraction}, ${prompt}, ${api_key}`)
if (!api_key) {
res.send("Please set your OpenAI API key in the extension options page.")
return
Expand Down
17 changes: 11 additions & 6 deletions src/lib/api/chatgpt-web.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ofetch } from "ofetch"
import { v4 as uuidv4 } from "uuid"

import type { PlasmoMessaging } from "@plasmohq/messaging"
Expand All @@ -18,7 +17,7 @@ async function getAccessToken(): Promise<string> {
return cacheToken as string
}

const resp = await ofetch(`${CHATGPT_HOST}/api/auth/session`)
const resp = await fetch(`${CHATGPT_HOST}/api/auth/session`)
const data = await resp.json()
if (!data.accessToken) {
throw new Error("401 UNAUTHORIZED")
Expand All @@ -41,7 +40,7 @@ async function ChatGPTWebChat(
console.error(err)
message = err.message
}
console.log(message)
// console.log(message)
}
res.send(message)
}
Expand All @@ -68,7 +67,7 @@ async function chat(prompt: string, res: PlasmoMessaging.Response<any>) {
model: CHATGPT_MODEL
}
const url = `${CHATGPT_HOST}/backend-api/conversation`
const resp = await ofetch(url, {
const resp = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -77,6 +76,12 @@ async function chat(prompt: string, res: PlasmoMessaging.Response<any>) {
body: JSON.stringify(data)
})

if (!resp.ok) {
const errMsg = `ChatGPT return error, status: ${resp.status}`
console.error(errMsg)
throw new Error(errMsg)
}

let conversationId: string = ""

await parseSSEResponse(resp, (message) => {
Expand Down Expand Up @@ -104,7 +109,7 @@ async function chat(prompt: string, res: PlasmoMessaging.Response<any>) {
async function removeConversation(id: string) {
const accessToken = await getAccessToken()
try {
const resp = await fetch(`${CHATGPT_HOST}/backend-api/conversation/${id}`, {
await fetch(`${CHATGPT_HOST}/backend-api/conversation/${id}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Expand All @@ -114,7 +119,7 @@ async function removeConversation(id: string) {
is_visible: false
})
})
console.log(await resp.json())
// console.log(await resp.json())
} catch (err) {
console.error(err)
}
Expand Down
12 changes: 8 additions & 4 deletions src/lib/api/notion-completion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ofetch } from "ofetch"
import { v4 as uuidv4 } from "uuid"

import type { PlasmoMessaging } from "@plasmohq/messaging"
Expand Down Expand Up @@ -62,12 +61,17 @@ async function complation(
accept: "application/x-ndjson"
}

const resp = await ofetch(url, {
const resp = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(data),
retry: 3
body: JSON.stringify(data)
})

if (!resp.ok) {
const errMsg = `NotionAI return error, status: ${resp.status}`
console.error(errMsg)
throw new Error(errMsg)
}
let fullMessage: string = ""
const onMessage = (msg: any) => {
// console.log(`msg: ${JSON.stringify(msg)}`)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/readability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function Parse(url: string): Promise<ParseResult> {
// console.log(`readability response: ${JSON.stringify(data)}`)
return data as ParseResult
} catch (error) {
console.log(`readability error: ${error}`)
console.error(`readability error: ${error}`)
throw new Error(error)
}
}
Expand Down

0 comments on commit 6e3fef0

Please sign in to comment.