Skip to content

Commit

Permalink
Tsify lib/shorteners (#4504)
Browse files Browse the repository at this point in the history
* Convert lib/shorteners to ts

* Improved types
  • Loading branch information
jeremy-rifkin authored and mattgodbolt committed Jan 24, 2023
1 parent 95d1d88 commit ce55a35
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 17 deletions.
File renamed without changes.
16 changes: 11 additions & 5 deletions lib/shortener/base.js → lib/shortener/base.ts
Expand Up @@ -22,11 +22,17 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

export class BaseShortener {
constructor(storageHandler) {
this.storageHandler = storageHandler;
}
import * as express from 'express';

import {StorageBase} from '../storage';

export abstract class BaseShortener {
constructor(protected storageHandler: StorageBase) {}

// eslint-disable-next-line no-unused-vars
handle(req, res) {}
abstract handle(req: express.Request, res: express.Response);

static get key(): string {
throw 'get key() must be overridden';
}
}
10 changes: 6 additions & 4 deletions lib/shortener/default.js → lib/shortener/default.ts
Expand Up @@ -22,14 +22,16 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import * as express from 'express';

import {BaseShortener} from './base';

export class DefaultShortener extends BaseShortener {
static get key() {
return 'default';
override handle(req: express.Request, res: express.Response) {
return this.storageHandler.handler(req, res);
}

handle(req, res) {
return this.storageHandler.handler(req, res);
static override get key() {
return 'default';
}
}
2 changes: 1 addition & 1 deletion lib/shortener/google.js → lib/shortener/google.ts
Expand Up @@ -25,7 +25,7 @@
import request from 'request';

export class ShortLinkResolver {
resolve(url) {
resolve(url: string) {
return new Promise((resolve, reject) => {
request({method: 'HEAD', uri: url, followRedirect: false}, (err, res) => {
if (err !== null) {
Expand Down
File renamed without changes.
15 changes: 8 additions & 7 deletions lib/shortener/tinyurl.js → lib/shortener/tinyurl.ts
Expand Up @@ -22,28 +22,29 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import * as express from 'express';
import request from 'request';

import {BaseShortener} from './base';

export class TinyUrlShortener extends BaseShortener {
static get key() {
return 'tinyurl';
}

handle(req, res) {
override handle(req: express.Request, res: express.Response) {
const url = `${req.protocol}://${req.get('host')}#${req.body.config}`;
const options = {
url: 'https://tinyurl.com/api-create.php?url=' + encodeURIComponent(url),
method: 'GET',
};
const callback = (err, resp, body) => {
const callback = (err, resp: request.Response, body) => {
if (!err && resp.statusCode === 200) {
res.send({url: body});
} else {
res.status(resp.statusCode).send(resp.error);
res.status(resp.statusCode).send('Tinyurl error');
}
};
request.post(options, callback);
}

static override get key() {
return 'tinyurl';
}
}
68 changes: 68 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -25,6 +25,7 @@
"@sentry/browser": "^6.16.1",
"@sentry/node": "^6.16.1",
"@types/file-saver": "^2.0.5",
"@types/request": "^2.48.8",
"aws-sdk": "^2.1048.0",
"big-integer": "^1.6.51",
"body-parser": "^1.19.1",
Expand Down

0 comments on commit ce55a35

Please sign in to comment.