1+ /* Copyright Spurwing.io and Healthie Inc.
2+ * Released under the MIT license
3+ * https://www.spurwing.io/
4+ */
5+
16const Spurwing = ( function ( ) {
27 'use strict' ;
38
@@ -7,45 +12,42 @@ const Spurwing = (function() {
712 }
813
914 this . get_appointment_types = async function ( provider_id , clients_can_book ) {
10- return await this . GET ( this . API_URL + 'appointment_types.json' , { provider_id, clients_can_book } )
15+ return await this . HTTP ( 'GET' , this . API_URL + 'appointment_types.json' , { provider_id, clients_can_book } )
1116 }
1217 this . get_days_available = async function ( provider_id , appointment_type_id , date_from_month , timezone , org_level ) {
13- return await this . GET ( this . API_URL + 'bookings/days_available.json' , { provider_id, appointment_type_id, date_from_month, timezone, org_level } )
18+ return await this . HTTP ( 'GET' , this . API_URL + 'bookings/days_available.json' , { provider_id, appointment_type_id, date_from_month, timezone, org_level } )
1419 }
1520 this . get_slots_available = async function ( provider_id , appointment_type_id , start_date , end_date , org_level ) {
16- return await this . GET ( this . API_URL + 'bookings/slots_available.json' , { provider_id, appointment_type_id, start_date, end_date, org_level } )
21+ return await this . HTTP ( 'GET' , this . API_URL + 'bookings/slots_available.json' , { provider_id, appointment_type_id, start_date, end_date, org_level } )
1722 }
1823 this . complete_booking = async function ( provider_id , appointment_type_id , date , timezone , first_name , last_name , email , phone_number , contact_type ) {
19- return await this . POST ( this . API_URL + 'bookings/complete_booking.json' , { provider_id, appointment_type_id, date, timezone, first_name, last_name, email, phone_number, contact_type } )
24+ return await this . HTTP ( 'POST' , this . API_URL + 'bookings/complete_booking.json' , { provider_id, appointment_type_id, date, timezone, first_name, last_name, email, phone_number, contact_type } )
2025 }
21-
22- this . GET = async function ( url , params ) {
23- return new Promise ( ( resolve , reject ) => {
24- this . ajax ( {
25- method : 'GET' ,
26- url : url ,
27- data : params ,
28- success : ( ( data ) => resolve ( data ) ) ,
29- error : ( ( data ) => reject ( data ) ) ,
30- } ) ;
31- } ) . catch ( error => { console . error ( 'GET error:' , error ) ; throw error } ) ;
26+ this . list_appointments = async function ( authorization , page_size , offset , provider_id ) {
27+ return await this . HTTP ( 'GET' , this . API_URL + 'appointments' , { page_size, offset, provider_id } , { authorization : 'Bearer ' + authorization } )
28+ }
29+ this . delete_appointment = async function ( appointment_id , authorization ) {
30+ return await this . HTTP ( 'DELETE' , this . API_URL + 'appointments/' + appointment_id , { } , { authorization : 'Bearer ' + authorization } )
3231 }
33- this . POST = async function ( url , data ) {
32+
33+ this . HTTP = async function ( method , url , data , headers ) {
3434 return new Promise ( ( resolve , reject ) => {
3535 this . ajax ( {
36- method : 'POST' ,
36+ method : method ,
3737 url : url ,
3838 data : data ,
39+ headers : headers || { } ,
3940 success : ( ( data ) => resolve ( data ) ) ,
4041 error : ( ( data ) => reject ( data ) ) ,
4142 } ) ;
42- } ) . catch ( error => { console . error ( 'POST error:', error ) ; throw error } ) ;
43+ } ) . catch ( error => { console . error ( method + ' error:', error ) ; throw error } ) ;
4344 }
45+
4446 this . ajax = function ( option ) {
4547 function isEmpty ( obj ) {
4648 return Object . keys ( obj ) . length === 0 ;
4749 }
48- let { method, url, success, error} = option ;
50+ let { method, url, success, error, headers } = option ;
4951 if ( ! method )
5052 return console . error ( 'not set method' ) ;
5153 if ( ! url )
@@ -74,6 +76,9 @@ const Spurwing = (function() {
7476 xhr . open ( method , url ) ;
7577 xhr . setRequestHeader ( 'Content-Type' , 'application/json;charset=utf-8' ) ;
7678 xhr . setRequestHeader ( 'Accept' , 'application/json, text/javascript' ) ;
79+ for ( const hdr in headers ) {
80+ xhr . setRequestHeader ( hdr , headers [ hdr ] ) ;
81+ }
7782 xhr . send ( data ) ;
7883 xhr . onreadystatechange = function ( ) {
7984 if ( xhr . readyState === 4 ) {
0 commit comments