1
0
Fork 0

Foxx: Introducing queryParam

This commit is contained in:
Lucas Dohmen 2013-03-27 11:20:18 +01:00
parent 847783a151
commit 1466429e88
3 changed files with 65 additions and 1 deletions

View File

@ -94,6 +94,9 @@ Furthermore you can describe your API by chaining the following methods onto you
#### Describing a pathParam
/// @fn JSF_foxx_RequestContext_pathParam
#### Describing a queryParam
/// @fn JSF_foxx_RequestContext_queryParam
### Before and After Hooks
You can use the following two functions to do something before or respectively after the normal routing process is happening. You could use that for logging or to manipulate the request or response (translate it to a certain format for example).

View File

@ -273,6 +273,25 @@ function DocumentationAndConstraintsSpec () {
assertEqual(routes[0].docs.parameters.id.description, "Id of the Foxx");
assertEqual(routes[0].docs.parameters.id.dataType, "int");
assertEqual(routes[0].docs.parameters.id.required, true);
},
testDefineQueryParam: function () {
app.get('/foxx', function () {
//nothing
}).queryParam("a", {
description: "The value of an a",
dataType: "int",
required: false,
allowMultiple: true
});
assertEqual(routes.length, 1);
assertEqual(routes[0].docs.parameters.a.paramType, "query");
assertEqual(routes[0].docs.parameters.a.name, "a");
assertEqual(routes[0].docs.parameters.a.description, "The value of an a");
assertEqual(routes[0].docs.parameters.a.dataType, "int");
assertEqual(routes[0].docs.parameters.a.required, false);
assertEqual(routes[0].docs.parameters.a.allowMultiple, true);
}
};
}

View File

@ -432,7 +432,10 @@ _.extend(RequestContext.prototype, {
/// @EXAMPLE:
/// app.get("/foxx/:id", function {
/// // Do something
/// }).constrain("id", /[a-z]+/);
/// }).pathParam("id", {
/// description: "Id of the Foxx",
/// dataType: "int"
/// });
////////////////////////////////////////////////////////////////////////////////
pathParam: function (paramName, attributes) {
'use strict';
@ -450,6 +453,45 @@ _.extend(RequestContext.prototype, {
required: true
};
return this;
},
////////////////////////////////////////////////////////////////////////////////
/// @fn JSF_foxx_RequestContext_queryParam
/// @brief Describe a Query Parameter
///
/// If you defined a route "/foxx", you can constrain which format a query
/// parameter (`/foxx?a=12`) can have by giving it a type.
/// We currently support the following types:
///
/// * int
/// * string
///
/// You can also provide a description of this parameter, if it is required and
/// if you can provide the parameter multiple times.
///
/// @EXAMPLE:
/// app.get("/foxx", function {
/// // Do something
/// }).queryParam("id", {
/// description: "Id of the Foxx",
/// dataType: "int",
/// required: true,
/// allowMultiple: false
/// });
////////////////////////////////////////////////////////////////////////////////
queryParam: function (paramName, attributes) {
'use strict';
this.route.docs.parameters[paramName] = {
paramType: "query",
name: paramName,
description: attributes.description,
dataType: attributes.dataType,
required: attributes.required,
allowMultiple: attributes.allowMultiple
};
return this;
}
});