Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
netchampfaris committed Nov 20, 2024
1 parent fd7b7a0 commit 92599e3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions histoire.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ const route = urlParams.get('route')
if (route) {
history.pushState({}, '', route)
}

// set theme attribute on load
if (document.documentElement.classList.contains('htw-dark')) {
document.documentElement.setAttribute('data-theme', 'dark')
}
Empty file added src/data-fetching/useDoctype.ts
Empty file.
Empty file.
60 changes: 60 additions & 0 deletions src/data-fetching/useFrappeFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createFetch } from '@vueuse/core'

const useFrappeFetch = createFetch({
options: {
beforeFetch({ options }) {
options.headers = setHeaders(options.headers || {})
return { options }
},
afterFetch(ctx) {
console.log('afterFetch', ctx)
let data = JSON.parse(ctx.data).data
ctx.data = data
return ctx
},
onFetchError(ctx) {
type FrappeError = {
title: string
message: string
type: string
indicator: string
}
let errors: Array<FrappeError> = []
try {
errors = JSON.parse(ctx.data).errors
} catch (e) {
errors = [
{
title: 'Internal Server Error',
message: 'Internal Server Error',
type: 'ServerError',
indicator: 'red',
},
]
}
let error = errors[0] // assuming only one error for now
let frappeError = new Error(`${error.type}: ${error.message}`)
// e.type = error.type
// e.exception = exception

ctx.error = frappeError
return ctx
},
},
})

function setHeaders(headers: HeadersInit) {
const defaultHeaders: Record<string, string> = {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'X-Frappe-Site-Name': window.location.hostname,
}

if (window.csrf_token && window.csrf_token !== '{{ csrf_token }}') {
defaultHeaders['X-Frappe-CSRF-Token'] = window.csrf_token
}

return { ...headers, ...defaultHeaders }
}

export default useFrappeFetch
41 changes: 41 additions & 0 deletions src/data-fetching/useList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import useFrappeFetch from './useFrappeFetch'

interface ListOptions {
filters: any
fields: string[]
}

export default function useList<T = any>(
doctype: string,
options: ListOptions = { filters: {}, fields: [] },
) {
let params = new URLSearchParams()
if (options.fields?.length) {
params.append('fields', JSON.stringify(options.fields))
}
if (options.filters) {
params.append('filters', JSON.stringify(options.filters))
}
let url = `/api/v2/document/${doctype}?${params.toString()}`
const {
data,
error,
isFetching,
isFinished,
canAbort,
abort,
aborted,
execute,
} = useFrappeFetch<T>(url)

return {
data,
error,
isFetching,
isFinished,
canAbort,
abort,
aborted,
execute,
}
}

0 comments on commit 92599e3

Please sign in to comment.