flayer
v0.1.1
Published
Expose your server functions to be directly used by your frontend
Downloads
21
Readme
Flayer
Warning! ⚠️
This library is experimental and should be used with caution.
Flayer (short for function layer) is a Node.js library, which allows you to "expose" your TypeScript functions to be directly used by your TypeScript frontend with minimal setup and boilerplate.
Quick start
Server setup
Install Flayer to your Node.js backend application
npm i flayer
Write a module file exporting the functions you want to expose
// ./modules/someModule.ts export function hello(name: string) { return `Hello ${name}!`; }
Create and start a Flayer server with your modules
import { createServer } from "flayer"; const server = createServer({ someModule: require("./modules/someModule"), }); server.start({ port: 1234, });
Generate a client package on server restart when in development mode
if (process.env.NODE_ENV === "development") { server.generatePackage({ path: "../server-pkg", }); }
Client setup
After generating the client package the first time, install it to your frontend application
npm i ../server-pkg
Configure the client package on app initialization
import { configure } from "server-pkg"; configure({ url: "ws://localhost:1234", });
Start using the backend functions in your frontend code!
import { hello } from "server-pkg/someModule"; console.log(hello("World")); // "Hello World!"
More detailed examples
Check https://github.com/jlaamanen/flayer/tree/master/examples/01-crud-with-auth for a more thorough example. To try it out yourself, copy the example repository by running:
npx degit jlaamanen/flayer/examples/01-crud-with-auth