Skip to content

Commit

Permalink
chore: deprecate api headers (#3764)
Browse files Browse the repository at this point in the history
  • Loading branch information
magiziz authored Feb 12, 2025
1 parent 7051a08 commit 73fbd0f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 26 deletions.
23 changes: 23 additions & 0 deletions .changeset/empty-teachers-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit-core': patch
'@reown/appkit-adapter-bitcoin': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Deprecated api headers to use query parameters
51 changes: 30 additions & 21 deletions packages/core/src/controllers/BlockchainApiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ const state = proxy<BlockchainApiControllerState>({
export const BlockchainApiController = {
state,

getSdkProperties() {
const { sdkType, sdkVersion } = OptionsController.state

return {
st: sdkType || 'unknown',
sv: sdkVersion || 'unknown'
}
},

async isNetworkSupported(network?: CaipNetworkId) {
if (!network) {
return false
Expand Down Expand Up @@ -287,7 +296,7 @@ export const BlockchainApiController = {
tokenAddress,
userAddress
}: BlockchainApiSwapAllowanceRequest) {
const { sdkType, sdkVersion } = OptionsController.state
const { st, sv } = BlockchainApiController.getSdkProperties()

const isSupported = await BlockchainApiController.isNetworkSupported(
ChainController.state.activeCaipNetwork?.caipNetworkId
Expand All @@ -301,18 +310,18 @@ export const BlockchainApiController = {
params: {
projectId,
tokenAddress,
userAddress
userAddress,
st,
sv
},
headers: {
'Content-Type': 'application/json',
'x-sdk-type': sdkType,
'x-sdk-version': sdkVersion || 'html-wagmi-4.2.2'
'Content-Type': 'application/json'
}
})
},

async fetchGasPrice({ projectId, chainId }: BlockchainApiGasPriceRequest) {
const { sdkType, sdkVersion } = OptionsController.state
const { st, sv } = BlockchainApiController.getSdkProperties()

const isSupported = await BlockchainApiController.isNetworkSupported(
ChainController.state.activeCaipNetwork?.caipNetworkId
Expand All @@ -324,13 +333,13 @@ export const BlockchainApiController = {
return state.api.get<BlockchainApiGasPriceResponse>({
path: `/v1/convert/gas-price`,
headers: {
'Content-Type': 'application/json',
'x-sdk-type': sdkType,
'x-sdk-version': sdkVersion || 'html-wagmi-4.2.2'
'Content-Type': 'application/json'
},
params: {
projectId,
chainId
chainId,
st,
sv
}
})
},
Expand Down Expand Up @@ -373,7 +382,7 @@ export const BlockchainApiController = {
to,
userAddress
}: BlockchainApiGenerateApproveCalldataRequest) {
const { sdkType, sdkVersion } = OptionsController.state
const { st, sv } = BlockchainApiController.getSdkProperties()

const isSupported = await BlockchainApiController.isNetworkSupported(
ChainController.state.activeCaipNetwork?.caipNetworkId
Expand All @@ -385,21 +394,22 @@ export const BlockchainApiController = {
return state.api.get<BlockchainApiGenerateApproveCalldataResponse>({
path: `/v1/convert/build-approve`,
headers: {
'Content-Type': 'application/json',
'x-sdk-type': sdkType,
'x-sdk-version': sdkVersion || 'html-wagmi-4.2.2'
'Content-Type': 'application/json'
},
params: {
projectId,
userAddress,
from,
to
to,
st,
sv
}
})
},

async getBalance(address: string, chainId?: string, forceUpdate?: string) {
const { sdkType, sdkVersion } = OptionsController.state
const { st, sv } = BlockchainApiController.getSdkProperties()

const isSupported = await BlockchainApiController.isNetworkSupported(
ChainController.state.activeCaipNetwork?.caipNetworkId
)
Expand All @@ -409,15 +419,14 @@ export const BlockchainApiController = {

return state.api.get<BlockchainApiBalanceResponse>({
path: `/v1/account/${address}/balance`,
headers: {
'x-sdk-type': sdkType,
'x-sdk-version': sdkVersion || 'html-wagmi-4.2.2'
},

params: {
currency: 'usd',
projectId: OptionsController.state.projectId,
chainId,
forceUpdate
forceUpdate,
st,
sv
}
})
},
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/controllers/EventsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ export const EventsController = {
return sub(state, () => callback(state))
},

_getApiHeaders() {
getSdkProperties() {
const { projectId, sdkType, sdkVersion } = OptionsController.state

return {
'x-project-id': projectId,
'x-sdk-type': sdkType,
'x-sdk-version': sdkVersion || 'html-wagmi-4.2.2'
projectId,
st: sdkType,
sv: sdkVersion || 'html-wagmi-4.2.2'
}
},

Expand All @@ -58,7 +58,7 @@ export const EventsController = {

await api.post({
path: '/e',
headers: EventsController._getApiHeaders(),
params: EventsController.getSdkProperties(),
body: {
eventId: CoreHelperUtil.getUUID(),
url: window.location.href,
Expand Down
29 changes: 29 additions & 0 deletions packages/core/tests/controllers/BlockchainApiController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, it, vi } from 'vitest'

import { BlockchainApiController, FetchUtil } from '../../exports/index.js'

// -- Tests --------------------------------------------------------------------
describe('BlockchainApiController', () => {
it('should include sdk properties when fetching blockchain data', async () => {
vi.spyOn(FetchUtil.prototype, 'get').mockResolvedValue({})
vi.spyOn(BlockchainApiController, 'isNetworkSupported').mockResolvedValue(true)

const swapAllowance = {
projectId: '123',
tokenAddress: '0x123',
userAddress: '0x456'
}

await BlockchainApiController.fetchSwapAllowance(swapAllowance)

expect(FetchUtil.prototype.get).toHaveBeenCalledWith(
expect.objectContaining({
path: '/v1/convert/allowance',
params: {
...swapAllowance,
...BlockchainApiController.getSdkProperties()
}
})
)
})
})
16 changes: 16 additions & 0 deletions packages/core/tests/controllers/EventsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,20 @@ describe('EventsController', () => {

expect(EventsController.state.reportedErrors['FORBIDDEN']).toBe(true)
})

it('should include sdk properties when sending an analytics event', async () => {
vi.spyOn(FetchUtil.prototype, 'post').mockResolvedValue({})

await EventsController._sendAnalyticsEvent({
...EventsController.state,
data: { type: 'track', event: 'MODAL_CLOSE', properties: { connected: true } }
})

expect(FetchUtil.prototype.post).toHaveBeenCalledWith(
expect.objectContaining({
path: '/e',
params: EventsController.getSdkProperties()
})
)
})
})

0 comments on commit 73fbd0f

Please sign in to comment.