Skip to content

Commit

Permalink
Allow instantiating an api Agent with a string or URL (bluesky-social…
Browse files Browse the repository at this point in the history
…#2917)

* Allow instantiating an api Agent with a string or URL

* changeset

* Update .changeset/stupid-spiders-buy.md

Co-authored-by: Bigint <[email protected]>

---------

Co-authored-by: Bigint <[email protected]>
  • Loading branch information
matthieusieben and bigint authored Nov 7, 2024
1 parent 1982693 commit 839202a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-spiders-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/api": patch
---

Allow instantiating an API Agent with a string or URL
23 changes: 19 additions & 4 deletions packages/api/src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { TID } from '@atproto/common-web'
import { AtUri, ensureValidDid } from '@atproto/syntax'
import { buildFetchHandler, FetchHandler, XrpcClient } from '@atproto/xrpc'
import {
buildFetchHandler,
BuildFetchHandlerOptions,
FetchHandler,
XrpcClient,
} from '@atproto/xrpc'
import AwaitLock from 'await-lock'
import {
AppBskyActorDefs,
Expand Down Expand Up @@ -105,8 +110,16 @@ export class Agent extends XrpcClient {
return this
}

constructor(readonly sessionManager: SessionManager) {
const fetchHandler = buildFetchHandler(sessionManager)
readonly sessionManager: SessionManager

constructor(options: string | URL | SessionManager) {
const sessionManager: SessionManager =
typeof options === 'string' || options instanceof URL
? {
did: undefined,
fetchHandler: buildFetchHandler(options),
}
: options

super((url, init) => {
const headers = new Headers(init?.headers)
Expand All @@ -128,8 +141,10 @@ export class Agent extends XrpcClient {
.join(', '),
)

return fetchHandler(url, { ...init, headers })
return this.sessionManager.fetchHandler(url, { ...init, headers })
}, schemas)

this.sessionManager = sessionManager
}

//#region Cloning utilities
Expand Down

0 comments on commit 839202a

Please sign in to comment.