diff options
author | Sebastian <sebasjm@gmail.com> | 2021-11-22 17:34:27 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-11-22 17:34:31 -0300 |
commit | 829a59e1a24d6a99ce7554d28acfd05f21baeaf8 (patch) | |
tree | 66ef9157905e71ebf9e252c533d1855f381902d0 /packages/taler-wallet-webextension/src/utils/index.ts | |
parent | a35604fd562a72e4e266bf6a4255d89d3c1374a1 (diff) | |
download | wallet-core-829a59e1a24d6a99ce7554d28acfd05f21baeaf8.tar.xz |
add exchange feature
Diffstat (limited to 'packages/taler-wallet-webextension/src/utils/index.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/utils/index.ts | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/packages/taler-wallet-webextension/src/utils/index.ts b/packages/taler-wallet-webextension/src/utils/index.ts new file mode 100644 index 000000000..477818520 --- /dev/null +++ b/packages/taler-wallet-webextension/src/utils/index.ts @@ -0,0 +1,162 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import { AmountJson, Amounts, GetExchangeTosResult } from "@gnu-taler/taler-util"; + + +function getJsonIfOk(r: Response): Promise<any> { + if (r.ok) { + return r.json(); + } + + if (r.status >= 400 && r.status < 500) { + throw new Error(`URL may not be right: (${r.status}) ${r.statusText}`); + } + + throw new Error( + `Try another server: (${r.status}) ${r.statusText || "internal server error" + }`, + ); + +} + +export async function queryToSlashConfig<T>( + url: string, +): Promise<T> { + return fetch(new URL("config", url).href) + .catch(() => { + throw new Error(`Network error`); + }) + .then(getJsonIfOk); +} + +export async function queryToSlashKeys<T>( + url: string, +): Promise<T> { + return fetch(new URL("keys", url).href) + .catch(() => { + throw new Error(`Network error`); + }) + .then(getJsonIfOk); +} + +export function buildTermsOfServiceState(tos: GetExchangeTosResult): TermsState { + + const content: TermsDocument | undefined = parseTermsOfServiceContent( + tos.contentType, + tos.content, + ); + + const status: TermsStatus = !content + ? "notfound" + : !tos.acceptedEtag + ? "new" + : tos.acceptedEtag !== tos.currentEtag + ? "changed" + : "accepted"; + + return { content, status, version: tos.currentEtag } +} + +function parseTermsOfServiceContent( + type: string, + text: string, +): TermsDocument | undefined { + if (type === "text/xml") { + try { + const document = new DOMParser().parseFromString(text, "text/xml"); + return { type: "xml", document }; + } catch (e) { + console.log(e); + } + } else if (type === "text/html") { + try { + const href = new URL(text); + return { type: "html", href }; + } catch (e) { + console.log(e); + } + } else if (type === "text/json") { + try { + const data = JSON.parse(text); + return { type: "json", data }; + } catch (e) { + console.log(e); + } + } else if (type === "text/pdf") { + try { + const location = new URL(text); + return { type: "pdf", location }; + } catch (e) { + console.log(e); + } + } else if (type === "text/plain") { + try { + const content = text; + return { type: "plain", content }; + } catch (e) { + console.log(e); + } + } + return undefined; +} + +export type TermsState = { + content: TermsDocument | undefined; + status: TermsStatus; + version: string; +}; + +type TermsStatus = "new" | "accepted" | "changed" | "notfound"; + +type TermsDocument = + | TermsDocumentXml + | TermsDocumentHtml + | TermsDocumentPlain + | TermsDocumentJson + | TermsDocumentPdf; + +interface TermsDocumentXml { + type: "xml"; + document: Document; +} + +interface TermsDocumentHtml { + type: "html"; + href: URL; +} + +interface TermsDocumentPlain { + type: "plain"; + content: string; +} + +interface TermsDocumentJson { + type: "json"; + data: any; +} + +interface TermsDocumentPdf { + type: "pdf"; + location: URL; +} + +export function amountToString(text: AmountJson): string { + const aj = Amounts.jsonifyAmount(text); + const amount = Amounts.stringifyValue(aj); + return `${amount} ${aj.currency}`; +} + |