Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Server Side Events #1614

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions fixtures/db.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
{
"posts": [
{ "id": "1", "title": "a title" },
{ "id": "2", "title": "another title" }
{
"id": "1",
"title": "a title"
},
{
"id": "2",
"title": "another title"
}
],
"comments": [
{ "id": "1", "text": "a comment about post 1", "postId": "1" },
{ "id": "2", "text": "another comment about post 1", "postId": "1" }
{
"id": "1",
"text": "a comment about post 1",
"postId": "1"
},
{
"id": "2",
"text": "another comment about post 1",
"postId": "1"
}
],
"profile": {
"name": "typicode"
Expand Down
33 changes: 19 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"json5": "^2.2.3",
"lowdb": "^7.0.1",
"milliparsec": "^4.0.0",
"rxjs": "^7.8.1",
"sirv": "^2.0.4",
"sort-on": "^6.1.0"
}
Expand Down
54 changes: 54 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Low } from 'lowdb'
import { json } from 'milliparsec'
import sirv from 'sirv'

import { Subject } from 'rxjs'

import { Data, isItem, Service } from './service.js'

const __dirname = dirname(fileURLToPath(import.meta.url))
Expand All @@ -30,6 +32,8 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
// Create app
const app = new App()

const event$ = new Subject<object>();

// Static files
app.use(sirv('public', { dev: !isProduction }))
options.static
Expand All @@ -55,6 +59,20 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
res.send(eta.render('index.html', { data: db.data })),
)

app.get('/events', (_req, res) => {
res.header('Content-Type', 'text/event-stream');

event$.subscribe({
next: (data) => {
res.write(`data: ${JSON.stringify(data)}\n\n`);
res.flushHeaders();
},
complete: () => {
res.end();
},
});
})

app.get('/:name', (req, res, next) => {
const { name = '' } = req.params
const query = Object.fromEntries(
Expand Down Expand Up @@ -86,6 +104,12 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
if (isItem(req.body)) {
res.locals['data'] = await service.create(name, req.body)
}
event$.next(
{
type: "post",
params: req.params
}
);
next?.()
})

Expand All @@ -94,6 +118,12 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
if (isItem(req.body)) {
res.locals['data'] = await service.update(name, req.body)
}
event$.next(
{
type: "put",
params: req.params
}
);
next?.()
})

Expand All @@ -102,6 +132,12 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
if (isItem(req.body)) {
res.locals['data'] = await service.updateById(name, id, req.body)
}
event$.next(
{
type: "put",
params: req.params
}
);
next?.()
})

Expand All @@ -110,6 +146,12 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
if (isItem(req.body)) {
res.locals['data'] = await service.patch(name, req.body)
}
event$.next(
{
type: "patch",
params: req.params
}
);
next?.()
})

Expand All @@ -118,6 +160,12 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
if (isItem(req.body)) {
res.locals['data'] = await service.patchById(name, id, req.body)
}
event$.next(
{
type: "patch",
params: req.params
}
);
next?.()
})

Expand All @@ -128,6 +176,12 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
id,
req.query['_dependent'],
)
event$.next(
{
type: "delete",
params: req.params
}
);
next?.()
})

Expand Down