Skip to content

Commit

Permalink
refactor: move api functions to core/api (#1700)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschlabach authored Dec 9, 2024
1 parent 5fe64c8 commit f9b12f4
Show file tree
Hide file tree
Showing 38 changed files with 91 additions and 82 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@
"default": "./esm/index.js"
},
"./api": {
"types": "./esm/api/index.d.ts",
"module": "./esm/api/index.js",
"import": "./esm/api/index.js",
"default": "./esm/api/index.js"
"types": "./esm/core/api/index.d.ts",
"module": "./esm/core/api/index.js",
"import": "./esm/core/api/index.js",
"default": "./esm/core/api/index.js"
},
"./checkout": {
"types": "./esm/checkout/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/checkout/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ContractFunctionParameters, TransactionReceipt } from 'viem';
import type { Address } from 'viem';
import type { Config } from 'wagmi';
import type { PayTransaction } from '../api/types';
import type { PayTransaction } from '../core/api/types';
import type { TransactionError } from '../transaction';

/**
Expand Down
5 changes: 3 additions & 2 deletions src/checkout/utils/handlePayRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Address } from 'viem';
import { type Mock, describe, expect, it, vi } from 'vitest';
import { buildPayTransaction } from '../../api';

import { buildPayTransaction } from '../../core/api';
import { handlePayRequest } from './handlePayRequest';

vi.mock('../../api', () => ({
vi.mock('../../core/api', () => ({
buildPayTransaction: vi.fn(),
}));

Expand Down
4 changes: 2 additions & 2 deletions src/checkout/utils/handlePayRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { buildPayTransaction } from '../../api';
import type { BuildPayTransactionParams } from '../../api';
import { buildPayTransaction } from '../../core/api';
import type { BuildPayTransactionParams } from '../../core/api';
import type { HandlePayRequestParams } from '../types';

export const handlePayRequest = async ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Mock, describe, expect, it, vi } from 'vitest';
import { CDP_MINT_TOKEN } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { CDP_MINT_TOKEN } from '../../network/definitions/nft';
import { sendRequest } from '../../network/request';
import { buildMintTransaction } from './buildMintTransaction';
import type { BuildMintTransactionParams } from './types';

vi.mock('../network/request', () => ({
vi.mock('../../network/request', () => ({
sendRequest: vi.fn(),
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CDP_MINT_TOKEN } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { CDP_MINT_TOKEN } from '../../network/definitions/nft';
import { sendRequest } from '../../network/request';
import type {
BuildMintTransactionParams,
BuildMintTransactionResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { Mock } from 'vitest';
import {
CHECKOUT_INVALID_CHARGE_ERROR_MESSAGE,
UNCAUGHT_CHECKOUT_ERROR_MESSAGE,
} from '../checkout/constants';
} from '../../checkout/constants';
import {
CDP_CREATE_PRODUCT_CHARGE,
CDP_HYDRATE_CHARGE,
} from '../network/definitions/pay';
import { sendRequest } from '../network/request';
} from '../../network/definitions/pay';
import { sendRequest } from '../../network/request';
/**
* @vitest-environment node
*/
Expand All @@ -28,7 +28,7 @@ import type {
HydrateChargeAPIParams,
} from './types';

vi.mock('../network/request');
vi.mock('../../network/request');

describe('buildPayTransaction', () => {
afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
CDP_CREATE_PRODUCT_CHARGE,
CDP_HYDRATE_CHARGE,
} from '../network/definitions/pay';
import { type JSONRPCResult, sendRequest } from '../network/request';
} from '../../network/definitions/pay';
import { type JSONRPCResult, sendRequest } from '../../network/request';
import type {
BuildPayTransactionParams,
BuildPayTransactionResponse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { CDP_GET_SWAP_TRADE } from '../network/definitions/swap';
import { sendRequest } from '../network/request';
import { DEGEN_TOKEN, ETH_TOKEN } from '../swap/mocks';
import type { BuildSwapTransaction } from '../swap/types';
import { CDP_GET_SWAP_TRADE } from '../../network/definitions/swap';
import { sendRequest } from '../../network/request';
import { DEGEN_TOKEN, ETH_TOKEN } from '../../swap/mocks';
import { buildSwapTransaction } from './buildSwapTransaction';
import type { BuildSwapTransaction } from './types';
/**
* @vitest-environment node
*/
import { getAPIParamsForToken } from './utils/getAPIParamsForToken';
import { getSwapTransaction } from './utils/getSwapTransaction';

vi.mock('../network/request');
vi.mock('../../network/request');

const testFromAddress = '0x6Cd01c0F55ce9E0Bf78f5E90f72b4345b16d515d';
const testAmount = '3305894409732200';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CDP_GET_SWAP_TRADE } from '../network/definitions/swap';
import { sendRequest } from '../network/request';
import type { SwapAPIResponse } from '../swap/types';
import { getSwapErrorCode } from '../swap/utils/getSwapErrorCode';
import { CDP_GET_SWAP_TRADE } from '../../network/definitions/swap';
import { sendRequest } from '../../network/request';
import type { SwapAPIResponse } from '../../swap/types';
import { getSwapErrorCode } from '../../swap/utils/getSwapErrorCode';
import type {
APIError,
BuildSwapTransactionParams,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Mock, describe, expect, it, vi } from 'vitest';
import { CDP_GET_MINT_DETAILS } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { CDP_GET_MINT_DETAILS } from '../../network/definitions/nft';
import { sendRequest } from '../../network/request';
import { getMintDetails } from './getMintDetails';
import type { GetMintDetailsParams } from './types';

vi.mock('../network/request', () => ({
vi.mock('../../network/request', () => ({
sendRequest: vi.fn(),
}));

Expand Down
4 changes: 2 additions & 2 deletions src/api/getMintDetails.ts → src/core/api/getMintDetails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CDP_GET_MINT_DETAILS } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { CDP_GET_MINT_DETAILS } from '../../network/definitions/nft';
import { sendRequest } from '../../network/request';
import type { GetMintDetailsParams, GetMintDetailsResponse } from './types';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { type Mock, afterEach, describe, expect, it, vi } from 'vitest';
import { CDP_GET_SWAP_QUOTE } from '../network/definitions/swap';
import { sendRequest } from '../network/request';
import { DEGEN_TOKEN, ETH_TOKEN } from '../swap/mocks';
import { CDP_GET_SWAP_QUOTE } from '../../network/definitions/swap';
import { sendRequest } from '../../network/request';
import { DEGEN_TOKEN, ETH_TOKEN } from '../../swap/mocks';
/**
* @vitest-environment node
*/
import { getSwapQuote } from './getSwapQuote';
import { getAPIParamsForToken } from './utils/getAPIParamsForToken';

vi.mock('../network/request');
vi.mock('../../network/request');

const testAmount = '3305894409732200';
const testAmountReference = 'from';
Expand Down
8 changes: 4 additions & 4 deletions src/api/getSwapQuote.ts → src/core/api/getSwapQuote.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CDP_GET_SWAP_QUOTE } from '../network/definitions/swap';
import { sendRequest } from '../network/request';
import type { SwapQuote } from '../swap/types';
import { getSwapErrorCode } from '../swap/utils/getSwapErrorCode';
import { CDP_GET_SWAP_QUOTE } from '../../network/definitions/swap';
import { sendRequest } from '../../network/request';
import type { SwapQuote } from '../../swap/types';
import { getSwapErrorCode } from '../../swap/utils/getSwapErrorCode';
import type {
APIError,
GetSwapQuoteParams,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Mock, describe, expect, it, vi } from 'vitest';
import { CDP_GET_TOKEN_DETAILS } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { CDP_GET_TOKEN_DETAILS } from '../../network/definitions/nft';
import { sendRequest } from '../../network/request';
import { getTokenDetails } from './getTokenDetails';
import type { GetTokenDetailsParams } from './types';

vi.mock('../network/request', () => ({
vi.mock('../../network/request', () => ({
sendRequest: vi.fn(),
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CDP_GET_TOKEN_DETAILS } from '../network/definitions/nft';
import { sendRequest } from '../network/request';
import { CDP_GET_TOKEN_DETAILS } from '../../network/definitions/nft';
import { sendRequest } from '../../network/request';
import type { GetTokenDetailsParams, GetTokenDetailsResponse } from './types';

/**
Expand Down
6 changes: 3 additions & 3 deletions src/api/getTokens.test.ts → src/core/api/getTokens.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type Mock, afterEach, describe, expect, it, vi } from 'vitest';
import { CDP_LIST_SWAP_ASSETS } from '../network/definitions/swap';
import { sendRequest } from '../network/request';
import { CDP_LIST_SWAP_ASSETS } from '../../network/definitions/swap';
import { sendRequest } from '../../network/request';
/**
* @vitest-environment node
*/
import { getTokens } from './getTokens';

vi.mock('../network/request');
vi.mock('../../network/request');

describe('getTokens', () => {
afterEach(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/api/getTokens.ts → src/core/api/getTokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CDP_LIST_SWAP_ASSETS } from '../network/definitions/swap';
import { sendRequest } from '../network/request';
import type { Token } from '../token/types';
import { CDP_LIST_SWAP_ASSETS } from '../../network/definitions/swap';
import { sendRequest } from '../../network/request';
import type { Token } from '../../token/types';
import type { GetTokensOptions, GetTokensResponse } from './types';

/**
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 8 additions & 3 deletions src/api/types.ts → src/core/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { Address } from 'viem';
import type { ContractType, NFTPrice } from '../nft/types';
import type { Fee, QuoteWarning, SwapQuote, Transaction } from '../swap/types';
import type { Token } from '../token/types';
import type { ContractType, NFTPrice } from '../../nft/types';
import type {
Fee,
QuoteWarning,
SwapQuote,
Transaction,
} from '../../swap/types';
import type { Token } from '../../token/types';

export type AddressOrETH = Address | 'ETH';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
/**
* @vitest-environment node
*/
import { DEGEN_TOKEN, ETH_TOKEN } from '../../swap/mocks';
import { DEGEN_TOKEN, ETH_TOKEN } from '../../../swap/mocks';
import { getAPIParamsForToken } from './getAPIParamsForToken';

describe('getAPIParamsForToken', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatDecimals } from '../../swap/utils/formatDecimals';
import { formatDecimals } from '../../../swap/utils/formatDecimals';
import type {
APIError,
BuildSwapTransactionParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
CHECKOUT_TOO_MANY_REQUESTS_ERROR_MESSAGE,
GENERAL_CHECKOUT_ERROR_MESSAGE,
UNCAUGHT_CHECKOUT_ERROR_MESSAGE,
} from '../../checkout/constants';
} from '../../../checkout/constants';
import { getPayErrorMessage } from './getPayErrorMessage';

describe('getPayErrorMessage', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CHECKOUT_TOO_MANY_REQUESTS_ERROR_MESSAGE,
GENERAL_CHECKOUT_ERROR_MESSAGE,
UNCAUGHT_CHECKOUT_ERROR_MESSAGE,
} from '../../checkout/constants';
} from '../../../checkout/constants';

export function getPayErrorMessage(errorCode?: number) {
if (!errorCode) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Address, Hex } from 'viem';
import type { Transaction } from '../../swap/types';
import type { Transaction } from '../../../swap/types';
import type { RawTransactionData } from '../types';

/**
Expand Down
4 changes: 2 additions & 2 deletions src/nft/hooks/useMintDetails.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { renderHook, waitFor } from '@testing-library/react';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { getMintDetails } from '../../api/getMintDetails';
import { getMintDetails } from '../../core/api/getMintDetails';
import { getNewReactQueryTestProvider } from '../../identity/hooks/getNewReactQueryTestProvider';
import { useMintDetails } from './useMintDetails';

vi.mock('../../api/getMintDetails');
vi.mock('../../core/api/getMintDetails');

describe('useMintDetails', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/nft/hooks/useMintDetails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type UseQueryResult, useQuery } from '@tanstack/react-query';
import { getMintDetails } from '../../api/getMintDetails';
import type { GetMintDetailsParams, MintDetails } from '../../api/types';
import { getMintDetails } from '../../core/api/getMintDetails';
import type { GetMintDetailsParams, MintDetails } from '../../core/api/types';
import { isNFTError } from '../utils/isNFTError';

export function useMintDetails({
Expand Down
4 changes: 2 additions & 2 deletions src/nft/hooks/useTokenDetails.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { renderHook, waitFor } from '@testing-library/react';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { getTokenDetails } from '../../api/getTokenDetails';
import { getTokenDetails } from '../../core/api/getTokenDetails';
import { getNewReactQueryTestProvider } from '../../identity/hooks/getNewReactQueryTestProvider';
import { useTokenDetails } from './useTokenDetails';

vi.mock('../../api/getTokenDetails');
vi.mock('../../core/api/getTokenDetails');

describe('useTokenDetails', () => {
beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/nft/hooks/useTokenDetails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type UseQueryResult, useQuery } from '@tanstack/react-query';
import { getTokenDetails } from '../../api/getTokenDetails';
import type { GetTokenDetailsParams, TokenDetails } from '../../api/types';
import { getTokenDetails } from '../../core/api/getTokenDetails';
import type { GetTokenDetailsParams, TokenDetails } from '../../core/api/types';
import { isNFTError } from '../utils/isNFTError';

export function useTokenDetails({
Expand Down
4 changes: 2 additions & 2 deletions src/nft/utils/buildMintTransactionData.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Address } from 'viem';
import { type Mock, describe, expect, it, vi } from 'vitest';
import { buildMintTransaction as buildMintTransationApi } from '../../api/buildMintTransaction';
import { buildMintTransaction as buildMintTransationApi } from '../../core/api/buildMintTransaction';
import { buildMintTransactionData } from './buildMintTransactionData';

vi.mock('../../api/buildMintTransaction');
vi.mock('../../core/api/buildMintTransaction');

describe('buildMintTransactionData', () => {
const mockBuildMintTransaction = buildMintTransationApi as Mock;
Expand Down
4 changes: 2 additions & 2 deletions src/nft/utils/buildMintTransactionData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Address } from 'viem';
import { buildMintTransaction as buildMintTransactionApi } from '../../api/buildMintTransaction';
import type { BuildMintTransactionParams } from '../../api/types';
import { buildMintTransaction as buildMintTransactionApi } from '../../core/api/buildMintTransaction';
import type { BuildMintTransactionParams } from '../../core/api/types';
import type { Call } from '../../transaction/types';

async function getMintTransaction({
Expand Down
8 changes: 4 additions & 4 deletions src/swap/components/SwapProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { waitForTransactionReceipt } from 'wagmi/actions';
import { base } from 'wagmi/chains';
import { mock } from 'wagmi/connectors';
import { useSendCalls } from 'wagmi/experimental';
import { buildSwapTransaction } from '../../api/buildSwapTransaction';
import { getSwapQuote } from '../../api/getSwapQuote';
import { buildSwapTransaction } from '../../core/api/buildSwapTransaction';
import { getSwapQuote } from '../../core/api/getSwapQuote';
import { useCapabilitiesSafe } from '../../internal/hooks/useCapabilitiesSafe';
import { DEGEN_TOKEN, ETH_TOKEN } from '../mocks';
import { getSwapErrorCode } from '../utils/getSwapErrorCode';
Expand All @@ -32,11 +32,11 @@ vi.mock('../hooks/useResetInputs', () => ({
useResetInputs: () => useCallback(mockResetFunction, []),
}));

vi.mock('../../api/getSwapQuote', () => ({
vi.mock('../../core/api/getSwapQuote', () => ({
getSwapQuote: vi.fn(),
}));

vi.mock('../../api/buildSwapTransaction', () => ({
vi.mock('../../core/api/buildSwapTransaction', () => ({
buildSwapTransaction: vi
.fn()
.mockRejectedValue(new Error('buildSwapTransaction')),
Expand Down
4 changes: 2 additions & 2 deletions src/swap/components/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { base } from 'viem/chains';
import { useAccount, useConfig, useSendTransaction } from 'wagmi';
import { useSwitchChain } from 'wagmi';
import { useSendCalls } from 'wagmi/experimental';
import { buildSwapTransaction } from '../../api/buildSwapTransaction';
import { getSwapQuote } from '../../api/getSwapQuote';
import { buildSwapTransaction } from '../../core/api/buildSwapTransaction';
import { getSwapQuote } from '../../core/api/getSwapQuote';
import { useCapabilitiesSafe } from '../../internal/hooks/useCapabilitiesSafe';
import { useValue } from '../../internal/hooks/useValue';
import { formatTokenAmount } from '../../internal/utils/formatTokenAmount';
Expand Down
Loading

0 comments on commit f9b12f4

Please sign in to comment.