Skip to content

Commit

Permalink
add X-Sequence-Token-Key header support in http fetch clients (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylix authored Oct 13, 2023
1 parent 1ca333d commit 8df5dc6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
16 changes: 13 additions & 3 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const fetch = typeof global === 'object' ? global.fetch : window.fetch
export class SequenceAPIClient extends ApiRpc {
constructor(
hostname: string,
public jwtAuth?: string
public authorization?: {
jwtAuth?: string
tokenKey?: string
}
) {
super(hostname.endsWith('/') ? hostname.slice(0, -1) : hostname, fetch)
this.fetch = this._fetch
Expand All @@ -17,8 +20,15 @@ export class SequenceAPIClient extends ApiRpc {
// automatically include jwt auth header to requests
// if its been set on the api client
const headers: { [key: string]: any } = {}
if (this.jwtAuth && this.jwtAuth.length > 0) {
headers['Authorization'] = `BEARER ${this.jwtAuth}`

const {jwtAuth, tokenKey} = this.authorization || {}

if (jwtAuth && jwtAuth.length > 0) {
headers['Authorization'] = `BEARER ${jwtAuth}`
}

if (tokenKey && tokenKey.length > 0) {
headers['X-Sequence-Token-Key'] = `${tokenKey}`
}

// before the request is made
Expand Down
6 changes: 4 additions & 2 deletions packages/auth/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ export class Services {
}
}

return new SequenceAPIClient(url, jwtAuth)
return new SequenceAPIClient(url, {
jwtAuth
})
})()

return this._initialAuthRequest
Expand Down Expand Up @@ -221,7 +223,7 @@ export class Services {
if (!url) throw Error('No sequence api url')

const jwtAuth = (await this.getJWT(tryAuth)).token
this.apiClient = new SequenceAPIClient(url, jwtAuth)
this.apiClient = new SequenceAPIClient(url, { jwtAuth })
}

return this.apiClient
Expand Down
16 changes: 13 additions & 3 deletions packages/indexer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const fetch = typeof global === 'object' ? global.fetch : window.fetch
export class SequenceIndexerClient extends IndexerRpc {
constructor(
hostname: string,
public jwtAuth?: string
public authorization?: {
jwtAuth?: string
tokenKey?: string
}
) {
super(hostname.endsWith('/') ? hostname.slice(0, -1) : hostname, fetch)
this.fetch = this._fetch
Expand All @@ -39,8 +42,15 @@ export class SequenceIndexerClient extends IndexerRpc {
// automatically include jwt auth header to requests
// if its been set on the api client
const headers: { [key: string]: any } = {}
if (this.jwtAuth && this.jwtAuth.length > 0) {
headers['Authorization'] = `BEARER ${this.jwtAuth}`

const {jwtAuth, tokenKey} = this.authorization || {}

if (jwtAuth && jwtAuth.length > 0) {
headers['Authorization'] = `BEARER ${jwtAuth}`
}

if (tokenKey && tokenKey.length > 0) {
headers['X-Sequence-Token-Key'] = `${tokenKey}`
}

// before the request is made
Expand Down

0 comments on commit 8df5dc6

Please sign in to comment.