Skip to content
This repository was archived by the owner on Feb 15, 2025. It is now read-only.

Commit ff3fb01

Browse files
committed
🧪 API + Docs
1 parent 7befe0d commit ff3fb01

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

README.md

+145
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,147 @@
11
# Data-API
22
The DATA API in Beaver Notes enables third-party applications to interact with the platform. It provides functionalities for creating notes, deleting notes, and adding labels, allowing for efficient management and organization of information within Beaver Notes.
3+
4+
The Following documentation is related on how to use said API if you are looking on building a plugin for beaver notes using the Data API consider checkign out the [Beaver-API-playground](https://github.com/Beaver-Notes/Data-API).
5+
6+
## API Documentation
7+
8+
### Base URL
9+
`http://localhost:3000`
10+
11+
### Authentication
12+
13+
#### Request Authentication
14+
15+
**Endpoint:** `POST /request-auth`
16+
17+
**Description:** Sends an authentication request to the renderer process in Electron.
18+
19+
**Request:**
20+
- **Headers:** `Content-Type: application/json`
21+
- **Body:**
22+
```json
23+
{
24+
"platform": "your-platform",
25+
"id": "your-id"
26+
}
27+
```
28+
29+
**Example `curl` Command:**
30+
```bash
31+
curl --location 'http://localhost:3000/request-auth' \
32+
--header 'Content-Type: application/json' \
33+
--data '{
34+
"platform": "your-platform",
35+
"id": "your-id"
36+
}'
37+
```
38+
39+
#### Verify Authentication
40+
41+
**Endpoint:** `GET /confirm-auth`
42+
43+
**Description:** Verifies the authentication token.
44+
45+
**Request:**
46+
- **Headers:**
47+
- `Authorization: Bearer your-token`
48+
- `Content-Type: application/json`
49+
50+
**Example `curl` Command:**
51+
```bash
52+
curl --location --request GET 'http://localhost:3000/confirm-auth' \
53+
--header 'Authorization: Bearer your-token' \
54+
--header 'Content-Type: application/json' \
55+
--data '{
56+
"platform": "your-platform",
57+
"id": "your-id"
58+
}'
59+
```
60+
61+
### Notes Management
62+
63+
#### Add Note
64+
65+
**Endpoint:** `POST /add-note`
66+
67+
**Description:** Adds a new note and broadcasts it to all connected clients.
68+
69+
**Request:**
70+
- **Headers:**
71+
- `Authorization: Bearer your-token`
72+
- `Content-Type: application/json`
73+
- **Body:**
74+
```json
75+
{
76+
"title": "Sample Note",
77+
"content": "This is a sample note content."
78+
}
79+
```
80+
81+
**Example `curl` Command:**
82+
```bash
83+
curl --location 'http://localhost:3000/add-note' \
84+
--header 'Authorization: Bearer your-token' \
85+
--header 'Content-Type: application/json' \
86+
--data '{
87+
"title": "Sample Note",
88+
"content": "This is a sample note content."
89+
}'
90+
```
91+
92+
#### Delete Note
93+
94+
**Endpoint:** `POST /delete-note`
95+
96+
**Description:** Deletes a note and broadcasts the deletion to all connected clients.
97+
98+
**Request:**
99+
- **Headers:**
100+
- `Authorization: Bearer your-token`
101+
- `Content-Type: application/json`
102+
- **Body:**
103+
```json
104+
{
105+
"id": "note-id"
106+
}
107+
```
108+
109+
**Example `curl` Command:**
110+
```bash
111+
curl --location 'http://localhost:3000/delete-note' \
112+
--header 'Authorization: Bearer your-token' \
113+
--header 'Content-Type: application/json' \
114+
--data '{
115+
"id": "note-id"
116+
}'
117+
```
118+
119+
#### Add Label to Note
120+
121+
**Endpoint:** `POST /add-label`
122+
123+
**Description:** Adds a label to a note and broadcasts the update to all connected clients.
124+
125+
**Request:**
126+
- **Headers:**
127+
- `Authorization: Bearer your-token`
128+
- `Content-Type: application/json`
129+
- **Body:**
130+
```json
131+
{
132+
"id": "note-id",
133+
"labelId": "label-id"
134+
}
135+
```
136+
137+
**Example `curl` Command:**
138+
```bash
139+
curl --location 'http://localhost:3000/add-label' \
140+
--header 'Authorization: Bearer your-token' \
141+
--header 'Content-Type: application/json' \
142+
--data '{
143+
"id": "note-id",
144+
"labelId": "label-id"
145+
}'
146+
```
147+

server.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {verify} from './token/middleware';
2+
3+
// backend/api.js
4+
const express = require('express');
5+
const http = require('http');
6+
const socketIo = require('socket.io');
7+
const cors = require('cors');
8+
9+
const app = express();
10+
const server = http.createServer(app);
11+
const io = socketIo(server, {
12+
cors: {
13+
origin: 'http://localhost:5173',
14+
methods: ['GET', 'POST'],
15+
credentials: true,
16+
},
17+
});
18+
19+
const port = 3000;
20+
21+
/**
22+
* @param {import('electron-better-ipc').ipcMain} ipcMain
23+
* @param {import('electron').BrowserWindow} win
24+
*/
25+
const api = (ipcMain, win) => {
26+
// Middleware
27+
app.use(express.json());
28+
app.use(cors());
29+
30+
// POST endpoint to receive note data
31+
app.post('/add-note', verify(['note:add']), (req, res) => {
32+
const note = req.body;
33+
console.log('Note received:', note);
34+
io.emit('newNote', note); // Broadcast the note to all connected clients
35+
res.send('Creating Note');
36+
});
37+
38+
app.post('/delete-note', verify(['note:delete']), (req, res) => {
39+
const { id } = req.body;
40+
console.log('Note deleted:', id);
41+
io.emit('deleteNote', id); // Broadcast the note ID to all connected clients
42+
res.send('Deleting Note');
43+
});
44+
45+
app.post('/add-label', verify(['label:add']), (req, res) => {
46+
const { id, labelId } = req.body;
47+
console.log('Label added to note:', labelId, 'Note ID:', id);
48+
io.emit('addLabel', { id, labelId }); // Broadcast the label addition to all connected clients
49+
res.send('Adding Label');
50+
});
51+
52+
app.post('/request-auth', (req, res) => {
53+
const { id, platform } = req.body;
54+
ipcMain.callRenderer(win, 'auth:request-auth', { id, platform });
55+
res.send('request sent!');
56+
});
57+
58+
app.get('/confirm-auth', verify([]), (req, res) => {
59+
console.log(req.auth);
60+
res.send('passed');
61+
});
62+
63+
// Start the server
64+
server.listen(port, () => {
65+
console.log(`Server running at http://localhost:${port}`);
66+
});
67+
};
68+
69+
export default api;

0 commit comments

Comments
 (0)