### File

`projects/app-base-library/src/lib/shared/api.ts`

### Index

###### Properties

- Public baseUrl
- Public cache
- Public credentials
- Public entities
- Public headers
- Public idField
- Public routes
- Public settings

###### Methods

- clearCredentials
- Public Async delete
- errorHandler
- flushLocalResource
- Public Async get
- getCredentials
- Public init
- initApiRoutes
- loadLocalResource
- login
- logout
- Public Async patch
- Public Async post
- saveLocalResource
- setCredentials

### Constructor

`constructor()`
Defined in projects/app-base-library/src/lib/shared/api.ts:15

### Properties

Public baseUrl
`baseUrl: `string``
Type : `string`
Default value : `'/api'`
Defined in projects/app-base-library/src/lib/shared/api.ts:11

Public cache
`cache: ```
Default value : `false`
Defined in projects/app-base-library/src/lib/shared/api.ts:14

Public credentials
`credentials: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/shared/api.ts:15

Public entities
`entities: `any``
Type : `any`
Default value : `{}`
Defined in projects/app-base-library/src/lib/shared/api.ts:13

Public headers
`headers: `any``
Type : `any`
Default value : `{}`
Defined in projects/app-base-library/src/lib/shared/api.ts:8

Public idField
`idField: `string``
Type : `string`
Default value : `'id'`
Defined in projects/app-base-library/src/lib/shared/api.ts:12

Public routes
`routes: `object``
Type : `object`
Default value : `{}`
Defined in projects/app-base-library/src/lib/shared/api.ts:10

Public settings
`settings: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/shared/api.ts:9

### Methods

clearCredentials
`clearCredentials(namespace: string)`
Defined in projects/app-base-library/src/lib/shared/api.ts:122

Parameters :

Name | Type | Optional | Default value
namespace | `string` | no | `'app:authData'`

Returns : `void`

Public Async delete
`delete(path: , id: )`
Defined in projects/app-base-library/src/lib/shared/api.ts:85

Parameters :

Name | Optional
path | no
id | no

Returns : `any`

errorHandler
`errorHandler(error: )`
Defined in projects/app-base-library/src/lib/shared/api.ts:145

Parameters :

Name | Optional
error | no

Returns : `void`

flushLocalResource
`flushLocalResource(ID: )`
Defined in projects/app-base-library/src/lib/shared/api.ts:184

Parameters :

Name | Optional
ID | no

Returns : `void`

Public Async get
`get(path: string, params: any)`
Defined in projects/app-base-library/src/lib/shared/api.ts:54

Parameters :

Name | Type | Optional | Default value
path | `string` | no |
params | `any` | no | `undefined`

Returns : `{}`

getCredentials
`getCredentials(namespace: string)`
Defined in projects/app-base-library/src/lib/shared/api.ts:115

Parameters :

Name | Type | Optional | Default value
namespace | `string` | no | `'app:authData'`

Returns : `any`

Public init
`init(settings: )`
Defined in projects/app-base-library/src/lib/shared/api.ts:19

Parameters :

Name | Optional
settings | no

Returns : `void`

initApiRoutes
`initApiRoutes()`
Defined in projects/app-base-library/src/lib/shared/api.ts:128
Returns : `void`

loadLocalResource
`loadLocalResource(ID: )`
Defined in projects/app-base-library/src/lib/shared/api.ts:178

Parameters :

Name | Optional
ID | no

Returns : `void`

login
`login(username: , password: )`
Defined in projects/app-base-library/src/lib/shared/api.ts:95

Parameters :

Name | Optional
username | no
password | no

Returns : `void`

logout
`logout(namespace: string, refresh: boolean)`
Defined in projects/app-base-library/src/lib/shared/api.ts:99

Parameters :

Name | Type | Optional | Default value
namespace | `string` | no | `'app:authData'`
refresh | `boolean` | no | `false`

Returns : `void`

Public Async patch
`patch(path: , id: , data: )`
Defined in projects/app-base-library/src/lib/shared/api.ts:75

Parameters :

Name | Optional
path | no
id | no
data | no

Returns : `any`

Public Async post
`post(path: , object: any)`
Defined in projects/app-base-library/src/lib/shared/api.ts:45

Parameters :

Name | Type | Optional
path |  | no
object | `any` | no

Returns : `any`

saveLocalResource
`saveLocalResource(resource: )`
Defined in projects/app-base-library/src/lib/shared/api.ts:174

Parameters :

Name | Optional
resource | no

Returns : `void`

setCredentials
`setCredentials(credentials: any, namespace: string)`
Defined in projects/app-base-library/src/lib/shared/api.ts:109

Parameters :

Name | Type | Optional | Default value
credentials | `any` | no |
namespace | `string` | no | `'app:authData'`

Returns : `void`

    declare var fetch: any;
    declare var _: any;

    export class Api {

        public headers: any = {};
        public settings: any;
        public routes = {};
        public baseUrl = '/api';
        public idField = 'id';
        public entities: any = {};
        public cache = false;
        public credentials: any;

        constructor() { }

        public init(settings) {
            this.settings = settings;
            if (this.settings.host) {
                this.baseUrl = settings.host;
            }
            if (this.settings.routes) {
                this.initApiRoutes();
            }
            if (this.settings.headers) {
                const self = this;
                this.settings.headers.forEach((obj) => {
                    for (const k in obj) {
                        self.headers[k] = obj[k];
                    }
                });
            }
            if(this['httpHeaders']) {
                // Angular only
                Object.keys(this.headers).forEach((k) => {
                    this['httpHeaders'].set(k, this.headers[k]);
                });
            }
        };

        // CRUD handlers
        public async post(path, object: any) {
            await fetch(`${this.baseUrl}${path}`, {
                headers: this.headers,
                method: 'post',
                // method: 'POST',
                body: JSON.stringify( object )
            });
        }

        public async get(path: string, params: any = undefined) {
            if (params) {
                path += '?';
                Object.keys(params).forEach(key => path += key + '=' + params[key] + '&');
            }
            let result: any = await fetch(`${this.baseUrl}${path}`, { headers: this.headers} )
                .then((response) => {
                    if (response.status >= 400) {
                        throw new Error('Bad response from server');
                    }
                    return response.json();
                })
                .then((data) => {
                    // if(data.data && data.data[this.idField]) {
                    //     this.entities[data.data[this.idField]] = data;
                    // }
                    return data;
                });
            return result;
        }

        public async patch(path, id, data) {
            await fetch(`${this.baseUrl}${path}/`+id, {
                headers: this.headers,
                // method: 'PATCH',
                method: 'patch',
                body: JSON.stringify( data )
            })
        }

        // FIXME: causes php server error on drupal content page
        public async delete(path, id) {
            // await fetch(`${this.baseUrl}${path}/` + id, {
            //     headers: this.headers,
            // //  method: 'DELETE'
            //     method: 'delete'
            // });
        }

        login(username, password) {

        }

        logout(namespace: string = 'app:authData', refresh: boolean = false) {
            this.clearCredentials(namespace);
            this.settings.authenticated = false;
            if (refresh) {
                // location.href = location.origin;
                location.href = location.href;
            }
        }

        // todo: use btoa + atob
        setCredentials(credentials: any, namespace: string = 'app:authData') {
            this.credentials = credentials;
            // window.localStorage.setItem(namespace, btoa(JSON.stringify(credentials)));
            window.localStorage.setItem(namespace, JSON.stringify(credentials));
        }

        getCredentials(namespace: string = 'app:authData') {
            if (window.localStorage.getItem(namespace)) {
                // return JSON.parse(atob(window.localStorage.getItem(namespace)));
                return JSON.parse(window.localStorage.getItem(namespace));
            }
        }

        clearCredentials(namespace: string = 'app:authData') {
            if (window.localStorage.getItem(namespace)) {
                window.localStorage.removeItem(namespace);
            }
        }

        initApiRoutes() {
            const self = this;
            self.settings.routes.forEach((obj) => {
                for (let k in obj) {
                    self.routes[k] = obj[k];
                }
            });
            if (self.settings.dev && self.settings.routes_dev) {
                self.settings.routes_dev.forEach((obj) => {
                    for (let k in obj) {
                        self.routes[k] = obj[k];
                    }
                });
            }
        }

        // todo
        errorHandler(error) {
            // AppService.scope.$broadcast('formError', error);
            console.error(error);
            if (!error) {
                return;
            }
            // if(error.data === "token expired"){
            //     toastr.warning($filter('translate')('LOGIN EXPIRED')+'.');
            //     service.logOut();
            //     return;
            // }
            // if (error.statusText === 'Bad Request' || error.status == 400) {
            //     if (error.data.message) {
            //         toastr.warning($filter('translate')(error.data.message));
            //     } else {
            //         toastr.warning($filter('translate')('ERROR BAD REQUEST') + '.');
            //     }
            // }
            // if(error.statusText === 'Unauthorized' || error.status == 401){
            //     toastr.warning($filter('translate')('UNAUTHORIZED ERROR')+'.');
            //     service.logOut();
            //     return;
            // }
            // if(error.statusText === 'Not found' || error.status == 404){
            //     toastr.warning($filter('translate')('ERROR NOT FOUND')+'.');
            // }
        }

        // todo: remove for Storage.js
        saveLocalResource(resource) {
            localStorage.setItem(resource.ID, resource);
        }

        loadLocalResource(ID) {
            if (localStorage.getItem(ID)) {
                localStorage.getItem(ID);
            }
        }

        flushLocalResource(ID) {
            if (localStorage.getItem(ID)) {
                localStorage.removeItem(ID);
            }
        }
    }

# results matching ""

# No results matching ""