Allow arrays to be used as arguments for POST/PUT requests

This commit is contained in:
Kegan Myers 2014-07-07 14:29:39 -05:00
parent 85c5fe3d0d
commit 769c779d8c
11 changed files with 57 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{
"name": "Typertext",
"version": "0.8.1",
"version": "0.8.2",
"homepage": "https://github.com/terribleplan/Typertext",
"authors": [
"Kegan Myers <kegan@keganmyers.com>"

10
build/typertext.d.ts vendored
View File

@ -92,7 +92,11 @@ declare module Typertext.Http {
}
declare module Typertext.Http {
interface HttpQueryString {
[index: string]: string;
[index: string]: any;
}
}
declare module Typertext.Http {
interface HttpResponseHandler extends GenericResponseHandler<HttpResponse> {
}
}
declare module Typertext.Transport {
@ -121,10 +125,6 @@ declare module Typertext.Http {
constructor(status: HttpResponseStatus, responseHeaderGetter?: (input: string) => string, httpResponseCode?: number, responseBody?: string);
}
}
declare module Typertext.Http {
interface HttpResponseHandler extends GenericResponseHandler<HttpResponse> {
}
}
declare module Typertext.Http {
enum HttpResponseStatus {
success = 0,

View File

@ -130,7 +130,22 @@ var Typertext;
var temp;
for (temp in data) {
rs += encodeURIComponent(temp) + "=" + encodeURIComponent(data[temp]) + "&";
var cur = data[temp];
if (cur instanceof Array) {
for (var i = 0; i < cur.length; i++) {
if (typeof cur[i] !== "string") {
continue;
}
rs += encodeURIComponent(temp) + "=" + encodeURIComponent(cur[i]) + "&";
}
continue;
}
if (typeof cur === "string") {
rs += encodeURIComponent(temp) + "=" + encodeURIComponent(cur) + "&";
}
}
return rs.slice(0, -1);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -5,8 +5,11 @@
module Typertext.Http {
/**
* @interface HttpQueryString
*
* Although it appears that this can provide any type of value, implementations
* are allowed to disregard types they are unable to handle.
*/
export interface HttpQueryString {
[index:string]:string
[index:string]:any
}
}

View File

@ -1,10 +1,9 @@
/// <reference path="HttpResponseHandler.ts" />
/// <reference path="../GenericRequest.ts" />
/// <reference path="../Transport/TransportConstructor.ts" />
/// <reference path="../Transport/GenericTransport.ts" />
/// <reference path="../Transport/TransportChooser.ts" />
//TODO add support for IE8-9 CORS via XDomain
//TODO better error handling, ala exceptions
/**
* @namespace Typertext
* @module Http

View File

@ -79,7 +79,22 @@ module Typertext.Http {
var temp:string;
for (temp in data) {
rs += encodeURIComponent(temp) + "=" + encodeURIComponent(data[temp]) + "&";
var cur = data[temp];
if (cur instanceof Array) {
for (var i = 0; i < cur.length; i++) {
if (typeof cur[i] !== "string") {
continue;
}
rs += encodeURIComponent(temp) + "=" + encodeURIComponent(cur[i]) + "&";
}
continue;
}
if (typeof cur === "string") {
rs += encodeURIComponent(temp) + "=" + encodeURIComponent(cur) + "&";
}
}
return rs.slice(0, -1);

View File

@ -5,7 +5,7 @@
"type": "git",
"url": "https://github.com/terribleplan/Typertext.git"
},
"version": "0.8.1",
"version": "0.8.2",
"devDependencies": {
"grunt": "~0.4.2",
"grunt-cli": "~0.1.13",

View File

@ -209,6 +209,15 @@ describe("Typertext.Http.HttpUrl", function () {
expect(actualOutput).toEqual(expectedOutput);
});
it("encodes a key with an array value", function () {
var input = {
"foo": ["bar", "fizz", "buzz"]
},
expectedOutput = "foo=bar&foo=fizz&foo=buzz",
actualOutput = Typertext.Http.HttpUrl.UrlEncodeObject(input);
expect(actualOutput).toEqual(expectedOutput);
});
});
describe("UrlDecodeObject", function () {