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

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