Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const bodyParsingHandler: RequestHandler = async (req, res) => {
const buf = await buffer(req)
console.log(buf)
//
const txt = await text(req)
// '{"price": 9.99}'
const js: any = await json(req)
// { price: 9.99 }
console.log(js.price)
return ''
}
async function onPost(req) {
const data = await json(req);
if (!data.web) {
const error = new Error(
`Please provide the web link as fallback url.`
);
error.statusCode = 400;
throw error;
}
// TODO: take android package name
const link = await generateDynamicLink(data);
return { link };
}
module.exports = async req => {
const data = await json(req);
console.log(data);
return 'Data logged to your console';
};
private async parseBody(req: IncomingMessage, contentType?: string): Promise<[undefined, HTTPBodyParams] | [Error, undefined]> {
let result: HTTPBodyParams;
try {
if (!contentType || !contentType.includes('json')) {
result = parseQuery(await text(req, REQUEST_PARSE_OPTIONS));
} else {
result = await json(req, REQUEST_PARSE_OPTIONS);
}
return [undefined, isObject(result) ? result : {}];
} catch (error) {
return [error, undefined];
}
}
async parseBody(contentType, req) {
let result;
try {
if (contentType.indexOf('json') >= 0) {
result = await micro_1.json(req, parseOpts);
}
else {
result = helpers_1.parseQuery(await micro_1.text(req, parseOpts));
}
return [undefined, helpers_1.isObject(result) ? result : {}];
}
catch (error) {
return [error, undefined];
}
}
};
async parseBody(contentType, req) {
let result;
try {
if (contentType.indexOf('json') >= 0) {
result = await micro_1.json(req, parseOpts);
}
else {
result = helpers_1.parseQuery(await micro_1.text(req, parseOpts));
}
return [undefined, helpers_1.isObject(result) ? result : {}];
}
catch (error) {
return [error, undefined];
}
}
};
return createSelectDecorators((req, res) => {
return json(req, options)
})
}
module.exports = cors(async (req, res) => {
try {
const js = await json(req)
const content = createFile(js.title, js.content)
res.setHeader('Content-Type', 'application/force-download')
res.setHeader('Content-Disposition', `attachment; filename="${noSpace(js.title)}.md"`)
send(res, 200, streamify(content))
} catch (error) {
sendError(req, res, error)
}
})
module.exports = cors(async (req, res) => {
try {
const js = await json(req)
const content = createFile(js)
res.setHeader('Content-Type', 'application/force-download')
res.setHeader('Content-Disposition', `attachment; filename="${noSpace(js.title)}.json"`)
send(res, 200, content)
} catch (error) {
sendError(req, res, error)
}
})