Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
feat: adds new adapter option and testing app
Browse files Browse the repository at this point in the history
- adds `multipleParamNameStyle` option to adapter options
- adds a node backend testing app for e-to-e
  • Loading branch information
alexsasharegan committed Mar 24, 2018
1 parent 1592bfb commit 2394cf9
Show file tree
Hide file tree
Showing 12 changed files with 1,160 additions and 208 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ node_modules
!.prettierrc
coverage
tmp

dist
lib
51 changes: 43 additions & 8 deletions src/upload-adapters/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import {
} from "../core/utils";

export type StaticOrDynamic<T> = T | ((files: VTransmitFile[]) => T);
export type ParamName = string | ((file: VTransmitFile) => string);

export enum MultipleParamNameStyle {
Empty,
Indexed,
Brackets,
}

/**
* Responsibilities:
Expand Down Expand Up @@ -55,7 +62,16 @@ export type XHRUploadOptions<T = any> = {
/**
* The name of the file param that gets transferred.
*/
paramName?: StaticOrDynamic<string>;
paramName?: ParamName;
/**
* The param name syntax for multiple uploads.
*
* **Options:**
* - `0 (Empty)` _(Default)_ Adds nothing to the paramName: `file`
* - `1 (Indexed)` Adds the array index of the file: `file[0]`
* - `2 (Brackets)` Adds the array-like brackets without index: `file[]`
*/
multipleParamNameStyle?: MultipleParamNameStyle;
/**
* An object of additional parameters to transfer to the server.
* This is the same as adding hidden input fields in the form element.
Expand Down Expand Up @@ -101,7 +117,8 @@ export class XHRUploadAdapter<T = any> implements UploaderInterface {
public method: StaticOrDynamic<string>;
public withCredentials: StaticOrDynamic<boolean>;
public timeout: StaticOrDynamic<number>;
public paramName: StaticOrDynamic<string>;
public paramName: ParamName;
public multipleParamNameStyle: MultipleParamNameStyle;
public params: StaticOrDynamic<Dictionary<string>>;
public headers: StaticOrDynamic<Dictionary<string>>;
public responseType: StaticOrDynamic<XMLHttpRequestResponseType>;
Expand All @@ -119,6 +136,7 @@ export class XHRUploadAdapter<T = any> implements UploaderInterface {
withCredentials = false,
timeout = 0,
paramName = "file",
multipleParamNameStyle = MultipleParamNameStyle.Empty,
params = Object.create(null),
headers = {
Accept: "application/json",
Expand Down Expand Up @@ -146,6 +164,7 @@ export class XHRUploadAdapter<T = any> implements UploaderInterface {
this.withCredentials = withCredentials;
this.timeout = timeout;
this.paramName = paramName;
this.multipleParamNameStyle = multipleParamNameStyle;
this.params = params;
this.headers = headers;
this.responseType = responseType;
Expand Down Expand Up @@ -304,7 +323,7 @@ export class XHRUploadAdapter<T = any> implements UploaderInterface {

for (let i = 0, len = files.length; i < len; i++) {
formData.append(
this.getParamName(i),
this.getParamName(files[i], i),
files[i].nativeFile,
this.renameFile(files[i].name)
);
Expand Down Expand Up @@ -352,12 +371,28 @@ export class XHRUploadAdapter<T = any> implements UploaderInterface {
};
}

getParamName(index: string | number): string {
let suffix = "";
if (this.context.props.uploadMultiple) {
suffix = `[${index}]`;
getParamName(file: VTransmitFile, index: string | number): string {
let paramName = is_function(this.paramName)
? this.paramName(file)
: this.paramName;

if (!this.context.props.uploadMultiple) {
return paramName;
}

switch (this.multipleParamNameStyle) {
case MultipleParamNameStyle.Indexed:
paramName += `[${index}]`;
break;
case MultipleParamNameStyle.Brackets:
paramName += `[]`;
break;
case MultipleParamNameStyle.Empty:
default:
break;
}
return this.paramName + suffix;

return paramName;
}

cancelUpload(file: VTransmitFile): VTransmitFile[] {
Expand Down
5 changes: 0 additions & 5 deletions test/component.test.ts

This file was deleted.

Empty file removed test/fixtures/component/index.html
Empty file.
Empty file removed test/fixtures/component/index.js
Empty file.
2 changes: 0 additions & 2 deletions test/js/vue-flex.js

This file was deleted.

Loading

0 comments on commit 2394cf9

Please sign in to comment.