From 19d90e59ed4be67d8dcaa2308703ea4f776b2c55 Mon Sep 17 00:00:00 2001 From: ProgNovel Date: Sat, 28 Mar 2020 20:45:35 +0800 Subject: [PATCH 01/14] server side init --- src/fund/main-client.ts | 36 +++++++++++++++++++++++++++++++++++ src/fund/main-server.ts | 5 +++++ src/fund/main.ts | 42 ++++++++++------------------------------- 3 files changed, 51 insertions(+), 32 deletions(-) create mode 100644 src/fund/main-client.ts create mode 100644 src/fund/main-server.ts diff --git a/src/fund/main-client.ts b/src/fund/main-client.ts new file mode 100644 index 0000000..2a4de4e --- /dev/null +++ b/src/fund/main-client.ts @@ -0,0 +1,36 @@ +import { isMultiplePointer } from './utils' +import { setPointerSingle } from './set-pointer-single' +import { setPointerFromTemplates } from './set-pointer-template' +import { setPointerMultiple } from './set-pointer-multiple' +import { defaultAddressNotFound, invalidAddress } from './errors' +import { defaultAddress, FundType, setFundType } from './main' + +export function clientSideFund(pointer?: WMAddress): FundType { + if (typeof pointer === 'string') { + if (pointer === 'default') { + if (defaultAddress !== undefined) { + if (typeof defaultAddress === 'string') { + setPointerSingle(defaultAddress) + } else { + setPointerMultiple(defaultAddress) + } + return setFundType(FundType.isDefault) + } else { + throw new Error(defaultAddressNotFound) + } + } + setPointerSingle(pointer) + return setFundType(FundType.isSingle) + } + + if (isMultiplePointer(pointer)) { + setPointerMultiple(pointer) + return setFundType(FundType.isMultiple) + } + + if (pointer === undefined) { + setPointerFromTemplates() + return setFundType(FundType.isFromTemplate) + } + throw new Error(invalidAddress) +} diff --git a/src/fund/main-server.ts b/src/fund/main-server.ts new file mode 100644 index 0000000..827bb9b --- /dev/null +++ b/src/fund/main-server.ts @@ -0,0 +1,5 @@ +export let walletAddress = '' + +export function serverSideFund(): string { + return ` Date: Thu, 9 Apr 2020 20:59:35 +0800 Subject: [PATCH 02/14] refactor error messages --- .prettierignore | 2 ++ RELEASE.md | 4 +++ src/fund/errors.ts | 35 ++++++++++++++------------- src/fund/main.ts | 10 ++++---- src/fund/set-pointer-multiple.ts | 8 +++--- src/fund/set-pointer-template.ts | 11 +++++---- src/fund/tests/template-throw.spec.ts | 2 +- src/fund/utils.ts | 2 +- 8 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..5673b46 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +README.md +RELEASE.md \ No newline at end of file diff --git a/RELEASE.md b/RELEASE.md index ea728ea..d5a9fc1 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,5 +1,9 @@ # Releases +## 0.1.1 + +- Errors now more readable. + ## 0.1.0 - FEAT: Custom syntax with `` tags. diff --git a/src/fund/errors.ts b/src/fund/errors.ts index f7bbe84..85c2d94 100644 --- a/src/fund/errors.ts +++ b/src/fund/errors.ts @@ -1,29 +1,30 @@ import { DEFAULT_WEIGHT } from './set-pointer-multiple' -export const defaultAddressNotFound = - 'Fundme.js: default address not found. Use setDefaultAddress(str: string) to set it first.' -export const invalidAddress = 'Fundme.js: Invalid Web Monetization pointer address is given.' -export const addressNotFound = 'Fundme.js: address not found.' -export const addressIsNotAString = 'Fundme.js: address must be a string.' -export const weightNotFound = 'Fundme.js: entries .weight not found.' +export function FundmeError(err: string): string { + return 'Fundme.js: ' + err +} + +export const defaultAddressNotFound = 'default address not found. Use setDefaultAddress(str: string) to set it first.' +export const invalidAddress = 'Invalid Web Monetization pointer address is given.' +export const addressNotFound = 'address not found.' +export const addressIsNotAString = 'address must be a string.' +export const weightNotFound = 'entries .weight not found.' export function weightIsNotANumber(str: string) { - return `Fundme.js: ${str} has weight that is not a number. It has been set to ${DEFAULT_WEIGHT} (default).` + return `${str} has weight that is not a number. It has been set to ${DEFAULT_WEIGHT} (default).` } // about meta tag for Web Monetization API -export const metaTagNotFound = 'Fundme.js: web monetization meta tag is not found.' +export const metaTagNotFound = 'web monetization meta tag is not found.' export const metaTagMultipleIsFound = - 'Fundme.js: multiple found - Web Monetization API only support a single meta tag.' + 'multiple found - Web Monetization API only support a single meta tag.' // pointers template -export const noTemplateFound = 'Fundme.js: no monetization template is found.' -export const failParsingTemplate = 'Fundme.js: fails to parse address from .' +export const noTemplateFound = 'no monetization template is found.' +export const failParsingTemplate = 'fails to parse address from .' export const templateSinglePointerHasWeight = - 'Fundme.js: found single but has weight - only address will be parsed.' + 'found single but has weight - only address will be parsed.' // script json template -export const cannotParseScriptJson = - 'Fundme.js: cannot parse JSON from ` + forceFundmeOnBrowser() fund() expect(getCurrentPointerPool()).toEqual(pointers) document.body.innerHTML = '' diff --git a/tests/fund/client-side/string-syntax-unique.spec.ts b/tests/fund/client-side/string-syntax-unique.spec.ts index 90b8814..7107aba 100644 --- a/tests/fund/client-side/string-syntax-unique.spec.ts +++ b/tests/fund/client-side/string-syntax-unique.spec.ts @@ -1,8 +1,9 @@ -import { fund, getCurrentPointerPool } from '../../../src/fund/main' +import { fund, getCurrentPointerPool, forceFundmeOnBrowser } from '../../../src/fund/main' import { convertToPointer } from '../../../src/fund/set-pointer-multiple' describe('Unique syntax on string', () => { test('will result correct weights', () => { + forceFundmeOnBrowser() fund(['$wallet.example.com/testing-1#33', '$wallet.example.com/testing-2#22']) const expectedPool = [ diff --git a/tests/fund/client-side/template-scrape-custom-syntax.spec.ts b/tests/fund/client-side/template-scrape-custom-syntax.spec.ts index 04cb6ff..8b6be5b 100644 --- a/tests/fund/client-side/template-scrape-custom-syntax.spec.ts +++ b/tests/fund/client-side/template-scrape-custom-syntax.spec.ts @@ -1,4 +1,4 @@ -import { fund, getCurrentPointerPool } from '../../../src/fund/main' +import { fund, getCurrentPointerPool, forceFundmeOnBrowser } from '../../../src/fund/main' import { toBeInTheDocument, toHaveAttribute } from '@testing-library/jest-dom/matchers' import { scriptFundmeIsNotApplicationJson } from '../../../src/fund/errors' @@ -7,6 +7,7 @@ expect.extend({ toBeInTheDocument, toHaveAttribute }) describe('parsing custom syntax', () => { test('works with hash # weight modifier', () => { + forceFundmeOnBrowser() document.body.innerHTML = ` ` + forceFundmeOnBrowser() fund() const expectedPool = [ { diff --git a/tests/fund/client-side/template-scrape-from-script-json.spec.ts b/tests/fund/client-side/template-scrape-from-script-json.spec.ts index bcadf09..9ad1db4 100644 --- a/tests/fund/client-side/template-scrape-from-script-json.spec.ts +++ b/tests/fund/client-side/template-scrape-from-script-json.spec.ts @@ -1,4 +1,4 @@ -import { fund, getCurrentPointerPool } from '../../../src/fund/main' +import { fund, getCurrentPointerPool, forceFundmeOnBrowser } from '../../../src/fund/main' import { toBeInTheDocument, toHaveAttribute } from '@testing-library/jest-dom/matchers' import { scriptFundmeIsNotApplicationJson } from '../../../src/fund/errors' @@ -8,16 +8,17 @@ expect.extend({ toBeInTheDocument, toHaveAttribute }) describe('parsing fundme template from a JSON array', () => { test('fund() will scrape from + ` + forceFundmeOnBrowser() fund() const pool = getCurrentPointerPool() // @ts-ignore @@ -30,6 +31,7 @@ describe('parsing fundme template from a JSON array', () => { "$coil.xrptipbot.com/my-pointer" ` + forceFundmeOnBrowser() fund() const pool = getCurrentPointerPool() // @ts-ignore @@ -50,6 +52,7 @@ describe('parsing fundme template from a JSON array', () => { ] ` + forceFundmeOnBrowser() // @ts-ignore expect(() => fund()).toThrowError(scriptFundmeIsNotApplicationJson) document.body.innerHTML = '' diff --git a/tests/fund/client-side/template-scrape-weight.spec.ts b/tests/fund/client-side/template-scrape-weight.spec.ts index dc95366..8dd3e20 100644 --- a/tests/fund/client-side/template-scrape-weight.spec.ts +++ b/tests/fund/client-side/template-scrape-weight.spec.ts @@ -1,4 +1,4 @@ -import { fund, getCurrentPointerPool } from '../../../src/fund/main' +import { fund, getCurrentPointerPool, forceFundmeOnBrowser } from '../../../src/fund/main' import { toBeInTheDocument, toHaveAttribute } from '@testing-library/jest-dom/matchers' @@ -6,6 +6,7 @@ expect.extend({ toBeInTheDocument, toHaveAttribute }) describe('get weight from