diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..0081bf7 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "trailingComma": "all" +} \ No newline at end of file diff --git a/LICENSE b/LICENSE index 6f5605d..78cdb06 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Ardalan Amini +Copyright (c) 2018-2019 Ardalan Amini Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index ad2253a..3cb2a12 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Kutt -Node.js client for Kutt.it url shortener +Node.js & browser client for Kutt.it url shortener [![Npm Version](https://img.shields.io/npm/v/kutt.svg)](https://www.npmjs.com/package/kutt) [![TypeScript Version](https://img.shields.io/npm/types/kutt.svg)](https://www.typescriptlang.org) @@ -172,7 +172,7 @@ We use [SemVer](http://semver.org) for versioning. For the versions available, s ## Authors -- **Ardalan Amini** - *Owner/Developer* - [@ardalanamini](https://github.com/ardalanamini) +- **Ardalan Amini** - *Core Maintainer* - [@ardalanamini](https://github.com/ardalanamini) See also the list of [contributors](https://github.com/ardalanamini/kutt/contributors) who participated in this project. diff --git a/package-lock.json b/package-lock.json index b3f568f..5b88a5e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "kutt", - "version": "1.1.0", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -48,9 +48,9 @@ } }, "@types/node": { - "version": "10.12.24", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.24.tgz", - "integrity": "sha512-GWWbvt+z9G5otRBW8rssOFgRY87J9N/qbhqfjMZ+gUuL6zoL+Hm6gP/8qQBG4jjimqdaNLCehcVapZ/Fs2WjCQ==", + "version": "11.13.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.2.tgz", + "integrity": "sha512-HOtU5KqROKT7qX/itKHuTtt5fV0iXbheQvrgbLNXFJQBY/eh+VS5vmmTAVlo3qIGMsypm0G4N1t2AXjy1ZicaQ==", "dev": true }, "ansi-regex": { @@ -179,9 +179,9 @@ "dev": true }, "commander": { - "version": "2.19.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz", - "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==", + "version": "2.20.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz", + "integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==", "dev": true }, "concat-map": { @@ -332,9 +332,9 @@ "dev": true }, "js-yaml": { - "version": "3.12.1", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.12.1.tgz", - "integrity": "sha512-um46hB9wNOKlwkHgiuyEVAybXBjwFUV0Z/RaHJblRd9DXltue9FTYvzCr9ErQrK9Adz5MU4gHWVaNUfdmrC8qA==", + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", + "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", "dev": true, "requires": { "argparse": "^1.0.7", @@ -350,6 +350,21 @@ "brace-expansion": "^1.1.7" } }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -393,9 +408,9 @@ } }, "semver": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", - "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==", + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", + "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", "dev": true }, "sprintf-js": { @@ -426,9 +441,9 @@ "dev": true }, "tslint": { - "version": "5.12.1", - "resolved": "https://registry.npmjs.org/tslint/-/tslint-5.12.1.tgz", - "integrity": "sha512-sfodBHOucFg6egff8d1BvuofoOQ/nOeYNfbp7LDlKBcLNrL3lmS5zoiDGyOMdT7YsEXAwWpTdAHwOGOc8eRZAw==", + "version": "5.15.0", + "resolved": "https://registry.npmjs.org/tslint/-/tslint-5.15.0.tgz", + "integrity": "sha512-6bIEujKR21/3nyeoX2uBnE8s+tMXCQXhqMmaIPJpHmXJoBJPTLcI7/VHRtUwMhnLVdwLqqY3zmd8Dxqa5CVdJA==", "dev": true, "requires": { "babel-code-frame": "^6.22.0", @@ -437,12 +452,13 @@ "commander": "^2.12.1", "diff": "^3.2.0", "glob": "^7.1.1", - "js-yaml": "^3.7.0", + "js-yaml": "^3.13.0", "minimatch": "^3.0.4", + "mkdirp": "^0.5.1", "resolve": "^1.3.2", "semver": "^5.3.0", "tslib": "^1.8.0", - "tsutils": "^2.27.2" + "tsutils": "^2.29.0" } }, "tslint-config-airbnb": { @@ -525,9 +541,9 @@ } }, "typescript": { - "version": "3.3.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.3.tgz", - "integrity": "sha512-Y21Xqe54TBVp+VDSNbuDYdGw0BpoR/Q6wo/+35M8PAU0vipahnyduJWirxxdxjsAkS7hue53x2zp8gz7F05u0A==", + "version": "3.4.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.2.tgz", + "integrity": "sha512-Og2Vn6Mk7JAuWA1hQdDQN/Ekm/SchX80VzLhjKN9ETYrIepBFAd8PkOdOTK2nKt0FCkmMZKBJvQ1dV1gIxPu/A==", "dev": true }, "wrappy": { diff --git a/package.json b/package.json index 4b7eb8b..ea9589e 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "kutt", - "version": "1.1.0", - "description": "Node.js client for Kutt.it url shortener", - "author": "Ardalan Amini [https://ardalanamini.com]", + "version": "1.2.0", + "description": "Node.js & browser client for Kutt.it url shortener", + "author": "Ardalan Amini (https://ardalanamini.com)", "license": "MIT", "homepage": "https://kutt.it", "repository": { @@ -28,10 +28,10 @@ }, "devDependencies": { "@types/axios": "^0.14.0", - "@types/node": "^10.12.24", + "@types/node": "^11.13.2", "axios": "^0.18.0", - "tslint": "^5.12.1", + "tslint": "^5.15.0", "tslint-config-airbnb": "^5.11.1", - "typescript": "^3.3.3" + "typescript": "^3.4.2" } } diff --git a/src/index.ts b/src/index.ts index 11f668f..fd933a1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,13 @@ import axios from "axios"; +const GET = "get"; +const POST = "post"; + const CONFIG = { API: "https://kutt.it", KEY: "", DOMAIN: undefined as string | undefined, - TIMEOUT: 1E4, // 10 seconds by default + TIMEOUT: 1e4, // 10 seconds by default }; namespace Kutt { @@ -30,10 +33,10 @@ namespace Kutt { } export interface Stats { - browser: Array<{ name: string, value: number }>; - os: Array<{ name: string, value: number }>; - country: Array<{ name: string, value: number }>; - referrer: Array<{ name: string, value: number }>; + browser: Array<{ name: string; value: number }>; + os: Array<{ name: string; value: number }>; + country: Array<{ name: string; value: number }>; + referrer: Array<{ name: string; value: number }>; } export interface StatResult { @@ -44,12 +47,30 @@ namespace Kutt { lastDay: { stats: Stats; views: [ - number, number, number, number, - number, number, number, number, - number, number, number, number, - number, number, number, number, - number, number, number, number, - number, number, number, number + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number, + number ]; }; lastWeek: { @@ -73,29 +94,43 @@ class Kutt { /** * Sets global API address */ - public static setAPI = (api: string) => CONFIG.API = api; + public static setAPI = (api: string) => (CONFIG.API = api); /** * Sets global API key */ - public static setKey = (key: string) => CONFIG.KEY = key; + public static setKey = (key: string) => (CONFIG.KEY = key); /** * Sets global custom domain */ - public static setDomain = (domain?: string) => CONFIG.DOMAIN = domain; + public static setDomain = (domain?: string) => (CONFIG.DOMAIN = domain); /** * Sets global timeout */ - public static setTimeout = (timeout: number) => CONFIG.TIMEOUT = timeout; + public static setTimeout = (timeout: number) => (CONFIG.TIMEOUT = timeout); protected _config = Object.assign({}, CONFIG); - protected _request(method: string, path: string, callback: Kutt.Callback): void; - protected _request(method: string, path: string, data?: object): Promise; - protected _request(method: string, path: string, data: object, callback: Kutt.Callback): void; - protected _request(method: string, path: string, data?: object | Kutt.Callback, callback?: Kutt.Callback) { + protected _request( + method: string, + url: string, + callback: Kutt.Callback, + ): void; + protected _request(method: string, url: string, data?: object): Promise; + protected _request( + method: string, + url: string, + data: object, + callback: Kutt.Callback, + ): void; + protected _request( + method: string, + url: string, + data?: object | Kutt.Callback, + callback?: Kutt.Callback, + ) { if (typeof data === "function") { callback = data as Kutt.Callback; data = undefined; @@ -106,8 +141,8 @@ class Kutt { const request = axios({ method, data, + url, baseURL: `${API}/api/url`, - url: path, timeout: TIMEOUT, headers: { "X-API-Key": KEY, @@ -163,7 +198,7 @@ class Kutt { public list(): Promise; public list(callback: Kutt.Callback): void; public list(callback?: Kutt.Callback) { - return this._request("get", "/geturls", callback as any) as any; + return this._request(GET, "/geturls", callback as any) as any; } /** @@ -172,7 +207,7 @@ class Kutt { public submit(data: Kutt.NewUrl): Promise; public submit(data: Kutt.NewUrl, callback: Kutt.Callback): void; public submit(data: Kutt.NewUrl, callback?: Kutt.Callback) { - return this._request("post", "/submit", data, callback as any) as any; + return this._request(POST, "/submit", data, callback as any) as any; } /** @@ -182,7 +217,7 @@ class Kutt { public delete(id: string, callback: Kutt.Callback): void; public delete(id: string, callback?: Kutt.Callback) { return this._request( - "post", + POST, "/deleteurl", { id, @@ -200,7 +235,11 @@ class Kutt { public stats(id: string, callback?: Kutt.Callback) { const domain = this._config.DOMAIN; - return this._request("get", `/stats?id=${id}${domain ? `&domain=${domain}` : ""}`, callback as any) as any; + return this._request( + GET, + `/stats?id=${id}${domain ? `&domain=${domain}` : ""}`, + callback as any, + ) as any; } }