base.ts 21 KB

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