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

@directus/sdk

v17.0.1

Published

Directus JavaScript SDK

Downloads

187,167

Readme

Directus JavaScript SDK

Features

  • TypeScript first: The SDK provides a robust and type-safe development experience.
  • Modular architecture: The SDK is split into separate modules, giving you granular control over which features to include and which can be pruned at build-time.
  • Lightweight and dependency-free: It does not require external libraries, ensuring a lighter bundle and streamlined experience.

Composable Client

The client is split up in separate features you can mix and match to compose a client with only the features you need or want.

const client = createDirectus<Schema>('https://api.directus.io');

This client is currently an empty wrapper without any functionality. Before you can do anything with it you'll need to add some features. The following composables are available/in progress:

  • rest() REST request functions
    • adds .request(...) on the client
  • graphql() GraphQL request functions
    • adds .query(...) on the client
  • staticToken() authentication functions
    • adds .getToken() and .setToken() on the client
  • authenticate() authentication functions
    • adds .login({ email, password }), .logout(), .refresh() on the client
    • adds .getToken() and .setToken() on the client
  • realtime() websocket connectivity
    • adds .subscribe(...), .sendMessage(...), .onWebsocket('message', (message) => {}) on the client

For this example we'll build a client including rest and graphql:

const client = createDirectus<Schema>('https://api.directus.io').with(rest()).with(graphql());

// do a REST request
const restResult = await client.request(readItems('articles'));

// do a GraphQL request
const gqlResult = await client.query<OutputType>(`
    query {
        articles {
            id
            title
            author {
                first_name
            }
        }
    }
`);

Authentication

const client = createDirectus<Schema>('https://api.directus.io').with(rest()).with(authentication('json'));

await client.login('[email protected]', 'd1r3ctu5');

// do authenticated requests
const client = createDirectus<Schema>('https://api.directus.io').with(rest()).with(staticToken('super-secure-token'));

// do authenticated requests

Real-Time

The realtime() extension allows you to work with a Directus REST WebSocket.

Subscribing to updates:

const client = createDirectus<Schema>('https://api.directus.io').with(
	realtime({
		authMode: 'public',
	}),
);

const { subscription, unsubscribe } = await client.subscribe('test', {
	query: { fields: ['*'] },
});

for await (const item of subscription) {
	console.log('subscription', { item });
}

// unsubscribe()

Receive/Send messages:

const client = createDirectus<Schema>('https://api.directus.io').with(
	realtime({
		authMode: 'public',
	}),
);

const stop = client.onWebSocket('message', (message) => {
	if ('type' in message && message['type'] === 'pong') {
		console.log('PONG received');
		stop();
	}
});

client.sendMessage({ type: 'ping' });

Build Your Schema

// The main schema type containing all collections available
interface MySchema {
	collection_a: CollectionA[]; // regular collections are array types
	collection_b: CollectionB[];
	collection_c: CollectionC; // this is a singleton
	// junction collections are collections too
	collection_a_b_m2m: CollectionAB_Many[];
	collection_a_b_m2a: CollectionAB_Any[];
}

// collection A
interface CollectionA {
	id: number;
	status: string;
	// relations
	m2o: number | CollectionB;
	o2m: number[] | CollectionB[];
	m2m: number[] | CollectionAB_Many[];
	m2a: number[] | CollectionAB_Any[];
}

// Many-to-Many junction table
interface CollectionAB_Many {
	id: number;
	collection_a_id: CollectionA;
	collection_b_id: CollectionB;
}

// Many-to-Any junction table
interface CollectionAB_Any {
	id: number;
	collection_a_id: CollectionA;
	collection: 'collection_b' | 'collection_c';
	item: string | CollectionB | CollectionC;
}

// collection B
interface CollectionB {
	id: number;
	value: string;
}

// singleton collection
interface CollectionC {
	id: number;
	app_settings: string;
	something: string;
}