headers-api-everywhere: Add headers to all api calls
This commit is contained in:
		
							parent
							
								
									95a4d9f357
								
							
						
					
					
						commit
						ca2c127cbd
					
				| @ -9,6 +9,7 @@ import { getPageShareUrl } from "~/services/ollama" | |||||||
| import { cleanUrl } from "~/libs/clean-url" | import { cleanUrl } from "~/libs/clean-url" | ||||||
| import { getUserId, saveWebshare } from "@/db" | import { getUserId, saveWebshare } from "@/db" | ||||||
| import { useTranslation } from "react-i18next" | import { useTranslation } from "react-i18next" | ||||||
|  | import fetcher from "@/libs/fetcher" | ||||||
| 
 | 
 | ||||||
| type Props = { | type Props = { | ||||||
|   messages: Message[] |   messages: Message[] | ||||||
| @ -94,7 +95,7 @@ export const ShareBtn: React.FC<Props> = ({ messages }) => { | |||||||
|     const chat = reformatMessages(messages, values.name) |     const chat = reformatMessages(messages, values.name) | ||||||
|     const title = values.title |     const title = values.title | ||||||
|     const url = await getPageShareUrl() |     const url = await getPageShareUrl() | ||||||
|     const res = await fetch(`${cleanUrl(url)}/api/v1/share/create`, { |     const res = await fetcher(`${cleanUrl(url)}/api/v1/share/create`, { | ||||||
|       method: "POST", |       method: "POST", | ||||||
|       headers: { |       headers: { | ||||||
|         "Content-Type": "application/json" |         "Content-Type": "application/json" | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ import { useQuery } from "@tanstack/react-query" | |||||||
| import { Skeleton } from "antd" | import { Skeleton } from "antd" | ||||||
| import { cleanUrl } from "@/libs/clean-url" | import { cleanUrl } from "@/libs/clean-url" | ||||||
| import { Descriptions } from "antd" | import { Descriptions } from "antd" | ||||||
|  | import fetcher from "@/libs/fetcher" | ||||||
| 
 | 
 | ||||||
| export const AboutApp = () => { | export const AboutApp = () => { | ||||||
|   const { t } = useTranslation("settings") |   const { t } = useTranslation("settings") | ||||||
| @ -14,7 +15,7 @@ export const AboutApp = () => { | |||||||
|       const chromeVersion = browser.runtime.getManifest().version |       const chromeVersion = browser.runtime.getManifest().version | ||||||
|       try { |       try { | ||||||
|         const url = await getOllamaURL() |         const url = await getOllamaURL() | ||||||
|         const req = await fetch(`${cleanUrl(url)}/api/version`) |         const req = await fetcher(`${cleanUrl(url)}/api/version`) | ||||||
| 
 | 
 | ||||||
|         if (!req.ok) { |         if (!req.ok) { | ||||||
|           return { |           return { | ||||||
|  | |||||||
| @ -7,6 +7,7 @@ import { deleteWebshare, getAllWebshares, getUserId } from "@/db" | |||||||
| import { getPageShareUrl, setPageShareUrl } from "~/services/ollama" | import { getPageShareUrl, setPageShareUrl } from "~/services/ollama" | ||||||
| import { verifyPageShareURL } from "~/utils/verify-page-share" | import { verifyPageShareURL } from "~/utils/verify-page-share" | ||||||
| import { useStorage } from "@plasmohq/storage/hook" | import { useStorage } from "@plasmohq/storage/hook" | ||||||
|  | import fetcher from "@/libs/fetcher" | ||||||
| 
 | 
 | ||||||
| export const OptionShareBody = () => { | export const OptionShareBody = () => { | ||||||
|   const queryClient = useQueryClient() |   const queryClient = useQueryClient() | ||||||
| @ -48,7 +49,7 @@ export const OptionShareBody = () => { | |||||||
|     api_url: string |     api_url: string | ||||||
|   }) => { |   }) => { | ||||||
|     const owner_id = await getUserId() |     const owner_id = await getUserId() | ||||||
|     const res = await fetch(`${api_url}/api/v1/share/delete`, { |     const res = await fetcher(`${api_url}/api/v1/share/delete`, { | ||||||
|       method: "POST", |       method: "POST", | ||||||
|       headers: { |       headers: { | ||||||
|         "Content-Type": "application/json" |         "Content-Type": "application/json" | ||||||
|  | |||||||
| @ -3,6 +3,8 @@ import { cleanUrl } from "../libs/clean-url" | |||||||
| import { urlRewriteRuntime } from "../libs/runtime" | import { urlRewriteRuntime } from "../libs/runtime" | ||||||
| import { getChromeAIModel } from "./chrome" | import { getChromeAIModel } from "./chrome" | ||||||
| import { setNoOfRetrievedDocs, setTotalFilePerKB } from "./app" | import { setNoOfRetrievedDocs, setTotalFilePerKB } from "./app" | ||||||
|  | import fetcher from "@/libs/fetcher" | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| const storage = new Storage() | const storage = new Storage() | ||||||
| 
 | 
 | ||||||
| @ -82,7 +84,7 @@ export const defaultModel = async () => { | |||||||
| export const isOllamaRunning = async () => { | export const isOllamaRunning = async () => { | ||||||
|   try { |   try { | ||||||
|     const baseUrl = await getOllamaURL() |     const baseUrl = await getOllamaURL() | ||||||
|     const response = await fetch(`${cleanUrl(baseUrl)}`) |     const response = await fetcher(`${cleanUrl(baseUrl)}`) | ||||||
|     if (!response.ok) { |     if (!response.ok) { | ||||||
|       throw new Error(response.statusText) |       throw new Error(response.statusText) | ||||||
|     } |     } | ||||||
| @ -100,7 +102,7 @@ export const getAllModels = async ({ | |||||||
| }) => { | }) => { | ||||||
|   try { |   try { | ||||||
|     const baseUrl = await getOllamaURL() |     const baseUrl = await getOllamaURL() | ||||||
|     const response = await fetch(`${cleanUrl(baseUrl)}/api/tags`) |     const response = await fetcher(`${cleanUrl(baseUrl)}/api/tags`) | ||||||
|     if (!response.ok) { |     if (!response.ok) { | ||||||
|       if (returnEmpty) { |       if (returnEmpty) { | ||||||
|         return [] |         return [] | ||||||
| @ -132,7 +134,7 @@ export const getAllModels = async ({ | |||||||
| 
 | 
 | ||||||
| export const deleteModel = async (model: string) => { | export const deleteModel = async (model: string) => { | ||||||
|   const baseUrl = await getOllamaURL() |   const baseUrl = await getOllamaURL() | ||||||
|   const response = await fetch(`${cleanUrl(baseUrl)}/api/delete`, { |   const response = await fetcher(`${cleanUrl(baseUrl)}/api/delete`, { | ||||||
|     method: "DELETE", |     method: "DELETE", | ||||||
|     headers: { |     headers: { | ||||||
|       "Content-Type": "application/json" |       "Content-Type": "application/json" | ||||||
| @ -154,7 +156,7 @@ export const fetchChatModels = async ({ | |||||||
| }) => { | }) => { | ||||||
|   try { |   try { | ||||||
|     const baseUrl = await getOllamaURL() |     const baseUrl = await getOllamaURL() | ||||||
|     const response = await fetch(`${cleanUrl(baseUrl)}/api/tags`) |     const response = await fetcher(`${cleanUrl(baseUrl)}/api/tags`) | ||||||
|     if (!response.ok) { |     if (!response.ok) { | ||||||
|       if (returnEmpty) { |       if (returnEmpty) { | ||||||
|         return [] |         return [] | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| import { setBadgeBackgroundColor, setBadgeText, setTitle } from "@/utils/action" | import { setBadgeBackgroundColor, setBadgeText, setTitle } from "@/utils/action" | ||||||
| 
 | import fetcher from "@/libs/fetcher" | ||||||
| 
 | 
 | ||||||
| export const progressHuman = (completed: number, total: number) => { | export const progressHuman = (completed: number, total: number) => { | ||||||
|     return ((completed / total) * 100).toFixed(0) + "%" |     return ((completed / total) * 100).toFixed(0) + "%" | ||||||
| @ -11,7 +11,7 @@ export const clearBadge = () => { | |||||||
| } | } | ||||||
| export const streamDownload = async (url: string, model: string) => { | export const streamDownload = async (url: string, model: string) => { | ||||||
|     url += "/api/pull" |     url += "/api/pull" | ||||||
|     const response = await fetch(url, { |     const response = await fetcher(url, { | ||||||
|         method: "POST", |         method: "POST", | ||||||
|         headers: { |         headers: { | ||||||
|             "Content-Type": "application/json" |             "Content-Type": "application/json" | ||||||
|  | |||||||
| @ -1,7 +1,8 @@ | |||||||
| import { cleanUrl } from "~/libs/clean-url" | import { cleanUrl } from "~/libs/clean-url" | ||||||
|  | import fetcher from "@/libs/fetcher" | ||||||
| 
 | 
 | ||||||
| export const verifyPageShareURL = async (url: string) => { | export const verifyPageShareURL = async (url: string) => { | ||||||
|     const res = await fetch(`${cleanUrl(url)}/api/v1/ping`) |     const res = await fetcher(`${cleanUrl(url)}/api/v1/ping`) | ||||||
|     if (!res.ok) { |     if (!res.ok) { | ||||||
|         throw new Error("Unable to verify page share") |         throw new Error("Unable to verify page share") | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user