npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@externdefs/bluesky-client

v0.5.25

Published

Lightweight API client for Bluesky/AT Protocol

Downloads

34

Readme

@mary/bluesky-client

Lightweight API client for Bluesky/AT Protocol (atproto).

[!IMPORTANT]
This is an ESM-only library, you need to configure your TypeScript correctly in order to correctly pick up the type declarations.

Why?

The official @atproto/api library is massive, weighing in at 662 KB minified. (v0.12.22)

  • The library relies on automatically generated classes and functions for representing the nature of RPC and namespaces, which can't be treeshaken at all if you only request to one or two endpoints.
  • The library depends on zod and graphemer, and as the library is shipped in CJS format it is unlikely that the treeshaking will be able to get them all off, resulting in a code bloat that's especially noticeable if you don't also rely on said functionality or dependencies.

Which leads to this alternative library, where it makes the following tradeoffs for its size:

  • The client does not attempt to validate if the responses are valid, or provide the means to check if what you're sending is correct during runtime. Proceed at your own risk.
  • IPLD and blob types are represented as-is.
    • CID links are not converted to a CID instance, and you'd need to rely on @mary/atproto-cid or multiformats if you need to parse them.
    • bytes are typed as Uint8Array, but please be aware that these only exists on subscriptions, which we don't actually cover here as it's a WebSocket connection.
    • Blobs are not turned into a BlobRef instance.
  • Additional APIs for handling rich text or moderation are not included, but there are some alternatives provided as examples in the repository.

The result is a very small API client that you can extend easily:

  • BskyXRPC alone: 1,725 b (918 b gzipped)
  • BskyXRPC and BskyAuth: 4,440 b (1,986 b gzipped)

Usage

Fiddling with AT Protocol lexicons...

Type declarations can be accessed via the ./lexicons module.

import type { AppBskyRichtextFacet, Brand } from '@mary/bluesky-client/lexicons';

type Facet = AppBskyRichtextFacet.Main;
type MentionFeature = Brand.Union<AppBskyRichtextFacet.Mention>;

const mention: MentionFeature = {
	$type: 'app.bsky.richtext.facet#mention',
	did: 'did:plc:ragtjsm2j2vknwkz3zp4oxrd',
};

const facet: Facet = {
	index: {
		byteStart: 7,
		byteEnd: 12,
	},
	features: [mention],
};

Unions in AT Protocol are done by discriminating the object's $type field, where the field only needs to exist if it's under a union. To make this possible, objects are branded with a (nonexistent) unique symbol. Note that object typings are slightly stricter than usual because of this (can't pass AppBskyActorDefs.ProfileView in functions that expects ProfileViewBasic).

Doing an unauthenticated request...

const rpc = new BskyXRPC({ service: 'https://public.api.bsky.app' });

const profile = await rpc.get('app.bsky.actor.getProfile', {
	params: {
		actor: 'did:plc:ragtjsm2j2vknwkz3zp4oxrd',
	},
});

console.log(profile.data); // -> { handle: 'pfrazee.com', ... }

Doing an authenticated request...

const rpc = new BskyXRPC({ service: 'https://bsky.social' });
const auth = new BskyAuth(rpc);

await auth.login({ identifier: '...', password: '...' });

const likes = await rpc.get('app.bsky.feed.getActorLikes', {
	params: {
		actor: auth.session.did,
		limit: 5,
	},
});

console.log(likes.data); // -> Array(5) [...]

Creating a post...

const record: AppBskyFeedPost.Record = {
	createdAt: new Date().toISOString(),
	text: `Hello world!`,
};

await rpc.call('com.atproto.repo.createRecord', {
	data: {
		repo: agent.session.did,
		collection: 'app.bsky.feed.post',
		record: record,
	},
});