refactor(types): remove any usages and strengthen typings across web and base (#26677)
This commit is contained in:
@@ -180,7 +180,7 @@ const handleStream = (
|
||||
let isFirstMessage = true
|
||||
function read() {
|
||||
let hasError = false
|
||||
reader?.read().then((result: any) => {
|
||||
reader?.read().then((result: ReadableStreamReadResult<Uint8Array>) => {
|
||||
if (result.done) {
|
||||
onCompleted?.()
|
||||
return
|
||||
@@ -322,7 +322,21 @@ const handleStream = (
|
||||
|
||||
const baseFetch = base
|
||||
|
||||
export const upload = async (options: any, isPublicAPI?: boolean, url?: string, searchParams?: string): Promise<any> => {
|
||||
type UploadOptions = {
|
||||
xhr: XMLHttpRequest
|
||||
method: string
|
||||
url?: string
|
||||
headers?: Record<string, string>
|
||||
data: FormData
|
||||
onprogress?: (this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => void
|
||||
}
|
||||
|
||||
type UploadResponse = {
|
||||
id: string
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export const upload = async (options: UploadOptions, isPublicAPI?: boolean, url?: string, searchParams?: string): Promise<UploadResponse> => {
|
||||
const urlPrefix = isPublicAPI ? PUBLIC_API_PREFIX : API_PREFIX
|
||||
const token = await getAccessToken(isPublicAPI)
|
||||
const defaultOptions = {
|
||||
@@ -331,18 +345,18 @@ export const upload = async (options: any, isPublicAPI?: boolean, url?: string,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
data: {},
|
||||
}
|
||||
options = {
|
||||
const mergedOptions = {
|
||||
...defaultOptions,
|
||||
...options,
|
||||
headers: { ...defaultOptions.headers, ...options.headers },
|
||||
url: options.url || defaultOptions.url,
|
||||
headers: { ...defaultOptions.headers, ...options.headers } as Record<string, string>,
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = options.xhr
|
||||
xhr.open(options.method, options.url)
|
||||
for (const key in options.headers)
|
||||
xhr.setRequestHeader(key, options.headers[key])
|
||||
const xhr = mergedOptions.xhr
|
||||
xhr.open(mergedOptions.method, mergedOptions.url)
|
||||
for (const key in mergedOptions.headers)
|
||||
xhr.setRequestHeader(key, mergedOptions.headers[key])
|
||||
|
||||
xhr.withCredentials = true
|
||||
xhr.responseType = 'json'
|
||||
@@ -354,8 +368,9 @@ export const upload = async (options: any, isPublicAPI?: boolean, url?: string,
|
||||
reject(xhr)
|
||||
}
|
||||
}
|
||||
xhr.upload.onprogress = options.onprogress
|
||||
xhr.send(options.data)
|
||||
if (mergedOptions.onprogress)
|
||||
xhr.upload.onprogress = mergedOptions.onprogress
|
||||
xhr.send(mergedOptions.data)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -432,7 +447,7 @@ export const ssePost = async (
|
||||
if (!/^[23]\d{2}$/.test(String(res.status))) {
|
||||
if (res.status === 401) {
|
||||
if (isPublicAPI) {
|
||||
res.json().then((data: any) => {
|
||||
res.json().then((data: { code?: string; message?: string }) => {
|
||||
if (isPublicAPI) {
|
||||
if (data.code === 'web_app_access_denied')
|
||||
requiredWebSSOLogin(data.message, 403)
|
||||
|
||||
Reference in New Issue
Block a user