bitpay-rates 1.2.17 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  A lightweight Node.js wrapper for [BitPay's](https://bitpay.com/rates) exchange rates API, now in TypeScript.
8
8
 
9
- Zero-dependency, `promise` and `callback` support for easy integration into your project. ✨
9
+ Zero-dependency and `promise` support for easy integration into your project. ✨
10
10
 
11
11
  ## Requirements
12
12
 
@@ -19,13 +19,27 @@ Getting a rate by `code`:
19
19
  ```js
20
20
  import bitpayRates from 'bitpay-rates';
21
21
 
22
- const code = 'ARS'; // see list of codes bellow
22
+ const code = 'ARS'; // see list of codes below
23
23
 
24
- // Using promise
24
+ // Using async/await
25
+ try {
26
+ const rate = await bitpayRates.get(code);
27
+ console.log(`[Async/Await][${code}] Rate:`, rate);
28
+ } catch (err) {
29
+ console.error(`[Async/Await][${code}] Error:`, err);
30
+ }
31
+ ```
32
+
33
+ Handling an invalid currency code:
34
+
35
+ ```js
36
+ import bitpayRates from 'bitpay-rates';
37
+
38
+ // Handling an invalid currency code
25
39
  bitpayRates
26
- .get(code)
27
- .then((rate) => console.log('Promise Rate:', rate))
28
- .catch((err) => console.error('Promise Error:', err));
40
+ .get('INVALID')
41
+ .then((rate) => console.log('[Promise][INVALID] Rate:', rate))
42
+ .catch((err) => console.error('[Promise][INVALID] Error:', err));
29
43
  ```
30
44
 
31
45
  Successful response:
@@ -34,7 +48,7 @@ Successful response:
34
48
  {
35
49
  "code": "ARS",
36
50
  "name": "Argentine Peso",
37
- "rate": 3793422.92
51
+ "rate": 60612542.16
38
52
  }
39
53
  ```
40
54
 
@@ -43,11 +57,19 @@ Getting `all` the rates:
43
57
  ```js
44
58
  import bitpayRates from 'bitpay-rates';
45
59
 
46
- // Using callback
47
- bitpayRates.get((err, res) => {
48
- console.error('Callback Error:', err);
49
- console.log('Callback Rates:', res);
50
- });
60
+ // Using async/await
61
+ try {
62
+ const rates = await bitpayRates.get();
63
+ console.log('[Async/Await] Rates:', rates);
64
+ } catch (err) {
65
+ console.error('[Async/Await] Error:', err);
66
+ }
67
+
68
+ // Handling an invalid currency code
69
+ bitpayRates
70
+ .get('INVALID')
71
+ .then((rate) => console.log('[Promise][INVALID] Rate:', rate))
72
+ .catch((err) => console.error('[Promise][INVALID] Error:', err));
51
73
  ```
52
74
 
53
75
  Successful response:
@@ -70,7 +92,7 @@ Successful response:
70
92
 
71
93
  More examples [here](example/rates-example.js).
72
94
 
73
- ## Available Codes (updated: 2023-01-11)
95
+ ## Available Codes (updated: 2024-01-24)
74
96
 
75
97
  [Follow this link](CODES.md) to see the complete list of codes.
76
98
 
package/dist/index.d.ts CHANGED
@@ -1 +1,11 @@
1
- export type RateObj={code:string;name:string;rate:number;};export type RateResponse=RateObj|[RateObj];export type Callback=(error:Error|null,data?:RateResponse)=>void;export declare const get:(code?:string|Callback,callback?:Callback)=>Promise<RateResponse>|void;declare const _default:{get:(code?:string|Callback|undefined,callback?:Callback|undefined)=>void|Promise<RateResponse>;};export default _default;
1
+ export type RateObj = {
2
+ code: string;
3
+ name: string;
4
+ rate: number;
5
+ };
6
+ export type RateResponse = RateObj | RateObj[];
7
+ export type Callback = (err: Error | null, data?: RateResponse) => void;
8
+ export declare function get(code?: string): Promise<RateResponse>;
9
+ export declare function get(code: string, cb: Callback): void;
10
+ export declare function get(cb: Callback): void;
11
+ export default get;
package/dist/index.js CHANGED
@@ -1 +1,39 @@
1
- "use strict";var __importDefault=(this&&this.__importDefault)||function(mod){return(mod&&mod.__esModule)?mod:{"default":mod};};Object.defineProperty(exports,"__esModule",{value:true});exports.get=void 0;const https_1=__importDefault(require("https"));const defaultOptions={host:'bitpay.com',path:'/rates',headers:{},agent:false,};const returnPromise=(options)=>{return new Promise((resolve,reject)=>{returnCallback(options,(err,data)=>{if(err)return reject(err);return resolve(data);});});};const returnCallback=(options,callback)=>{https_1.default.get(options,(res)=>{let dataBuffer='';res.on('data',(chunk)=>{dataBuffer+=chunk.toString('utf8');});res.on('end',()=>{try{const{data}=JSON.parse(dataBuffer);return callback(null,data);}catch(err){return callback(err);}});}).on('error',(err)=>{return callback(err);});};const get=(code,callback)=>{const options={...defaultOptions};if(typeof code==='string'){options.path+=`/${code.toUpperCase()}`;}if(typeof code==='function'){return returnCallback(options,code);}else if(callback){return returnCallback(options,callback);}else{return returnPromise(options);}};exports.get=get;exports.default={get:exports.get};
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.get = get;
7
+ const https_1 = __importDefault(require("https"));
8
+ function get(codeOrCb, cb) {
9
+ const code = typeof codeOrCb === 'string' ? codeOrCb : undefined;
10
+ const callback = typeof codeOrCb === 'function' ? codeOrCb : cb;
11
+ const p = new Promise((resolve, reject) => {
12
+ https_1.default
13
+ .get(`https://bitpay.com/api/rates${code ? `/${code.toUpperCase()}` : ''}`, (res) => {
14
+ let d = '';
15
+ res.on('data', (c) => (d += c));
16
+ res.on('end', () => {
17
+ var _a;
18
+ try {
19
+ const json = JSON.parse(d);
20
+ if (json.error)
21
+ reject(new Error(json.error));
22
+ else
23
+ resolve((_a = json.data) !== null && _a !== void 0 ? _a : json);
24
+ }
25
+ catch (e) {
26
+ reject(e);
27
+ }
28
+ });
29
+ })
30
+ .on('error', reject);
31
+ });
32
+ if (callback) {
33
+ p.then((data) => callback(null, data)).catch((err) => callback(err));
34
+ }
35
+ else {
36
+ return p;
37
+ }
38
+ }
39
+ exports.default = get;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "bitpay-rates",
3
- "version": "1.2.17",
3
+ "version": "2.0.0",
4
4
  "description": "A tiny Node.js wrapper for the BitPay Rates API",
5
5
  "engines": {
6
- "node": ">=12"
6
+ "node": ">=18"
7
7
  },
8
8
  "keywords": [
9
9
  "bitcoin",
@@ -28,33 +28,26 @@
28
28
  "test": "jest",
29
29
  "test:watch": "jest --watch",
30
30
  "test:coverage": "jest --coverage",
31
- "build": "tsc --declaration && tsc --removeComments && npm run minifyDeclarations && npm run minify",
31
+ "build": "tsc",
32
32
  "format": "prettier --write \"src/**/*.ts\"",
33
- "lint": "tsc --noEmit && eslint '*/**/*.{js,ts}' --quiet --fix",
34
- "minify": "jsmin -l 3 -o dist/index.js dist/index.js",
35
- "minifyDeclarations": "jsmin -l 3 -o dist/index.d.ts dist/index.d.ts",
33
+ "lint": "tsc --noEmit && eslint \"src/**/*.ts\"",
36
34
  "prepublishOnly": "npm test && npm run lint && npm run build",
37
35
  "preversion": "npm run lint",
38
36
  "version": "npm run format && git add -A src package.json",
39
37
  "postversion": "git push && git push --tags"
40
38
  },
41
39
  "devDependencies": {
42
- "@types/jest": "^26.0.21",
43
- "@types/node": "^14.14.37",
44
- "@typescript-eslint/eslint-plugin": "^4.18.0",
45
- "@typescript-eslint/parser": "^4.18.0",
46
- "eslint": "^7.21.0",
47
- "eslint-config-prettier": "^8.1.0",
48
- "eslint-plugin-prettier": "^3.3.1",
49
- "jest": "^26.6.3",
50
- "jsmin": "^1.0.1",
51
- "nock": "^13.0.11",
52
- "prettier": "^2.2.1",
53
- "ts-jest": "^26.5.4",
54
- "typescript": "^4.2.3"
55
- },
56
- "resolutions": {
57
- "json-schema": "^0.4.0"
40
+ "@types/jest": "^29.5.14",
41
+ "@types/node": "^22.10.2",
42
+ "@typescript-eslint/eslint-plugin": "^8.18.2",
43
+ "@typescript-eslint/parser": "^8.18.2",
44
+ "eslint": "^9.17.0",
45
+ "eslint-config-prettier": "^9.1.0",
46
+ "eslint-plugin-prettier": "^5.2.1",
47
+ "jest": "^29.7.0",
48
+ "prettier": "^3.4.2",
49
+ "ts-jest": "^29.2.5",
50
+ "typescript": "^5.7.2"
58
51
  },
59
52
  "files": [
60
53
  "dist/**/*"