} -->
pattern ini berguna untuk mengumpulkan semua operasi ke database di satu tempat.
berikut ini contohnya
// @noErrors
// @filename: repository.ts
interface IPost {
title: string
body: string
userId: number
}
export class FakeRepository {
public async create(body: IPost) {
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
const json = await res.json()
return json
}
public async read() {
const res = await fetch('https://jsonplaceholder.typicode.com/posts')
const json = await res.json()
return json
}
public async update(body: IPost) {
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'PUT',
body: JSON.stringify(body),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
const json = await res.json()
return json
}
public async delete() {
const res = await fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'DELETE',
})
const json = await res.json()
return json
}
}
// @noErrors
// @filename: index.ts
import { FakeRepository } from '$lib/pattern/repository'
const fakeRepo = new FakeRepository()
async function createData() {
const res = await fakeRepo.create(form)
console.log(res)
}
async function readData() {
const res = await fakeRepo.delete()
console.log(res)
}
readData()