-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd7b7a0
commit 92599e3
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |