All files index.ts

100% Statements 81/81
96.43% Branches 27/28
100% Functions 26/26
100% Lines 71/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1511x 1x                                             1x   36x 36x   36x   1x 3x       4x 4x   1x     4x 4x   1x     4x 4x   1x     4x 4x   1x     4x 4x   1x     4x 4x   1x     1x 36x 36x 35x   36x 36x   36x   36x 1x     36x 36x 36x   36x 36x 9x   36x 8x     36x 4x     36x     1x 20x   1x 6x   1x 24x 18x   6x   1x 3x 2x   1x     1x 3x   1x 12x   1x 4x   1x 4x   1x 4x   1x 27x                 1x  
require('es6-promise').polyfill();
require('isomorphic-fetch');
 
export interface Identifyable {
    id: any;
}
 
export interface IFetchConfig {
    protocol: string;
    ip: string;
    port?: number;
    api?: string;
}
 
export interface IFetchBase<T extends Identifyable> {
    delete(item: T): Promise<any>;
    find(id: any): Promise<T>;
    findAll<TResult>(suffix?: string): Promise<TResult[]>;
    get(queryParams?: string): Promise<T[]>;
    head(item: T): Promise<any>;
    post(item: T): Promise<any>;
    put(item: T): Promise<any>;
}
 
export abstract class FetchBase<T extends Identifyable>
    implements IFetchBase<T> {
    protected endpoint: string = '';
    protected requestMode: RequestMode = 'cors';
 
    constructor(private config: IFetchConfig) {}
 
    head(item: T): Promise<any> {
        return fetch(this.getUrl(item), this.headOptions())
            .then(this.handleHeadFetchResponse)
            .catch(this.rejectErrorPromise);
    }
    findAll<TResult>(suffix?: string): Promise<TResult[]> {
        return fetch(`${this.getUrl()}${suffix}`, this.getOptions())
            .then(this.handleFetchResponse)
            .then((json: any) => this.jsonResponse<TResult[]>(json))
            .catch(this.rejectErrorPromise);
    }
    find(id: any): Promise<T> {
        return fetch(this.getUrl(<T>{ id }), this.getOptions())
            .then(this.handleFetchResponse)
            .then((json: any) => this.jsonResponse<T>(json))
            .catch(this.rejectErrorPromise);
    }
    get(queryParams?: string): Promise<T[]> {
        return fetch(this.getUrl(void 0, queryParams), this.getOptions())
            .then(this.handleFetchResponse)
            .then((json: any) => this.jsonResponse<T[]>(json))
            .catch(this.rejectErrorPromise);
    }
    put(item: T): Promise<any> {
        return fetch(this.getUrl(item), this.putOptions(item))
            .then(this.handleFetchResponse)
            .then((json: any) => this.jsonResponse<any>(json))
            .catch(this.rejectErrorPromise);
    }
    post(item: T): Promise<any> {
        return fetch(this.getUrl(item), this.postOptions(item))
            .then(this.handleFetchResponse)
            .then((json: any) => this.jsonResponse<any>(json))
            .catch(this.rejectErrorPromise);
    }
    delete(item: T): Promise<any> {
        return fetch(this.getUrl(item), this.deleteOptions())
            .then(this.handleFetchResponse)
            .then((json: any) => this.jsonResponse<any>(json))
            .catch(this.rejectErrorPromise);
    }
    getUrl(resource?: T, queryParams?: string): string {
        let { protocol, ip, port } = this.config;
        if (!protocol) {
            protocol = 'http';
        }
        Eif (!ip) {
            ip = 'localhost';
        }
        let portString = '';
 
        if (port != null && port > 0) {
            portString += `:${port}`;
        }
 
        let url = `${protocol}://${ip}${portString}`;
        url += this.config.api ? `/${this.config.api}` : '';
        url += this.endpoint ? `/${this.endpoint}` : '';
 
        let resourceId = '';
        if (resource != null && resource.id != null) {
            resourceId = resource.id.toString();
        }
        if (resourceId.length > 0 && resourceId != '0') {
            url += `/${resourceId}`;
        }
 
        if (queryParams != null && queryParams.length > 0) {
            url += queryParams;
        }
 
        return url;
    }
 
    protected rejectErrorPromise(reason: Error) {
        return Promise.reject(reason);
    }
    protected jsonResponse<TResult>(json) {
        return Promise.resolve<TResult>(json);
    }
    protected handleFetchResponse(response: Response) {
        if (!response.ok) {
            return Promise.reject(new Error(response.statusText));
        }
        return response.json();
    }
    protected handleHeadFetchResponse(response: Response) {
        if (!response.ok) {
            return Promise.reject(new Error(response.statusText));
        }
        return Promise.resolve(response.headers);
    }
 
    protected headOptions(): RequestInit {
        return this.getRequestInit('HEAD');
    }
    protected getOptions(): RequestInit {
        return this.getRequestInit('GET');
    }
    protected postOptions(item: T): RequestInit {
        return this.getRequestInit('POST', JSON.stringify(item));
    }
    protected putOptions(item: T): RequestInit {
        return this.getRequestInit('PUT', JSON.stringify(item));
    }
    protected deleteOptions(): RequestInit {
        return this.getRequestInit('DELETE');
    }
    protected getRequestInit(method: string, body?: string): RequestInit {
        return {
            body,
            method,
            mode: this.requestMode,
            headers: {
                'Content-Type': 'application/json'
            }
        };
    }
}