cms/app-client/mirage/config.js
author ymh <ymh.work@gmail.com>
Mon, 03 Oct 2016 16:32:41 +0200
changeset 318 5564f5065f81
parent 307 07b44a378ad8
child 319 78990a8a069b
permissions -rw-r--r--
add a root url for corpus back apis. it is set to the same than root api for the moment

import ENV from 'app-client/config/environment';
import _ from 'lodash/lodash';
import * as constants from 'corpus-common-addon/utils/constants';

export default function() {

    // These comments are here to help you get started. Feel free to delete them.

    /*
      Config (with defaults).

      Note: these only affect routes defined *after* them!
    */
    // this.urlPrefix = '';    // make this `http://localhost:8080`, for example, if your API is on a different server
    // this.namespace = '';    // make this `api`, for example, if your API is namespaced
    this.namespace = ENV.backRootURL.replace(/\/$/,'')+'/api/v1';
    // this.timing = 400;      // delay for each request, automatically set to 0 during testing

    this.get('/documents', function({ documents }) {
        return this.serialize(documents.all(), 'sparse-document');
    });

    this.get('/documents/:id', ({documents}, request) => {
        let id = decodeURIComponent(request.params.id);
        return documents.find(id);
    });

    this.get('/documents/:id/transcript',  ({transcripts}, request) => {
        let id = decodeURIComponent(request.params.id);
        return transcripts.find(id).transcript;
    });

    this.get('/stats/languages', 'languages');

    this.get('/stats/geostats', 'geostats');

    this.get('/stats/themes', 'themes');

    this.get('/stats/discourses', 'discourses');

    this.get('/stats/datestats', 'datestats');

    this.get('/resolvers/lexvo/:ids', ({lexvos}, request) => {
        var langIds = decodeURIComponent(request.params.ids);
        var resMap = _.reduce(langIds.split(','), function(res, id) {
            var fullId = id;
            if(!_.startsWith(fullId, constants.LEXVO_BASE_URL)) {
                fullId = constants.LEXVO_BASE_URL + id;
            }
            var lexvoRes = lexvos.find(fullId);
            res[id] = lexvoRes?lexvoRes.name:null;
            return res;
        }, {});
        return {
            'lexvoids': resMap
        };
    });

    this.get('/resolvers/bnf/:ids', ({ bnfs }, request) => {
        var bnfIds = decodeURIComponent(request.params.ids);
        var resMap = _.reduce(bnfIds.split(','), function(res, id) {
            var fullId = id;
            if(_.startsWith(fullId, constants.BNF_BASE_URL)) {
                fullId = fullId.slice(constants.BNF_BASE_URL.length);
            } else if (_.startsWith(fullId, constants.BNF_ARK_BASE_URL)) {
                fullId = fullId.slice(constants.BNF_ARK_BASE_URL.length);
            } else if (!_.startsWith(fullId, constants.BNF_ARK_BASE_ID)) {
                fullId = constants.BNF_ARK_BASE_ID + fullId;
            }
            var bnfRes = bnfs.find(fullId);
            res[id] = bnfRes?bnfRes.label:null;
            return res;
        }, {});
        return {
            'bnfids': resMap
        };
    });

    this.get('/resolvers/geonames/:ids', ({ geonames }, request) => {
        var geonamesIds = decodeURIComponent(request.params.ids);
        var resMap = _.reduce(geonamesIds.split(','), function(res, id) {
            var code = id;
            var m = code.match(constants.GEONAMES_BASE_URLS);
            if(m) {
                code = code.slice(m[0].length);
            }
            code = code.replace(/\/+$/, '');
            var geonamesRes = geonames.find(code);
            res[id] = geonamesRes?geonamesRes.label:null;
            return res;
        }, {});
        return {
            'geonamesids': resMap
        };
    });

}