Skip to content

Commit

Permalink
Merge pull request #17 from dsanchezcr/fix-npm
Browse files Browse the repository at this point in the history
Fix npm
  • Loading branch information
dsanchezcr committed Nov 18, 2023
2 parents ba5d469 + b70a864 commit b693b08
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 3,744 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ jobs:
npm publish --access public || echo "Version already exists on npmjs.com"
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_API_KEY }}

- name: Set up Node.js for GitHub Packages
uses: actions/setup-node@v4
with:
node-version: 'latest'
registry-url: 'https://npm.pkg.github.com'
- name: Publish to GitHub Packages
working-directory: ${{ env.npm-path }}
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The GetDollarExchangeRate and GetEuroExchangeRate methods return a tuple with th

To install ColonesExchangeRate using npm, run the following command in the Package Manager Console:
```cli
npm install @dsanchezcr/colones-exchange-rate
npm i @dsanchezcr/colonesexchangerate
```

### Usage
Expand Down
19 changes: 10 additions & 9 deletions src/npm/colonesexchangerate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const axios = require('axios');
import axios from 'axios';

class colonesexchangerate {
constructor() {
Expand Down Expand Up @@ -68,30 +68,31 @@ class colonesexchangerate {
}
}

async getDollarExchangeRate(includeDate = false) {
async getDollarExchangeRate() {
try {
const rate = await this.getExchangeRate();
return {
date: includeDate ? rate?.dolar?.venta?.fecha : null,
date: rate?.dolar?.venta?.fecha || '',
sale: rate?.dolar?.venta?.valor || 0,
purchase: rate?.dolar?.compra?.valor || 0
};
} catch (ex) {
throw new Error(`Error trying to get the dollar exchange rate. Details: ${ex}`);
throw new Error(`Error getting dollar exchange rate. Details: ${ex}`);
}
}

async getEuroExchangeRate(includeDate = false) {
async getEuroExchangeRate() {
try {
const rate = await this.getExchangeRate();
return {
date: includeDate ? rate?.euro?.fecha : null,
dollars: rate?.euro?.valor || 0
date: rate?.euro?.fecha || '',
dollars: rate?.euro?.dolares || 0,
colones: rate?.euro?.colones || 0
};
} catch (ex) {
throw new Error(`Error trying to get the euro exchange rate. Details: ${ex}`);
throw new Error(`Error getting euro exchange rate. Details: ${ex}`);
}
}
}

module.exports = colonesexchangerate;
export default colonesexchangerate;
102 changes: 49 additions & 53 deletions src/npm/colonesexchangerate.test.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
const colonesexchangerate = require('./colonesexchangerate');
import assert from 'assert';
import colonesexchangerate from './colonesexchangerate.js';

describe('colonesexchangerate', () => {
let converter;
let converter = new colonesexchangerate();

beforeEach(() => {
converter = new colonesexchangerate();
});

test('converts dollars to colones', async () => {
const amount = 100;
const result = await converter.dollarsToColones(amount);
expect(Number(result)).toBeGreaterThan(0);
});

test('converts colones to dollars', async () => {
const amount = 1000;
const result = await converter.colonesToDollars(amount);
expect(Number(result)).toBeGreaterThan(0);
});

test('converts dollars to euros', async () => {
const amount = 100;
const result = await converter.dollarsToEuros(amount);
expect(Number(result)).toBeGreaterThan(0);
});

test('converts euros to dollars', async () => {
const amount = 100;
const result = await converter.eurosToDollars(amount);
expect(Number(result)).toBeGreaterThan(0);
});

test('converts colones to euros', async () => {
const amount = 1000;
const result = await converter.colonesToEuros(amount);
expect(Number(result)).toBeGreaterThan(0);
});

test('converts euros to colones', async () => {
async function runTests() {
try {
const amount = 100;
const result = await converter.eurosToColones(amount);
expect(Number(result)).toBeGreaterThan(0);
});

test('gets dollar exchange rate', async () => {
const rate = await converter.getDollarExchangeRate();
expect(rate).toHaveProperty('sale');
expect(rate).toHaveProperty('purchase');
});

test('gets euro exchange rate', async () => {
const rate = await converter.getEuroExchangeRate();
expect(rate).toHaveProperty('dollars');
});
});
let result = await converter.dollarsToColones(amount);
console.log(`Dollars to colones: ${result}`);
assert(Number(result) > 0, 'Dollars to colones conversion result should be greater than 0');

result = await converter.colonesToDollars(amount);
console.log(`Colones to dollars: ${result}`);
assert(Number(result) > 0, 'Colones to dollars conversion result should be greater than 0');

result = await converter.dollarsToEuros(amount);
console.log(`Dollars to euros: ${result}`);
assert(Number(result) > 0, 'Dollars to euros conversion result should be greater than 0');

result = await converter.eurosToDollars(amount);
console.log(`Euros to dollars: ${result}`);
assert(Number(result) > 0, 'Euros to dollars conversion result should be greater than 0');

result = await converter.colonesToEuros(amount);
console.log(`Colones to euros: ${result}`);
assert(Number(result) > 0, 'Colones to euros conversion result should be greater than 0');

result = await converter.eurosToColones(amount);
console.log(`Euros to colones: ${result}`);
assert(Number(result) > 0, 'Euros to colones conversion result should be greater than 0');

let rate = await converter.getDollarExchangeRate();
console.log(`Dollar exchange rate: ${JSON.stringify(rate)}`);
assert(rate.hasOwnProperty('date'), 'Dollar exchange rate should have a date property');
assert(rate.hasOwnProperty('sale'), 'Dollar exchange rate should have a sale property');
assert(rate.hasOwnProperty('purchase'), 'Dollar exchange rate should have a purchase property');

rate = await converter.getEuroExchangeRate();
console.log(`Euro exchange rate: ${JSON.stringify(rate)}`);
assert(rate.hasOwnProperty('date'), 'Euro exchange rate should have a date property');
assert(rate.hasOwnProperty('dollars'), 'Euro exchange rate should have a dollars property');
assert(rate.hasOwnProperty('colones'), 'Euro exchange rate should have a colones property');

console.log('All tests passed!');
} catch (error) {
console.error(error);
process.exit(1);
}
}

runTests();
Loading

0 comments on commit b693b08

Please sign in to comment.