From f8a07ce9c64981976e74031fdbfc542eb657b625 Mon Sep 17 00:00:00 2001 From: matthewhudson Date: Thu, 1 Aug 2019 05:03:25 -0300 Subject: [PATCH 1/3] WIP: made a simple http request helper. --- blocks/convert-markdown-to-html/index.js | 47 ++++++++++++++++++++++++ helpers/http.js | 27 ++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 blocks/convert-markdown-to-html/index.js create mode 100644 helpers/http.js diff --git a/blocks/convert-markdown-to-html/index.js b/blocks/convert-markdown-to-html/index.js new file mode 100644 index 0000000..6f66abf --- /dev/null +++ b/blocks/convert-markdown-to-html/index.js @@ -0,0 +1,47 @@ +const https = require('https') +const { Block } = require('node-webpipe') + +new Block() + .name('Convert Markdown to HTML') + .description('Determine whether or not a hostname exists.') + .input( + 'md', + 'string', + 'Required. The Markdown text to render in HTML. Markdown content must be 400 KB or less.' + ) + .input( + 'mode', + 'string', + 'The rendering mode. Can be either: [`gfm`,`markdown`]' + ) + .input( + 'context', + 'string', + 'The repository context to use when creating references in gfm mode. Omit this parameter when using markdown mode.' + ) + .output( + 'html', + 'string', + 'Returns an HTML version preferred flavor of Markdown.' + ) + .handle((inputs, cb) => { + const postData = querystring.stringify(inputs) + + const options = { + hostname: 'api.github.com', + path: '/markdown', + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Content-Length': Buffer.byteLength(postData) + } + } + // url (parsed iiinnto host name and path in func) + // inputs (let the func do the magic querystrinnging) + await post(options) + + return { + html: Math.sqrt({ html: data }) + } + }) + .listen() diff --git a/helpers/http.js b/helpers/http.js new file mode 100644 index 0000000..48798cd --- /dev/null +++ b/helpers/http.js @@ -0,0 +1,27 @@ +const https = require('https') + +const post = (options, data) => { + return new Promise((resolve, reject) => { + const req = https.request(options, res => { + let body = '' + res.on('data', chunk => (body += chunk.toString())) + res.on('error', reject) + res.on('end', () => { + if (res.statusCode >= 200 && res.statusCode <= 299) { + resolve({ + statusCode: res.statusCode, + headers: res.headers, + body: body + }) + } else { + reject('Request failed. status: ${res.statusCode}, body: ${body}') + } + }) + }) + req.on('error', reject) + req.write(data, 'binary') + req.end() + }) +} + +module.exports = { post } From db4cc3cf7ad1351da54e8eedf8062ddd15520205 Mon Sep 17 00:00:00 2001 From: matthewhudson Date: Thu, 1 Aug 2019 05:14:23 -0300 Subject: [PATCH 2/3] Almost got it. --- blocks/convert-markdown-to-html/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/blocks/convert-markdown-to-html/index.js b/blocks/convert-markdown-to-html/index.js index 6f66abf..bbc896e 100644 --- a/blocks/convert-markdown-to-html/index.js +++ b/blocks/convert-markdown-to-html/index.js @@ -1,4 +1,4 @@ -const https = require('https') +const post = require('../../helpers/http') const { Block } = require('node-webpipe') new Block() @@ -24,7 +24,7 @@ new Block() 'string', 'Returns an HTML version preferred flavor of Markdown.' ) - .handle((inputs, cb) => { + .handle(async inputs => { const postData = querystring.stringify(inputs) const options = { @@ -36,9 +36,10 @@ new Block() 'Content-Length': Buffer.byteLength(postData) } } + console.log('post') // url (parsed iiinnto host name and path in func) // inputs (let the func do the magic querystrinnging) - await post(options) + const { data } = await post(options) return { html: Math.sqrt({ html: data }) From a9ff023a6743f04b38fdf7248dcc5fb87acc846d Mon Sep 17 00:00:00 2001 From: matthewhudson Date: Thu, 1 Aug 2019 06:23:40 -0300 Subject: [PATCH 3/3] Adding Markdown-to-HTML converter. --- blocks/convert-markdown-to-html/index.js | 24 +++++--------------- helpers/http.js | 28 +++++++++++++++--------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/blocks/convert-markdown-to-html/index.js b/blocks/convert-markdown-to-html/index.js index bbc896e..aef1a5c 100644 --- a/blocks/convert-markdown-to-html/index.js +++ b/blocks/convert-markdown-to-html/index.js @@ -1,11 +1,11 @@ -const post = require('../../helpers/http') +const { post } = require('../../helpers/http') const { Block } = require('node-webpipe') new Block() .name('Convert Markdown to HTML') .description('Determine whether or not a hostname exists.') .input( - 'md', + 'text', 'string', 'Required. The Markdown text to render in HTML. Markdown content must be 400 KB or less.' ) @@ -24,25 +24,13 @@ new Block() 'string', 'Returns an HTML version preferred flavor of Markdown.' ) - .handle(async inputs => { - const postData = querystring.stringify(inputs) - + .handle(async function (inputs, cb) { const options = { hostname: 'api.github.com', path: '/markdown', - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'Content-Length': Buffer.byteLength(postData) - } - } - console.log('post') - // url (parsed iiinnto host name and path in func) - // inputs (let the func do the magic querystrinnging) - const { data } = await post(options) - - return { - html: Math.sqrt({ html: data }) + method: 'POST' } + const html = await post(options, inputs) + cb(null, { html }) }) .listen() diff --git a/helpers/http.js b/helpers/http.js index 48798cd..5ab86fb 100644 --- a/helpers/http.js +++ b/helpers/http.js @@ -1,27 +1,35 @@ const https = require('https') -const post = (options, data) => { +const post = (options, params) => { + const paramsified = JSON.stringify(params) + options.headers = { + 'Content-Type': 'text/plain', + 'Content-Length': Buffer.byteLength(paramsified), + 'User-Agent': 'WebPipes-Block-Examples' + } return new Promise((resolve, reject) => { const req = https.request(options, res => { let body = '' + res.setEncoding('utf8') res.on('data', chunk => (body += chunk.toString())) res.on('error', reject) res.on('end', () => { if (res.statusCode >= 200 && res.statusCode <= 299) { - resolve({ - statusCode: res.statusCode, - headers: res.headers, - body: body - }) + resolve(body) } else { - reject('Request failed. status: ${res.statusCode}, body: ${body}') + reject(`Request failed. status: ${res.statusCode}, body: ${body}`) } }) }) - req.on('error', reject) - req.write(data, 'binary') + + req.on('error', e => { + console.error(`problem with request: ${e.message}`) + }) + + req.write(paramsified) req.end() }) } +const get = (options, params) => {} -module.exports = { post } +module.exports = { get, post }