Skip to content

Commit

Permalink
feat(api): expose service url getter on AtpAgent class
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed May 2, 2024
1 parent 42101f2 commit 918774c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 29 deletions.
15 changes: 10 additions & 5 deletions packages/api/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { AtpClient } from './client'
import { BSKY_LABELER_DID } from './const'
import { AtpDispatcher } from './dispatcher/atp-dispatcher'
import {
ServiceDispatcher,
ServiceDispatcherOptions,
} from './dispatcher/service-dispatcher'
StatelessDispatcher,
StatelessDispatcherOptions,
} from './dispatcher/stateless-dispatcher'
import { AtpAgentGlobalOpts, AtprotoServiceType } from './types'

const MAX_LABELERS = 10
Expand Down Expand Up @@ -34,11 +34,11 @@ export class AtpAgent {
return this.api.com
}

constructor(options: AtpDispatcher | ServiceDispatcherOptions) {
constructor(options: AtpDispatcher | StatelessDispatcherOptions) {
this.dispatcher =
options instanceof AtpDispatcher
? options
: new ServiceDispatcher(options)
: new StatelessDispatcher(options)

this.api = new AtpClient(this.dispatcher)
this.api.setHeader('atproto-accept-labelers', () =>
Expand Down Expand Up @@ -70,6 +70,11 @@ export class AtpAgent {
return inst
}

async getServiceUrl(): Promise<URL> {
// Clone to prevent mutation of the original dispatcher's URL
return this.dispatcher.getServiceUrl()
}

/**
* Get the active session's repo DID
*/
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/dispatcher/atp-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { XrpcDispatcher } from '@atproto/xrpc'

export abstract class AtpDispatcher extends XrpcDispatcher {
abstract getRepo(): string | PromiseLike<string>
abstract getServiceUrl(): URL | PromiseLike<URL>
}
2 changes: 1 addition & 1 deletion packages/api/src/dispatcher/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './atp-dispatcher'
export * from './service-dispatcher'
export * from './session-dispatcher'
export * from './stateless-dispatcher'
14 changes: 0 additions & 14 deletions packages/api/src/dispatcher/service-dispatcher.ts

This file was deleted.

14 changes: 9 additions & 5 deletions packages/api/src/dispatcher/session-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ export class SessionDispatcher extends AtpDispatcher {
// service endpoint.
this.client = new AtpClient({
fetch: async (request) => (0, this.fetch)(request),
service: () => this.serviceUrl,
service: () => this.getServiceUrl(),
headers: {
authorization: () =>
this.session?.accessJwt && `Bearer ${this.session.accessJwt}`,
},
})
}

getDispatchUri(url) {
return new URL(url, this.pdsUrl || this.serviceUrl)
getServiceUrl() {
return this.serviceUrl
}

getDispatchUrl() {
return this.pdsUrl || this.serviceUrl
}

/**
Expand All @@ -79,7 +83,7 @@ export class SessionDispatcher extends AtpDispatcher {
// wait for any active session-refreshes to finish
await this.refreshSessionPromise

const initialUri = this.getDispatchUri(url)
const initialUri = new URL(url, this.getDispatchUrl())
const initialReq = new Request(initialUri, reqInit)

const initialToken = this.session?.accessJwt
Expand Down Expand Up @@ -122,7 +126,7 @@ export class SessionDispatcher extends AtpDispatcher {
await initialRes.body?.cancel()

// We need to re-compute the URI in case the PDS endpoint has changed
const updatedUri = this.getDispatchUri(url)
const updatedUri = new URL(url, this.getDispatchUrl())
const updatedReq = new Request(updatedUri, reqInit)

updatedReq.headers.set('authorization', `Bearer ${updatedToken}`)
Expand Down
19 changes: 19 additions & 0 deletions packages/api/src/dispatcher/stateless-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AtpDispatcher } from './atp-dispatcher'

export type StatelessDispatcherOptions = {
service: string | URL
headers?: { [_ in string]?: null | string }
}

export class StatelessDispatcher extends AtpDispatcher {
getServiceUrl: () => URL | PromiseLike<URL>

constructor({ service, headers }: StatelessDispatcherOptions) {
super({ service, headers })
this.getServiceUrl = () => new URL(service)
}

async getRepo(): Promise<string> {
throw new Error('Not logged in')
}
}
5 changes: 4 additions & 1 deletion packages/xrpc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { ValidationError } from '@atproto/lexicon'

export type QueryParams = Record<string, any>
export type Headers = Record<string, string>
export type Gettable<V> = V | (() => undefined | V | PromiseLike<undefined | V>)
export type Awaitable<V> = V | PromiseLike<V>
export type Gettable<V, GetterFallback = undefined> =
| V
| (() => Awaitable<V | GetterFallback>)

export interface CallOptions {
encoding?: string
Expand Down
10 changes: 7 additions & 3 deletions packages/xrpc/src/xrpc-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export type Dispatch = (
init: RequestInit,
) => Promise<Response>

export type XrpcDispatcherOptions = Dispatch | DispatchConfig | string | URL
export type XrpcDispatcherOptions =
| Dispatch
| BuildDispatchOptions
| string
| URL

/**
* Default {@link FetchAgent} implementation that uses WHATWG's `fetch` API and
Expand Down Expand Up @@ -63,13 +67,13 @@ export class XrpcDispatcher {
}
}

export type DispatchConfig = {
export type BuildDispatchOptions = {
/**
* The service URL to make requests to. This can be a string, URL, or a
* function that returns a string or URL. This is useful for dynamic URLs,
* such as a service URL that changes based on authentication.
*/
service: Gettable<string | URL>
service: Gettable<string | URL, never>

/**
* Headers to be added to every request. If a function is provided, it will be
Expand Down

0 comments on commit 918774c

Please sign in to comment.