Skip to content

Commit

Permalink
refactor: proof api (#10647)
Browse files Browse the repository at this point in the history
* fix: proof api requets

* fix: nextid api cache
  • Loading branch information
guanbinrui committed Aug 28, 2023
1 parent 72daa31 commit 0aa5e13
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/web3-providers/src/DSearch/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { fetchCachedJSON } from '../helpers/fetchJSON.js'
export function fetchFromDSearch<T>(request: RequestInfo | URL, init?: RequestInit) {
return fetchCachedJSON<T>(request, init, {
squashExpiration: 0,
cacheDuration: Duration.MEDIUM,
cacheDuration: Duration.THIRTY_MINUTES,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class DeBankFungibleTokenAPI implements FungibleTokenAPI.Provider<ChainId
}),
undefined,
{
cacheDuration: Duration.MINIMAL,
cacheDuration: Duration.TEN_SECONDS,
},
)

Expand Down
14 changes: 8 additions & 6 deletions packages/web3-providers/src/NextID/proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { staleNextIDCached } from './helpers.js'
import PRESET_LENS from './preset-lens.json'
import { fetchCachedJSON, fetchJSON, fetchSquashedJSON } from '../helpers/fetchJSON.js'
import type { NextIDBaseAPI } from '../entry-types.js'
import { Duration, Expiration } from '../entry-helpers.js'

const BASE_URL =
env.channel === 'stable' && process.env.NODE_ENV === 'production' ? PROOF_BASE_URL_PROD : PROOF_BASE_URL_DEV
Expand Down Expand Up @@ -223,7 +224,10 @@ const getExistedBindingQueryURL = (platform: string, identity: string, personaPu

export class NextIDProofAPI implements NextIDBaseAPI.Proof {
fetchFromProofService<T>(request: Request | RequestInfo, init?: RequestInit) {
return fetchCachedJSON<T>(request, init)
return fetchCachedJSON<T>(request, init, {
squashExpiration: Expiration.THIRTY_MINUTES,
cacheDuration: Duration.THIRTY_MINUTES,
})
}

async clearPersonaQueryCache(personaPublicKey: string) {
Expand Down Expand Up @@ -276,9 +280,9 @@ export class NextIDProofAPI implements NextIDBaseAPI.Proof {
}

async queryExistedBindingByPersona(personaPublicKey: string) {
const url = getPersonaQueryURL(NextIDPlatform.NextID, personaPublicKey)

const { ids } = await this.fetchFromProofService<NextIDBindings>(url)
const { ids } = await this.fetchFromProofService<NextIDBindings>(
getPersonaQueryURL(NextIDPlatform.NextID, personaPublicKey),
)
// Will have only one item when query by personaPublicKey
return first(ids)
}
Expand All @@ -293,8 +297,6 @@ export class NextIDProofAPI implements NextIDBaseAPI.Proof {
page,
exact: true,
// TODO workaround for the API, and will sort the result manually
// sort: 'activated_at',
// order: 'desc',
}),
)

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-providers/src/R2D2/apis/TokenListAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const fetchTokenList = memoizePromise(
url,
{ cache: 'default' },
{
cacheDuration: Duration.LONG,
cacheDuration: Duration.TWELVE_HOURS,
},
)
},
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-providers/src/SmartPay/apis/BundlerAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class SmartPayBundlerAPI implements BundlerAPI.Provider {
method: 'GET',
},
{
cacheDuration: Duration.LONG,
cacheDuration: Duration.TWELVE_HOURS,
},
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-providers/src/Twitter/apis/getUserViaWebAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export async function getUserViaWebAPI(screenName: string): Promise<TwitterBaseA
if (!request) return null

const response = await fetchGlobal(request, undefined, {
cacheDuration: Duration.SHORT,
squashExpiration: Expiration.SHORT,
cacheDuration: Duration.ONE_MINUTE,
squashExpiration: Expiration.ONE_SECOND,
})
if (response.ok) {
const json: TwitterBaseAPI.UserByScreenNameResponse = await response.json()
Expand Down
13 changes: 7 additions & 6 deletions packages/web3-providers/src/helpers/fetchCached.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import type { Fetcher } from './fetch.js'
const { fetch: originalFetch } = globalThis

export enum Duration {
MINIMAL = 10000, // 10 seconds
SHORT = 60000, // 1 min
MEDIUM = 1800000, // 30 mins
LONG = 43200000, // 12 hours
TEN_SECONDS = 10000,
ONE_MINUTE = 60000,
THIRTY_MINUTES = 1800000,
TWELVE_HOURS = 43200000,
ONE_DAY = 86400000,
}

function __open__(url: string) {
Expand Down Expand Up @@ -56,7 +57,7 @@ export async function fetchCached(
input: RequestInfo | URL,
init?: RequestInit,
next = originalFetch,
duration = Duration.SHORT,
duration = Duration.ONE_MINUTE,
): Promise<Response> {
// why: the caches doesn't define in test env
if (process.env.NODE_ENV === 'test') return next(input, init)
Expand Down Expand Up @@ -89,7 +90,7 @@ export async function staleCached(info: RequestInfo | URL, init?: RequestInit):

export function createFetchCached({
next = originalFetch,
duration = Duration.SHORT,
duration = Duration.ONE_MINUTE,
}: {
next?: Fetcher
duration?: number
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-providers/src/helpers/fetchChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export interface ChainConfig {
export async function fetchChains() {
return fetchCachedJSON<ChainConfig[]>('https://chainid.network/chains.json', undefined, {
squashExpiration: 0,
cacheDuration: Duration.LONG,
cacheDuration: Duration.TWELVE_HOURS,
})
}
6 changes: 3 additions & 3 deletions packages/web3-providers/src/helpers/fetchJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function fetchSquashedJSON<T = unknown>(
options?: NextFetchersOptions,
): Promise<T> {
return fetchJSON<T>(input, init, {
squashExpiration: Expiration.SHORT,
squashExpiration: Expiration.ONE_SECOND,
...options,
})
}
Expand All @@ -30,8 +30,8 @@ export async function fetchCachedJSON<T = unknown>(
options?: NextFetchersOptions,
): Promise<T> {
return fetchJSON<T>(input, init, {
squashExpiration: Expiration.SHORT,
cacheDuration: Duration.SHORT,
squashExpiration: Expiration.ONE_SECOND,
cacheDuration: Duration.ONE_MINUTE,
...options,
})
}
7 changes: 5 additions & 2 deletions packages/web3-providers/src/helpers/fetchSquashed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const { fetch: originalFetch } = globalThis

export enum Expiration {
SHORT = 1000,
ONE_SECOND = 1000,
ONE_MINUTE = 60000,
THIRTY_MINUTES = 1800000,
ONE_HOUR = 3600000,
}

const CACHE = new Map<
Expand Down Expand Up @@ -38,7 +41,7 @@ export async function fetchSquashed(
init?: RequestInit,
next = originalFetch,
resolver = defaultResolver,
expiration = Expiration.SHORT,
expiration = Expiration.ONE_SECOND,
): Promise<Response> {
// why: the caches doesn't define in test env
if (process.env.NODE_ENV === 'test') return next(input, init)
Expand Down

0 comments on commit 0aa5e13

Please sign in to comment.