mirror of
https://github.com/YFGaia/dify-plus.git
synced 2026-06-04 10:14:00 +08:00
7947b7976b
Merge upstream release cd03e0a (hotfix/1.12.1-fix.0) into main
# Conflicts:
# api/.env.example
# api/controllers/service_api/app/annotation.py
# api/controllers/service_api/app/completion.py
# api/controllers/service_api/app/conversation.py
# api/controllers/service_api/app/message.py
# api/core/file/file_manager.py
# api/core/rag/datasource/retrieval_service.py
# api/extensions/ext_celery.py
# api/libs/gmpy2_pkcs10aep_cipher.py
# api/uv.lock
# web/pnpm-lock.yaml
# web/service/client.ts
102 lines
2.8 KiB
TypeScript
102 lines
2.8 KiB
TypeScript
import type { ContractRouterClient } from '@orpc/contract'
|
|
import type { JsonifiedClient } from '@orpc/openapi-client'
|
|
import { createORPCClient, onError } from '@orpc/client'
|
|
import { OpenAPILink } from '@orpc/openapi-client/fetch'
|
|
import { createTanstackQueryUtils } from '@orpc/tanstack-query'
|
|
import {
|
|
API_PREFIX,
|
|
APP_VERSION,
|
|
IS_MARKETPLACE,
|
|
MARKETPLACE_API_PREFIX,
|
|
} from '@/config'
|
|
import {
|
|
consoleRouterContract,
|
|
marketplaceRouterContract,
|
|
} from '@/contract/router'
|
|
import { isClient } from '@/utils/client'
|
|
import { request } from './base'
|
|
|
|
// extend: CVE-2025-63387 跨域时 Cookie 可能为 None,用 Header 携带 JWT
|
|
let loginConfigToken: string | null = null
|
|
export function setLoginConfigToken(token: string | null) {
|
|
loginConfigToken = token
|
|
}
|
|
|
|
const getMarketplaceHeaders = () => new Headers({
|
|
'X-Dify-Version': !IS_MARKETPLACE ? APP_VERSION : '999.0.0',
|
|
})
|
|
|
|
function isURL(path: string) {
|
|
try {
|
|
// eslint-disable-next-line no-new
|
|
new URL(path)
|
|
return true
|
|
}
|
|
catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
export function getBaseURL(path: string) {
|
|
const url = new URL(path, isURL(path) ? undefined : isClient ? window.location.origin : 'http://localhost')
|
|
|
|
if (!isClient && !isURL(path)) {
|
|
console.warn('Using localhost as base URL in server environment, please configure accordingly.')
|
|
}
|
|
|
|
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
console.warn(`Unexpected protocol for API requests, expected http or https. Current protocol: ${url.protocol}. Please configure accordingly.`)
|
|
}
|
|
|
|
return url
|
|
}
|
|
|
|
const getConsoleHeaders = () => {
|
|
const h = new Headers()
|
|
if (loginConfigToken)
|
|
h.set('X-Login-Config-Token', loginConfigToken)
|
|
return h
|
|
}
|
|
|
|
const marketplaceLink = new OpenAPILink(marketplaceRouterContract, {
|
|
url: MARKETPLACE_API_PREFIX,
|
|
headers: () => (getMarketplaceHeaders()),
|
|
fetch: (request, init) => {
|
|
return globalThis.fetch(request, {
|
|
...init,
|
|
cache: 'no-store',
|
|
})
|
|
},
|
|
interceptors: [
|
|
onError((error) => {
|
|
console.error(error)
|
|
}),
|
|
],
|
|
})
|
|
|
|
export const marketplaceClient: JsonifiedClient<ContractRouterClient<typeof marketplaceRouterContract>> = createORPCClient(marketplaceLink)
|
|
export const marketplaceQuery = createTanstackQueryUtils(marketplaceClient, { path: ['marketplace'] })
|
|
|
|
const consoleLink = new OpenAPILink(consoleRouterContract, {
|
|
url: getBaseURL(API_PREFIX),
|
|
headers: () => getConsoleHeaders(),
|
|
fetch: (input, init) => {
|
|
return request(
|
|
input.url,
|
|
init,
|
|
{
|
|
fetchCompat: true,
|
|
request: input,
|
|
},
|
|
)
|
|
},
|
|
interceptors: [
|
|
onError((error) => {
|
|
console.error(error)
|
|
}),
|
|
],
|
|
})
|
|
|
|
export const consoleClient: JsonifiedClient<ContractRouterClient<typeof consoleRouterContract>> = createORPCClient(consoleLink)
|
|
export const consoleQuery = createTanstackQueryUtils(consoleClient, { path: ['console'] })
|