Skip to content

Commit

Permalink
Refactor: move api calls to module
Browse files Browse the repository at this point in the history
  • Loading branch information
wong2 committed Feb 4, 2023
1 parent eb6702c commit a598db6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 35 deletions.
32 changes: 32 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getExtensionVersion } from './utils'

const API_HOST = 'https://chatgpt4google.com'
// const API_HOST = 'http://localhost:3000'

export interface PromotionResponse {
url: string
title?: string
text?: string
image?: { url: string; size?: number }
footer?: { text: string; url: string }
label?: { text: string; url: string }
}

export async function fetchPromotion(): Promise<PromotionResponse | null> {
return fetch(`${API_HOST}/api/p`, {
headers: {
'x-version': getExtensionVersion(),
},
}).then((r) => r.json())
}

export async function fetchExtensionConfigs(): Promise<{
chatgpt_webapp_model_name: string
openai_model_names: string[]
}> {
return fetch(`${API_HOST}/api/config`, {
headers: {
'x-version': getExtensionVersion(),
},
}).then((r) => r.json())
}
6 changes: 3 additions & 3 deletions src/background/providers/chatgpt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ExpiryMap from 'expiry-map'
import { v4 as uuidv4 } from 'uuid'
import { API_HOST } from '../../utils'
import { fetchExtensionConfigs } from '../../api'
import { fetchSSE } from '../fetch-sse'
import { GenerateAnswerParams, Provider } from '../types'

Expand Down Expand Up @@ -49,8 +49,8 @@ export async function getChatGPTAccessToken(): Promise<string> {

async function fetchModelName() {
try {
const config = await fetch(`${API_HOST}/api/config`).then((r) => r.json())
return config.chatgpt_webapp_model_name
const configs = await fetchExtensionConfigs()
return configs.chatgpt_webapp_model_name
} catch (err) {
console.error(err)
return null
Expand Down
16 changes: 9 additions & 7 deletions src/content-script/ChatGPTContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TriggerMode } from '../config'
import { useState } from 'react'
import useSWRImmutable from 'swr/immutable'
import { fetchPromotion } from '../api'
import { TriggerMode } from '../config'
import ChatGPTCard from './ChatGPTCard'
import Promotion from './Promotion'
import { getPromotion } from './api'
import { useState } from 'react'
import { QueryStatus } from './ChatGPTQuery'
import Promotion from './Promotion'

interface Props {
question: string
Expand All @@ -13,9 +13,11 @@ interface Props {

function ChatGPTContainer(props: Props) {
const [queryStatus, setQueryStatus] = useState<QueryStatus>()
const query = useSWRImmutable(queryStatus === 'success' ? 'promotion' : undefined, getPromotion, {
shouldRetryOnError: false,
})
const query = useSWRImmutable(
queryStatus === 'success' ? 'promotion' : undefined,
fetchPromotion,
{ shouldRetryOnError: false },
)
return (
<>
<div className="chat-gpt-card">
Expand Down
2 changes: 1 addition & 1 deletion src/content-script/Promotion.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'
import { captureEvent } from '../analytics'
import { PromotionResponse } from './api'
import type { PromotionResponse } from '../api'

interface Props {
data: PromotionResponse
Expand Down
18 changes: 0 additions & 18 deletions src/content-script/api.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/options/ProviderSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Button, Input, Select, Spinner, Tabs, useInput, useToasts } from '@geist-ui/core'
import { FC, useCallback, useState } from 'react'
import useSWR from 'swr'
import { fetchExtensionConfigs } from '../api'
import { getProviderConfigs, ProviderConfigs, ProviderType, saveProviderConfigs } from '../config'
import { API_HOST } from '../utils'

interface ConfigProps {
config: ProviderConfigs
models: string[]
}

async function loadModels(): Promise<string[]> {
const config = await fetch(`${API_HOST}/api/config`).then((r) => r.json())
return config.openai_model_names
const configs = await fetchExtensionConfigs()
return configs.openai_model_names
}

const ConfigPanel: FC<ConfigProps> = ({ config, models }) => {
Expand Down
3 changes: 0 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ export function detectSystemColorScheme() {
export function getExtensionVersion() {
return Browser.runtime.getManifest().version
}

export const API_HOST = 'https://chatgpt4google.com'
// const API_HOST = 'http://localhost:3000'

0 comments on commit a598db6

Please sign in to comment.