Skip to content
Snippets Groups Projects
Select Git revision
  • 7ea25cd0a533789eb4e8e053e939a968ca1ccca9
  • master default protected
  • 1.31
  • 4.38.6
  • 4.38.5
  • 4.38.4
  • 4.38.3
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
23 results

restapi.mjs

Blame
  • restapi.mjs 7.50 KiB
    /**
     * Copyright schukai GmbH and contributors 2022. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    import {internalSymbol, instanceSymbol} from "../../../constants.mjs";
    import {isObject} from "../../../types/is.mjs";
    import {Server} from "../server.mjs";
    import {Pathfinder} from "../../pathfinder.mjs";
    import {Pipe} from "../../pipe.mjs";
    import {WriteError} from "./restapi/writeerror.mjs";
    
    export {RestAPI}
    
    /**
     * The RestAPI is a class that enables a REST API server.
     *
     * @externalExample ../../../../example/data/datasource/server/restapi.mjs
     * @license AGPLv3
     * @since 1.22.0
     * @copyright schukai GmbH
     * @memberOf Monster.Data.Datasource.Server
     * @summary The RestAPI is a class that binds a REST API server.
     */
    class RestAPI extends Server {
    
        /**
         *
         * @param {Object} [options] options contains definitions for the datasource.
         */
        constructor(options) {
            super();
    
            if (isObject(options)) {
                this.setOptions(options);
            }
    
        }
    
        /**
         * This method is called by the `instanceof` operator.
         * @returns {symbol}
         * @since 2.1.0
         */
        static get [instanceSymbol]() {
            return Symbol.for("@schukai/monster/data/datasource/server/restapi");
        }
    
        /**
         * @property {Object} write={} Options
         * @property {Object} write.init={} An options object containing any custom settings that you want to apply to the request. The parameters are identical to those of the {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/Request|Request constructor}
         * @property {string} write.init.method=POST
         * @property {Object} write.init.headers Object containing any custom headers that you want to apply to the request.
         * @property {string} write.acceptedStatus=[200,201]
         * @property {string} write.url URL
         * @property {Object} write.mapping the mapping is applied before writing.
         * @property {String} write.mapping.transformer Transformer to select the appropriate entries
         * @property {Monster.Data.Datasource~exampleCallback[]} write.mapping.callback with the help of the callback, the structures can be adjusted before writing.
         * @property {Object} write.report
         * @property {String} write.report.path Path to validations
         * @property {Object} write.sheathing
         * @property {Object} write.sheathing.object Object to be wrapped
         * @property {string} write.sheathing.path Path to the data
         * @property {Object} read={} Options
         * @property {Object} read.init={} An options object containing any custom settings that you want to apply to the request. The parameters are identical to those of the {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/Request|Request constructor}
         * @property {string} read.init.method=GET
         * @property {string} read.acceptedStatus=[200]
         * @property {string} read.url URL
         * @property {Object} read.mapping the mapping is applied after reading.
         * @property {String} read.mapping.transformer Transformer to select the appropriate entries
         * @property {Monster.Data.Datasource~exampleCallback[]} read.mapping.callback with the help of the callback, the structures can be adjusted after reading.
         */
        get defaults() {
            return Object.assign({}, super.defaults, {
                write: {
                    init: {
                        method: 'POST',
                    },
                    acceptedStatus: [200, 201],
                    url: undefined,
                    mapping: {
                        transformer: undefined,
                        callbacks: []
                    },
                    sheathing: {
                        object: undefined,
                        path: undefined,
                    },
                    report: {
                        path: undefined
                    }
                },
                read: {
                    init: {
                        method: 'GET'
                    },
                    acceptedStatus: [200],
                    url: undefined,
                    mapping: {
                        transformer: undefined,
                        callbacks: []
                    },
                },
    
            });
        }
    
        /**
         * @return {Promise}
         * @throws {Error} the options does not contain a valid json definition
         * @throws {TypeError} value is not a object
         * @throws {Error} the data cannot be read
         */
        read() {
            const self = this;
            let response;
    
            let init = self.getOption('read.init');
            if (!isObject(init)) init = {};
    
            return new Promise((resolve, reject) => {
                fetch(self.getOption('read.url'), init).then(resp => {
                    response = resp;
    
                    const acceptedStatus = self.getOption('read.acceptedStatus', [200]);
    
                    if (acceptedStatus.indexOf(resp.status) === -1) {
                        throw Error('the data cannot be read (response ' + resp.status + ')')
                    }
    
                    return resp.text()
                }).then(body => {
    
                    let obj;
    
                    try {
                        obj = JSON.parse(body);
    
                    } catch (e) {
    
                        if (body.length > 100) {
                            body = body.substring(0, 97) + '...';
                        }
    
                        throw new Error('the response does not contain a valid json (actual: ' + body + ').');
                    }
    
                    self.set(self.transformServerPayload.call(self, obj));
                    resolve(response);
                }).catch(reject);
    
            })
        }
    
        /**
         * @return {Promise}
         * @throws {WriteError} the data cannot be written
         */
        write() {
            const self = this;
    
            let init = self.getOption('write.init');
            if (!isObject(init)) init = {};
            if (typeof init['headers'] !== 'object') {
                init['headers'] = {
                    'Content-Type': 'application/json'
                }
            }
    
            let obj = self.prepareServerPayload(self.get());
            init['body'] = JSON.stringify(obj);
    
            return new Promise((resolve, reject) => {
                fetch(self.getOption('write.url'), init).then(response => {
                    const acceptedStatus = self.getOption('write.acceptedStatus', [200, 201]);
    
                    if (acceptedStatus.indexOf(response.status) > -1) {
                        reject(response);
                        return;
                    }
    
                    response.text().then((body) => {
    
                        let obj = {}, validation = {};
                        try {
                            obj = JSON.parse(body);
    
                            if (reportPath) {
                                validation = (new Pathfinder(obj)).getVia(reportPath);
                            }
    
    
                        } catch (e) {
    
                            if (body.length > 100) {
                                body = body.substring(0, 97) + '...';
                            }
    
                            reject(new Error('the response does not contain a valid json (actual: ' + body + ').'));
                            return;
                        }
    
                        reject(new WriteError('the data cannot be written (response ' + response.status + ')', response, validation))
                        return;
    
                    }).catch(reject);
    
    
                }).catch(reject);
    
            })
    
        }
    
    
        /**
         * @return {RestAPI}
         */
        getClone() {
            const self = this;
            return new RestAPI(self[internalSymbol].getRealSubject()['options'].read, self[internalSymbol].getRealSubject()['options'].write);
        }
    
    }