Skip to content

Commit 5ec6afa

Browse files
authored
fix: ignore client-provided resource IDs (#1736)
1 parent 352144e commit 5ec6afa

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/service.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ await test('find', async (t) => {
101101

102102
await test('create', async () => {
103103
const post = { title: 'new post' }
104-
const res = await service.create(POSTS, post)
104+
let res = await service.create(POSTS, post)
105105
assert.equal(res?.['title'], post.title)
106106
assert.equal(typeof res?.['id'], 'string', 'id should be a string')
107107

108+
res = await service.create(POSTS, { ...post, id: 'foo' })
109+
assert.notEqual(res?.['id'], 'foo', 'user should not be able to set id')
110+
108111
assert.equal(await service.create(UNKNOWN_RESOURCE, post), undefined)
109112
})
110113

src/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class Service {
146146
const items = this.#get(name)
147147
if (items === undefined || !Array.isArray(items)) return
148148

149-
const item = { id: randomId(), ...data }
149+
const item = { ...data, id: randomId() }
150150
items.push(item)
151151

152152
await this.#db.write()

0 commit comments

Comments
 (0)