Skip to content

Commit

Permalink
feat(hukkup): add hukkup function
Browse files Browse the repository at this point in the history
hukkup help you send a webhook request to a Hukk server
  • Loading branch information
nampdn committed Nov 19, 2018
1 parent dc98591 commit 36b6b67
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/hukkup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {request} from 'http';

export interface HukkupOptions {
hostname: string;
port: number;
endpoint: string;
data: any;
}

export const hukkup = (
{hostname, port, endpoint, data}: HukkupOptions,
callback: (err: any, data: any) => void,
) => {
const body = JSON.stringify(data)
const opts = {
hostname,
port,
path: endpoint,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body),
},
};

const req = request(opts, res => {
let body = '';
res.setEncoding('utf8');
res.on('data', chunk => {
body += chunk;
});
res.on('end', () => {
callback(null, body);
});
});

req.on('error', err => {
callback(err, null);
});

req.write(body);
req.end();
};

0 comments on commit 36b6b67

Please sign in to comment.