diff --git a/.eslintrc.yml b/.eslintrc.yml index 79e3f73..4106151 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -3,37 +3,13 @@ env: es2021: true node: true extends: - - eslint:recommended - - plugin:@typescript-eslint/recommended -parser: '@typescript-eslint/parser' + - noir/all parserOptions: - ecmaVersion: 12 + project: ./tsconfig.json + ecmaVersion: latest sourceType: module -plugins: - - '@typescript-eslint' - - import rules: - indent: off - "@typescript-eslint/indent": - - error - - 2 - - SwitchCase: 1 - quotes: off - "@typescript-eslint/quotes": - - error - - double - - avoidEscape: true - semi: off - "@typescript-eslint/semi": - - error - - always - comma-dangle: off - "@typescript-eslint/comma-dangle": - - error - - always-multiline - max-len: - - error - - 120 + import/no-unresolved: off import/extensions: - error - always diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bc4affc --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.ts text eol=lf +*.js text eol=lf diff --git a/package-lock.json b/package-lock.json index b20fbc7..abd798a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@typescript-eslint/parser": "^5.28.0", "axios": "^0.27.2", "eslint": "^8.18.0", + "eslint-config-noir": "^1.2.0", "eslint-plugin-import": "^2.26.0", "typescript": "^4.7.4" }, @@ -658,6 +659,25 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/eslint-config-noir": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/eslint-config-noir/-/eslint-config-noir-1.2.0.tgz", + "integrity": "sha512-t7r164bxJurGANlyB3AC52c1p+9ju6n9G3XNeOGq/YlcKlMQlftJPr4QfRpmVRgSryerZK/GGKBTiXMpTjt5tQ==", + "dev": true, + "peerDependencies": { + "@typescript-eslint/eslint-plugin": ">=5.28.0", + "eslint": ">=8.17.0", + "eslint-plugin-import": ">=2.26.0" + }, + "peerDependenciesMeta": { + "@typescript-eslint/eslint-plugin": { + "optional": true + }, + "eslint-plugin-import": { + "optional": true + } + } + }, "node_modules/eslint-import-resolver-node": { "version": "0.3.6", "resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.6.tgz", @@ -2814,6 +2834,13 @@ } } }, + "eslint-config-noir": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/eslint-config-noir/-/eslint-config-noir-1.2.0.tgz", + "integrity": "sha512-t7r164bxJurGANlyB3AC52c1p+9ju6n9G3XNeOGq/YlcKlMQlftJPr4QfRpmVRgSryerZK/GGKBTiXMpTjt5tQ==", + "dev": true, + "requires": {} + }, "eslint-import-resolver-node": { "version": "0.3.6", "resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.6.tgz", diff --git a/package.json b/package.json index 4490fae..3a24484 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "@typescript-eslint/parser": "^5.28.0", "axios": "^0.27.2", "eslint": "^8.18.0", + "eslint-config-noir": "^1.2.0", "eslint-plugin-import": "^2.26.0", "typescript": "^4.7.4" } diff --git a/src/Kutt.ts b/src/Kutt.ts index 13553c0..7e352ae 100644 --- a/src/Kutt.ts +++ b/src/Kutt.ts @@ -1,22 +1,17 @@ -import { Domain, Health, Link, User } from "./api/index.js"; import CONFIG, { ConfigI } from "./config.js"; +import { Domain, Health, Link, User } from "./api/index.js"; /** * * @see {@link https://docs.kutt.it} */ export default class Kutt { + /** - * Sets default/global config. * - * @param config - * @param value + * @private */ - public static set(config: Config, value: ConfigI[Config]): typeof Kutt { - CONFIG[config] = value; - - return this; - } + #config: ConfigI = { ...CONFIG }; /** * Gets default/global config. @@ -28,39 +23,33 @@ export default class Kutt { } /** - * - * @private - */ - #config: ConfigI = { ...CONFIG }; - - /** - * Sets instance config. + * Sets default/global config. * * @param config * @param value */ - public set(config: Config, value: ConfigI[Config]): this { - this.#config[config] = value; + public static set(config: Config, value: ConfigI[Config]): typeof Kutt { + CONFIG[config] = value; return this; } /** - * Gets instance config. + * Domains API. * - * @param config + * @see {@link https://docs.kutt.it/#tag/domains} */ - public get(config: Config): ConfigI[Config] { - return this.#config[config]; + public domains(): Domain { + return new Domain(this.#config); } /** - * Domains API. + * Gets instance config. * - * @see {@link https://docs.kutt.it/#tag/domains} + * @param config */ - public domains(): Domain { - return new Domain(this.#config); + public get(config: Config): ConfigI[Config] { + return this.#config[config]; } /** @@ -81,6 +70,18 @@ export default class Kutt { return new Link(this.#config); } + /** + * Sets instance config. + * + * @param config + * @param value + */ + public set(config: Config, value: ConfigI[Config]): this { + this.#config[config] = value; + + return this; + } + /** * Users API. * @@ -89,4 +90,5 @@ export default class Kutt { public users(): User { return new User(this.#config); } + } diff --git a/src/api/API.ts b/src/api/API.ts index 350d0aa..e4f398f 100644 --- a/src/api/API.ts +++ b/src/api/API.ts @@ -1,10 +1,11 @@ +import { ConfigI } from "../config.js"; import axios, { AxiosInstance } from "axios"; -import type { ConfigI } from "../config.js"; /** * */ export default abstract class API { + /** * * @protected @@ -21,7 +22,7 @@ export default abstract class API { * * @param config */ - constructor(config: ConfigI) { + public constructor(config: ConfigI) { const { api, key, timeout } = config; this.axios = axios.create({ @@ -39,6 +40,7 @@ export default abstract class API { * @protected */ protected url(url = ""): string { - return `${this.prefix}${url}`; + return `${ this.prefix }${ url }`; } + } diff --git a/src/api/Domain.ts b/src/api/Domain.ts index 92a33e0..cd3970a 100644 --- a/src/api/Domain.ts +++ b/src/api/Domain.ts @@ -5,6 +5,7 @@ import API from "./API.js"; * @see {@link https://docs.kutt.it/#tag/domains} */ export default class Domain extends API { + /** * * @protected @@ -16,7 +17,7 @@ export default class Domain extends API { * * @param domain */ - public create(domain: NewDomainI): Promise { + public async create(domain: NewDomainI): Promise { return this.axios .post(this.url(), domain) .then(({ data }) => data); @@ -27,11 +28,12 @@ export default class Domain extends API { * * @param id */ - public remove(id: string): Promise { + public async remove(id: string): Promise { return this.axios - .delete<{ message: string }>(this.url(`/${id}`)) + .delete<{ message: string }>(this.url(`/${ id }`)) .then(({ data }) => data.message); } + } export interface NewDomainI { @@ -40,10 +42,10 @@ export interface NewDomainI { } export interface DomainI { - id: string; address: string; banned: boolean; created_at: string; - updated_at: string; homepage?: string; + id: string; + updated_at: string; } diff --git a/src/api/Health.ts b/src/api/Health.ts index cf4bae0..a9d1920 100644 --- a/src/api/Health.ts +++ b/src/api/Health.ts @@ -5,6 +5,7 @@ import API from "./API.js"; * @see {@link https://docs.kutt.it/#tag/health} */ export default class Health extends API { + /** * * @protected @@ -14,9 +15,10 @@ export default class Health extends API { /** * Checks API health. */ - public check(): Promise { + public async check(): Promise { return this.axios.get(this.url()) .then(() => true) .catch(() => false); } + } diff --git a/src/api/Link.ts b/src/api/Link.ts index 40aae6f..e671e5d 100644 --- a/src/api/Link.ts +++ b/src/api/Link.ts @@ -5,12 +5,24 @@ import API from "./API.js"; * @see {@link https://docs.kutt.it/#tag/links} */ export default class Link extends API { + /** * * @protected */ protected readonly prefix = "/links"; + /** + * Creates a short link. + * + * @param link + */ + public async create(link: NewLinkI): Promise { + return this.axios + .post(this.url(), link) + .then(({ data }) => data); + } + /** * Gets list of links. * @@ -19,22 +31,28 @@ export default class Link extends API { * @param [params.limit=10] * @param [params.all=false] */ - public list(params: ListLinkParamsI = {}): Promise { + public async list(params: ListLinkParamsI = {}): Promise { const { skip = 0, limit = 10, all = false } = params; - return this.axios.get(this.url(), { params: { skip, limit, all } }) + return this.axios.get(this.url(), { + params: { + skip, + limit, + all, + }, + }) .then(({ data }) => data); } /** - * Creates a short link. + * Deletes a link. * - * @param link + * @param id */ - public create(link: NewLinkI): Promise { + public async remove(id: string): Promise { return this.axios - .post(this.url(), link) - .then(({ data }) => data); + .delete<{ message: string }>(this.url(`/${ id }`)) + .then(({ data }) => data.message); } /** @@ -42,9 +60,9 @@ export default class Link extends API { * * @param id */ - public stats(id: string): Promise { + public async stats(id: string): Promise { return this.axios - .get(this.url(`/${id}/stats`)) + .get(this.url(`/${ id }/stats`)) .then(({ data }) => data); } @@ -54,78 +72,68 @@ export default class Link extends API { * @param id * @param link */ - public update(id: string, link: UpdateLinkI): Promise { + public async update(id: string, link: UpdateLinkI): Promise { return this.axios - .patch(this.url(`/${id}`), link) + .patch(this.url(`/${ id }`), link) .then(({ data }) => data); } - /** - * Deletes a link. - * - * @param id - */ - public remove(id: string): Promise { - return this.axios - .delete<{ message: string }>(this.url(`/${id}`)) - .then(({ data }) => data.message); - } } export interface ListLinkParamsI { - skip?: number; - limit?: number; all?: boolean; + limit?: number; + skip?: number; } export interface ListLinkResultI { - skip: number; + data: LinkI[]; limit: number; + skip: number; total: number; - data: LinkI[]; } export interface LinkI { - id: string; - link: string; address: string; - target: string; - description: string; - visit_count: number; - password: boolean; banned: boolean; created_at: string; + description: string; + id: string; + link: string; + password: boolean; + target: string; updated_at: string; + visit_count: number; } export interface NewLinkI { - target: string; + customurl?: string; description?: string; + domain?: string; expire_in?: string; password?: string; - customurl?: string; reuse?: boolean; - domain?: string; + target: string; } export interface UpdateLinkI { - target: string; address: string; description?: string; expire_in?: string; + target: string; } export interface LinkStatsI { - id: string; - link: string; address: string; - target: string; - visit_count: number; - password: boolean; banned: boolean; created_at: string; + id: string; + link: string; + password: boolean; + target: string; updatedAt: string; updated_at: string; + visit_count: number; } export interface LinkDurationStatsI { @@ -135,8 +143,8 @@ export interface LinkDurationStatsI { export interface LinkStatDetailsI { browser: LinkStatDetailI[]; - os: LinkStatDetailI[]; country: LinkStatDetailI[]; + os: LinkStatDetailI[]; referrer: LinkStatDetailI[]; } diff --git a/src/api/User.ts b/src/api/User.ts index 89d5b6e..0dfee3d 100644 --- a/src/api/User.ts +++ b/src/api/User.ts @@ -1,11 +1,12 @@ import API from "./API.js"; -import type { DomainI } from "./Domain.js"; +import { DomainI } from "./Domain.js"; /** * * @see {@link https://docs.kutt.it/#tag/users} */ export default class User extends API { + /** * * @protected @@ -15,15 +16,17 @@ export default class User extends API { /** * Gets user info. */ - public info(): Promise { + public async info(): Promise { + // TODO: Dates? return this.axios .get(this.url()) - .then(({ data }) => data); // TODO: Dates? + .then(({ data }) => data); } + } export interface UserI { apikey: string; - email: string; domains: DomainI[]; + email: string; } diff --git a/src/config.ts b/src/config.ts index 3f28bf7..520bad7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,7 +1,9 @@ const CONFIG: ConfigI = { api: "https://kutt.it/api/v2", key: "", - timeout: 1e4, // 10 seconds by default + + // 10 seconds by default + timeout: 1e4, }; export default CONFIG;