import type { AppendOptions } from 'form-data' ; import type { Config , RequestConfig , RequestFunctionParams } from './types' ; param config export function defineConfig ( config ) { return config ; } export class FileData < T = any > { /**
     * 原始文件数据。
     */ private originalFileData : T ; /**
     * 选项。
     */ private options : AppendOptions | undefined ; param originalFileData param options public constructor ( originalFileData , options ? ) { this . originalFileData = originalFileData ; this . options = options ; } returns public getOriginalFileData ( ) { return this . originalFileData ; } /**
     * 获取选项。
     */ public getOptions ( ) { return this . options ; } } param requestData returns export function parseRequestData ( requestData ? ) { const result = { data : { } as any , fileData : { } as any , } ; if ( requestData != null ) { if ( typeof requestData === 'object' && ! Array . isArray ( requestData ) ) { Object . keys ( requestData ) . forEach ( key => { if ( requestData [ key ] && requestData [ key ] instanceof FileData ) { result . fileData [ key ] = ( requestData [ key ] as FileData ) . getOriginalFileData ( ) ; } else { result . data [ key ] = requestData [ key ] ; } } ) ; } else { result . data = requestData ; } } return result ; } /**
 * 准备要传给请求函数的参数。
 */ export function prepare ( requestConfig , requestData ) { let requestPath = requestConfig . path ; const { data , fileData } = parseRequestData ( requestData ) ; const dataIsObject = data != null && typeof data === 'object' && ! Array . isArray ( data ) ; if ( dataIsObject ) { if ( Array . isArray ( requestConfig . paramNames ) && requestConfig . paramNames . length > 0 ) { Object . keys ( data ) . forEach ( key => { if ( requestConfig . paramNames . indexOf ( key ) >= 0 ) { requestPath = requestPath . replace ( new RegExp ( `\\{${ key }\\}` , 'g' ) , data [ key ] ) . replace ( new RegExp ( `/:${ key }(?=/|$)` , 'g' ) , `/${ data [ key ] }` ) ; delete data [ key ] ; } } ) ; } let queryString = '' ; if ( Array . isArray ( requestConfig . queryNames ) && requestConfig . queryNames . length > 0 ) { Object . keys ( data ) . forEach ( key => { if ( requestConfig . queryNames . indexOf ( key ) >= 0 ) { if ( data [ key ] != null ) { queryString += `${ queryString ? '&' : '' }${ encodeURIComponent ( key ) }=${ encodeURIComponent ( data [ key ] ) }` ; } delete data [ key ] ; } } ) ; } if ( queryString ) { requestPath += `${ requestPath . indexOf ( '?' ) > - 1 ? '&' : '?' }${ queryString }` ; } } const allData = { ... ( dataIsObject ? data : { } ) , ... fileData , } ; const getFormData = ( ) => { const useNativeFormData = typeof FormData !== 'undefined' ; const useNodeFormData = ! useNativeFormData && typeof global === 'object' && typeof global [ 'process' ] === 'object' && typeof global [ 'process' ] [ 'versions' ] === 'object' && global [ 'process' ] [ 'versions' ] [ 'node' ] != null ; const UniFormData = useNativeFormData ? FormData : useNodeFormData ? eval ( `require('form-data')` ) : undefined ; if ( ! UniFormData ) { throw new Error ( '当前环境不支持 FormData' ) ; } const formData = new UniFormData ( ) ; Object . keys ( data ) . forEach ( key => { formData . append ( key , data [ key ] ) ; } ) ; Object . keys ( fileData ) . forEach ( key => { const options = ( requestData [ key ] as FileData ) . getOptions ( ) ; formData . append ( key , fileData [ key ] , useNativeFormData ? options ?. filename : ( options as any ) ) ; } ) ; return formData as any ; } ; return { ... requestConfig , path : requestPath , rawData : requestData , data : data , hasFileData : fileData && Object . keys ( fileData ) . length > 0 , fileData : fileData , allData : allData , getFormData : getFormData , } ; }