Skip to content

Commit

Permalink
feat(typescript): initial type declarations (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 authored Aug 19, 2020
1 parent 33a50dd commit 44c6213
Show file tree
Hide file tree
Showing 7 changed files with 1,953 additions and 810 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ logs.json
server/public/main.min.*
coverage
.vscode
index.js
*.d.ts
*.tsbuildinfo
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
index.ts
tsconfig.json
tsconfig.tsbuildinfo
53 changes: 33 additions & 20 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
const validator = require('validator')
const EventSource = require('eventsource')
const superagent = require('superagent')
const url = require('url')
const querystring = require('querystring')
import validator = require('validator')
import EventSource = require('eventsource')
import superagent = require('superagent')
import url = require('url')
import querystring = require('querystring')

type Severity = 'info' | 'error'

interface Options {
source: string
target: string
logger?: Pick<Console, Severity>
}

class Client {
constructor ({ source, target, logger = console }) {
source: string;
target: string;
logger: Pick<Console, Severity>;
events!: EventSource;

constructor ({ source, target, logger = console }: Options) {
this.source = source
this.target = target
this.logger = logger
this.logger = logger!

if (!validator.isURL(this.source)) {
throw new Error('The provided URL is invalid.')
}
}

onmessage (msg) {
static async createChannel () {
return superagent.head('https://smee.io/new').redirects(0).catch((err) => {
return err.response.headers.location
})
}

onmessage (msg: any) {
const data = JSON.parse(msg.data)

const target = url.parse(this.target, true)
Expand All @@ -24,7 +43,7 @@ class Client {

delete data.query

const req = superagent.post(target).send(data.body)
const req = superagent.post(url.format(target)).send(data.body)

delete data.body

Expand All @@ -36,7 +55,7 @@ class Client {
if (err) {
this.logger.error(err)
} else {
this.logger.info(`${req.method} ${req.url} - ${res.statusCode}`)
this.logger.info(`${req.method} ${req.url} - ${res.status}`)
}
})
}
Expand All @@ -45,15 +64,15 @@ class Client {
this.logger.info('Connected', this.events.url)
}

onerror (err) {
onerror (err: any) {
this.logger.error(err)
}

start () {
const events = new EventSource(this.source)
const events = new EventSource(this.source);

// Reconnect immediately
events.reconnectInterval = 0
(events as any).reconnectInterval = 0 // This isn't a valid property of EventSource

events.addEventListener('message', this.onmessage.bind(this))
events.addEventListener('open', this.onopen.bind(this))
Expand All @@ -66,10 +85,4 @@ class Client {
}
}

Client.createChannel = async () => {
return superagent.head('https://smee.io/new').redirects(0).catch((err, res) => {
return err.response.headers.location
})
}

module.exports = Client
export = Client
Loading

0 comments on commit 44c6213

Please sign in to comment.