From 1d015d5a2d2641cd7864e4d5fb3506d8281c5813 Mon Sep 17 00:00:00 2001 From: Gabriele Genta Date: Sun, 18 Jul 2021 15:51:40 +0200 Subject: [PATCH] REF: convert fiatUnit to typescript --- ios/BlueWallet.xcodeproj/project.pbxproj | 16 ++++++------- ios/Widgets/Shared/Fiat/FiatUnit.swift | 2 +- models/{fiatUnit.js => fiatUnit.ts} | 29 ++++++++++++++++-------- models/{fiatUnit.json => fiatUnits.json} | 0 tsconfig.json | 3 ++- typings/frisbee.d.ts | 15 ++++++++++++ 6 files changed, 45 insertions(+), 20 deletions(-) rename models/{fiatUnit.js => fiatUnit.ts} (81%) rename models/{fiatUnit.json => fiatUnits.json} (100%) create mode 100644 typings/frisbee.d.ts diff --git a/ios/BlueWallet.xcodeproj/project.pbxproj b/ios/BlueWallet.xcodeproj/project.pbxproj index d718cdd5c..efce2c675 100644 --- a/ios/BlueWallet.xcodeproj/project.pbxproj +++ b/ios/BlueWallet.xcodeproj/project.pbxproj @@ -32,7 +32,7 @@ 6DD410A1266CADF10087DE03 /* Widgets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DD410A0266CADF10087DE03 /* Widgets.swift */; }; 6DD410A7266CADF40087DE03 /* WidgetsExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 6DD4109C266CADF10087DE03 /* WidgetsExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; 6DD410AC266CAE470087DE03 /* PriceWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D6CA4BC255872E3009312A5 /* PriceWidget.swift */; }; - 6DD410AE266CAF1F0087DE03 /* fiatUnit.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnit.json */; }; + 6DD410AE266CAF1F0087DE03 /* fiatUnits.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnits.json */; }; 6DD410AF266CAF5C0087DE03 /* WalletInformationAndMarketWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D9A2E06254BA347007B5B82 /* WalletInformationAndMarketWidget.swift */; }; 6DD410B0266CAF5C0087DE03 /* WalletInformationWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DEB4AB1254FB59C00E9F9AA /* WalletInformationWidget.swift */; }; 6DD410B1266CAF5C0087DE03 /* WidgetAPI+Electrum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D6CA5142558EBA3009312A5 /* WidgetAPI+Electrum.swift */; }; @@ -75,8 +75,8 @@ B43D037C225847C500FBAA95 /* Wallet.swift in Sources */ = {isa = PBXBuildFile; fileRef = B43D0376225847C500FBAA95 /* Wallet.swift */; }; B43D037D225847C500FBAA95 /* WalletInformation.swift in Sources */ = {isa = PBXBuildFile; fileRef = B43D0377225847C500FBAA95 /* WalletInformation.swift */; }; B4EE583C226703320003363C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B40D4E35225841ED00428FCC /* Assets.xcassets */; }; - E5D4794B26781FC0007838C1 /* fiatUnit.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnit.json */; }; - E5D4794C26781FC1007838C1 /* fiatUnit.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnit.json */; }; + E5D4794B26781FC0007838C1 /* fiatUnits.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnits.json */; }; + E5D4794C26781FC1007838C1 /* fiatUnits.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnits.json */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -274,7 +274,7 @@ 6DD4109C266CADF10087DE03 /* WidgetsExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WidgetsExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 6DD410A0266CADF10087DE03 /* Widgets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Widgets.swift; sourceTree = ""; }; 6DD410A4266CADF40087DE03 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 6DD410AD266CAF1F0087DE03 /* fiatUnit.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = fiatUnit.json; path = ../../../../models/fiatUnit.json; sourceTree = ""; }; + 6DD410AD266CAF1F0087DE03 /* fiatUnits.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = fiatUnits.json; path = ../../../../models/fiatUnits.json; sourceTree = ""; }; 6DD410C3266CCB780087DE03 /* WidgetsExtension.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = WidgetsExtension.entitlements; sourceTree = SOURCE_ROOT; }; 6DEB496F254E38DE00E9F9AA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/MainInterface.strings; sourceTree = ""; }; 6DEB4AB1254FB59C00E9F9AA /* WalletInformationWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletInformationWidget.swift; sourceTree = ""; }; @@ -512,7 +512,7 @@ 6D2AA8062568B8E50090B089 /* Fiat */ = { isa = PBXGroup; children = ( - 6DD410AD266CAF1F0087DE03 /* fiatUnit.json */, + 6DD410AD266CAF1F0087DE03 /* fiatUnits.json */, 6D2AA8072568B8F40090B089 /* FiatUnit.swift */, ); path = Fiat; @@ -968,7 +968,7 @@ buildActionMask = 2147483647; files = ( 6DD410B7266CAF5C0087DE03 /* Assets.xcassets in Resources */, - 6DD410AE266CAF1F0087DE03 /* fiatUnit.json in Resources */, + 6DD410AE266CAF1F0087DE03 /* fiatUnits.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -977,7 +977,7 @@ buildActionMask = 2147483647; files = ( B40D4E36225841ED00428FCC /* Assets.xcassets in Resources */, - E5D4794C26781FC1007838C1 /* fiatUnit.json in Resources */, + E5D4794C26781FC1007838C1 /* fiatUnits.json in Resources */, B40D4E34225841EC00428FCC /* Interface.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -987,7 +987,7 @@ buildActionMask = 2147483647; files = ( B4EE583C226703320003363C /* Assets.xcassets in Resources */, - E5D4794B26781FC0007838C1 /* fiatUnit.json in Resources */, + E5D4794B26781FC0007838C1 /* fiatUnits.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ios/Widgets/Shared/Fiat/FiatUnit.swift b/ios/Widgets/Shared/Fiat/FiatUnit.swift index f1d0672b0..eaf2c73e8 100644 --- a/ios/Widgets/Shared/Fiat/FiatUnit.swift +++ b/ios/Widgets/Shared/Fiat/FiatUnit.swift @@ -16,7 +16,7 @@ struct FiatUnit: Codable { } func fiatUnit(currency: String) -> FiatUnit? { - return Bundle.main.decode([String: FiatUnit].self, from: "fiatUnit.json").first(where: {$1.endPointKey == currency})?.value + return Bundle.main.decode([String: FiatUnit].self, from: "fiatUnits.json").first(where: {$1.endPointKey == currency})?.value } extension Bundle { diff --git a/models/fiatUnit.js b/models/fiatUnit.ts similarity index 81% rename from models/fiatUnit.js rename to models/fiatUnit.ts index 9526c3823..0027427c5 100644 --- a/models/fiatUnit.js +++ b/models/fiatUnit.ts @@ -1,14 +1,15 @@ import Frisbee from 'frisbee'; +import untypedFiatUnit from './fiatUnits.json'; -export const FiatUnitSource = Object.freeze({ +export const FiatUnitSource = { CoinDesk: 'CoinDesk', Yadio: 'Yadio', BitcoinduLiban: 'BitcoinduLiban', Exir: 'Exir', -}); +} as const; -const RateExtractors = Object.freeze({ - CoinDesk: async ticker => { +const RateExtractors = { + CoinDesk: async (ticker: string): Promise => { const api = new Frisbee({ baseURI: 'https://api.coindesk.com' }); const res = await api.get(`/v1/bpi/currentprice/${ticker}.json`); if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`); @@ -26,7 +27,7 @@ const RateExtractors = Object.freeze({ if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`); return rate; }, - Yadio: async ticker => { + Yadio: async (ticker: string): Promise => { const api = new Frisbee({ baseURI: 'https://api.yadio.io/json' }); const res = await api.get(`/${ticker}`); if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`); @@ -44,7 +45,7 @@ const RateExtractors = Object.freeze({ if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`); return rate; }, - BitcoinduLiban: async ticker => { + BitcoinduLiban: async (ticker: string): Promise => { const api = new Frisbee({ baseURI: 'https://bitcoinduliban.org' }); const res = await api.get('/api.php?key=lbpusd'); if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`); @@ -62,7 +63,7 @@ const RateExtractors = Object.freeze({ if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`); return rate; }, - Exir: async ticker => { + Exir: async (ticker: string): Promise => { const api = new Frisbee({ baseURI: 'https://api.exir.io' }); const res = await api.get('/v1/ticker?symbol=btc-irt'); if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`); @@ -80,10 +81,18 @@ const RateExtractors = Object.freeze({ if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`); return rate; }, -}); +} as const; -export const FiatUnit = require('./fiatUnit.json'); +type FiatUnit = { + [key: string]: { + endPointKey: string; + symbol: string; + locale: string; + source: 'CoinDesk' | 'Yadio' | 'Exir' | 'BitcoinduLiban'; + }; +}; +export const FiatUnit = untypedFiatUnit as FiatUnit; -export async function getFiatRate(ticker) { +export async function getFiatRate(ticker: string): Promise { return await RateExtractors[FiatUnit[ticker].source](ticker); } diff --git a/models/fiatUnit.json b/models/fiatUnits.json similarity index 100% rename from models/fiatUnit.json rename to models/fiatUnits.json diff --git a/tsconfig.json b/tsconfig.json index 9658e3303..b42eea8a1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,8 @@ "noEmit": true, "strict": true, "target": "esnext", - "skipLibCheck": true + "skipLibCheck": true, + "resolveJsonModule": true }, "exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"] } diff --git a/typings/frisbee.d.ts b/typings/frisbee.d.ts new file mode 100644 index 000000000..b42c1affe --- /dev/null +++ b/typings/frisbee.d.ts @@ -0,0 +1,15 @@ +declare module 'frisbee' { + // TODO: improve with more specific types + type RequestOptions = Record; + type Response = any; + + declare class Frisbee { + constructor(options: { baseURI?: string; headers?: Record }); + + get(path: string, options?: RequestOptions): Promise; + post(path: string, options?: RequestOptions): Promise; + // TODO: add missing methods + } + + export default Frisbee; +}