Skip to content

Commit

Permalink
Closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Apr 9, 2019
1 parent 1e550d5 commit 77915fe
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "all"
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Kutt <!-- omit in toc -->

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)
Expand Down Expand Up @@ -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.

Expand Down
58 changes: 37 additions & 21 deletions package-lock.json

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

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "kutt",
"version": "1.1.0",
"description": "Node.js client for Kutt.it url shortener",
"author": "Ardalan Amini <[email protected]> [https://ardalanamini.com]",
"version": "1.2.0",
"description": "Node.js & browser client for Kutt.it url shortener",
"author": "Ardalan Amini <[email protected]> (https://ardalanamini.com)",
"license": "MIT",
"homepage": "https://kutt.it",
"repository": {
Expand All @@ -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"
}
}
87 changes: 63 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -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: {
Expand All @@ -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<any>): void;
protected _request(method: string, path: string, data?: object): Promise<any>;
protected _request(method: string, path: string, data: object, callback: Kutt.Callback<any>): void;
protected _request(method: string, path: string, data?: object | Kutt.Callback<any>, callback?: Kutt.Callback<any>) {
protected _request(
method: string,
url: string,
callback: Kutt.Callback<any>,
): void;
protected _request(method: string, url: string, data?: object): Promise<any>;
protected _request(
method: string,
url: string,
data: object,
callback: Kutt.Callback<any>,
): void;
protected _request(
method: string,
url: string,
data?: object | Kutt.Callback<any>,
callback?: Kutt.Callback<any>,
) {
if (typeof data === "function") {
callback = data as Kutt.Callback<any>;
data = undefined;
Expand All @@ -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,
Expand Down Expand Up @@ -163,7 +198,7 @@ class Kutt {
public list(): Promise<Kutt.ListResult>;
public list(callback: Kutt.Callback<Kutt.ListResult>): void;
public list(callback?: Kutt.Callback<Kutt.ListResult>) {
return this._request("get", "/geturls", callback as any) as any;
return this._request(GET, "/geturls", callback as any) as any;
}

/**
Expand All @@ -172,7 +207,7 @@ class Kutt {
public submit(data: Kutt.NewUrl): Promise<Kutt.Url>;
public submit(data: Kutt.NewUrl, callback: Kutt.Callback<Kutt.Url>): void;
public submit(data: Kutt.NewUrl, callback?: Kutt.Callback<Kutt.Url>) {
return this._request("post", "/submit", data, callback as any) as any;
return this._request(POST, "/submit", data, callback as any) as any;
}

/**
Expand All @@ -182,7 +217,7 @@ class Kutt {
public delete(id: string, callback: Kutt.Callback<Kutt.Url>): void;
public delete(id: string, callback?: Kutt.Callback<Kutt.Url>) {
return this._request(
"post",
POST,
"/deleteurl",
{
id,
Expand All @@ -200,7 +235,11 @@ class Kutt {
public stats(id: string, callback?: Kutt.Callback<Kutt.StatResult>) {
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;
}
}

Expand Down

0 comments on commit 77915fe

Please sign in to comment.