Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yodalightsabr/function-apis
Stop exposing APIs that are only used once; start exposing functions that seamlessly work between your server and your client
https://github.com/yodalightsabr/function-apis
apis fullstack-javascript javascript nodejs
Last synced: 9 days ago
JSON representation
Stop exposing APIs that are only used once; start exposing functions that seamlessly work between your server and your client
- Host: GitHub
- URL: https://github.com/yodalightsabr/function-apis
- Owner: YodaLightsabr
- Created: 2022-03-05T23:25:55.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-03-06T04:16:51.000Z (over 2 years ago)
- Last Synced: 2024-10-11T11:49:28.520Z (about 1 month ago)
- Topics: apis, fullstack-javascript, javascript, nodejs
- Language: JavaScript
- Homepage: https://github.com/yodalightsabr/function-apis
- Size: 8.79 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# function-apis
Pass functions to the client instead of API routes
--------------------
API routes are great for APIs, but for small projects where you have to access server data or hide application logic, you can just call a server function from the client.## Examples
```js
// Hide server logic// Server
const functions = require('function-apis');
app.use(functions({
hello: (name) => {
return `Hello, ${name}!`;
}
}, true));// Client
hello('World').then(alert); // => 'Hello, World!'
``````js
// Log something to the console// Server
const functions = require('function-apis');
app.use(functions({
consoleLog: (...data) => {
console.log(...data);
return 'OK';
}
}, true));// Client
consoleLog('Hello!') // => 'OK'
```## Documentation
### `functions(functions, defineGlobally)`
Use as express middleware. Default export.* `functions`: Object of functions to expose to the client.
* `defineGlobally?`: Whether or not to define the functions globally. Defaults to `false`.`?` Optional
### window.functions
An object of client-side functions. Available after including the following in your HTML. If `defineGlobally` is set to `true`, the functions will be available to `window[function]` in addition to `window.functions[function]`.
```html```
[yodacode.xyz](https://yodacode.xyz)