Skip to content

Commit

Permalink
release(v0.0.2-1): now NCB HK supported now
Browse files Browse the repository at this point in the history
  • Loading branch information
186526 committed Jan 7, 2025
1 parent 7781bee commit 35199f5
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 63 deletions.
92 changes: 46 additions & 46 deletions dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fxrate",
"version": "0.0.2",
"version": "0.0.2-1",
"license": "SEE LICENSE IN LICENSE",
"author": "Bo Xu <[email protected]> (https://186526.xyz/)",
"dependencies": {
Expand Down
24 changes: 16 additions & 8 deletions src/FXGetter/mastercard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const currenciesList: string[] = [
'XPF',
'CLP',
'CNY',
'CNH',
'COP',
'KMF',
'CDF',
Expand Down Expand Up @@ -171,8 +172,12 @@ export default class mastercardFXM extends fxManager {
const fxRateList: fxManager['_fxRateList'] = {} as any;

currenciesList.forEach((from) => {
const _from = from == 'CNH' ? 'CNY' : from;

fxRateList[from] = {} as any;
currenciesList.forEach((to) => {
const _to = to == 'CNH' ? 'CNY' : to;

const currency = new Proxy(
{},
{
Expand All @@ -188,10 +193,10 @@ export default class mastercardFXM extends fxManager {
return undefined;
}

if (!cache.has(`${from}${to}`)) {
if (!cache.has(`${_from}${_to}`)) {
const request = syncRequest(
'GET',
`https://www.mastercard.co.uk/settlement/currencyrate/conversion-rate?fxDate=0000-00-00&transCurr=${to}&crdhldBillCurr=${from}&bankFee=0&transAmt=1`,
`https://www.mastercard.co.uk/settlement/currencyrate/conversion-rate?fxDate=0000-00-00&transCurr=${_to}&crdhldBillCurr=${_from}&bankFee=0&transAmt=1`,
{
headers: {
'user-agent':
Expand All @@ -202,7 +207,7 @@ export default class mastercardFXM extends fxManager {
},
);
cache.set(
`${from}${to}`,
`${_from}${_to}`,
request.getBody().toString(),
);
}
Expand All @@ -213,15 +218,15 @@ export default class mastercardFXM extends fxManager {
)
) {
const data = JSON.parse(
cache.get(`${from}${to}`),
cache.get(`${_from}${_to}`),
);
return divide(
fraction(data.data.transAmt),
fraction(data.data.conversionRate),
);
} else {
const data = JSON.parse(
cache.get(`${from}${to}`),
cache.get(`${_from}${_to}`),
);
return new Date(data.data.fxDate);
}
Expand All @@ -236,6 +241,9 @@ export default class mastercardFXM extends fxManager {
}

public async getfxRateList(from: currency, to: currency) {
const _from = from == 'CNH' ? 'CNY' : from;
const _to = to == 'CNH' ? 'CNY' : to;

if (
!(
currenciesList.includes(from as string) &&
Expand All @@ -245,12 +253,12 @@ export default class mastercardFXM extends fxManager {
throw new Error('Currency not supported');
}

if (cache.has(`${from}${to}`)) {
if (cache.has(`${_from}${_to}`)) {
return this.fxRateList[from][to];
}

const req = await axios.get(
`https://www.mastercard.co.uk/settlement/currencyrate/conversion-rate?fxDate=0000-00-00&transCurr=${to}&crdhldBillCurr=${from}&bankFee=0&transAmt=1`,
`https://www.mastercard.co.uk/settlement/currencyrate/conversion-rate?fxDate=0000-00-00&transCurr=${_to}&crdhldBillCurr=${_from}&bankFee=0&transAmt=1`,
{
headers: {
'User-Agent':
Expand All @@ -261,7 +269,7 @@ export default class mastercardFXM extends fxManager {
);

const data = req.data;
cache.set(`${from}${to}`, JSON.stringify(data));
cache.set(`${_from}${_to}`, JSON.stringify(data));

return this.fxRateList[from][to];
}
Expand Down
68 changes: 68 additions & 0 deletions src/FXGetter/ncb.hk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import axios from 'axios';

import { FXRate, currency } from 'src/types';

const currencyMapping = {
'156': 'CNY' as currency.CNY,
A04: 'CNH' as currency.CNH,
'840': 'USD' as currency.USD,
'826': 'GBP' as currency.GBP,
'392': 'JPY' as currency.JPY,
'036': 'AUD' as currency.AUD,
'554': 'NZD' as currency.NZD,
'124': 'CAD' as currency.CAD,
'978': 'EUR' as currency.EUR,
'756': 'CHF' as currency.CHF,
'208': 'DKK' as currency.DKK,
'578': 'NOK' as currency.NOK,
'752': 'SEK' as currency.SEK,
'702': 'SGD' as currency.SGD,
'764': 'THB' as currency.THB,
};

const getNCBHKFXRates = async (): Promise<FXRate[]> => {
const res = await axios.post(
'https://www.ncb.com.hk/api/precious/findConversionRateAll',
{
headers: {
language: 'en',
'User-Agent':
process.env['HEADER_USER_AGENT'] ?? 'fxrate axios/latest',
},
body: {
language: 3,
custType: 1,
},
},
);

return res.data.data.resultList
.map((fx) => {
const currencyName = currencyMapping[fx.currency];

const buy = fx.outNum < fx.inNum ? fx.outNum : fx.inNum;
const sell = fx.outNum < fx.inNum ? fx.inNum : fx.outNum;

return {
currency: {
to: currencyName as unknown as currency.unknown,
from: 'HKD' as currency.HKD,
},
rate: {
buy: {
remit: buy,
cash: buy,
},
sell: {
remit: sell,
cash: sell,
},
},
unit: 100,
updated: new Date(fx.createTime + ' UTC+8'),
};
})
.sort();
};

export default getNCBHKFXRates;
23 changes: 15 additions & 8 deletions src/FXGetter/visa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const currenciesList: string[] = [
'CHF',
'CLP',
'CNY',
'CNH',
'COP',
'CRC',
'CVE',
Expand Down Expand Up @@ -211,6 +212,9 @@ export default class visaFXM extends fxManager {
currenciesList.forEach((from) => {
fxRateList[from] = {} as any;
currenciesList.forEach((to) => {
const _from = from == 'CNH' ? 'CNY' : from;
const _to = to == 'CNH' ? 'CNY' : to;

const currency = new Proxy(
{},
{
Expand All @@ -230,16 +234,16 @@ export default class visaFXM extends fxManager {
.utc()
.format('MM/DD/YYYY');

if (!cache.has(`${from}${to}`)) {
if (!cache.has(`${_from}${_to}`)) {
const request = syncRequest(
'GET',
`https://usa.visa.com/cmsapi/fx/rates?amount=1&fee=0&utcConvertedDate=${dateString}&exchangedate=${dateString}&fromCurr=${to}&toCurr=${from}`,
`https://usa.visa.com/cmsapi/fx/rates?amount=1&fee=0&utcConvertedDate=${dateString}&exchangedate=${dateString}&fromCurr=${_to}&toCurr=${_from}`,
{
headers,
},
);
cache.set(
`${from}${to}`,
`${_from}${_to}`,
request.getBody().toString(),
);
}
Expand All @@ -250,12 +254,12 @@ export default class visaFXM extends fxManager {
)
) {
const data = JSON.parse(
cache.get(`${from}${to}`),
cache.get(`${_from}${_to}`),
);
return fraction(data.originalValues.fxRateVisa);
} else {
const data = JSON.parse(
cache.get(`${from}${to}`),
cache.get(`${_from}${_to}`),
);
return new Date(
data.originalValues.lastUpdatedVisaRate *
Expand All @@ -273,6 +277,9 @@ export default class visaFXM extends fxManager {
}

public async getfxRateList(from: currency, to: currency) {
const _from = from == 'CNH' ? 'CNY' : from;
const _to = to == 'CNH' ? 'CNY' : to;

if (
!(
currenciesList.includes(from as string) &&
Expand All @@ -282,21 +289,21 @@ export default class visaFXM extends fxManager {
throw new Error('Currency not supported');
}

if (cache.has(`${from}${to}`)) {
if (cache.has(`${_from}${_to}`)) {
return this.fxRateList[from][to];
}

const dateString = dayjs().utc().format('MM/DD/YYYY');

const req = await axios.get(
`https://usa.visa.com/cmsapi/fx/rates?amount=1&fee=0&utcConvertedDate=${dateString}&exchangedate=${dateString}&fromCurr=${to}&toCurr=${from}`,
`https://usa.visa.com/cmsapi/fx/rates?amount=1&fee=0&utcConvertedDate=${dateString}&exchangedate=${dateString}&fromCurr=${_to}&toCurr=${_from}`,
{
headers,
},
);

const data = req.data;
cache.set(`${from}${to}`, JSON.stringify(data));
cache.set(`${_from}${_to}`, JSON.stringify(data));

return this.fxRateList[from][to];
}
Expand Down
1 change: 1 addition & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const sourceNamesInZH = {
'citic.cn': '中信银行',
spdb: '浦发银行',
'ncb.cn': '南洋商业银行(中国)',
'ncb.hk': '南洋商业银行(香港)',
xib: '厦门国际银行',
pab: '平安银行',
ceb: '中国光大银行',
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import getHSBCAUFXRates from './FXGetter/hsbc.au';
import getCITICCNFXRates from './FXGetter/citic.cn';
import getSPDBFXRates from './FXGetter/spdb';
import getNCBCNFXRates from './FXGetter/ncb.cn';
import getNCBHKFXRates from './FXGetter/ncb.hk';
import getXIBFXRates from './FXGetter/xib';
import getPABFXRates from './FXGetter/pab';
import getCEBFXRates from './FXGetter/ceb';
Expand Down Expand Up @@ -54,6 +55,7 @@ const Manager = new fxmManager({
'hsbc.au': getHSBCAUFXRates,
'citic.cn': getCITICCNFXRates,
'ncb.cn': getNCBCNFXRates,
'ncb.hk': getNCBHKFXRates,
spdb: getSPDBFXRates,
xib: getXIBFXRates,
pab: getPABFXRates,
Expand Down

0 comments on commit 35199f5

Please sign in to comment.