base.ts 21 KB

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