UNPKG

@cardql/react

Version:

CardQL SDK for React web applications with hooks and context providers

281 lines (250 loc) 6.82 kB
import { useQuery, UseQueryOptions } from "./useQuery"; import { useMutation, UseMutationOptions } from "./useMutation"; import { GET_ACCOUNTS, GET_ACCOUNT, CREATE_ACCOUNT, UPDATE_ACCOUNT, DELETE_ACCOUNT, GET_APPS, GET_APP, CREATE_APP, UPDATE_APP, DELETE_APP, GET_CUSTOMERS, GET_CUSTOMER, CREATE_CUSTOMER, UPDATE_CUSTOMER, DELETE_CUSTOMER, GET_MERCHANTS, GET_MERCHANT, CREATE_MERCHANT, UPDATE_MERCHANT, DELETE_MERCHANT, GET_PAYMENTS, GET_PAYMENT, CREATE_PAYMENT, UPDATE_PAYMENT, DELETE_PAYMENT, GET_LEDGERS, GET_LEDGER, CREATE_LEDGER, UPDATE_LEDGER, DELETE_LEDGER, } from "@cardql/core"; import type { Account, App, Customer, Merchant, Payment, Ledger, CreateAccountInput, UpdateAccountInput, CreateAppInput, UpdateAppInput, CreateCustomerInput, UpdateCustomerInput, CreateMerchantInput, UpdateMerchantInput, CreatePaymentInput, UpdatePaymentInput, CreateLedgerInput, UpdateLedgerInput, } from "@cardql/core"; // Account hooks export function useAccounts(options?: UseQueryOptions) { return useQuery<{ accounts: Account[] }>(GET_ACCOUNTS, undefined, options); } export function useAccount(accountID: string, options?: UseQueryOptions) { return useQuery<{ account: Account | null }>( GET_ACCOUNT, { accountID }, options ); } export function useCreateAccount( options?: UseMutationOptions<{ createAccount: Account }, CreateAccountInput> ) { return useMutation<{ createAccount: Account }, CreateAccountInput>( CREATE_ACCOUNT, options ); } export function useUpdateAccount( options?: UseMutationOptions<{ updateAccount: Account }, UpdateAccountInput> ) { return useMutation<{ updateAccount: Account }, UpdateAccountInput>( UPDATE_ACCOUNT, options ); } export function useDeleteAccount( options?: UseMutationOptions<{ deleteAccount: string }, { id: string }> ) { return useMutation<{ deleteAccount: string }, { id: string }>( DELETE_ACCOUNT, options ); } // App hooks export function useApps(options?: UseQueryOptions) { return useQuery<{ apps: App[] }>(GET_APPS, undefined, options); } export function useApp(id: string, options?: UseQueryOptions) { return useQuery<{ app: App | null }>(GET_APP, { id }, options); } export function useCreateApp( options?: UseMutationOptions<{ createApp: App }, CreateAppInput> ) { return useMutation<{ createApp: App }, CreateAppInput>(CREATE_APP, options); } export function useUpdateApp( options?: UseMutationOptions<{ updateApp: App }, UpdateAppInput> ) { return useMutation<{ updateApp: App }, UpdateAppInput>(UPDATE_APP, options); } export function useDeleteApp( options?: UseMutationOptions<{ deleteApp: string }, { id: string }> ) { return useMutation<{ deleteApp: string }, { id: string }>( DELETE_APP, options ); } // Customer hooks export function useCustomers(options?: UseQueryOptions) { return useQuery<{ customers: Customer[] }>(GET_CUSTOMERS, undefined, options); } export function useCustomer(id: string, options?: UseQueryOptions) { return useQuery<{ customer: Customer | null }>(GET_CUSTOMER, { id }, options); } export function useCreateCustomer( options?: UseMutationOptions< { createCustomer: Customer }, CreateCustomerInput > ) { return useMutation<{ createCustomer: Customer }, CreateCustomerInput>( CREATE_CUSTOMER, options ); } export function useUpdateCustomer( options?: UseMutationOptions< { updateCustomer: Customer }, UpdateCustomerInput > ) { return useMutation<{ updateCustomer: Customer }, UpdateCustomerInput>( UPDATE_CUSTOMER, options ); } export function useDeleteCustomer( options?: UseMutationOptions<{ deleteCustomer: string }, { id: string }> ) { return useMutation<{ deleteCustomer: string }, { id: string }>( DELETE_CUSTOMER, options ); } // Merchant hooks export function useMerchants(options?: UseQueryOptions) { return useQuery<{ merchants: Merchant[] }>(GET_MERCHANTS, undefined, options); } export function useMerchant(id: string, options?: UseQueryOptions) { return useQuery<{ merchant: Merchant | null }>(GET_MERCHANT, { id }, options); } export function useCreateMerchant( options?: UseMutationOptions< { createMerchant: Merchant }, CreateMerchantInput > ) { return useMutation<{ createMerchant: Merchant }, CreateMerchantInput>( CREATE_MERCHANT, options ); } export function useUpdateMerchant( options?: UseMutationOptions< { updateMerchant: Merchant }, UpdateMerchantInput > ) { return useMutation<{ updateMerchant: Merchant }, UpdateMerchantInput>( UPDATE_MERCHANT, options ); } export function useDeleteMerchant( options?: UseMutationOptions<{ deleteMerchant: string }, { id: string }> ) { return useMutation<{ deleteMerchant: string }, { id: string }>( DELETE_MERCHANT, options ); } // Payment hooks export function usePayments(options?: UseQueryOptions) { return useQuery<{ payments: Payment[] }>(GET_PAYMENTS, undefined, options); } export function usePayment(id: string, options?: UseQueryOptions) { return useQuery<{ payment: Payment | null }>(GET_PAYMENT, { id }, options); } export function useCreatePayment( options?: UseMutationOptions<{ createPayment: Payment }, CreatePaymentInput> ) { return useMutation<{ createPayment: Payment }, CreatePaymentInput>( CREATE_PAYMENT, options ); } export function useUpdatePayment( options?: UseMutationOptions<{ updatePayment: Payment }, UpdatePaymentInput> ) { return useMutation<{ updatePayment: Payment }, UpdatePaymentInput>( UPDATE_PAYMENT, options ); } export function useDeletePayment( options?: UseMutationOptions<{ deletePayment: string }, { id: string }> ) { return useMutation<{ deletePayment: string }, { id: string }>( DELETE_PAYMENT, options ); } // Ledger hooks export function useLedgers(options?: UseQueryOptions) { return useQuery<{ ledgers: Ledger[] }>(GET_LEDGERS, undefined, options); } export function useLedger(id: string, options?: UseQueryOptions) { return useQuery<{ ledger: Ledger | null }>(GET_LEDGER, { id }, options); } export function useCreateLedger( options?: UseMutationOptions<{ createLedger: Ledger }, CreateLedgerInput> ) { return useMutation<{ createLedger: Ledger }, CreateLedgerInput>( CREATE_LEDGER, options ); } export function useUpdateLedger( options?: UseMutationOptions<{ updateLedger: Ledger }, UpdateLedgerInput> ) { return useMutation<{ updateLedger: Ledger }, UpdateLedgerInput>( UPDATE_LEDGER, options ); } export function useDeleteLedger( options?: UseMutationOptions<{ deleteLedger: string }, { id: string }> ) { return useMutation<{ deleteLedger: string }, { id: string }>( DELETE_LEDGER, options ); }