Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
AppBskyActorDefs,
AppBskyFeedGetAuthorFeed,
} from '@tsky/lexicons';
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';
import { Paginator } from '~/utils';

Expand Down
55 changes: 55 additions & 0 deletions packages/client/src/agent/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { type CredentialManager, XRPC } from '@atcute/client';
import type { Queries } from '@tsky/lexicons';
import { Actor } from '~/actor';
import { Feed } from '~/feed';
import { List } from '~/list';
import { StarterPack } from '~/starterpack';
import { User } from '~/user';
import { Video } from '~/video';
import { Client } from './client';

export class Agent {
private client: Client<Queries>;

constructor(private handler: CredentialManager) {
// Initialize the client
const xrpc = new XRPC({ handler: this.handler });
this.client = new Client(xrpc);
}

get session() {
return this.handler.session;
}

actor(identifier: string) {
return new Actor(this.client, identifier);
}

list(uri: string) {
return new List(this.client, uri);
}

get feed() {
return new Feed(this.client);
}

get user() {
if (!this.session) {
throw new Error('There is no active session');
}

return new User(this.client, this.session.handle);
}

get video() {
if (!this.session) {
throw new Error('There is no active session');
}

return new Video(this.client);
}

get starterpack() {
return new StarterPack(this.client);
}
}
File renamed without changes.
1 change: 1 addition & 0 deletions packages/client/src/agent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './agent';
45 changes: 0 additions & 45 deletions packages/client/src/auth/auth.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/client/src/auth/index.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/client/src/bsky/bsky.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/client/src/bsky/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { Tsky } from '~/index';
import { createAgent } from '~/tsky';

const TEST_CREDENTIALS = {
alice: {
Expand All @@ -14,21 +14,13 @@ const TEST_CREDENTIALS = {
},
};

async function getAliceTsky() {
const tsky = new Tsky();

await tsky.auth.login(
TEST_CREDENTIALS.alice.handle,
TEST_CREDENTIALS.alice.password,
);

return tsky;
}

describe('feed', () => {
it('.getFeed()', async () => {
const tsky = await getAliceTsky();
const paginator = await tsky.bsky.feed.get({
const agent = await createAgent({
identifier: TEST_CREDENTIALS.alice.handle,
password: TEST_CREDENTIALS.alice.password,
});
const paginator = await agent.feed.get({
// "Birds! 🦉" custom feed
// - https://bsky.app/profile/daryllmarie.bsky.social/feed/aaagllxbcbsje
feed: 'at://did:plc:ffkgesg3jsv2j7aagkzrtcvt/app.bsky.feed.generator/aaagllxbcbsje',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
AppBskyFeedGetFeed,
AppBskyFeedSendInteractions,
} from '@tsky/lexicons';
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';
import { Paginator } from '~/utils';
import { FeedGenerator } from './generator';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
AppBskyFeedGetFeedGenerators,
AppBskyFeedGetFeedSkeleton,
} from '@tsky/lexicons';
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';
import { Paginator } from '~/utils';

Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './bsky';
export * from './tsky';
export * from '~/tsky';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';
import { Paginator } from '~/utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
AppBskyFeedGetRepostedBy,
AppBskyFeedSearchPosts,
} from '@tsky/lexicons';
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';
import { Paginator } from '~/utils';

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/starterpack/starterpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
AppBskyGraphGetStarterPack,
AppBskyGraphGetStarterPacks,
} from '@tsky/lexicons';
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';
import { Paginator } from '~/utils';

Expand Down
64 changes: 19 additions & 45 deletions packages/client/src/tsky/tsky.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,20 @@
import { XRPC } from '@atcute/client';
import type { Queries } from '@tsky/lexicons';
import { Auth } from '~/auth';
import { Bsky } from '~/bsky';
import { StarterPack } from '~/starterpack';
import { User } from '~/user';
import { Video } from '~/video';
import { Client } from './client';

export class Tsky {
auth: Auth;
private client: Client<Queries>;

constructor() {
// Initialize the auth manager
this.auth = new Auth();

// Initialize the client
const xrpc = new XRPC({ handler: this.auth.manager });
this.client = new Client(xrpc);
}

get user() {
if (!this.auth.currentSession) {
throw new Error('There is no active session');
}

return new User(this.client, this.auth.currentSession.handle);
}

get bsky() {
return new Bsky(this.client);
}

get video() {
if (!this.auth.currentSession) {
throw new Error('There is no active session');
}

return new Video(this.client);
}

get starterpack() {
return new StarterPack(this.client);
}
import {
CredentialManager,
type CredentialManagerOptions,
} from '@atcute/client';
import { Agent } from '~/agent';
import type { CreateAgentOptions } from './types';

export async function createAgent(
credentials: CreateAgentOptions,
options?: CredentialManagerOptions,
) {
const manager = new CredentialManager(
options ?? { service: 'https://bsky.social' },
);

if ('session' in credentials) manager.resume(credentials.session);
else await manager.login(credentials);

return new Agent(manager);
}
10 changes: 10 additions & 0 deletions packages/client/src/tsky/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { AtpSessionData } from '@atcute/client';

export type CreateAgentOptions =
| {
identifier: string;
password: string;
}
| {
session: AtpSessionData;
};
2 changes: 1 addition & 1 deletion packages/client/src/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AppBskyFeedGetTimeline } from '@tsky/lexicons';
import { Actor } from '~/bsky/actor';
import { Actor } from '~/actor';
import type { RPCOptions } from '~/types';
import { Paginator } from '~/utils';
import { Mute } from './mute';
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/user/mute/mute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';

export class Mute {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/user/muted/muted.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';
import { Paginator } from '~/utils';

Expand Down
29 changes: 12 additions & 17 deletions packages/client/src/user/preferences/preferences.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { Tsky } from '~/index';
import { createAgent } from '~/index';

const TEST_CREDENTIALS = {
alice: {
Expand All @@ -14,36 +14,31 @@ const TEST_CREDENTIALS = {
},
};

async function getAliceTsky() {
const tsky = new Tsky();

await tsky.auth.login(
TEST_CREDENTIALS.alice.handle,
TEST_CREDENTIALS.alice.password,
);

return tsky;
}

describe('preferences', () => {
it('.get()', async () => {
const tsky = await getAliceTsky();
const preferences = await tsky.user.preferences.get();
const agent = await createAgent({
identifier: TEST_CREDENTIALS.alice.handle,
password: TEST_CREDENTIALS.alice.password,
});
const preferences = await agent.user.preferences.get();

expect(preferences).toBeDefined();
});

it('.set()', async () => {
const tsky = await getAliceTsky();
const agent = await createAgent({
identifier: TEST_CREDENTIALS.alice.handle,
password: TEST_CREDENTIALS.alice.password,
});

const payload = {
$type: 'app.bsky.actor.defs.adultContentPref',
enabled: false,
};

await tsky.user.preferences.set([payload]);
await agent.user.preferences.set([payload]);

const preferences = await tsky.user.preferences.get();
const preferences = await agent.user.preferences.get();

expect(preferences).toBeDefined();

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/user/preferences/preferences.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AppBskyActorPutPreferences } from '@tsky/lexicons';
import type { Client } from '~/tsky/client';
import type { Client } from '~/agent/client';
import type { RPCOptions } from '~/types';

export class Preferences {
Expand Down
Loading