Skip to content

Commit

Permalink
Merge pull request #28 from bchainhub/update/es-03
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
rastislavcore authored Aug 16, 2024
2 parents 2652c80 + 231c858 commit 61eccb0
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 27 deletions.
15 changes: 15 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -120,15 +120,15 @@ 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);
```

#### CommonJS Syntax (Legacy Support)

```js
```javascript
var txms = require('txms.js').default;
var encoded = txms.encode(hex);
var decoded = txms.decode(string);
Expand Down Expand Up @@ -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`.
Expand Down
23 changes: 23 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -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'],
},
},
];
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -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"
}
}
47 changes: 29 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'],
Expand All @@ -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') {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 61eccb0

Please sign in to comment.