An open API service indexing awesome lists of open source software.

https://github.com/zerotohero-dev/tunel

Tunel: Tunnels REST-like requests over IPC/Channels.
https://github.com/zerotohero-dev/tunel

Last synced: 8 months ago
JSON representation

Tunel: Tunnels REST-like requests over IPC/Channels.

Awesome Lists containing this project

README

          

```text
___ /
_-_- _/\____ / __\\__
_-_-__ / ,-. -| / - ,-.`-.
_-_- `( o )-- / --( o )-'
`-' / `-'
tunel (n): RESTful message tunneling over IPC.
```

## Quick Example

Here’s the code for Electron’s **main thread**:

```javascript
//
// Main Thread
//

const electron = require('electron');
const channel = electron.ipcMain;

const { app, registerChannel } = require('tunel/server');

registerChannel(channel);

// `app` API is similar to `express.js`
app.get('/api/v1/profile', async req => {
// You can access req.body, req.params…
void req;

// Unlike express, there is no `res` object.
// As soon as you return from the function, you’ll relay
// a response back.
// So, returning a JSON `json`, is similar to `res.send(json)`.
return {
userName: 'robert',
fullName: 'Robert Denir Ona'
};
});
```

Here’s the cod for Electron’s **renderer thread** (_i.e., the browser_):

```javascript
//
// Renderer Thread
//

import request from 'tunel';

const doFetch = async () => {
// `request` API is similar to `axios`.
const response = await request({
method: 'GET',
url: '/api/v1/profile',
data: { some: 'payload' },
headers: {
'Content-Type': 'application/json'
}
});

// Will log
// `{ status: 200, data: { userName: 'robert',
// fullName: 'Robert Denir Ona' }}`
console.log(response);
};

doFetch();
```