-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: TAKAHASHI Shuuji <[email protected]>
- Loading branch information
Showing
30 changed files
with
6,136 additions
and
6,899 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 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,50 @@ | ||
import type { | ||
AppBskyFeedGetFeed, | ||
AppBskyFeedGetTimeline, | ||
} from '@tsky/lexicons'; | ||
import type { Client } from '~/tsky/client'; | ||
import { Paginator } from '~/tsky/paginator'; | ||
|
||
export class Feed { | ||
constructor(private client: Client) {} | ||
|
||
/** | ||
* Get a hydrated feed from an actor's selected feed generator. Implemented by App View. | ||
*/ | ||
async getFeed( | ||
params: AppBskyFeedGetFeed.Params, | ||
options?: AppBskyFeedGetFeed.Input, | ||
): Promise<Paginator<AppBskyFeedGetFeed.Output>> { | ||
return Paginator.init(async (cursor) => { | ||
const res = await this.client.get('app.bsky.feed.getFeed', { | ||
...(options ?? {}), | ||
params: { | ||
cursor, | ||
...params, | ||
}, | ||
}); | ||
|
||
return res.data; | ||
}); | ||
} | ||
|
||
/** | ||
* Get a view of the requesting account's home timeline. This is expected to be some form of reverse-chronological feed. | ||
*/ | ||
getTimeline( | ||
params: AppBskyFeedGetTimeline.Params, | ||
options?: AppBskyFeedGetTimeline.Input, | ||
): Promise<Paginator<AppBskyFeedGetTimeline.Output>> { | ||
return Paginator.init(async (cursor) => { | ||
const res = await this.client.get('app.bsky.feed.getTimeline', { | ||
...(options ?? {}), | ||
params: { | ||
cursor, | ||
...params, | ||
}, | ||
}); | ||
|
||
return res.data; | ||
}); | ||
} | ||
} |
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,79 @@ | ||
import { CredentialManager } from '@atcute/client'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { Tsky } from '~/index'; | ||
|
||
const formatSecret = (secret: string | undefined) => { | ||
if (!secret) { | ||
throw new Error('Secret is required'); | ||
} | ||
|
||
return secret.replace(/^tsky /g, '').trim(); | ||
}; | ||
|
||
const TEST_CREDENTIALS = { | ||
alice: { | ||
handle: 'alice.tsky.dev', | ||
did: 'did:plc:jguhdmnjclquqf5lsvkyxqy3', | ||
password: 'alice_and_bob', | ||
}, | ||
bob: { | ||
handle: 'bob.tsky.dev', | ||
did: 'did:plc:2ig7akkyfq256j42uxvc4g2h', | ||
password: 'alice_and_bob', | ||
}, | ||
}; | ||
|
||
async function getAliceTsky() { | ||
const manager = new CredentialManager({ service: 'https://bsky.social' }); | ||
await manager.login({ | ||
identifier: TEST_CREDENTIALS.alice.handle, | ||
password: TEST_CREDENTIALS.alice.password, | ||
}); | ||
|
||
return new Tsky(manager); | ||
} | ||
|
||
describe('bsky', () => { | ||
it('.profile()', async () => { | ||
const tsky = await getAliceTsky(); | ||
const profile = await tsky.bsky.profile(TEST_CREDENTIALS.alice.did); | ||
|
||
expect(profile).toBeDefined(); | ||
expect(profile).toHaveProperty('handle', TEST_CREDENTIALS.alice.handle); | ||
}); | ||
|
||
describe('feed', () => { | ||
it('.timeline()', async () => { | ||
const tsky = await getAliceTsky(); | ||
|
||
const paginator = await tsky.bsky.feed.getTimeline({ | ||
limit: 30, | ||
}); | ||
|
||
expect(paginator).toBeDefined(); | ||
expect(paginator.values).toBeDefined(); | ||
expect(paginator.values).toBeInstanceOf(Array); | ||
expect(paginator.values.length).toBe(1); // we should get the first page from the paginator | ||
expect(paginator.values[0].feed.length).toBeGreaterThan(0); // alice has some posts ;) | ||
expect(paginator.values[0].feed[0]).toHaveProperty('post'); | ||
}); | ||
|
||
it('.feed()', async () => { | ||
const tsky = await getAliceTsky(); | ||
|
||
const paginator = await tsky.bsky.feed.getFeed({ | ||
// "Birds! 🦉" custom feed | ||
// - https://bsky.app/profile/daryllmarie.bsky.social/feed/aaagllxbcbsje | ||
feed: 'at://did:plc:ffkgesg3jsv2j7aagkzrtcvt/app.bsky.feed.generator/aaagllxbcbsje', | ||
limit: 30, | ||
}); | ||
|
||
expect(paginator).toBeDefined(); | ||
expect(paginator.values).toBeDefined(); | ||
expect(paginator.values).toBeInstanceOf(Array); | ||
expect(paginator.values.length).toBe(1); // we should get the first page from the paginator | ||
expect(paginator.values[0].feed.length).toBeGreaterThan(0); // we found some birds posts ;) | ||
expect(paginator.values[0].feed[0]).toHaveProperty('post'); | ||
}); | ||
}); | ||
}); |
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,28 @@ | ||
import type { AppBskyActorDefs } from '@tsky/lexicons'; | ||
import { Feed } from '~/bsky/feed'; | ||
import type { Client } from '~/tsky/client'; | ||
|
||
export class Bsky { | ||
client: Client; | ||
|
||
constructor(client: Client) { | ||
this.client = client; | ||
} | ||
|
||
/** | ||
* Get detailed profile view of an actor. Does not require auth, but contains relevant metadata with auth. | ||
*/ | ||
async profile( | ||
identifier: string, | ||
): Promise<AppBskyActorDefs.ProfileViewDetailed> { | ||
const res = await this.client.get('app.bsky.actor.getProfile', { | ||
params: { actor: identifier }, | ||
}); | ||
|
||
return res.data; | ||
} | ||
|
||
get feed() { | ||
return new Feed(this.client); | ||
} | ||
} |
File renamed without changes.
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,56 @@ | ||
import type { | ||
RPCOptions, | ||
XRPC, | ||
XRPCRequestOptions, | ||
XRPCResponse, | ||
} from '@atcute/client'; | ||
import type { Procedures, Queries } from '@tsky/lexicons'; | ||
|
||
// From @atcute/client | ||
type OutputOf<T> = T extends { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
output: any; | ||
} | ||
? T['output'] | ||
: never; | ||
|
||
export class Client<Q = Queries, P = Procedures> { | ||
xrpc: XRPC; | ||
|
||
constructor(xrpc: XRPC) { | ||
this.xrpc = xrpc; | ||
} | ||
|
||
/** | ||
* Makes a query (GET) request | ||
* @param nsid Namespace ID of a query endpoint | ||
* @param options Options to include like parameters | ||
* @returns The response of the request | ||
*/ | ||
async get<K extends keyof Q>( | ||
nsid: K, | ||
options: RPCOptions<Q[K]>, | ||
): Promise<XRPCResponse<OutputOf<Q[K]>>> { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
return this.xrpc.get(nsid as any, options); | ||
} | ||
|
||
/** | ||
* Makes a procedure (POST) request | ||
* @param nsid Namespace ID of a procedure endpoint | ||
* @param options Options to include like input body or parameters | ||
* @returns The response of the request | ||
*/ | ||
async call<K extends keyof P>( | ||
nsid: K, | ||
options: RPCOptions<P[K]>, | ||
): Promise<XRPCResponse<OutputOf<P[K]>>> { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
return this.xrpc.call(nsid as any, options); | ||
} | ||
|
||
/** Makes a request to the XRPC service */ | ||
async request(options: XRPCRequestOptions): Promise<XRPCResponse> { | ||
return this.xrpc.request(options); | ||
} | ||
} |
File renamed without changes.
16 changes: 12 additions & 4 deletions
16
packages/core/src/tsky/paginator.ts → packages/client/src/tsky/paginator.ts
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,18 @@ | ||
import type { CredentialManager } from '@atcute/client'; | ||
import { XRPC } from '@atcute/client'; | ||
import type { Queries } from '@tsky/lexicons'; | ||
import { Bsky } from '~/bsky'; | ||
import { Client } from './client'; | ||
|
||
export class Tsky { | ||
client: Client<Queries>; | ||
|
||
constructor(manager: CredentialManager) { | ||
const xrpc = new XRPC({ handler: manager }); | ||
this.client = new Client(xrpc); | ||
} | ||
|
||
get bsky() { | ||
return new Bsky(this.client); | ||
} | ||
} |
Oops, something went wrong.