one less dependency

This commit is contained in:
2021-05-07 14:09:26 +02:00
parent 70b53106ea
commit 2aa186450a
3 changed files with 21 additions and 49 deletions

View File

@@ -1,8 +1,5 @@
import axios from 'axios'
import { dev } from '$app/env'
const base = axios.create({ baseURL: dev ? 'http://localhost:5000' : undefined })
export type Note = {
contents: string
views?: number
@@ -11,17 +8,34 @@ export type Note = {
export type NoteInfo = {}
export type NotePublic = Pick<Note, 'contents'>
type CallOptions = {
url: string
method: string
body?: any
}
const base = dev ? 'http://localhost:5000' : undefined
async function call(options: CallOptions) {
return fetch(base + options.url, {
method: options.method,
body: options.body === undefined ? undefined : JSON.stringify(options.body),
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
}).then((r) => r.json())
}
export async function create(note: Note) {
const { data } = await base({
const data = await call({
url: '/api/notes',
method: 'post',
data: note,
body: note,
})
return data as { id: string }
}
export async function get(id: string) {
const { data } = await base({
const data = await call({
url: `/api/notes/${id}`,
method: 'delete',
})
@@ -29,7 +43,7 @@ export async function get(id: string) {
}
export async function info(id: string) {
const { data } = await base({
const data = await call({
url: `/api/notes/${id}`,
method: 'get',
})