From 6e24c84cb6a9d5c7e71ba5ab0a52b02ebdf4e62f Mon Sep 17 00:00:00 2001 From: Jared Date: Sun, 8 Jan 2023 16:40:23 -0500 Subject: [PATCH] Broke out requests into another file --- src/api/composed.ts | 105 +++++++++++++++++++++++++++++++++++ src/api/requests.ts | 23 +++++--- src/components/EditQueue.vue | 19 +++---- src/router/index.ts | 3 +- src/views/AddFundView.vue | 42 +++++++------- src/views/FundView.vue | 85 ++++++++-------------------- src/views/HomeView.vue | 16 ++---- src/views/LoginView.vue | 10 +--- src/views/RegisterView.vue | 11 +--- tests/unit/example.spec.ts | 9 ++- 10 files changed, 191 insertions(+), 132 deletions(-) create mode 100644 src/api/composed.ts diff --git a/src/api/composed.ts b/src/api/composed.ts new file mode 100644 index 0000000..6f60f0c --- /dev/null +++ b/src/api/composed.ts @@ -0,0 +1,105 @@ +import SignetRequestController from '@/api/requests'; +import { + AuthenticationRequest, + Bonus, + CloseRewardFundRequest, + ContributeRequest, + CreateQueueRequest, + CreateQueueResponse, + CreateRewardFundRequest, + GetBalanceRequest, + GetBalanceResponse, + GetContributionsRequest, + GetContributionsResponse, + GetQueueMembersRequest, + GetQueueMembersResponse, + GetQueuesResponse, + GetRewardFundRequest, + GetRewardFundResponse, + GetRewardFundsRequest, + GetRewardFundsResponse, + LoginResponse, + SuccessResponse, +} from '@/api/types'; + +const controller = new SignetRequestController(); + +export const register = (username: string, password: string) => controller.post('Register', { + username, + password, +}); + +export const login = (username: string, password: string) => controller.post('Login', { + username, + password, +}); + +export const getRewardFunds = (offset: number) => controller.post('GetRewardFunds', { offset }); + +export const getQueues = () => controller.post('GetQueues', null); + +export const getQueueMembers = (id: number) => controller.post('GetQueueMembers', { id }); + +export const createQueue = (name: string) => controller.post('CreateQueue', { name }); + +export const createRewardFund = ( + asset: string, + fundWallet: string, + sellingWallet: string, + issuerWallet: string, + memo: string, + minContribution: number, + title: string, + description: string, + bonuses: Bonus[], + queueID?: number | null | undefined, +) => controller.post('CreateRewardFund', { + asset, + fundWallet, + sellingWallet, + issuerWallet, + memo, + minContribution, + title, + description, + bonuses, + queueID, +}); + +export const deleteRewardFund = (id: number, close: boolean) => controller.post< + SuccessResponse, CloseRewardFundRequest +>( + 'CloseRewardFund', + { + id, + close, + }, +); + +export const getRewardFund = (id: number, consolidateContributions: boolean) => controller.post('GetRewardFund', { + id, + consolidateContributions, +}); + +export const getContributions = ( + id: number, + offset: number, + forDate: string | undefined, + consolidateContributions: boolean, +) => controller.post( + 'GetContributions', + { + id, + offset, + forDate, + consolidateContributions, + }, +); + +export const getBalance = (secretKey: string) => controller.post('GetBalance', { secretKey }); + +export const contribute = (privateKey: string, amount: number, rewardFund: number) => controller.post('Contribute', { + privateKey, + amount, + rewardFund, +}); diff --git a/src/api/requests.ts b/src/api/requests.ts index 602f51f..0df5dcb 100644 --- a/src/api/requests.ts +++ b/src/api/requests.ts @@ -1,3 +1,5 @@ +import store from '@/store'; + const setHeaders = (headers: HeadersInit | undefined, token: string | undefined) => { if (!headers && !!token) { return { Authorization: `Bearer ${token}` }; @@ -6,24 +8,24 @@ const setHeaders = (headers: HeadersInit | undefined, token: string | undefined) return headers; } if (!!headers && !!token) { - return { ...headers, Authorization: `Bearer ${token}` }; + return { + ...headers, + Authorization: `Bearer ${token}`, + }; } return {}; }; class SignetRequestController { - token?: string = undefined; - - constructor(token?: string) { - this.token = token; - } - get = async (endpoint: string): Promise => { const resp = await fetch( `/api/${endpoint}`, { method: 'GET', - headers: setHeaders(undefined, this.token), + headers: setHeaders( + undefined, + store.getters.getToken, + ), }, ); if (resp.status === 404) { @@ -38,7 +40,10 @@ class SignetRequestController { { method: 'POST', body: JSON.stringify(payload), - headers: setHeaders({ 'Content-Type': 'application/json' }, this.token), + headers: setHeaders( + { 'Content-Type': 'application/json' }, + store.getters.getToken, + ), }, ); if (resp.status === 404) { diff --git a/src/components/EditQueue.vue b/src/components/EditQueue.vue index 539b1d6..f9aedd5 100644 --- a/src/components/EditQueue.vue +++ b/src/components/EditQueue.vue @@ -47,17 +47,14 @@ import { watch, } from 'vue'; import { - GetQueueMembersRequest, - GetQueueMembersResponse, - GetQueuesResponse, Queue, RewardFund, } from '@/api/types'; import Draggable from 'vuedraggable'; -import SignetRequestController from '@/api/requests'; -import store from '@/store'; - -const controller = new SignetRequestController(store.getters.getToken); +import { + getQueueMembers, + getQueues, +} from '@/api/composed'; // eslint-disable-next-line no-undef const emits = defineEmits(['selected', 'created']); @@ -69,9 +66,9 @@ const drag = ref(false); const queues = ref([] as Queue[]); const fetchQueues = async () => { - const v = await controller.post('GetQueues', null); - if (v) { - queues.value = v.queues; + const resp = await getQueues(); + if (resp) { + queues.value = resp.queues; } }; @@ -86,7 +83,7 @@ const createdQueue = () => { }; const populateQueueMembers = async (id: number) => { - const resp = await controller.post('GetQueueMembers', { id }); + const resp = await getQueueMembers(id); queueMembers.value = resp?.members; }; diff --git a/src/router/index.ts b/src/router/index.ts index 9a1849c..40d8013 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -14,7 +14,6 @@ import FundView from '@/views/FundView.vue'; import AddFundView from '@/views/AddFundView.vue'; import hasPermission from '@/lib/auth'; import SignetRequestController from '@/api/requests'; -import store from '@/store'; const routes: Array = [ { @@ -42,7 +41,7 @@ const routes: Array = [ meta: { requiredRights: Privileges.AdminPlus, accessible: async () => { - const controller = new SignetRequestController(store.getters.getToken); + const controller = new SignetRequestController(); const canProceed = await controller.post('/UsersExist', null); return canProceed?.success; }, diff --git a/src/views/AddFundView.vue b/src/views/AddFundView.vue index ce04940..eff659a 100644 --- a/src/views/AddFundView.vue +++ b/src/views/AddFundView.vue @@ -66,23 +66,23 @@ import SignetRequestController from '@/api/requests'; import { Bonus, - CreateQueueRequest, CreateQueueResponse, - CreateRewardFundRequest, - SuccessResponse, } from '@/api/types'; import { ref } from 'vue'; -import store from '@/store'; import { useRouter } from 'vue-router'; import FundTierInput from '@/components/FundTierInput.vue'; -import { sanitize } from '@/lib/helpers'; import EditQueue from '@/components/EditQueue.vue'; +import { + createQueue, + createRewardFund, +} from '@/api/composed'; +import { sanitize } from '@/lib/helpers'; const router = useRouter(); document.title = 'Beignet - Add Fund'; -const controller = new SignetRequestController(store.getters.getToken); +const controller = new SignetRequestController(); const title = ref(''); const description = ref(''); @@ -111,10 +111,10 @@ const saveBonuses = (evt: Bonus[]) => { bonuses.value = evt; }; -const createQueue = async () => { +const doCreateQueue = async () => { if (!queueName.value) return null; const resp = ref(undefined as CreateQueueResponse | undefined); - resp.value = await controller.post('CreateQueue', { name: queueName.value }) ?? undefined; + resp.value = await createQueue(queueName.value) ?? undefined; if (resp.value?.id) { return resp.value?.id; } @@ -129,22 +129,22 @@ const submit = async () => { requesting.value = true; const forQueue = ref(undefined as null | number | undefined); if (queueSelection.value === -1) { - forQueue.value = await createQueue(); + forQueue.value = await doCreateQueue(); } else { forQueue.value = queueSelection.value; } - const resp = await controller.post('CreateRewardFund', { - asset: asset.value, - fundWallet: sanitize(fundWallet.value), - sellingWallet: sanitize(sellWallet.value), - issuerWallet: sanitize(issuerWallet.value), - memo: sanitize(memo.value), - minContribution: minContribution.value, - title: sanitize(title.value), - description: sanitize(description.value), - bonuses: bonuses.value, - queueID: forQueue.value, - }); + const resp = await createRewardFund( + asset.value, + sanitize(fundWallet.value), + sanitize(sellWallet.value), + sanitize(issuerWallet.value), + sanitize(memo.value), + minContribution.value, + sanitize(title.value), + sanitize(description.value), + bonuses.value, + forQueue.value, + ); requesting.value = false; if (!resp) throw new Error('Could not get response for fund creation'); diff --git a/src/views/FundView.vue b/src/views/FundView.vue index b8fca21..dcc3bee 100644 --- a/src/views/FundView.vue +++ b/src/views/FundView.vue @@ -193,18 +193,10 @@ import { } from 'vue-router'; import { Bonus, - CloseRewardFundRequest, - ContributeRequest, Contribution, FundInfo, - GetBalanceRequest, - GetBalanceResponse, - GetContributionsRequest, - GetContributionsResponse, - GetRewardFundRequest, GetRewardFundResponse, Privileges, - SuccessResponse, } from '@/api/types'; import { computed, @@ -213,7 +205,6 @@ import { watch, } from 'vue'; import { useWebSocket } from '@vueuse/core'; -import SignetRequestController from '@/api/requests'; import store from '@/store'; import { sanitize, @@ -222,8 +213,13 @@ import { import * as luxon from 'luxon'; import hasPermission from '@/lib/auth'; import ErrorDisplay, { SignetError } from '@/components/ErrorDisplay.vue'; - -const controller = new SignetRequestController(store.getters.getToken); +import { + contribute, + deleteRewardFund, + getBalance, + getContributions, + getRewardFund, +} from '@/api/composed'; const route = useRoute(); const router = useRouter(); @@ -245,17 +241,9 @@ const selectedDate = ref(undefined as string | undefined); const allowDelete = ref(false); const deleteFund = async () => { - if (allowDelete.value) { - const deleted = await controller.post( - 'CloseRewardFund', - { - id: identifier, - close: true, - }, - ); - if (deleted && deleted.success) { - await router.push('/'); - } + const deleted = await deleteRewardFund(identifier, allowDelete.value); + if (deleted && deleted.success) { + await router.push('/'); } }; @@ -276,10 +264,7 @@ const fundDetails = ref([{ val: '', }]); -fund.value = await controller.post('GetRewardFund', { - id: identifier, - consolidateContributions: enableConsolidation.value, -}); +fund.value = await getRewardFund(identifier, enableConsolidation.value); if (!fund.value) { router.push('/'); throw new Error('Fund not found'); @@ -394,17 +379,7 @@ document.title = `Beignet - ${fund.value.fundInfo.title}`; watch(selectedDate, async (newVal) => { offset.value = 0; - const conts = await controller.post< - GetContributionsResponse, GetContributionsRequest - >( - 'GetContributions', - { - id: identifier, - offset: offset.value, - forDate: newVal, - consolidateContributions: enableConsolidation.value, - }, - ); + const conts = await getContributions(identifier, offset.value, newVal, enableConsolidation.value); if (!fund.value) throw new Error('Fund not found'); if (!conts) throw new Error('Contributions not found'); contributions.value = conts.list; @@ -420,14 +395,11 @@ const loadMoreIfNeeded = async (e: Event) => { if (canLoadMore()) { contributionsLoading.value = true; - const moreContribs = await controller.post( - 'GetContributions', - { - id: identifier, - offset: offset.value, - forDate: selectedDate.value, - consolidateContributions: enableConsolidation.value, - }, + const moreContribs = await getContributions( + identifier, + offset.value, + selectedDate.value, + enableConsolidation.value, ); if (!moreContribs) throw new Error('Contributions not found'); offset.value += moreContribs.list.length; @@ -451,7 +423,7 @@ const { const queryAccount = async () => { if (pk.value && pk.value.startsWith('S')) { loading.value.balance = true; - const resp = await controller.post('GetBalance', { secretKey: pk.value }); + const resp = await getBalance(pk.value); if (resp === null) { unknownAcct.value = true; acctBalance.value = undefined; @@ -475,11 +447,7 @@ const makeContribution = async () => { if (unknownAcct.value) return; if (!loading.value.contribution && pk.value && amt.value && amt.value <= amountAvailable.value) { loading.value.contribution = true; - await controller.post('Contribute', { - privateKey: sanitize(pk.value), - amount: amt.value, - rewardFund: fund.value.fundInfo.id, - }); + await contribute(sanitize(pk.value), amt.value, fund.value.fundInfo.id); loading.value.contribution = false; pk.value = ''; amt.value = undefined; @@ -488,16 +456,11 @@ const makeContribution = async () => { watch(enableConsolidation, async () => { offset.value = 0; - const conts = await controller.post< - GetContributionsResponse, GetContributionsRequest - >( - 'GetContributions', - { - id: identifier, - offset: offset.value, - forDate: selectedDate.value, - consolidateContributions: enableConsolidation.value, - }, + const conts = await getContributions( + identifier, + offset.value, + selectedDate.value, + enableConsolidation.value, ); if (!fund.value) throw new Error('Fund not found'); if (!conts) throw new Error('Contributions not found'); diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index fd6e7d7..c57127c 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -18,22 +18,18 @@ diff --git a/src/views/LoginView.vue b/src/views/LoginView.vue index 4205f0c..50117f5 100644 --- a/src/views/LoginView.vue +++ b/src/views/LoginView.vue @@ -21,13 +21,7 @@ import { ref } from 'vue'; import { useRouter } from 'vue-router'; import store from '@/store'; -import { - AuthenticationRequest, - LoginResponse, -} from '@/api/types'; -import SignetRequestController from '@/api/requests'; - -const controller = new SignetRequestController(store.getters.getToken); +import { login } from '@/api/composed'; const router = useRouter(); @@ -35,7 +29,7 @@ const username = ref(''); const password = ref(''); const submit = async () => { - const resp = await controller.post('Login', { username: username.value, password: password.value }); + const resp = await login(username.value, password.value); if (!resp) throw new Error('Could not get response from login'); if (resp.token !== null) { sessionStorage.setItem('jwt', JSON.stringify({ token: resp.token })); diff --git a/src/views/RegisterView.vue b/src/views/RegisterView.vue index 393c38f..98d7e4c 100644 --- a/src/views/RegisterView.vue +++ b/src/views/RegisterView.vue @@ -19,15 +19,8 @@