From d78e17fe4ac46fd1209c959de9d358a5aa9aabea Mon Sep 17 00:00:00 2001 From: Walledgarden Date: Sat, 22 Apr 2023 17:40:01 +0200 Subject: [PATCH] feat: Add documentation, adjust package.json (preparation for CI/CD), Replace local types by @tipccjs/tipcc-api-types --- .eslintrc.js | 22 ---- .eslintrc.json | 22 ++++ .gitignore | 3 + .prettierrc | 6 +- docs.css | 5 + package-lock.json | 123 +++++++++++++++++- package.json | 24 +++- src/index.ts | 51 ++++---- src/structures/Amount.ts | 11 +- src/structures/CryptoCurrency.ts | 6 +- ...ptoCurrencyFormat.ts => CurrencyFormat.ts} | 10 +- ...yFormatUnits.ts => CurrencyFormatUnits.ts} | 15 ++- src/structures/ExchangeRate.ts | 4 +- src/structures/FiatCurrency.ts | 23 ++++ src/structures/Transaction.ts | 8 +- src/structures/User.ts | 8 +- src/structures/Wallet.ts | 4 +- src/types/TipccApi.ts | 110 ---------------- src/utils/CacheHandler.ts | 23 ++-- tsconfig.json | 19 ++- typedoc.json | 14 ++ 21 files changed, 294 insertions(+), 217 deletions(-) delete mode 100644 .eslintrc.js create mode 100644 .eslintrc.json create mode 100644 docs.css rename src/structures/{CryptoCurrencyFormat.ts => CurrencyFormat.ts} (55%) rename src/structures/{CryptoCurrencyFormatUnits.ts => CurrencyFormatUnits.ts} (69%) create mode 100644 src/structures/FiatCurrency.ts delete mode 100644 src/types/TipccApi.ts create mode 100644 typedoc.json diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 01d7fb5..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,22 +0,0 @@ -module.exports = { - root: true, - parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint', 'import', 'prettier'], - extends: [ - 'eslint:recommended', - 'plugin:@typescript-eslint/recommended', - 'airbnb-typescript/base', - ], - parserOptions: { - project: './tsconfig.json', - }, - ignorePatterns: ['dist/**/*.js'], - rules: { - '@typescript-eslint/no-explicit-any': 'off', - '@typescript-eslint/no-var-requires': 'off', - '@typescript-eslint/no-unused-vars': 'off', - }, - env: { - node: true, - }, -}; diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..fd8fb91 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,22 @@ +{ + "root": true, + "parser": "@typescript-eslint/parser", + "plugins": ["@typescript-eslint", "import", "prettier"], + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended", + "airbnb-typescript/base" + ], + "parserOptions": { + "project": "./tsconfig.json" + }, + "ignorePatterns": ["lib/**/*.js"], + "rules": { + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-var-requires": "off", + "@typescript-eslint/no-unused-vars": "off" + }, + "env": { + "node": true + } +} diff --git a/.gitignore b/.gitignore index c6bba59..3d54c58 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,6 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +/docs +/lib \ No newline at end of file diff --git a/.prettierrc b/.prettierrc index 6f53ad1..8d6ce6a 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,5 @@ { - "tabWidth": 2, - "singleQuote": true, - "trailingComma": "all" + "tabWidth": 2, + "singleQuote": true, + "trailingComma": "all" } diff --git a/docs.css b/docs.css new file mode 100644 index 0000000..716dbbb --- /dev/null +++ b/docs.css @@ -0,0 +1,5 @@ +@import url('https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap'); + +* { + font-family: 'Inter', sans-serif; +} diff --git a/package-lock.json b/package-lock.json index 903818b..9490066 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,12 @@ "version": "1.0.3", "license": "ISC", "dependencies": { + "@mxssfd/typedoc-theme": "^1.1.1", + "@tipccjs/tipcc-api-types": "^1.0.3", "@types/node": "^18.15.11", "axios": "^1.3.4", - "bignumber.js": "^9.1.1" + "bignumber.js": "^9.1.1", + "typedoc": "^0.24.4" }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.57.0", @@ -114,6 +117,17 @@ "integrity": "sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==", "dev": true }, + "node_modules/@mxssfd/typedoc-theme": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@mxssfd/typedoc-theme/-/typedoc-theme-1.1.1.tgz", + "integrity": "sha512-XhXy2MDoMJFeHgbE0cTxpB3K8AizQMJcm6gvf9lqNwfN6+ZfDnmIZub6lCuDgpL0O0fvckKTq7fGSP6XIU/17Q==", + "engines": { + "node": ">= 14" + }, + "peerDependencies": { + "typedoc": "^0.24.1" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -149,6 +163,14 @@ "node": ">= 8" } }, + "node_modules/@tipccjs/tipcc-api-types": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tipccjs/tipcc-api-types/-/tipcc-api-types-1.0.3.tgz", + "integrity": "sha512-rtjcDI52mm9kVo3QQzxj4KcfdX+Q+2x/o0sJbagAoDMS/eFlEd8WtkYlh654VnszMWmQ6fiCsnQ+nDFSSG9fgQ==", + "dependencies": { + "typescript": "^5.0.4" + } + }, "node_modules/@types/json-schema": { "version": "7.0.11", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz", @@ -406,6 +428,11 @@ "node": ">=8" } }, + "node_modules/ansi-sequence-parser": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/ansi-sequence-parser/-/ansi-sequence-parser-1.1.0.tgz", + "integrity": "sha512-lEm8mt52to2fT8GhciPCGeCXACSz2UwIN4X2e2LJSnZ5uAbn2/dsYdOmUXq0AtWS5cpAupysIneExOgH0Vd2TQ==" + }, "node_modules/ansi-styles": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", @@ -534,8 +561,7 @@ "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", - "dev": true + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, "node_modules/bignumber.js": { "version": "9.1.1", @@ -1956,6 +1982,11 @@ "json5": "lib/cli.js" } }, + "node_modules/jsonc-parser": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz", + "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==" + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -2002,6 +2033,22 @@ "node": ">=10" } }, + "node_modules/lunr": { + "version": "2.3.9", + "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.9.tgz", + "integrity": "sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==" + }, + "node_modules/marked": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/marked/-/marked-4.3.0.tgz", + "integrity": "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==", + "bin": { + "marked": "bin/marked.js" + }, + "engines": { + "node": ">= 12" + } + }, "node_modules/merge2": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", @@ -2482,6 +2529,17 @@ "node": ">=8" } }, + "node_modules/shiki": { + "version": "0.14.1", + "resolved": "https://registry.npmjs.org/shiki/-/shiki-0.14.1.tgz", + "integrity": "sha512-+Jz4nBkCBe0mEDqo1eKRcCdjRtrCjozmcbTUjbPTX7OOJfEbTZzlUWlZtGe3Gb5oV1/jnojhG//YZc3rs9zSEw==", + "dependencies": { + "ansi-sequence-parser": "^1.1.0", + "jsonc-parser": "^3.2.0", + "vscode-oniguruma": "^1.7.0", + "vscode-textmate": "^8.0.0" + } + }, "node_modules/side-channel": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", @@ -2696,11 +2754,52 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/typedoc": { + "version": "0.24.4", + "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.24.4.tgz", + "integrity": "sha512-vQuliyGhJEGeKzzCFHbkS3m0gHoIL6cfr0fHf6eX658iGELtq2J9mWe0b+X5McEYgFoMuHFt5Py3Zug6Sxjn/Q==", + "dependencies": { + "lunr": "^2.3.9", + "marked": "^4.3.0", + "minimatch": "^9.0.0", + "shiki": "^0.14.1" + }, + "bin": { + "typedoc": "bin/typedoc" + }, + "engines": { + "node": ">= 14.14" + }, + "peerDependencies": { + "typescript": "4.6.x || 4.7.x || 4.8.x || 4.9.x || 5.0.x" + } + }, + "node_modules/typedoc/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/typedoc/node_modules/minimatch": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.0.tgz", + "integrity": "sha512-0jJj8AvgKqWN05mrwuqi8QYKx1WmYSUoKSxu5Qhs9prezTz10sxAHGNZe9J9cqIJzta8DWsleh2KaVaLl6Ru2w==", + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/typescript": { - "version": "5.0.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.3.tgz", - "integrity": "sha512-xv8mOEDnigb/tN9PSMTwSEqAnUvkoXMQlicOb0IUVDBSQCgBSaAAROUZYy2IcUy5qU6XajK5jjjO7TMWqBTKZA==", - "dev": true, + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz", + "integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -2733,6 +2832,16 @@ "punycode": "^2.1.0" } }, + "node_modules/vscode-oniguruma": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/vscode-oniguruma/-/vscode-oniguruma-1.7.0.tgz", + "integrity": "sha512-L9WMGRfrjOhgHSdOYgCt/yRMsXzLDJSL7BPrOZt73gU0iWO4mpqzqQzOz5srxqTvMBaR0XZTSrVWo4j55Rc6cA==" + }, + "node_modules/vscode-textmate": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/vscode-textmate/-/vscode-textmate-8.0.0.tgz", + "integrity": "sha512-AFbieoL7a5LMqcnOF04ji+rpXadgOXnZsxQr//r83kLPr7biP7am3g9zbaZIaBGwBRWeSvoMD4mgPdX3e4NWBg==" + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 75c0ec1..4070fd0 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,29 @@ { "name": "tipcc-api-client", "version": "1.0.3", - "description": "\"# tip.cc API Client\"", - "main": "index.js", + "description": "# tip.cc API Client", + "main": "lib/src/index.js", + "types": "lib/src/index.d.ts", "files": [ - "dist" + "lib/**/*", + "LICENSE", + "README.md", + "package.json", + "tsconfig.json", + "package-lock.json" ], "scripts": { "lint": "eslint . --ext .js,.jsx,.ts,.tsx --fix", "prettify": "prettier --write .", "format": "prettier --write . && eslint . --ext .js,.jsx,.ts,.tsx --fix", "test": "echo \"Error: no test specified\" && exit 1", - "build": "tsc" + "build": "tsc", + "build:docs": "typedoc" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { - "typescript": "^5.0.3", "@typescript-eslint/eslint-plugin": "^5.57.0", "@typescript-eslint/parser": "^5.57.0", "eslint": "^8.37.0", @@ -25,11 +31,15 @@ "eslint-config-prettier": "^8.8.0", "eslint-plugin-import": "^2.27.5", "eslint-plugin-prettier": "^4.2.1", - "prettier": "2.8.7" + "prettier": "2.8.7", + "typescript": "^5.0.3" }, "dependencies": { + "@mxssfd/typedoc-theme": "^1.1.1", + "@tipccjs/tipcc-api-types": "^1.0.3", "@types/node": "^18.15.11", "axios": "^1.3.4", - "bignumber.js": "^9.1.1" + "bignumber.js": "^9.1.1", + "typedoc": "^0.24.4" } } diff --git a/src/index.ts b/src/index.ts index 55a89ad..8593474 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,5 @@ import { EventEmitter } from 'node:events'; import RequestHandler from './structures/RequestHandler'; -import { - APIRESTGetAccountTransactions, - APIRESTGetCurrenciesRates, - APIRESTGetTransaction, - APIRESTGetWallets, - APIRESTPostTipPayload, - APIRESTPostTips, -} from './types/TipccApi'; import Transaction from './structures/Transaction'; import { getCachedCryptoCurrencies, @@ -18,7 +10,16 @@ import { updateCurrenciesCache } from './utils/CacheHandler'; import CryptoCurrency from './structures/CryptoCurrency'; import ExchangeRate from './structures/ExchangeRate'; import Wallet from './structures/Wallet'; -import { APIRESTGetWallet } from './types/TipccApi'; +import { + RESTGetAPIAccountTransactionResult, + RESTGetAPIAccountTransactionsResult, + RESTGetAPIAccountWalletResult, + RESTGetAPIAccountWalletsResult, + RESTGetAPICurrenciesRatesResult, + RESTPostAPITipBody, + RESTPostAPITipResult, + Routes, +} from '@tipccjs/tipcc-api-types'; interface Events { tip: Transaction; @@ -28,7 +29,7 @@ interface Events { /** * A tip.cc client to interact with the API. */ -export default class TipccClient extends EventEmitter { +class TipccClient extends EventEmitter { public token: string; public REST: RequestHandler; @@ -97,7 +98,7 @@ export default class TipccClient extends EventEmitter { types: [...this.polling], since: this.lastPoll.toISOString(), until: now.toISOString(), - })) as APIRESTGetAccountTransactions + })) as RESTGetAPIAccountTransactionsResult ).transactions; break; @@ -186,9 +187,9 @@ export default class TipccClient extends EventEmitter { ): Promise { const { transactions } = (await this.REST.request( 'GET', - '/account/transactions', + Routes.accountWalletTransactions(), options, - )) as APIRESTGetAccountTransactions; + )) as RESTGetAPIAccountTransactionsResult; return transactions.map((t) => new Transaction(t)); } @@ -198,8 +199,8 @@ export default class TipccClient extends EventEmitter { public async getExchangeRates(): Promise { const { rates } = (await this.REST.request( 'GET', - '/exchange/rates', - )) as APIRESTGetCurrenciesRates; + Routes.currenciesRates(), + )) as RESTGetAPICurrenciesRatesResult; return rates.map((r) => new ExchangeRate(r)); } @@ -210,8 +211,8 @@ export default class TipccClient extends EventEmitter { public async getTransaction(id: string): Promise { const { transaction } = (await this.REST.request( 'GET', - `/account/transactions/${id}`, - )) as APIRESTGetTransaction; + Routes.accountWalletTransaction(id), + )) as RESTGetAPIAccountTransactionResult; if (!transaction) return null; return new Transaction(transaction); } @@ -221,13 +222,13 @@ export default class TipccClient extends EventEmitter { * @param payload The post tip payload */ public async postTip( - payload: APIRESTPostTipPayload, - ): Promise { + payload: RESTPostAPITipBody, + ): Promise { return (await this.REST.request( 'POST', '/tips', payload, - )) as APIRESTPostTips; + )) as RESTPostAPITipResult; } /** @@ -241,8 +242,8 @@ export default class TipccClient extends EventEmitter { ): Promise { const result = (await this.REST.request( 'GET', - `/account/wallets/${currency}`, - )) as APIRESTGetWallet | null; + Routes.accountWallet(currency), + )) as RESTGetAPIAccountWalletResult | null; if (!result && !fallback) return null; return new Wallet( result ?? { @@ -266,8 +267,10 @@ export default class TipccClient extends EventEmitter { public async getWallets(): Promise { const { wallets } = (await this.REST.request( 'GET', - '/account/wallets', - )) as APIRESTGetWallets; + Routes.accountWallets(), + )) as RESTGetAPIAccountWalletsResult; return wallets.map((w) => new Wallet(w)); } } + +export default TipccClient; diff --git a/src/structures/Amount.ts b/src/structures/Amount.ts index 48fea32..63331ee 100644 --- a/src/structures/Amount.ts +++ b/src/structures/Amount.ts @@ -1,10 +1,12 @@ import BigNumber from 'bignumber.js'; +import type { APICoin, APIMonetary } from '@tipccjs/tipcc-api-types'; + import CryptoCurrency from './CryptoCurrency'; -import { ApiAmount } from '../types/TipccApi'; import { getCachedCryptoCurrency, getCachedFiatCurrency, } from '../utils/CacheHandler'; +import FiatCurrency from './FiatCurrency'; /** * A class for storing an API amount. This can be used for either fiats or cryptocurrencies. @@ -12,14 +14,17 @@ import { export default class Amount { public value: BigNumber; - public currency?: CryptoCurrency; + public currency?: CryptoCurrency | FiatCurrency; /** * Create an Amount. * @param payload An amount from the API * @param currencyType The type of currency */ - constructor(payload: ApiAmount, currencyType: 'fiat' | 'crypto' = 'crypto') { + constructor( + payload: APIMonetary | APICoin, + currencyType: 'fiat' | 'crypto' = 'crypto', + ) { this.value = BigNumber(payload.value); switch (currencyType) { case 'crypto': diff --git a/src/structures/CryptoCurrency.ts b/src/structures/CryptoCurrency.ts index 8efe890..a499daa 100644 --- a/src/structures/CryptoCurrency.ts +++ b/src/structures/CryptoCurrency.ts @@ -1,5 +1,5 @@ -import { ApiCurrency } from '../types/TipccApi'; -import CryptoCurrencyFormat from './CryptoCurrencyFormat'; +import type { APICryptoCurrency } from '@tipccjs/tipcc-api-types'; +import CryptoCurrencyFormat from './CurrencyFormat'; /** * A class for storing an API cryptocurrency. @@ -19,7 +19,7 @@ export default class CryptoCurrency { * Create a CryptoCurrency. * @param payload The currency from the API */ - constructor(payload: ApiCurrency) { + constructor(payload: APICryptoCurrency) { this.code = payload.code; this.name = payload.name; this.icon = payload.icon; diff --git a/src/structures/CryptoCurrencyFormat.ts b/src/structures/CurrencyFormat.ts similarity index 55% rename from src/structures/CryptoCurrencyFormat.ts rename to src/structures/CurrencyFormat.ts index 2c182cc..5d46fd3 100644 --- a/src/structures/CryptoCurrencyFormat.ts +++ b/src/structures/CurrencyFormat.ts @@ -1,10 +1,10 @@ -import { ApiCurrencyFormat } from '../types/TipccApi'; -import CryptoCurrencyUnit from './CryptoCurrencyFormatUnits'; +import { APICryptoCurrency, APIFiatCurrency } from '@tipccjs/tipcc-api-types'; +import CryptoCurrencyUnit from './CurrencyFormatUnits'; /** * A class for storing an API cryptocurrency format. */ -export default class CryptoCurrencyFormat { +export default class CurrencyFormat { public scale: number; public units: CryptoCurrencyUnit[]; @@ -13,7 +13,9 @@ export default class CryptoCurrencyFormat { * Create a CryptoCurrencyFormat. * @param payload The format from the API */ - constructor(payload: ApiCurrencyFormat) { + constructor( + payload: APIFiatCurrency['format'] | APICryptoCurrency['format'], + ) { this.scale = payload.scale; this.units = payload.units.map((unit) => new CryptoCurrencyUnit(unit)); } diff --git a/src/structures/CryptoCurrencyFormatUnits.ts b/src/structures/CurrencyFormatUnits.ts similarity index 69% rename from src/structures/CryptoCurrencyFormatUnits.ts rename to src/structures/CurrencyFormatUnits.ts index 57b4d4c..187c645 100644 --- a/src/structures/CryptoCurrencyFormatUnits.ts +++ b/src/structures/CurrencyFormatUnits.ts @@ -1,9 +1,12 @@ -import { ApiCurrencyFormatUnit } from '../types/TipccApi'; +import type { + APICryptoCurrencyUnit, + APIFiatCurrencyUnit, +} from '@tipccjs/tipcc-api-types'; /** * A class for storing an API cryptocurrency unit. */ -export default class CryptoCurrencyUnit { +export default class CurrencyUnit { public singular: string; public plural?: string; @@ -16,17 +19,17 @@ export default class CryptoCurrencyUnit { public aliases: string[]; - public minDecimals: number; + public minDecimals?: number; - public optionalDecimals: number; + public optionalDecimals?: boolean; - public min: number; + public min?: number; /** * Create a CryptoCurrencyUnit. * @param payload The format unit from the API */ - constructor(payload: ApiCurrencyFormatUnit) { + constructor(payload: APIFiatCurrencyUnit | APICryptoCurrencyUnit) { this.singular = payload.singular; this.plural = payload.plural ?? undefined; this.prefix = payload.prefix; diff --git a/src/structures/ExchangeRate.ts b/src/structures/ExchangeRate.ts index 30b9762..a8af0fb 100644 --- a/src/structures/ExchangeRate.ts +++ b/src/structures/ExchangeRate.ts @@ -1,4 +1,4 @@ -import { ApiRate } from '../types/TipccApi'; +import type { APIExchangeRate } from '@tipccjs/tipcc-api-types'; import Amount from './Amount'; /** @@ -15,7 +15,7 @@ export default class ExchangeRate { * Create an ExchangeRate. * @param payload The rate from the API */ - constructor(payload: ApiRate) { + constructor(payload: APIExchangeRate) { this.code = payload.code; this.name = payload.name; if (payload.usd_value) diff --git a/src/structures/FiatCurrency.ts b/src/structures/FiatCurrency.ts new file mode 100644 index 0000000..8c0dc97 --- /dev/null +++ b/src/structures/FiatCurrency.ts @@ -0,0 +1,23 @@ +import type { APIFiatCurrency } from '@tipccjs/tipcc-api-types'; +import CurrencyFormat from './CurrencyFormat'; + +/** + * A class for storing an API cryptocurrency. + */ +export default class FiatCurrency { + public code: string; + + public name: string; + + public format: CurrencyFormat; + + /** + * Create a CryptoCurrency. + * @param payload The currency from the API + */ + constructor(payload: APIFiatCurrency) { + this.code = payload.code; + this.name = payload.name; + this.format = new CurrencyFormat(payload.format); + } +} diff --git a/src/structures/Transaction.ts b/src/structures/Transaction.ts index aa1fc02..984a951 100644 --- a/src/structures/Transaction.ts +++ b/src/structures/Transaction.ts @@ -1,4 +1,4 @@ -import { ApiTransaction } from '../types/TipccApi'; +import type { APITransaction } from '@tipccjs/tipcc-api-types'; import Amount from './Amount'; import User from './User'; @@ -32,13 +32,13 @@ export default class Transaction { * Create a Transaction. * @param payload The transaction from the API */ - constructor(payload: ApiTransaction) { + constructor(payload: APITransaction) { this.id = payload.id; this.type = payload.type; this.amount = new Amount(payload.amount); this.fee = payload.fee ? new Amount(payload.fee) : null; - this.usdValue = payload.usdValue - ? new Amount(payload.usdValue, 'fiat') + this.usdValue = payload.usd_value + ? new Amount(payload.usd_value, 'fiat') : null; this.service = payload.service; this.chatId = payload.chat_id; diff --git a/src/structures/User.ts b/src/structures/User.ts index 8eabe60..a8fe991 100644 --- a/src/structures/User.ts +++ b/src/structures/User.ts @@ -1,4 +1,4 @@ -import { ApiUser } from '../types/TipccApi'; +import type { APIConnection } from '@tipccjs/tipcc-api-types'; /** * A class for storing an API user. @@ -6,9 +6,9 @@ import { ApiUser } from '../types/TipccApi'; export default class User { public identifier: string; - public username: string; + public username?: string; - public avatarUrl: string; + public avatarUrl?: string; public service: 'discord'; @@ -16,7 +16,7 @@ export default class User { * Create a User. * @param payload The user from the API */ - constructor(payload: ApiUser) { + constructor(payload: APIConnection) { this.identifier = payload.identifier; this.username = payload.username; this.avatarUrl = payload.avatar_url; diff --git a/src/structures/Wallet.ts b/src/structures/Wallet.ts index 0a82f4a..067b825 100644 --- a/src/structures/Wallet.ts +++ b/src/structures/Wallet.ts @@ -1,4 +1,4 @@ -import { ApiWallet } from '../types/TipccApi'; +import { APIWallet } from '@tipccjs/tipcc-api-types'; import Amount from './Amount'; /** @@ -17,7 +17,7 @@ export default class Wallet { * Create a Wallet. * @param payload The wallet from the API */ - constructor(payload: ApiWallet) { + constructor(payload: APIWallet) { this.code = payload.code; this.name = payload.name; this.balance = new Amount(payload.balance); diff --git a/src/types/TipccApi.ts b/src/types/TipccApi.ts deleted file mode 100644 index 51993dc..0000000 --- a/src/types/TipccApi.ts +++ /dev/null @@ -1,110 +0,0 @@ -import Amount from '../structures/Amount'; - -export interface ApiUser { - identifier: string; - username: string; - avatar_url: string; - service: 'discord'; -} - -export interface ApiAmount { - value: string; - currency: string; -} - -export interface ApiTip { - id: string; - recipient: ApiUser; - amount: ApiAmount; -} - -export interface ApiWallet { - code: string; - name: string; - balance: ApiAmount; - usd_value: ApiAmount | null; -} - -export interface ApiTransaction { - id: string; - type: 'tip' | 'withdrawal' | 'deposit'; - amount: ApiAmount; - fee: ApiAmount | null; - usdValue: ApiAmount | null; - service: 'discord'; - chat_id: string; - subchat_id: string; - sender: ApiUser; - recipient: ApiUser; - created: string; -} - -export interface ApiCurrencyFormatUnit { - singular: string; - plural?: string; - prefix?: string; - suffix?: string; - scale: number; - aliases?: string[]; - minDecimals: number; - optionalDecimals: number; - min: number; -} - -export interface ApiCurrencyFormat { - scale: number; - units: ApiCurrencyFormatUnit[]; - code: string; -} - -export interface ApiCurrency { - code: string; - name: string; - color: string; - icon: string; - explorer: string; - format: ApiCurrencyFormat; -} - -export interface ApiRate { - code: string; - name: string; - usd_value: ApiAmount; -} - -export type APIRESTGetAccountTransactions = { - transactions: ApiTransaction[]; -}; - -export type APIRESTGetCurrenciesCryptocurrencies = { - cryptocurrencies: ApiCurrency[]; -}; - -export type APIRESTGetCurrentciesFiats = { - fiats: ApiCurrency[]; -}; - -export type APIRESTGetCurrenciesRates = { - rates: ApiRate[]; -}; - -export type APIRESTPostTips = { - tips: ApiTip[]; - total: ApiAmount; -}; - -export type APIRESTGetTransaction = { - transaction: ApiTransaction; -}; - -export type APIRESTPostTipPayload = { - service?: 'discord'; - recipient: string; - amount: ApiAmount | Amount; -}; - -export type APIRESTGetWallets = { - wallets: ApiWallet[]; -}; - -export type APIRESTGetWallet = ApiWallet; diff --git a/src/utils/CacheHandler.ts b/src/utils/CacheHandler.ts index 4b8f0d2..feb3239 100644 --- a/src/utils/CacheHandler.ts +++ b/src/utils/CacheHandler.ts @@ -1,12 +1,13 @@ +import { + RESTGetAPICurrenciesCryptoCurrenciesResult, + RESTGetAPICurrenciesFiatsResult, +} from '@tipccjs/tipcc-api-types'; import TipccClient from '../'; import CryptoCurrency from '../structures/CryptoCurrency'; -import { - APIRESTGetCurrenciesCryptocurrencies, - APIRESTGetCurrentciesFiats, -} from '../types/TipccApi'; +import FiatCurrency from '../structures/FiatCurrency'; const cryptoCurrenciesCache: CryptoCurrency[] = []; -const fiatCurrenciesCache: CryptoCurrency[] = []; +const fiatCurrenciesCache: FiatCurrency[] = []; export const updateCurrenciesCache = async ( client: TipccClient, @@ -14,7 +15,7 @@ export const updateCurrenciesCache = async ( const { cryptocurrencies } = (await client.REST.request( 'GET', '/currencies/cryptocurrencies', - )) as APIRESTGetCurrenciesCryptocurrencies; + )) as RESTGetAPICurrenciesCryptoCurrenciesResult; cryptoCurrenciesCache.splice( 0, cryptoCurrenciesCache.length, @@ -34,16 +35,14 @@ export const updateFiatCurrenciesCache = async ( const { fiats } = (await client.REST.request( 'GET', '/currencies/fiats', - )) as APIRESTGetCurrentciesFiats; + )) as RESTGetAPICurrenciesFiatsResult; fiatCurrenciesCache.splice( 0, fiatCurrenciesCache.length, - ...fiats.map((c) => new CryptoCurrency(c)), + ...fiats.map((c) => new FiatCurrency(c)), ); }; -export const getCachedFiatCurrency = ( - code: string, -): CryptoCurrency | undefined => +export const getCachedFiatCurrency = (code: string): FiatCurrency | undefined => fiatCurrenciesCache.find((c) => c.code === code); -export const getCachedFiatCurrencies = (): CryptoCurrency[] => +export const getCachedFiatCurrencies = (): FiatCurrency[] => fiatCurrenciesCache; diff --git a/tsconfig.json b/tsconfig.json index cbc0edd..0d0b50a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,9 +3,20 @@ "target": "ES2022", "module": "commonjs", "declaration": true, - "outDir": "./dist", - "strict": true + "outDir": "./lib", + "strict": true, + "alwaysStrict": true, + "lib": ["ES2022"], + "noUnusedParameters": true, + "sourceMap": true, + "declarationMap": true, + "noUnusedLocals": true, + "removeComments": false, + "strictNullChecks": true, + "preserveConstEnums": true, + "exactOptionalPropertyTypes": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true }, - "include": ["src/**/*", ".eslintrc.js"], - "exclude": ["node_modules"] + "exclude": ["node_modules", "docs", "lib"] } diff --git a/typedoc.json b/typedoc.json new file mode 100644 index 0000000..944d5cf --- /dev/null +++ b/typedoc.json @@ -0,0 +1,14 @@ +{ + "entryPoints": ["./src/**/*.ts"], + "out": "docs", + "name": "tipcc.js Documentation", + "cname": "tipccjs.org", + "hideGenerator": true, + "navigationLinks": { + "GitHub": "https://github.com/tipccjs/tipcc.js" + }, + "defaultCategory": "Classes", + "categorizeByGroup": true, + "customCss": "./docs.css", + "githubPages": false +}