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

table-talk

v2.1.0-beta.1

Published

A simple HTTP and socket server library that facilitates clear and error-free API design.

Downloads

36

Readme

table-talk

npm version Build Status Coverage Status Dependencies Status Conventional Commits

A simple HTTP and socket server library built for TypeScript on Node.js.

The primary goal of this library is to provide a framework that facilitates the design of a clear and error-free communication structure between the server and client:

  • every message that is sent needs to have a corresponding message handler on the other side
  • all messages should have a well-defined format
  • At no point should the programmer be able to mistakenly send the wrong arguments because they misread the docs

All of these things are guaranteed by the clever typing system.

Getting Started

Installation

Via npm:

npm install table-talk

Documentation

Detailed documentation can be found at tannerntannern.github.io/table-talk. For help getting started, keep reading.

Examples

Creating an HTTP Server/Client

Start by sketching an API that you want implemented:

type User = {
	name: string,
	age: number
}

interface API {
	get: {
		'/users': {args: {}, return: User[]},
		'/user': {args: {id: number}, return: User}
	},
	post: {
		'/user': {args: User, return: boolean}
	},
	delete: {
		'/user': {args: {index: number}, return: boolean}
	}
}

Then create a ServerManager class that implement the API. The typings will ensure that all the httpHandlers are implemented properly:

import {ExpressServerManager, ExpressClient} from 'table-talk';

class ServerManager extends ExpressServerManager<API> {
	protected users: User[];

	protected httpHandlers = {
		get: {
			'/users': () => {
				return this.users;
			},
			'/user': (data: {id: number}) => {
				return this.users[data.id];
			}
		},
		post: {
			'/user': (data: User) => {
				this.users.push(data);
				return true;
			}
		},
		delete: {
			'/user': (data) => {
				let user = this.users[data.index];
				if (user !== undefined) {
					this.users.splice(data.index, 1);
					return true;
				} else {
					return false;
				}
			}
		}
	};
}

Then create an HttpServer and attach the manager you just implemented.

import {HttpServer} from 'table-talk';

let server = new HttpServer({port: 3000})
	.with('my-manager', new ServerManager());

await server.start();

When the client connects to the server, it will only be able to make requests described by the API, which helps prevent mistakes.

import {ExpressClient} from 'table-talk';

class Client extends ExpressClient<API> {
	// Nothing to implement here
}

let client = new Client('http://localhost:3000');

// The compiler accepts this because the GET route exists and is passed the proper arguments
// Note that the Promise value is also properly typed according to the API
let user: User = await client.get('/user', {id: 0});

// This won't work because the passed object is not properly typed; missing prop 'age'
await client.post('/user', {name: 'Josh'});

// This won't work because the /users route is a GET route, not PUT
await client.PUT('/users', {foo: 'bar'})

Note: The ExpressClient is intended to be used within the browser, and it requires the axios request library. It is not bundled by default to avoid unnecessary bloat and give the option of using axios's CDN. Simply include the CDN <script> before the other client code for this to work properly.

Creating a Socket Server/Client

Coming soon...

A Note on Bundling

Coming soon...

Author

Tanner Nielsen [email protected]