Skip to content

Commit

Permalink
feat: add FileData
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 30, 2018
1 parent c955ba7 commit 20b7cc3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
21 changes: 21 additions & 0 deletions src/FileData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default class FileData<T = any> {
private content: T

/**
* 创建一个文件数据。
*
* @param content 文件内容,依平台而不同,如 web 上为 File 实例,微信小程序里是文件路径字符串
*/
public constructor(content: T) {
this.content = content
}

/**
* 获取文件内容。
*
* @returns 文件内容
*/
public get(): T {
return this.content
}
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// @index(.ts$)
export { default as Disposer } from './Disposer'
export { default as EventBus } from './EventBus'
export { default as Validator } from './Validator'
export { default as base64Decode } from './base64Decode'
export { default as base64Encode } from './base64Encode'
export { default as base64UrlDecode } from './base64UrlDecode'
export { default as base64UrlEncode } from './base64UrlEncode'
export { default as bindEvent } from './bindEvent'
export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as Disposer } from './Disposer'
export { default as endsWith } from './endsWith'
export { default as EventBus } from './EventBus'
export { default as FileData } from './FileData'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as formatCurrency } from './formatCurrency'
export { default as formatDate } from './formatDate'
export { default as forOwn } from './forOwn'
export { default as get } from './get'
export { default as getType } from './getType'
export { default as has } from './has'
Expand Down Expand Up @@ -72,4 +72,5 @@ export { default as toDate } from './toDate'
export { default as toPath } from './toPath'
export { default as transformElement } from './transformElement'
export { default as upperCaseFirst } from './upperCaseFirst'
export { default as Validator } from './Validator'
export { default as values } from './values'
19 changes: 4 additions & 15 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import inBrowser from './inBrowser'
import inWechatMiniProgram from './inWechatMiniProgram'
import { objectToQueryString } from './jsonp'
import omit from './omit'
import FileData from './FileData'

export interface RequestOptions {
url: string,
Expand All @@ -28,18 +29,6 @@ const defaultRequestOptions: Partial<RequestOptions> = {
responseDataType: 'json',
}

export class RequestFile {
private file: string | File

public constructor(file: string | File) {
this.file = file
}

public output(): string | File {
return this.file
}
}

export default function request<T extends RequestOptions>(options: T): Promise<{
data: (
T['responseDataType'] extends 'json'
Expand All @@ -58,9 +47,9 @@ export default function request<T extends RequestOptions>(options: T): Promise<{
}

// 解析文件参数
let file: { key: string, value: RequestFile }
let file: { key: string, value: FileData }
forOwn(options.data, (value, key) => {
if (value instanceof RequestFile) {
if (value instanceof FileData) {
file = { key, value }
return false
}
Expand All @@ -79,7 +68,7 @@ export default function request<T extends RequestOptions>(options: T): Promise<{
if (file) {
wx.uploadFile({
url: options.url,
filePath: file.value.output() as string,
filePath: file.value.get() as string,
name: file.key,
header: options.header,
formData: options.data,
Expand Down
8 changes: 8 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,3 +1386,11 @@ describe('isChineseName', () => {
expect(vtils.isChineseName('·不多')).toBeFalsy()
})
})

describe('FileData', () => {
test('ok', () => {
[1, true, /x/, {}].forEach(item => {
expect(new vtils.FileData(item).get()).toBe(item)
})
})
})

0 comments on commit 20b7cc3

Please sign in to comment.