1- import type { EventId , EventType , ServerEvent } from '../../types' ;
1+ import type { EventId , EventType , ServerEvent } from '../../types' ;
22import { REST_API_URL } from "./constants" ;
3+ import { useNuxtApp } from "#app"
34
45type TUseEventsRequests = ( ) => {
56 getAll : ( ) => Promise < ServerEvent < unknown > [ ] > ,
@@ -11,23 +12,33 @@ type TUseEventsRequests = () => {
1112 getEventRestUrl : ( param : EventId | undefined ) => string
1213}
1314
15+ // TODO: add 403 response handling
16+
1417export const useEventsRequests : TUseEventsRequests = ( ) => {
15- const getEventRestUrl = ( param ?: string ) => `${ REST_API_URL } /api/event${ param ? `/${ param } ` : 's' } `
18+ const app = useNuxtApp ( )
19+ const token : string | null = app . $authToken . token
20+ const headers = { "X-Auth-Token" : token }
21+ const getEventRestUrl = ( param ?: string ) : string => `${ REST_API_URL } /api/event${ param ? `/${ param } ` : 's' } `
1622
17- const getAll = ( ) => fetch ( getEventRestUrl ( ) )
23+ const getAll = ( ) => fetch ( getEventRestUrl ( ) , { headers } )
1824 . then ( ( response ) => response . json ( ) )
1925 . then ( ( response ) => {
2026 if ( response ?. data ) {
2127 return response . data as ServerEvent < unknown > [ ]
2228 }
2329
30+ if ( response ?. code === 403 ) {
31+ console . error ( 'Forbidden' )
32+ return [ ] ;
33+ }
34+
2435 console . error ( 'Fetch Error' )
2536
2637 return [ ] ;
2738 } )
2839 . then ( ( events : ServerEvent < unknown > [ ] ) => events )
2940
30- const getSingle = ( id : EventId ) => fetch ( getEventRestUrl ( id ) )
41+ const getSingle = ( id : EventId ) => fetch ( getEventRestUrl ( id ) , { headers } )
3142 . then ( ( response ) => response . json ( ) )
3243 . then ( ( response ) => {
3344 if ( response ?. data ) {
@@ -36,22 +47,30 @@ export const useEventsRequests: TUseEventsRequests = () => {
3647 return null ;
3748 } )
3849
39- const deleteSingle = ( id : EventId ) => fetch ( getEventRestUrl ( id ) , { method : 'DELETE' } )
50+ const deleteSingle = ( id : EventId ) => fetch ( getEventRestUrl ( id ) , { method : 'DELETE' , headers } )
4051 . catch ( ( err ) => {
4152 console . error ( 'Fetch Error' , err )
4253 } )
4354
44- const deleteAll = ( ) => fetch ( getEventRestUrl ( ) , { method : 'DELETE' } )
55+ const deleteAll = ( ) => fetch ( getEventRestUrl ( ) , { method : 'DELETE' , headers } )
4556 . catch ( ( err ) => {
4657 console . error ( 'Fetch Error' , err )
4758 } )
4859
49- const deleteList = ( uuids : EventId [ ] ) => fetch ( getEventRestUrl ( ) , { method : 'DELETE' , body : JSON . stringify ( { uuids } ) } )
60+ const deleteList = ( uuids : EventId [ ] ) => fetch ( getEventRestUrl ( ) , {
61+ method : 'DELETE' ,
62+ headers,
63+ body : JSON . stringify ( { uuids} )
64+ } )
5065 . catch ( ( err ) => {
5166 console . error ( 'Fetch Error' , err )
5267 } )
5368
54- const deleteByType = ( type : EventType ) => fetch ( getEventRestUrl ( ) , { method : 'DELETE' , body : JSON . stringify ( { type} ) } )
69+ const deleteByType = ( type : EventType ) => fetch ( getEventRestUrl ( ) , {
70+ method : 'DELETE' ,
71+ headers,
72+ body : JSON . stringify ( { type} )
73+ } )
5574 . catch ( ( err ) => {
5675 console . error ( 'Fetch Error' , err )
5776 } )
0 commit comments