Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
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
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ bun add tsky
## Usage

```ts
import { Tsky } from 'tsky'
import { Tsky } from 'tsky';
import { CredentialManager } from '@atcute/client';

// use either the credential manager or
const manager = new CredentialManager({ service: 'https://bsky.social' });
await manager.login({
identifier: 'alice.tsky.dev',
password: 'password',
});

const app = new AppBskyNS(); // TODO
const tsky = new Tsky(app);
const tsky = new Tsky(manager);

const profile = await tsky.profile('did:plc:giohuovwawlijq7jkuysq5dd');
// get the profile of a user
const profile = await tsky.bsky.profile('did:plc:giohuovwawlijq7jkuysq5dd');

console.log(profile.handle);
```
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"url": "git+https://github.com/tsky-dev/tsky.git"
},
"scripts": {
"dev": "pnpm run --filter @tsky/core dev",
"build": "pnpm run --filter @tsky/core build",
"dev": "pnpm run -r dev",
"build": "pnpm run -r build",
"docs:dev": "pnpm run --filter @tsky/docs dev",
"docs:build": "pnpm run --filter @tsky/docs build",
"docs:preview": "pnpm run --filter @tsky/docs preview",
Expand All @@ -21,7 +21,8 @@
"lint": "biome lint .",
"lint:fix": "biome lint --write .",
"check": "biome check",
"check:fix": "biome check --write ."
"check:fix": "biome check --write .",
"typecheck": "pnpm run -r typecheck"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json → packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@tsky/core",
"name": "@tsky/client",
"type": "module",
"version": "0.0.1",
"license": "MIT",
Expand Down Expand Up @@ -29,7 +29,8 @@
"test:typescript": "tsc --noEmit"
},
"dependencies": {
"@atproto/api": "^0.13.18"
"@atcute/client": "^2.0.6",
"@tsky/lexicons": "workspace:*"
},
"devDependencies": {
"globals": "^15.12.0",
Expand Down
50 changes: 50 additions & 0 deletions packages/client/src/bsky/feed.ts
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;
});
}
}
28 changes: 28 additions & 0 deletions packages/client/src/bsky/index.ts
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);
}
}
54 changes: 54 additions & 0 deletions packages/client/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { CredentialManager } from '@atcute/client';
import { describe, expect, it } from 'vitest';
import { Tsky } from './index';

const env = process.env;
const TEST_CREDENTIALS = {
alice: {
handle: 'alice.tsky.dev',
did: 'did:plc:jguhdmnjclquqf5lsvkyxqy3',
appPassword: env.ALICE_APP_PASSWORD ?? '',
},
bob: {
handle: 'bob.tsky.dev',
did: 'did:plc:2ig7akkyfq256j42uxvc4g2h',
appPassword: env.BOB_APP_PASSWORD ?? '',
},
};

async function getAliceTsky() {
const manager = new CredentialManager({ service: 'https://bsky.social' });
await manager.login({
identifier: TEST_CREDENTIALS.alice.handle,
password: TEST_CREDENTIALS.alice.appPassword,
});

return new Tsky(manager);
}

describe('tSky', () => {
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');
});
});
});
File renamed without changes.
57 changes: 57 additions & 0 deletions packages/client/src/tsky/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
type FetchHandler,
type RPCOptions,
XRPC,
type XRPCRequestOptions,
type 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(handler: FetchHandler) {
this.xrpc = new XRPC({ handler });
}

/**
* 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.
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
interface CursorResponse {
cursor?: string;
[key: string]: unknown;
}

export class Paginator<T extends CursorResponse> {
readonly values: T[] = [];

constructor(
private constructor(
private onNext: (cursor?: string) => Promise<T>,
defaultValues?: T[],
) {
if (defaultValues) {
this.values = defaultValues;
}
}

static async init<T extends CursorResponse>(
onNext: (cursor?: string) => Promise<T>,
defaultValues?: T[],
): Promise<Paginator<T>> {
const paginator = new Paginator<T>(onNext, defaultValues);

// load the first page
await paginator.next();

// TODO: Should we call this here to get the first value?
// this.next();
return paginator;
}

clone() {
Expand Down
16 changes: 16 additions & 0 deletions packages/client/src/tsky/tsky.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { FetchHandler } from '@atcute/client';
import type { Queries } from '@tsky/lexicons';
import { Bsky } from '~/bsky';
import { Client } from './client';

export class Tsky {
client: Client<Queries>;

constructor({ handle }: { handle: FetchHandler }) {
this.client = new Client(handle);
}

get bsky() {
return new Bsky(this.client);
}
}
20 changes: 10 additions & 10 deletions packages/core/tsconfig.json → packages/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["ESNext"],
"lib": ["ESNext", "DOM"],
"moduleDetection": "force",
"useDefineForClassFields": false,
"experimentalDecorators": true,
"baseUrl": ".",
"module": "ESNext",
"moduleResolution": "node",
"moduleResolution": "bundler",
"paths": {
"~/*": ["src/*"]
},
"resolveJsonModule": true,
"allowJs": true,
"strict": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"declaration": false,
"noEmit": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"moduleDetection": "force",
"resolveJsonModule": true,
"allowJs": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"declaration": false,
"noEmit": true,
"outDir": "dist/",
"sourceMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
},
"include": ["./src/**/*", "./tests/**/*", "./test-utils/**/*"],
"typedocOptions": {
Expand Down
File renamed without changes.
Loading
Loading