Skip to content

Commit 6e0308a

Browse files
committed
Add client types and some cleanup
1 parent 4cffca3 commit 6e0308a

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

api/src/app.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { feathers } from 'feathers'
2+
import type { ClientServices } from 'feathers/client'
23
import type { HookContext as FeathersHookContext, Application as FeathersApplication } from 'feathers'
34
import { MessageService } from './services/messages.js'
45
import { SseService } from './services/sse.js'
@@ -16,6 +17,8 @@ export type HookContext = FeathersHookContext<Application>
1617

1718
const app: Application = feathers<Services, Configuration>()
1819

20+
export type Client = FeathersApplication<ClientServices<Services, unknown>>
21+
1922
app.use('sse', new SseService())
2023
app.use('messages', new MessageService())
2124

api/src/services/messages.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export type Message = {
1313
user: string
1414
}
1515

16+
export type MessageParams = Params & {
17+
user: TalonAuthUser
18+
}
19+
1620
const db = new DatabaseSync('chat.sqlite')
1721

1822
db.exec(`
@@ -26,14 +30,14 @@ db.exec(`
2630

2731
@hooks([authenticate])
2832
export class MessageService {
29-
async find(params: Params) {
33+
async find(params: MessageParams) {
3034
const limit = params.query?.$limit ?? 25
3135
const data = db.prepare('SELECT * FROM messages ORDER BY createdAt DESC LIMIT ?').all(limit) as Message[]
3236

3337
return { limit, data }
3438
}
3539

36-
async get(id: string, _params: Params) {
40+
async get(id: string, _params: MessageParams) {
3741
const message = db.prepare('SELECT * FROM messages WHERE id = ?').get(id) as Message | undefined
3842

3943
if (!message) {
@@ -43,24 +47,14 @@ export class MessageService {
4347
return message
4448
}
4549

46-
async create(data: Pick<Message, 'text'>, params: Params & {
47-
user: TalonAuthUser
48-
}) {
50+
async create(data: Pick<Message, 'text'>, params: MessageParams) {
4951
const createdAt = new Date().toISOString()
5052
const result = db.prepare('INSERT INTO messages (text, createdAt, user) VALUES (?, ?, ?)').run(
5153
data.text,
5254
createdAt,
5355
params.user.email!
5456
)
5557

56-
return this.get(result.lastInsertRowid, params)
57-
}
58-
59-
async remove(id: string, _params: Params) {
60-
const message = await this.get(id, _params)
61-
62-
db.prepare('DELETE FROM messages WHERE id = ?').run(id)
63-
64-
return message
58+
return this.get(`${result.lastInsertRowid}`, params)
6559
}
6660
}

frontend/src/main.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { feathers } from 'feathers'
22
import { fetchClient } from 'feathers/client'
33
import { getLoginElement } from 'talon-auth'
4+
import type { Client } from '../../api/src/app.js'
45

5-
// Initialize our Feathers client application through Socket.io
6-
// with hooks and authentication.
7-
const client = feathers()
6+
const client: Client = feathers()
87

98
client.configure(fetchClient(window.fetch.bind(window), {
109
baseUrl: 'http://localhost:3030',
@@ -14,10 +13,11 @@ client.configure(fetchClient(window.fetch.bind(window), {
1413
client.hooks([
1514
async (context, next) => {
1615
const login = await getLoginElement()
17-
const header = await login?.getHeader()
16+
const authorization = await login?.getHeader()
17+
1818
context.params.headers = {
1919
...context.params.headers,
20-
authorization: header
20+
authorization
2121
}
2222
return next()
2323
}
@@ -132,8 +132,6 @@ addEventListener('#send-message', 'submit', async (ev) => {
132132
client.service('messages').on('created', addMessage)
133133

134134
const init = async () => {
135-
document.getElementById('app').innerHTML = chatTemplate()
136-
137135
await client.setup()
138136

139137
// Find the latest 25 messages. They will come with the newest first
@@ -143,6 +141,8 @@ const init = async () => {
143141
}
144142
})
145143

144+
document.getElementById('app').innerHTML = chatTemplate()
145+
146146
// We want to show the newest message last
147147
for (const message of messages.data.reverse()) {
148148
await addMessage(message)

0 commit comments

Comments
 (0)