Skip to content

Commit 4dbc426

Browse files
authored
Add jwt to SequenceHooksConfig so api, metadata, and indexer clients can pass it correctly (#377)
* Store jwt value on context and pass to api, metadata, and indexer clients * Exposing setJWT on SequenceHooksConfig context * Allow jwt to be set via provider props
1 parent 7ddd968 commit 4dbc426

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

packages/hooks/src/contexts/ConfigContext.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { createContext } from 'react'
3+
import { createContext, useState } from 'react'
44

55
export interface SequenceHooksEnv {
66
indexerGatewayUrl: string
@@ -13,11 +13,14 @@ export interface SequenceHooksEnv {
1313
export interface SequenceHooksConfigProviderValue {
1414
projectAccessKey: string
1515
env?: Partial<SequenceHooksEnv>
16+
jwt?: string
1617
}
1718

1819
export interface SequenceHooksConfig {
1920
projectAccessKey: string
2021
env: Required<SequenceHooksEnv>
22+
jwt: string | undefined
23+
setJWT: React.Dispatch<React.SetStateAction<string | undefined>>
2124
}
2225

2326
const defaultEnv: Required<SequenceHooksEnv> = {
@@ -36,12 +39,16 @@ interface SequenceHooksProviderProps {
3639
}
3740

3841
export const SequenceHooksProvider = (props: SequenceHooksProviderProps) => {
39-
const config = {
42+
const [jwt, setJWT] = useState<string | undefined>(props.config.jwt)
43+
44+
const config: SequenceHooksConfig = {
4045
...props.config,
4146
env: {
4247
...defaultEnv,
4348
...props.config.env
44-
}
49+
},
50+
jwt,
51+
setJWT
4552
}
4653

4754
return <SequenceHooksContext.Provider value={config}>{props.children}</SequenceHooksContext.Provider>

packages/hooks/src/hooks/API/useAPIClient.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import { useMemo } from 'react'
44
import { useConfig } from '../useConfig'
55

66
export const useAPIClient = () => {
7-
const { projectAccessKey, env } = useConfig()
7+
const { projectAccessKey, jwt, env } = useConfig()
88

99
const apiClient = useMemo(() => {
10-
const clientUrl = env.apiUrl
11-
12-
return new SequenceAPIClient(clientUrl, projectAccessKey)
13-
}, [projectAccessKey])
10+
return new SequenceAPIClient(env.apiUrl, projectAccessKey, jwt)
11+
}, [projectAccessKey, jwt])
1412

1513
return apiClient
1614
}

packages/hooks/src/hooks/Indexer/useIndexerClient.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import { useConfig } from '../useConfig'
77
import { useNetwork } from '../useNetwork'
88

99
export const useIndexerClient = (chainId: ChainId) => {
10-
const { projectAccessKey, env } = useConfig()
10+
const { env, projectAccessKey, jwt } = useConfig()
1111

1212
const indexerClients = useMemo(() => {
1313
return new Map<ChainId, SequenceIndexer>()
14-
}, [projectAccessKey])
14+
}, [projectAccessKey, jwt])
1515

1616
const network = useNetwork(chainId)
1717
const indexerUrl = envString(env.indexerUrl, 'indexer', network.name)
1818

1919
if (!indexerClients.has(chainId)) {
20-
indexerClients.set(chainId, new SequenceIndexer(indexerUrl, projectAccessKey))
20+
indexerClients.set(chainId, new SequenceIndexer(indexerUrl, projectAccessKey, jwt))
2121
}
2222

2323
const indexerClient = indexerClients.get(chainId)
@@ -30,11 +30,11 @@ export const useIndexerClient = (chainId: ChainId) => {
3030
}
3131

3232
export const useIndexerClients = (chainIds: number[]) => {
33-
const { projectAccessKey, env } = useConfig()
33+
const { env, projectAccessKey, jwt } = useConfig()
3434

3535
const indexerClients = useMemo(() => {
3636
return new Map<ChainId, SequenceIndexer>()
37-
}, [projectAccessKey])
37+
}, [projectAccessKey, jwt])
3838

3939
const result = new Map<ChainId, SequenceIndexer>()
4040

@@ -43,7 +43,7 @@ export const useIndexerClients = (chainIds: number[]) => {
4343
const indexerUrl = envString(env.indexerUrl, 'indexer', network.name)
4444

4545
if (!indexerClients.has(chainId)) {
46-
indexerClients.set(chainId, new SequenceIndexer(indexerUrl, projectAccessKey))
46+
indexerClients.set(chainId, new SequenceIndexer(indexerUrl, projectAccessKey, jwt))
4747
}
4848

4949
const indexerClient = indexerClients.get(chainId)

packages/hooks/src/hooks/IndexerGateway/useIndexerGatewayClient.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import { useMemo } from 'react'
44
import { useConfig } from '../useConfig'
55

66
export const useIndexerGatewayClient = () => {
7-
const { projectAccessKey, env } = useConfig()
7+
const { env, projectAccessKey, jwt } = useConfig()
88

99
const indexerGatewayClient = useMemo(() => {
10-
const clientUrl = env.indexerGatewayUrl
11-
12-
return new SequenceIndexerGateway(clientUrl, projectAccessKey)
13-
}, [projectAccessKey])
10+
return new SequenceIndexerGateway(env.indexerGatewayUrl, projectAccessKey, jwt)
11+
}, [projectAccessKey, jwt])
1412

1513
return indexerGatewayClient
1614
}

packages/hooks/src/hooks/Metadata/useMetadataClient.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import { useMemo } from 'react'
44
import { useConfig } from '../useConfig'
55

66
export const useMetadataClient = () => {
7-
const { projectAccessKey, env } = useConfig()
7+
const { env, projectAccessKey, jwt } = useConfig()
88

99
const metadataClient = useMemo(() => {
10-
const clientUrl = env.metadataUrl
11-
12-
return new SequenceMetadata(clientUrl, projectAccessKey)
13-
}, [projectAccessKey])
10+
return new SequenceMetadata(env.metadataUrl, projectAccessKey)
11+
}, [projectAccessKey, jwt])
1412

1513
return metadataClient
1614
}

0 commit comments

Comments
 (0)