Seamlessly support serialization to json and csv from NestJS controllers - nestjs

In spring, you could activate support for returning json or csv by attaching .json or .csv to the rest request, and the data would be returned in the requested format. I've looked over the NestJS docs, and short of handling the raw response manually and doing my own json to csv conversion, there doesn't seem to be a way to do this.
I have a project with dozens of rest routes, I'm ideally searching for a solution that at worst, would involve a custom controller decorator that would introspect requests, and serialize to csv if the .csv extension was added to the rest request. I would be ok with writing the decorator, but I'm not sure where to start, or if this is possible?

Related

Return many files with json from backend to client

I want to know may i respond to client from backend controller with many files (with application/octet-stream). It will be more better, if i respond array of files with json data (for example: user data (nickname, email) with his collection of images in the same response).
So frontend can send json + several files to backend. Can i do the same thing back?
After research i found only information about opportunities to respond application\json and the only file separately (in different requests and responces).
I use nestjs. Nestjs wraps multer lib for handling multipart\form-data.
Of course, there is way to use base64 for files, but i found that better way for files is to use content-type.

Can I create a Alexa Skill that can read my report available in CSV (or JSON) format?

Can we create an Alexa Skill that could read out the data on my CSV (or JSON) file? I would like to create a voice-bot to read out my weekly report. I can have my report available in either CSV or JSON format.
It would be great if we aren't using any already built 3rd party apps. I would like to write it from scratch.
As a short answer, yes. That should totally be possible. I currently have a skill that gets the response from a JSON file (it gets the required response, based on the intent of the user).
A CSV file should be fine too - you just need to parse it.
If your file is dynamic and changes often, you could potentially store the file in AWS S3, and fetch/read the content from there.
Once you have the string value for your speech response, from the JSON/CSV - just use that.
It doesn't say how you'd write your skill - but the SDK for Node.js is pretty straight forward, and the documentation is good (with lots of examples too).

Creating an HTML or PDF "file" in memory and streaming it in Node.js

I have a need to create a pdf or html document within a Node.js express API which then sends that document over HTTP to an API managing our CMS.
So functionally I would like to create the document and POST it as part of a multipart-form upload POST request to an external service.
I see how to do this if after I create the file, I then turn around and write it disk. After that point I can do a read stream of the file from that path to format the POST request with the file.
However I'm wondering how I can perform this action without writing the file to disk and then reading it into a read stream. It seems I should be able to accomplish this without that IO.
Anybody able to point me to a good example or library that does something along these lines?
You can extend Writable and/or Readable streams. By the first look this library do what you need, with the same way - extending built-in streams.

Is there any way to access non-file fields in a multipart/form-data POST before parsing the file?

I'm writing some middleware to handle an image upload for my app (written in Node.js). I'm using the multiparty module and things have been going great, but I've encountered a problem. I'm trying to access some fields that have been included with the request before I parse the form (so that I can perform some validation). From the examples that I've found, fields included with a multipart/form-data POST are only available after the form has been parsed. Is there a way to get access to non-file fields included in the file upload request before parsing the file?
The best way that I can think to do this is by including some customer headers. Would this be the best way to go about this? I want to avoid the expense of uploading the file if the validation on the field(s) fails.
I'm using the Loopback framework which is built on top of Express if that makes any difference.
Thanks in advance for your help and happy Thanksgiving!
Generally the way to do this is to put your non-file fields before your file fields. At least HTML forms submit their fields in the order they are in in the DOM.
I'm not sure what module loopback uses for parsing forms, but busboy for example passes fields to you in the order it sees them in the request (not saving anything to disk or buffering entire files in memory).

Writing a Node SDK for API- How to upload a file along with other params in multipart format?

I'm writing a node SDK and one of the endpoints allows for a file_upload parameter. I'm currently using the standard https library to make my calls, and I wonder if I should continue using it or move to something the "requests" library given that I need to do file uploads.
Here is an article I was reading through to build multi-part file upload functionality into the https module, but the article doesn't say the best way to combine the multi-part file form data and additional parameters say "test_mode=true" or something like that.
how to upload a file from node.js
Wondering if I should move over to requests complete or if this approach seems good then how can I add the above multi-part form functionality but extend it to allow for additional parameters in the body as well as a file binary.
The request module uses the form-data module for sending multipart/form-data requests. You could also use form-data by itself if you don't want to use request.

Resources