From 231c858696b4069c0a49410f4836155de4893020 Mon Sep 17 00:00:00 2001 From: Rastislav <rastislav@onion.email> Date: Fri, 16 Aug 2024 11:43:11 +0200 Subject: [PATCH] Fixes --- .eslintrc.json | 15 +++++++++++++++ README.md | 40 ++++++++++++++++++++++++++++++++++++---- eslint.config.js | 23 +++++++++++++++++++++++ package.json | 11 ++++++----- src/index.ts | 47 +++++++++++++++++++++++++++++------------------ 5 files changed, 109 insertions(+), 27 deletions(-) create mode 100644 .eslintrc.json create mode 100644 eslint.config.js diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..f915958 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,15 @@ +{ + "parser": "@typescript-eslint/parser", + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/recommended" + ], + "parserOptions": { + "ecmaVersion": 2020, + "sourceType": "module" + }, + "rules": { + "semi": ["error", "always"], + "quotes": ["error", "single"] + } +} diff --git a/README.md b/README.md index c534e0a..5ec5a6d 100644 --- a/README.md +++ b/README.md @@ -104,13 +104,13 @@ The `sms` function has been extended to support multiple numbers. You can now pr ### Using NPM -```sh +```bash npm i txms.js ``` ### Using Yarn -```sh +```bash yarn add txms.js ``` @@ -120,7 +120,7 @@ yarn add txms.js #### ES Module Syntax (Recommended for Modern JavaScript/TypeScript) -```ts +```typescript import txms from 'txms.js'; let encoded = txms.encode(hex); let decoded = txms.decode(string); @@ -128,7 +128,7 @@ let decoded = txms.decode(string); #### CommonJS Syntax (Legacy Support) -```js +```javascript var txms = require('txms.js').default; var encoded = txms.encode(hex); var decoded = txms.decode(string); @@ -181,6 +181,38 @@ txms {type} {value} {location} echo {value} | txms {type} {location} ``` +## Extending Aliases and Countries + +The `aliases` and `countries` objects in `txms.js` are designed to be extendable, allowing you to add new networks and countries as needed. + +### Extending Aliases + +To add a new alias for a network, you can use the `addAlias` function: + +```typescript +import { addAlias } from 'txms.js'; + +// Add a new alias +addAlias('testnet', 2); +``` + +This will allow you to use testnet as an alias for the network with ID `2`. + +### Extending Countries + +To add new country codes and phone numbers for a specific network, use the addCountry function: + +```typescript +import { addCountry } from 'txms.js'; + +// Add new country codes and phone numbers for the testnet +addCountry(2, 'uk', ['+441234567890']); +``` + +This will associate the UK country code (`'uk'`) and the phone number `+441234567890` with the network ID `2`. + +These utility functions make it easy to customize `txms.js` to support additional networks and countries based on your needs. + ## Tests Unit tests are included and can be executed with the command `yarn test` or `npm run test`. diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..d18b4d2 --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,23 @@ +import js from '@eslint/js'; +import typescriptPlugin from '@typescript-eslint/eslint-plugin'; +import typescriptParser from '@typescript-eslint/parser'; + +export default [ + { + files: ['src/**/*.{js,ts}'], + languageOptions: { + parser: typescriptParser, + ecmaVersion: 'latest', + sourceType: 'module', + }, + plugins: { + '@typescript-eslint': typescriptPlugin, + }, + rules: { + ...js.configs.recommended.rules, + ...typescriptPlugin.configs.recommended.rules, + 'semi': ['error', 'always'], + 'quotes': ['error', 'single'], + }, + }, +]; diff --git a/package.json b/package.json index f490cdc..28b618f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "txms.js", - "version": "1.2.1", + "version": "1.2.2", "description": "Transaction messaging service protocol", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -13,7 +13,7 @@ "test": "npm run unit", "preunit": "npm run build", "unit": "tape test/*.js", - "lint": "standard '**/*.js' --fix" + "lint": "eslint 'src/**/*.{js,ts}' --fix" }, "engines": { "node": ">= 14" @@ -47,9 +47,10 @@ "license": "CORE", "devDependencies": { "@types/node": "^22.3.0", - "standard": "^17.1.0", + "@typescript-eslint/eslint-plugin": "^8.1.0", + "@typescript-eslint/parser": "^8.1.0", + "eslint": "^9.9.0", "tape": "^5.8.1", "typescript": "^5.5.4" - }, - "packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447" + } } diff --git a/src/index.ts b/src/index.ts index 69145c7..c1f6f8f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,22 @@ -const aliases: { [key: string]: number } = { +export interface Transport { + encode(hex: string): string; + decode(data: string): string; + getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> }; + sms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; + mms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; + generateMessageUri(type: 'sms' | 'mms', number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; +} + +export interface Error extends globalThis.Error { + code?: number; +} + +export const aliases: { [key: string]: number } = { 'mainnet': 1, 'devin': 3, }; -const countries: Record<string, { [key: string]: string[] }> = { +export const countries: Record<string, { [key: string]: string[] }> = { '1': { 'global': ['+12019715152'], 'us': ['+12019715152'], @@ -14,7 +27,19 @@ const countries: Record<string, { [key: string]: string[] }> = { }, }; -const txms: txms.Transport = { +export function addAlias(name: string, id: number): void { + aliases[name] = id; +} + +export function addCountry(networkId: number | string, countryCode: string, phoneNumbers: string[]): void { + const networkKey = networkId.toString(); + if (!countries[networkKey]) { + countries[networkKey] = {}; + } + countries[networkKey][countryCode] = phoneNumbers; +} + +const txms: Transport = { encode(hex: string): string { let data = ''; if (hex.substring(0, 2).toLowerCase() === '0x') { @@ -23,7 +48,7 @@ const txms: txms.Transport = { const hextest = /^[0-9a-fA-F]+$/; if (!hextest.test(hex)) { const errorHex = new Error('Not a hex format'); - (errorHex as txms.Error).code = 415; + (errorHex as Error).code = 415; throw errorHex; } while (hex.length % 4 !== 0) { @@ -135,17 +160,3 @@ const txms: txms.Transport = { }; export default txms; - -declare namespace txms { - interface Transport { - encode(hex: string): string; - decode(data: string): string; - getEndpoint(network?: number | string, countriesList?: string | Array<string>): { [key: string]: Array<string> }; - sms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; - mms(number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; - generateMessageUri(type: 'sms' | 'mms', number?: boolean | string | number | Array<string>, message?: string, network?: number | string, encodeMessage?: boolean, platform?: string): string; - } - interface Error extends globalThis.Error { - code?: number; - } -}