-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(xrpc): replace dispatcher for fetchHandler
- Loading branch information
1 parent
f54fda7
commit 8c3a56f
Showing
17 changed files
with
219 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './session-manager' | ||
export * from './atp-session-manager' | ||
export * from './stateless-session-handler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface SessionManager { | ||
fetchHandler(url: string, reqInit: RequestInit): Promise<Response> | ||
getDid(): string | PromiseLike<string> | ||
|
||
/** @deprecated only used for a very particular use-case in the official Bluesky app */ | ||
getServiceUrl(): URL | PromiseLike<URL> | ||
} | ||
|
||
export function isSessionManager<T>(value: T): value is T & SessionManager { | ||
return ( | ||
value !== null && | ||
typeof value === 'object' && | ||
'fetchHandler' in value && | ||
typeof value.fetchHandler === 'function' && | ||
'getDid' in value && | ||
typeof value.getDid === 'function' && | ||
'getServiceUrl' in value && | ||
typeof value.getServiceUrl === 'function' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { SessionManager } from './session-manager' | ||
|
||
export type StatelessSessionManagerOptions = { | ||
service: string | URL | ||
headers?: { [_ in string]?: null | string } | ||
} | ||
|
||
export class StatelessSessionManager implements SessionManager { | ||
readonly serviceUrl: URL | ||
readonly headers: Map<string, string> | ||
|
||
constructor({ service, headers }: StatelessSessionManagerOptions) { | ||
this.headers = new Map( | ||
headers | ||
? Object.entries(headers).filter( | ||
<T extends [string, unknown]>( | ||
e: T, | ||
): e is T & [T[0], NonNullable<T[1]>] => e[1] != null, | ||
) | ||
: headers, | ||
) | ||
this.serviceUrl = new URL(service) | ||
} | ||
|
||
async getServiceUrl(): Promise<URL> { | ||
return this.serviceUrl | ||
} | ||
|
||
async getDid(): Promise<string> { | ||
throw new Error('Not logged in') | ||
} | ||
|
||
async fetchHandler(url: string, reqInit: RequestInit): Promise<Response> { | ||
const fullUrl = new URL(url, this.serviceUrl) | ||
const headers = new Headers(reqInit.headers) | ||
|
||
for (const [key, value] of this.headers) { | ||
if (value != null) headers.set(key, value) | ||
} | ||
|
||
return globalThis.fetch(fullUrl.toString(), { ...reqInit, headers }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
import AtpAgent, { | ||
SessionDispatcher, | ||
SessionDispatcherOptions, | ||
AtpSessionManager, | ||
AtpSessionManagerOptions, | ||
} from '@atproto/api' | ||
import { EXAMPLE_LABELER } from './const' | ||
|
||
export class TestAgent extends AtpAgent { | ||
readonly dispatcher: SessionDispatcher | ||
readonly sessionManager: AtpSessionManager | ||
|
||
constructor(options: SessionDispatcherOptions) { | ||
const dispatcher = new SessionDispatcher(options) | ||
super(dispatcher) | ||
this.dispatcher = dispatcher | ||
constructor(options: AtpSessionManagerOptions) { | ||
const sessionManager = new AtpSessionManager(options) | ||
super(sessionManager) | ||
this.sessionManager = sessionManager | ||
this.configureLabelersHeader([EXAMPLE_LABELER]) | ||
} | ||
|
||
get session() { | ||
return this.dispatcher.session | ||
return this.sessionManager.session | ||
} | ||
|
||
get hasSession() { | ||
return this.dispatcher.hasSession | ||
return this.sessionManager.hasSession | ||
} | ||
|
||
get service() { | ||
return this.dispatcher.serviceUrl | ||
return this.sessionManager.serviceUrl | ||
} | ||
|
||
login(...args: Parameters<SessionDispatcher['login']>) { | ||
return this.dispatcher.login(...args) | ||
login(...args: Parameters<AtpSessionManager['login']>) { | ||
return this.sessionManager.login(...args) | ||
} | ||
|
||
createAccount(...args: Parameters<SessionDispatcher['createAccount']>) { | ||
return this.dispatcher.createAccount(...args) | ||
createAccount(...args: Parameters<AtpSessionManager['createAccount']>) { | ||
return this.sessionManager.createAccount(...args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.