Object for JSON Payload to be map using Promise function with Callback
Object for JSON Payload to be map using Promise function with Callback
I have a JSON payload that looks like
"name": "Reports",
"subject": "Monthly Reports",
"attachments":[
"attachment":
"name": "Month1.pdf",
"type": "application/pdf",
"path": "http://example.com/Month1.pdf"
,
"attachment":
"name": "Month2.pdf",
"type": "application/pdf",
"path": "http://example.com/Month2.pdf"
]
And I want to take the attachment object part received information from the JSON post request and convert the file in the path to base64 format using a promise function which will allow for it to loop through the attachment object for each like the following which is in TypeScript:
NB: No looping has been done as I don't know how to achieve this but there should be a for each loop
app.post('/send', (req, res) => {
let data = req.body;
function getMonthlyReport() : Promise<object>
let blob = request(
method: 'GET',
encoding: null,
uri: data.attachment.path //http://example.com/Month1.pdf
, (err, resp, file)=>
let encoded = file.toString('base64');
// And get the result outside the function which be base64encoded string for each attachment
);
And be able to map it like this outside the function with an await function
let attachmentPayload = await Object.keys(data.attachment.path)
.map(k => (
"name":data.attachment.name[k], // Name from payload
"type": data.attachment.type[k], // Type from payload
"content": encoded //base64encoded string from promise callback
));
This is just an example of what I'm trying to achieve using promise functions as I want the information to populate first before move onto the rest of the code.
1 Answer
1
This should be close to what you're looking for; modify as necessary:
interface Attachment
attachment:
name: string;
type: string;
path: string;
interface InlineAttachment
name: string;
type: string;
content: string;
interface Payload
name: string;
subject: string;
attachments: Attachment;
interface InlinedPayload
name: string;
subject: string;
attachments: InlineAttachment;
app.post('/send', async (req, res) =>
let data: Payload = req.body;
function getMonthlyReport(att: Attachment) : Promise<InlineAttachment>
return new Promise((resolve, reject) =>
let blob = request(
method: 'GET',
encoding: null,
uri: att.attachment.path //http://example.com/Month1.pdf
, (err, resp, file)=>
if (err)
reject(err);
else
let encoded = file.toString('base64');
resolve(
name: att.attachment.name,
type: att.attachment.type,
content: encoded
);
);
);
let inlinedData: InlinedPayload =
name: data.name,
subject: data.subject,
attachments: await Promise.all(data.attachments.map(getMonthlyReport))
;
);
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you so much. You layered everything so well. I was able to make it work.
– Motsane M
Sep 16 '18 at 12:56