/tyfon

typed functions over network

Primary LanguageJavaScriptMIT LicenseMIT

Typed Functions Over Network

version docs



TyFON is a zero-config RPC for TypeScript. It automatically creates all required networking code on the server-side and generates client-side SDK using same type definitions for client-server consistency.

// server:

export async function getMessage(name: string) {
  return `Hellow ${name}!`;
}
// client:

import { getMessage } from '@api/my-server';

getMessage('World').then(console.log):

👉 Read the docs.



Installation

TyFON is a singular CLI tool that runs on Node, so you need Node.js installed beforehand.

npm i -g tyfon



Usage

Read the docs for detailed usage information and a getting-started tutorial.


Server Side

Export your functions in index.ts:

export async const getMessage = name => `Hellow ${name}!`;

Now serve them:

tyfon serve

👉 Check it out on localhost:8000/message?0="World".



Client Side

Add the SDK on client side and use it:

tyfon i localhost:8000
import { getMessage } from '@api/my-server';

getMessage('World').then(console.log);

👉 The name my-server comes from package.json of your server code.



Syncing Updates

Use tyfon watch while working on server and client simultaneously:

tyfon watch -c <client-path>     # --> run this on server side code

Or sync updates manually (in other situations):

  • On server-side code, rebuild client SDK metadata and serve it again:
tyfon build                      # --> run this on server side code
tyfon serve                      # --> run this on server side code
  • On client-side code, update TyFONs you are using:
tyfon i                          # --> run this on client side code



Server Environment Variables

You can pass environment variables to typhon serve command using -e option:

typhon serve -e ENV=dev -e LOGS=verbose



Client Environments

It is common practice for client-code to use different API URLs for different environments (i.e. development, production, staging, etc). You can use the --env flag on typhon i command to mark the environment a TyFON should be used in:

tyfon i https://my-server.cloud --env production
tyfon i https://staging.my-server.cloud --env staging
tyfon i localhost:8000 --env dev

Now for building client-code in production, use the following command:

tyfon i --env production        # --> this will install all generic TyFONs and all production TyFONs



Deploying

Run your TyFON in production mode:

tyfon serve --mode prod

Docker

Build a docker image and deploy it:

tyfon build --image my-server

docker tag my-server https://my-registry.cloud/my-server
docker push my-server

👉 docker MUST be installed for using this option.



Conventions

TyFON leans heavily towards the convention over configuration principle. It is pretty opinionated in how it wraps normal functions within API end-points and how code should be structured, for example it picks endpoint methods based on the name of the function, or it expects all API functions to be exported from index.ts from the root of the project.

👉 Read the docs.



CLI Commands