https://github.com/loreanvictor/tyfon
typed functions over network
https://github.com/loreanvictor/tyfon
api api-client cloud-functions nodejs serverless typescript
Last synced: about 1 year ago
JSON representation
typed functions over network
- Host: GitHub
- URL: https://github.com/loreanvictor/tyfon
- Owner: loreanvictor
- License: mit
- Created: 2020-11-16T17:33:35.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-19T10:28:27.000Z (over 5 years ago)
- Last Synced: 2025-03-18T02:44:40.479Z (about 1 year ago)
- Topics: api, api-client, cloud-functions, nodejs, serverless, typescript
- Language: JavaScript
- Homepage: https://loreanvictor.github.io/tyfon
- Size: 1.13 MB
- Stars: 38
- Watchers: 3
- Forks: 4
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Typed Functions Over Network
[](https://www.npmjs.com/package/tyfon)
[](https://loreanvictor.github.io/tyfon/)
Seamlessly use server-side TypeScript functions on the client.
```ts
// server:
export async function getMessage(name: string) {
return `Hellow ${name}!`;
}
```
```ts
// client:
import { getMessage } from '@api/my-server';
getMessage('World').then(console.log):
```
👉 [Read the docs](https://loreanvictor.github.io/tyfon).
# Installation
TyFON is a singular CLI tool that runs on Node, so you need Node.js installed beforehand.
```bash
npm i -g tyfon
```
# Usage
[Read the docs](https://loreanvictor.github.io/tyfon) for detailed usage information and
a getting-started tutorial.
## Server Side
Export your functions in `index.ts`:
```ts
export async const getMessage = name => `Hellow ${name}!`;
```
Now serve them:
```bash
tyfon serve
```
👉 Check it out on `localhost:8000/message?0="World"`.
## Client Side
Add the SDK on client side and use it:
```bash
tyfon i localhost:8000
```
```ts
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:
```bash
tyfon watch -c # --> 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:
```bash
tyfon build # --> run this on server side code
```
```bash
tyfon serve # --> run this on server side code
```
- On client-side code, update TyFONs you are using:
```bash
tyfon i # --> run this on client side code
```
## Server Environment Variables
You can pass environment variables to `tyfon serve` command using `-e` option:
```bash
tyfon 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 `tyfon i` command to mark the environment a TyFON should be used in:
```bash
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:
```bash
tyfon i --env production # --> this will install all generic TyFONs and all production TyFONs
```
## Deploying
Run your TyFON in production mode:
```bash
tyfon serve --mode prod
```
### Docker
Build a docker image and deploy it:
```bash
tyfon build --image my-server
docker tag my-server https://my-registry.cloud/my-server
docker push my-server
```
👉 [docker](https://www.docker.com) **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](https://loreanvictor.github.io/tyfon).
# CLI Commands
- [`tyfon init`](https://loreanvictor.github.io/tyfon/cli/init)
- [`tyfon build`](https://loreanvictor.github.io/tyfon/cli/build)
- [`tyfon serve`](https://loreanvictor.github.io/tyfon/cli/serve)
- [`tyfon install`](https://loreanvictor.github.io/tyfon/cli/install)
- [`tyfon uninstall`](https://loreanvictor.github.io/tyfon/cli/uninstall)
- [`tyfon help`](https://loreanvictor.github.io/tyfon/cli/help)
- [`tyfon version`](https://loreanvictor.github.io/tyfon/cli/version)