You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.ts 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. import { API_PREFIX, IS_CE_EDITION, PUBLIC_API_PREFIX } from '@/config'
  2. import { refreshAccessTokenOrRelogin } from './refresh-token'
  3. import Toast from '@/app/components/base/toast'
  4. import { basePath } from '@/utils/var'
  5. import type { AnnotationReply, MessageEnd, MessageReplace, ThoughtItem } from '@/app/components/base/chat/chat/type'
  6. import type { VisionFile } from '@/types/app'
  7. import type {
  8. AgentLogResponse,
  9. IterationFinishedResponse,
  10. IterationNextResponse,
  11. IterationStartedResponse,
  12. LoopFinishedResponse,
  13. LoopNextResponse,
  14. LoopStartedResponse,
  15. NodeFinishedResponse,
  16. NodeStartedResponse,
  17. ParallelBranchFinishedResponse,
  18. ParallelBranchStartedResponse,
  19. TextChunkResponse,
  20. TextReplaceResponse,
  21. WorkflowFinishedResponse,
  22. WorkflowStartedResponse,
  23. } from '@/types/workflow'
  24. import { removeAccessToken } from '@/app/components/share/utils'
  25. import type { FetchOptionType, ResponseError } from './fetch'
  26. import { ContentType, base, baseOptions, getAccessToken } from './fetch'
  27. import { asyncRunSafe } from '@/utils'
  28. const TIME_OUT = 100000
  29. export type IOnDataMoreInfo = {
  30. conversationId?: string
  31. taskId?: string
  32. messageId: string
  33. errorMessage?: string
  34. errorCode?: string
  35. }
  36. export type IOnData = (message: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => void
  37. export type IOnThought = (though: ThoughtItem) => void
  38. export type IOnFile = (file: VisionFile) => void
  39. export type IOnMessageEnd = (messageEnd: MessageEnd) => void
  40. export type IOnMessageReplace = (messageReplace: MessageReplace) => void
  41. export type IOnAnnotationReply = (messageReplace: AnnotationReply) => void
  42. export type IOnCompleted = (hasError?: boolean, errorMessage?: string) => void
  43. export type IOnError = (msg: string, code?: string) => void
  44. export type IOnWorkflowStarted = (workflowStarted: WorkflowStartedResponse) => void
  45. export type IOnWorkflowFinished = (workflowFinished: WorkflowFinishedResponse) => void
  46. export type IOnNodeStarted = (nodeStarted: NodeStartedResponse) => void
  47. export type IOnNodeFinished = (nodeFinished: NodeFinishedResponse) => void
  48. export type IOnIterationStarted = (workflowStarted: IterationStartedResponse) => void
  49. export type IOnIterationNext = (workflowStarted: IterationNextResponse) => void
  50. export type IOnNodeRetry = (nodeFinished: NodeFinishedResponse) => void
  51. export type IOnIterationFinished = (workflowFinished: IterationFinishedResponse) => void
  52. export type IOnParallelBranchStarted = (parallelBranchStarted: ParallelBranchStartedResponse) => void
  53. export type IOnParallelBranchFinished = (parallelBranchFinished: ParallelBranchFinishedResponse) => void
  54. export type IOnTextChunk = (textChunk: TextChunkResponse) => void
  55. export type IOnTTSChunk = (messageId: string, audioStr: string, audioType?: string) => void
  56. export type IOnTTSEnd = (messageId: string, audioStr: string, audioType?: string) => void
  57. export type IOnTextReplace = (textReplace: TextReplaceResponse) => void
  58. export type IOnLoopStarted = (workflowStarted: LoopStartedResponse) => void
  59. export type IOnLoopNext = (workflowStarted: LoopNextResponse) => void
  60. export type IOnLoopFinished = (workflowFinished: LoopFinishedResponse) => void
  61. export type IOnAgentLog = (agentLog: AgentLogResponse) => void
  62. export type IOtherOptions = {
  63. isPublicAPI?: boolean
  64. isMarketplaceAPI?: boolean
  65. bodyStringify?: boolean
  66. needAllResponseContent?: boolean
  67. deleteContentType?: boolean
  68. silent?: boolean
  69. onData?: IOnData // for stream
  70. onThought?: IOnThought
  71. onFile?: IOnFile
  72. onMessageEnd?: IOnMessageEnd
  73. onMessageReplace?: IOnMessageReplace
  74. onError?: IOnError
  75. onCompleted?: IOnCompleted // for stream
  76. getAbortController?: (abortController: AbortController) => void
  77. onWorkflowStarted?: IOnWorkflowStarted
  78. onWorkflowFinished?: IOnWorkflowFinished
  79. onNodeStarted?: IOnNodeStarted
  80. onNodeFinished?: IOnNodeFinished
  81. onIterationStart?: IOnIterationStarted
  82. onIterationNext?: IOnIterationNext
  83. onIterationFinish?: IOnIterationFinished
  84. onNodeRetry?: IOnNodeRetry
  85. onParallelBranchStarted?: IOnParallelBranchStarted
  86. onParallelBranchFinished?: IOnParallelBranchFinished
  87. onTextChunk?: IOnTextChunk
  88. onTTSChunk?: IOnTTSChunk
  89. onTTSEnd?: IOnTTSEnd
  90. onTextReplace?: IOnTextReplace
  91. onLoopStart?: IOnLoopStarted
  92. onLoopNext?: IOnLoopNext
  93. onLoopFinish?: IOnLoopFinished
  94. onAgentLog?: IOnAgentLog
  95. }
  96. function unicodeToChar(text: string) {
  97. if (!text)
  98. return ''
  99. return text.replace(/\\u[0-9a-f]{4}/g, (_match, p1) => {
  100. return String.fromCharCode(Number.parseInt(p1, 16))
  101. })
  102. }
  103. function requiredWebSSOLogin(message?: string, code?: number) {
  104. const params = new URLSearchParams()
  105. params.append('redirect_url', encodeURIComponent(`${globalThis.location.pathname}${globalThis.location.search}`))
  106. if (message)
  107. params.append('message', message)
  108. if (code)
  109. params.append('code', String(code))
  110. globalThis.location.href = `/webapp-signin?${params.toString()}`
  111. }
  112. export function format(text: string) {
  113. let res = text.trim()
  114. if (res.startsWith('\n'))
  115. res = res.replace('\n', '')
  116. return res.replaceAll('\n', '<br/>').replaceAll('```', '')
  117. }
  118. const handleStream = (
  119. response: Response,
  120. onData: IOnData,
  121. onCompleted?: IOnCompleted,
  122. onThought?: IOnThought,
  123. onMessageEnd?: IOnMessageEnd,
  124. onMessageReplace?: IOnMessageReplace,
  125. onFile?: IOnFile,
  126. onWorkflowStarted?: IOnWorkflowStarted,
  127. onWorkflowFinished?: IOnWorkflowFinished,
  128. onNodeStarted?: IOnNodeStarted,
  129. onNodeFinished?: IOnNodeFinished,
  130. onIterationStart?: IOnIterationStarted,
  131. onIterationNext?: IOnIterationNext,
  132. onIterationFinish?: IOnIterationFinished,
  133. onLoopStart?: IOnLoopStarted,
  134. onLoopNext?: IOnLoopNext,
  135. onLoopFinish?: IOnLoopFinished,
  136. onNodeRetry?: IOnNodeRetry,
  137. onParallelBranchStarted?: IOnParallelBranchStarted,
  138. onParallelBranchFinished?: IOnParallelBranchFinished,
  139. onTextChunk?: IOnTextChunk,
  140. onTTSChunk?: IOnTTSChunk,
  141. onTTSEnd?: IOnTTSEnd,
  142. onTextReplace?: IOnTextReplace,
  143. onAgentLog?: IOnAgentLog,
  144. ) => {
  145. if (!response.ok)
  146. throw new Error('Network response was not ok')
  147. const reader = response.body?.getReader()
  148. const decoder = new TextDecoder('utf-8')
  149. let buffer = ''
  150. let bufferObj: Record<string, any>
  151. let isFirstMessage = true
  152. function read() {
  153. let hasError = false
  154. reader?.read().then((result: any) => {
  155. if (result.done) {
  156. onCompleted && onCompleted()
  157. return
  158. }
  159. buffer += decoder.decode(result.value, { stream: true })
  160. const lines = buffer.split('\n')
  161. try {
  162. lines.forEach((message) => {
  163. if (message.startsWith('data: ')) { // check if it starts with data:
  164. try {
  165. bufferObj = JSON.parse(message.substring(6)) as Record<string, any>// remove data: and parse as json
  166. }
  167. catch {
  168. // mute handle message cut off
  169. onData('', isFirstMessage, {
  170. conversationId: bufferObj?.conversation_id,
  171. messageId: bufferObj?.message_id,
  172. })
  173. return
  174. }
  175. if (bufferObj.status === 400 || !bufferObj.event) {
  176. onData('', false, {
  177. conversationId: undefined,
  178. messageId: '',
  179. errorMessage: bufferObj?.message,
  180. errorCode: bufferObj?.code,
  181. })
  182. hasError = true
  183. onCompleted?.(true, bufferObj?.message)
  184. return
  185. }
  186. if (bufferObj.event === 'message' || bufferObj.event === 'agent_message') {
  187. // can not use format here. Because message is splitted.
  188. onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
  189. conversationId: bufferObj.conversation_id,
  190. taskId: bufferObj.task_id,
  191. messageId: bufferObj.id,
  192. })
  193. isFirstMessage = false
  194. }
  195. else if (bufferObj.event === 'agent_thought') {
  196. onThought?.(bufferObj as ThoughtItem)
  197. }
  198. else if (bufferObj.event === 'message_file') {
  199. onFile?.(bufferObj as VisionFile)
  200. }
  201. else if (bufferObj.event === 'message_end') {
  202. onMessageEnd?.(bufferObj as MessageEnd)
  203. }
  204. else if (bufferObj.event === 'message_replace') {
  205. onMessageReplace?.(bufferObj as MessageReplace)
  206. }
  207. else if (bufferObj.event === 'workflow_started') {
  208. onWorkflowStarted?.(bufferObj as WorkflowStartedResponse)
  209. }
  210. else if (bufferObj.event === 'workflow_finished') {
  211. onWorkflowFinished?.(bufferObj as WorkflowFinishedResponse)
  212. }
  213. else if (bufferObj.event === 'node_started') {
  214. onNodeStarted?.(bufferObj as NodeStartedResponse)
  215. }
  216. else if (bufferObj.event === 'node_finished') {
  217. onNodeFinished?.(bufferObj as NodeFinishedResponse)
  218. }
  219. else if (bufferObj.event === 'iteration_started') {
  220. onIterationStart?.(bufferObj as IterationStartedResponse)
  221. }
  222. else if (bufferObj.event === 'iteration_next') {
  223. onIterationNext?.(bufferObj as IterationNextResponse)
  224. }
  225. else if (bufferObj.event === 'iteration_completed') {
  226. onIterationFinish?.(bufferObj as IterationFinishedResponse)
  227. }
  228. else if (bufferObj.event === 'loop_started') {
  229. onLoopStart?.(bufferObj as LoopStartedResponse)
  230. }
  231. else if (bufferObj.event === 'loop_next') {
  232. onLoopNext?.(bufferObj as LoopNextResponse)
  233. }
  234. else if (bufferObj.event === 'loop_completed') {
  235. onLoopFinish?.(bufferObj as LoopFinishedResponse)
  236. }
  237. else if (bufferObj.event === 'node_retry') {
  238. onNodeRetry?.(bufferObj as NodeFinishedResponse)
  239. }
  240. else if (bufferObj.event === 'parallel_branch_started') {
  241. onParallelBranchStarted?.(bufferObj as ParallelBranchStartedResponse)
  242. }
  243. else if (bufferObj.event === 'parallel_branch_finished') {
  244. onParallelBranchFinished?.(bufferObj as ParallelBranchFinishedResponse)
  245. }
  246. else if (bufferObj.event === 'text_chunk') {
  247. onTextChunk?.(bufferObj as TextChunkResponse)
  248. }
  249. else if (bufferObj.event === 'text_replace') {
  250. onTextReplace?.(bufferObj as TextReplaceResponse)
  251. }
  252. else if (bufferObj.event === 'agent_log') {
  253. onAgentLog?.(bufferObj as AgentLogResponse)
  254. }
  255. else if (bufferObj.event === 'tts_message') {
  256. onTTSChunk?.(bufferObj.message_id, bufferObj.audio, bufferObj.audio_type)
  257. }
  258. else if (bufferObj.event === 'tts_message_end') {
  259. onTTSEnd?.(bufferObj.message_id, bufferObj.audio)
  260. }
  261. }
  262. })
  263. buffer = lines[lines.length - 1]
  264. }
  265. catch (e) {
  266. onData('', false, {
  267. conversationId: undefined,
  268. messageId: '',
  269. errorMessage: `${e}`,
  270. })
  271. hasError = true
  272. onCompleted?.(true, e as string)
  273. return
  274. }
  275. if (!hasError)
  276. read()
  277. })
  278. }
  279. read()
  280. }
  281. const baseFetch = base
  282. export const upload = async (options: any, isPublicAPI?: boolean, url?: string, searchParams?: string): Promise<any> => {
  283. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  284. const token = await getAccessToken(isPublicAPI)
  285. const defaultOptions = {
  286. method: 'POST',
  287. url: (url ? `${urlPrefix}${url}` : `${urlPrefix}/files/upload`) + (searchParams || ''),
  288. headers: {
  289. Authorization: `Bearer ${token}`,
  290. },
  291. data: {},
  292. }
  293. options = {
  294. ...defaultOptions,
  295. ...options,
  296. headers: { ...defaultOptions.headers, ...options.headers },
  297. }
  298. return new Promise((resolve, reject) => {
  299. const xhr = options.xhr
  300. xhr.open(options.method, options.url)
  301. for (const key in options.headers)
  302. xhr.setRequestHeader(key, options.headers[key])
  303. xhr.withCredentials = true
  304. xhr.responseType = 'json'
  305. xhr.onreadystatechange = function () {
  306. if (xhr.readyState === 4) {
  307. if (xhr.status === 201)
  308. resolve(xhr.response)
  309. else
  310. reject(xhr)
  311. }
  312. }
  313. xhr.upload.onprogress = options.onprogress
  314. xhr.send(options.data)
  315. })
  316. }
  317. export const ssePost = async (
  318. url: string,
  319. fetchOptions: FetchOptionType,
  320. otherOptions: IOtherOptions,
  321. ) => {
  322. const {
  323. isPublicAPI = false,
  324. onData,
  325. onCompleted,
  326. onThought,
  327. onFile,
  328. onMessageEnd,
  329. onMessageReplace,
  330. onWorkflowStarted,
  331. onWorkflowFinished,
  332. onNodeStarted,
  333. onNodeFinished,
  334. onIterationStart,
  335. onIterationNext,
  336. onIterationFinish,
  337. onNodeRetry,
  338. onParallelBranchStarted,
  339. onParallelBranchFinished,
  340. onTextChunk,
  341. onTTSChunk,
  342. onTTSEnd,
  343. onTextReplace,
  344. onAgentLog,
  345. onError,
  346. getAbortController,
  347. onLoopStart,
  348. onLoopNext,
  349. onLoopFinish,
  350. } = otherOptions
  351. const abortController = new AbortController()
  352. const token = localStorage.getItem('console_token')
  353. const options = Object.assign({}, baseOptions, {
  354. method: 'POST',
  355. signal: abortController.signal,
  356. headers: new Headers({
  357. Authorization: `Bearer ${token}`,
  358. }),
  359. } as RequestInit, fetchOptions)
  360. const contentType = (options.headers as Headers).get('Content-Type')
  361. if (!contentType)
  362. (options.headers as Headers).set('Content-Type', ContentType.json)
  363. getAbortController?.(abortController)
  364. const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
  365. const urlWithPrefix = (url.startsWith('http://') || url.startsWith('https://'))
  366. ? url
  367. : `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
  368. const { body } = options
  369. if (body)
  370. options.body = JSON.stringify(body)
  371. const accessToken = await getAccessToken(isPublicAPI)
  372. ; (options.headers as Headers).set('Authorization', `Bearer ${accessToken}`)
  373. globalThis.fetch(urlWithPrefix, options as RequestInit)
  374. .then((res) => {
  375. if (!/^[23]\d{2}$/.test(String(res.status))) {
  376. if (res.status === 401) {
  377. refreshAccessTokenOrRelogin(TIME_OUT).then(() => {
  378. ssePost(url, fetchOptions, otherOptions)
  379. }).catch(() => {
  380. res.json().then((data: any) => {
  381. if (isPublicAPI) {
  382. if (data.code === 'web_app_access_denied')
  383. requiredWebSSOLogin(data.message, 403)
  384. if (data.code === 'web_sso_auth_required') {
  385. removeAccessToken()
  386. requiredWebSSOLogin()
  387. }
  388. if (data.code === 'unauthorized') {
  389. removeAccessToken()
  390. requiredWebSSOLogin()
  391. }
  392. }
  393. })
  394. })
  395. }
  396. else {
  397. res.json().then((data) => {
  398. Toast.notify({ type: 'error', message: data.message || 'Server Error' })
  399. })
  400. onError?.('Server Error')
  401. }
  402. return
  403. }
  404. return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => {
  405. if (moreInfo.errorMessage) {
  406. onError?.(moreInfo.errorMessage, moreInfo.errorCode)
  407. // TypeError: Cannot assign to read only property ... will happen in page leave, so it should be ignored.
  408. if (moreInfo.errorMessage !== 'AbortError: The user aborted a request.' && !moreInfo.errorMessage.includes('TypeError: Cannot assign to read only property'))
  409. Toast.notify({ type: 'error', message: moreInfo.errorMessage })
  410. return
  411. }
  412. onData?.(str, isFirstMessage, moreInfo)
  413. },
  414. onCompleted,
  415. onThought,
  416. onMessageEnd,
  417. onMessageReplace,
  418. onFile,
  419. onWorkflowStarted,
  420. onWorkflowFinished,
  421. onNodeStarted,
  422. onNodeFinished,
  423. onIterationStart,
  424. onIterationNext,
  425. onIterationFinish,
  426. onLoopStart,
  427. onLoopNext,
  428. onLoopFinish,
  429. onNodeRetry,
  430. onParallelBranchStarted,
  431. onParallelBranchFinished,
  432. onTextChunk,
  433. onTTSChunk,
  434. onTTSEnd,
  435. onTextReplace,
  436. onAgentLog,
  437. )
  438. }).catch((e) => {
  439. if (e.toString() !== 'AbortError: The user aborted a request.' && !e.toString().errorMessage.includes('TypeError: Cannot assign to read only property'))
  440. Toast.notify({ type: 'error', message: e })
  441. onError?.(e)
  442. })
  443. }
  444. // base request
  445. export const request = async<T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  446. try {
  447. const otherOptionsForBaseFetch = otherOptions || {}
  448. const [err, resp] = await asyncRunSafe<T>(baseFetch(url, options, otherOptionsForBaseFetch))
  449. if (err === null)
  450. return resp
  451. const errResp: Response = err as any
  452. if (errResp.status === 401) {
  453. const [parseErr, errRespData] = await asyncRunSafe<ResponseError>(errResp.json())
  454. const loginUrl = `${globalThis.location.origin}${basePath}/signin`
  455. if (parseErr) {
  456. globalThis.location.href = loginUrl
  457. return Promise.reject(err)
  458. }
  459. // special code
  460. const { code, message } = errRespData
  461. // webapp sso
  462. if (code === 'web_app_access_denied') {
  463. requiredWebSSOLogin(message, 403)
  464. return Promise.reject(err)
  465. }
  466. if (code === 'web_sso_auth_required') {
  467. removeAccessToken()
  468. requiredWebSSOLogin()
  469. return Promise.reject(err)
  470. }
  471. if (code === 'unauthorized_and_force_logout') {
  472. localStorage.removeItem('console_token')
  473. localStorage.removeItem('refresh_token')
  474. globalThis.location.reload()
  475. return Promise.reject(err)
  476. }
  477. const {
  478. isPublicAPI = false,
  479. silent,
  480. } = otherOptionsForBaseFetch
  481. if (isPublicAPI && code === 'unauthorized') {
  482. removeAccessToken()
  483. requiredWebSSOLogin()
  484. return Promise.reject(err)
  485. }
  486. if (code === 'init_validate_failed' && IS_CE_EDITION && !silent) {
  487. Toast.notify({ type: 'error', message, duration: 4000 })
  488. return Promise.reject(err)
  489. }
  490. if (code === 'not_init_validated' && IS_CE_EDITION) {
  491. globalThis.location.href = `${globalThis.location.origin}${basePath}/init`
  492. return Promise.reject(err)
  493. }
  494. if (code === 'not_setup' && IS_CE_EDITION) {
  495. globalThis.location.href = `${globalThis.location.origin}${basePath}/install`
  496. return Promise.reject(err)
  497. }
  498. // refresh token
  499. const [refreshErr] = await asyncRunSafe(refreshAccessTokenOrRelogin(TIME_OUT))
  500. if (refreshErr === null)
  501. return baseFetch<T>(url, options, otherOptionsForBaseFetch)
  502. if (location.pathname !== `${basePath}/signin` || !IS_CE_EDITION) {
  503. globalThis.location.href = loginUrl
  504. return Promise.reject(err)
  505. }
  506. if (!silent) {
  507. Toast.notify({ type: 'error', message })
  508. return Promise.reject(err)
  509. }
  510. globalThis.location.href = loginUrl
  511. return Promise.reject(err)
  512. }
  513. else {
  514. return Promise.reject(err)
  515. }
  516. }
  517. catch (error) {
  518. console.error(error)
  519. return Promise.reject(error)
  520. }
  521. }
  522. // request methods
  523. export const get = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  524. return request<T>(url, Object.assign({}, options, { method: 'GET' }), otherOptions)
  525. }
  526. // For public API
  527. export const getPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  528. return get<T>(url, options, { ...otherOptions, isPublicAPI: true })
  529. }
  530. // For Marketplace API
  531. export const getMarketplace = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  532. return get<T>(url, options, { ...otherOptions, isMarketplaceAPI: true })
  533. }
  534. export const post = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  535. return request<T>(url, Object.assign({}, options, { method: 'POST' }), otherOptions)
  536. }
  537. // For Marketplace API
  538. export const postMarketplace = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  539. return post<T>(url, options, { ...otherOptions, isMarketplaceAPI: true })
  540. }
  541. export const postPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  542. return post<T>(url, options, { ...otherOptions, isPublicAPI: true })
  543. }
  544. export const put = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  545. return request<T>(url, Object.assign({}, options, { method: 'PUT' }), otherOptions)
  546. }
  547. export const putPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  548. return put<T>(url, options, { ...otherOptions, isPublicAPI: true })
  549. }
  550. export const del = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  551. return request<T>(url, Object.assign({}, options, { method: 'DELETE' }), otherOptions)
  552. }
  553. export const delPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  554. return del<T>(url, options, { ...otherOptions, isPublicAPI: true })
  555. }
  556. export const patch = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  557. return request<T>(url, Object.assign({}, options, { method: 'PATCH' }), otherOptions)
  558. }
  559. export const patchPublic = <T>(url: string, options = {}, otherOptions?: IOtherOptions) => {
  560. return patch<T>(url, options, { ...otherOptions, isPublicAPI: true })
  561. }