Provide default value for querystring param with pipes on NestJS - nestjs

I'm trying to implement this example. It looks so simple to implement but I have some troubles.
#Get('test')
async test(
#Query('size', new DefaultValuePipe(10), ParseIntPipe) size: number,
#Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
) {
// Do some stuff
}
The first request I've made is http://localhost/api/test
The requests I sent without params are replied with 400 Bad Request and the message is Validation failed (numeric string is expected).
The second request I've made is http://localhost/api/test?size=&page=
The requests I sent with empty params are passed as 0, not with the value that I want to be provided by default.
In both requests, the default values are expected to pass, right? If so, what am I doing wrong?

Related

What response should be send to zapier if not get any desired data to send from my server to zapier

I want to send some data from my server to zapier which is requested by the zapier. What to send in response if my server does not have that data. It should be empty array response or some error message
if(contact?.length){
const contactClone = _.clone(contact);
_.filter(contactClone, function(o: any) { return o.tag_updated_on; }); // if some specific contact has this field which should be rerturned
_.orderBy(contactClone, 'tag_updated_on', 'desc');
if(contactClone?.length){
res.status(200).json([contactClone[0]]); ////First one contact from the array would be returned
return;
}
res.status(200).json([contact[0]]); //What should be returned here
should be empty array response or some error message
Either of these would be appropriate, depending on how you want the caller to respond. A 404 response would be fine if you want it to error. A [] or {} could make sense too, if the caller is expecting to handle it.

How to add a GET request endpoint without path parameters in elixir

I have following in my router get "/flow/reserve/", FlowController, :set_flow_reserved.
when a send a get request to that path, it matches "reserve" to id in path_params in conn. path_params: %{"id" => "reserve"}. and it gives Bad Request error. how to resolve this?
The case you're getting because the order in your router.ex doesn't match the order from action in controller.
Let's say you have routes in this order:
get "/flow/:id", FlowController, :edit
get "/flow/reserve/", FlowController, :set_flow_reserved
so in your controller should be in the same order.
def edit(conn, params)
def set_flow_reserved(conn, params)
When a request is made, all request information from the request path( the route path when sending request) will be sent to controller and it will try to match to the action in controller. So in your case it will match :id to reverse

ArangoDB Foxx Microservices Getting Started tutorial: what is a working URI for this example?

The tutorial here Getting started ยท ArangoDB v3.4.0 Documentation uses this code:
// continued
router.post('/sum', function (req, res) {
const values = req.body.values;
res.send({
result: values.reduce(function (a, b) {
return a + b;
}, 0)
});
})
.body(joi.object({
values: joi.array().items(joi.number().required()).required()
}).required(), 'Values to add together.')
.response(joi.object({
result: joi.number().required()
}).required(), 'Sum of the input values.')
.summary('Add up numbers')
.description('Calculates the sum of an array of number values.');
What is an example of a URI that supplies the expected parameters (two numbers)?
Assuming that your server instance runs on localhost:8529 over HTTP, that the database is _system and the mount point of the Foxx service /getting-started, then the URL of the /sum endpoint is:
http://localhost:8529/getting-started/sum
Note that the database _system is special: It is the default, which means you don't have to specify it explicitly. The following URL is equivalent:
http://localhost:8529/_db/_system/getting-started/sum
Replace _system with the name of the actual database if the Foxx service is mounted in another one.
/sum is a POST route (router.post(...)) and the expected body (content/payload of the HTTP request) is described by the joi schema: A JSON object with the attribute name values and as attribute value a number array (one or more numbers).
With Curl, you can query the service like so:
curl --data "{\"values\":[5,6]}" http://localhost:8529/getting-started/sum
(The request method -X POST is inferred by Curl)
The response is a JSON object with an attribute key result and the calculated number as attribute value:
{"result":11}
If you try to access the URL in a browser instead, it will be a GET request (without payload) and fail with an HTTP error: 405 Method Not Allowed

In azure logic App how to create a 10 integer digit unique id

In azure logic App how to create a 10 digit unique id per request. Currently, I am using Function Javascript but it is creating duplicates.
JAVAscript funaction is:
a=1000000000;
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
a=a+1;
if (true) {
context.res = {
// status: 200, /* Defaults to 200 */
body: a
};
}
else {
context.res = {
status: 400,
body: "Please pass a name on the query string or in the request body"
};
}
context.done();
};
how to solve this
The proper way would be to use something like guid as recommended by #Thomas but it sounds like it would have to be a separate conversation with your back-end guys.
Another approach is to define some sort of formula using rand() together with salting methods to really try minimize the chance of duplication.
The third approach, which i think fits better in your scenario, is to use unix timestamp which would give you 10 digit unique integer that will increment in seconds.
Perhaps, you could use #3 and salt it with #2 to minimize any duplication as well so that the uniqueness is more time-bound.
You could just access the numeric run identifier for the logic app request which is unique per request anyway and will be available to your logic app request context. It is a rather long numeric string (longer than 10 digits). However, as it is numeric you could re-encode and shorten it.
The run identifier is accessed via: #workflow()['run']['name']
e.g. if the run identifier is 08586676754160363885 when compressed as base 64 it would be Y29tcHJlc3M= (This is 12 characters)
If you needed to compress it to 10 characters you could compress it to base 72 or more.

How can I check my request body in a nock intercept but leave a single integer value to have various values?

I'm trying to nock a route call while checking that the passed request body adheres to my assumptions. This route is called multiple times to test three inheriting implementations. The call is the same except that a single value (param3 below) in the request body will differ in value.
nock('someAddress')
.post('/path', {
param1: [someArray],
param2: someInteger,
param3: /[0-9]+/ /* param3 has varying positive integer values */
})
.reply(200, { responseObject });
If I put the exact value for param3, I can get the first iteration of the test to succeed. I can also get the test to work by not specifying the request body at all, obviously. However, if I either just leave out param3 or try to specify various wildcard parameters such as the regex in the example, I always get the following error message:
[route] 404 error: Nock: No match for request POST [someAddress/path] {"param1":[someArray],"param2":someInteger,"param3":exactValueOfThisCall}
How can I check my request body in a nock intercept but allow a single parameter to have various integer values?
You can add an assignment when checking the body, and check the assigned parameter later:
let attr;
nock("https://url.com").put(`/path`, body => {
attr = body;
return true;
}).reply(200);

Resources