1- import { ofetch } from 'ofetch '
2- import { useCookie } from 'nuxt/app '
1+ import { useCookie , useNuxtApp } from 'nuxt/app '
2+ import type { ofetch } from 'ofetch '
33import { useLaravelConfig } from '#imports'
44
55export const useLaravelApi = ( ) => {
66 const config = useLaravelConfig ( )
77
8- const xsrfToken = useCookie ( 'XSRF-TOKEN' ) . value
9-
108 const getApiUrl = ( url : string ) : string => {
119 if ( ! url . startsWith ( '/' ) ) {
1210 url = `/${ url } `
@@ -24,9 +22,15 @@ export const useLaravelApi = () => {
2422 return `${ apiBase } ${ url } `
2523 }
2624
27- const get = async ( url : string , options : any = { } ) => {
28- return await ofetch ( getApiUrl ( url ) , {
29- method : 'GET' ,
25+ const makeRequest = async ( url : string , options : any = { } ) => {
26+ const xsrfToken = useCookie ( 'XSRF-TOKEN' ) . value
27+
28+ const fetch =
29+ typeof useNuxtApp ( ) . $apiFetch === 'function'
30+ ? ( useNuxtApp ( ) . $apiFetch as typeof ofetch )
31+ : ( $fetch as typeof ofetch )
32+
33+ return await fetch ( getApiUrl ( url ) , {
3034 headers : {
3135 Accept : 'application/json' ,
3236 'X-XSRF-TOKEN' : xsrfToken || '' ,
@@ -35,52 +39,39 @@ export const useLaravelApi = () => {
3539 ...options ,
3640 } )
3741 }
42+
43+ const get = async ( url : string , options : any = { } ) => {
44+ return makeRequest ( url , {
45+ ...options ,
46+ method : 'GET' ,
47+ } )
48+ }
3849 const post = async ( url : string , body : any , options : any = { } ) => {
39- return await ofetch ( getApiUrl ( url ) , {
50+ return makeRequest ( url , {
51+ ...options ,
4052 method : 'POST' ,
41- headers : {
42- Accept : 'application/json' ,
43- 'X-XSRF-TOKEN' : xsrfToken || '' ,
44- } ,
4553 body,
46- credentials : 'include' ,
47- ...options ,
4854 } )
4955 }
5056 const put = async ( url : string , body : any , options : any = { } ) => {
51- return await ofetch ( getApiUrl ( url ) , {
57+ return makeRequest ( url , {
58+ ...options ,
5259 method : 'PUT' ,
53- headers : {
54- Accept : 'application/json' ,
55- 'X-XSRF-TOKEN' : xsrfToken || '' ,
56- } ,
5760 body,
58- credentials : 'include' ,
59- ...options ,
6061 } )
6162 }
6263 const patch = async ( url : string , body : any , options : any = { } ) => {
63- return await ofetch ( getApiUrl ( url ) , {
64+ return makeRequest ( url , {
65+ ...options ,
6466 method : 'PATCH' ,
65- headers : {
66- Accept : 'application/json' ,
67- 'X-XSRF-TOKEN' : xsrfToken || '' ,
68- } ,
6967 body,
70- credentials : 'include' ,
71- ...options ,
7268 } )
7369 }
7470 const destroy = async ( url : string , body : any , options : any = { } ) => {
75- return await ofetch ( getApiUrl ( url ) , {
71+ return makeRequest ( url , {
72+ ...options ,
7673 method : 'DELETE' ,
77- headers : {
78- Accept : 'application/json' ,
79- 'X-XSRF-TOKEN' : xsrfToken || '' ,
80- } ,
81- credentials : 'include' ,
8274 body,
83- ...options ,
8475 } )
8576 }
8677
@@ -100,6 +91,7 @@ export const useLaravelApi = () => {
10091 put,
10192 patch,
10293 destroy,
94+ makeRequest,
10395 client,
10496 }
10597}
0 commit comments