Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSR basic api #58

Merged
merged 5 commits into from
Dec 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 88 additions & 22 deletions server/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Route from './route.js';
import Group from './group.js';
import { Meteor } from 'meteor/meteor';
import { _helpers } from './../lib/_helpers.js';
import { Meteor } from 'meteor/meteor';
import page from 'page';
import Route from './route.js';
import Group from './group.js';
import { _helpers } from '../lib/_helpers.js';

const qs = require('qs');

Expand All @@ -10,6 +11,24 @@ class Router {
this.pathRegExp = /(:[\w\(\)\\\+\*\.\?\[\]\-]+)+/g;
this._routes = [];
this._routesMap = {};
this._current = {};
this._specialChars = ['/', '%', '+'];
this._encodeParam = param => {
const paramArr = param.split('');
let _param = '';
for (let i = 0; i < paramArr.length; i++) {
if (this._specialChars.includes(paramArr[i])) {
_param += encodeURIComponent(encodeURIComponent(paramArr[i]));
} else {
try {
_param += encodeURIComponent(paramArr[i]);
} catch (e) {
_param += paramArr[i];
}
}
}
return _param;
};
this.subscriptions = Function.prototype;

// holds onRoute callbacks
Expand All @@ -21,10 +40,31 @@ class Router {
},
exit() {
// client only
}
},
};
}

matchPath(path) {
const params = {};
const route = this._routes.find(r => {
const pageRoute = new page.Route(r.pathDef);

return pageRoute.match(path, params);
});
if (!route) {
return null;
}

return {
params: _helpers.clone(params),
route: _helpers.clone(route),
};
}

setCurrent(current) {
this._current = current;
}

route(pathDef, options = {}) {
if (!/^\/.*/.test(pathDef) && pathDef !== '*') {
throw new Error('route\'s path must start with "/"');
Expand All @@ -51,14 +91,21 @@ class Router {
pathDef = this._routesMap[pathDef].path;
}

let path = pathDef.replace(this.pathRegExp, (_key) => {
let path = pathDef.replace(this.pathRegExp, _key => {
const firstRegexpChar = _key.indexOf('(');
// get the content behind : and (\\d+/)
let key = _key.substring(1, (firstRegexpChar > 0) ? firstRegexpChar : undefined);
let key = _key.substring(
1,
firstRegexpChar > 0 ? firstRegexpChar : undefined
);
// remove +?*
key = key.replace(/[\+\*\?]+/g, '');

return fields[key] || '';
if (fields[key]) {
return this._encodeParam(`${fields[key]}`);
}

return '';
});

path = path.replace(/\/\/+/g, '/'); // Replace multiple slashes with single slash
Expand All @@ -68,8 +115,8 @@ class Router {
path = path.match(/^\/{1}$/) ? path : path.replace(/\/$/, '');

const strQueryParams = qs.stringify(queryParams || {});
if(strQueryParams) {
path += '?' + strQueryParams;
if (strQueryParams) {
path += `?${strQueryParams}`;
}

return path;
Expand All @@ -84,59 +131,71 @@ class Router {
// object.
// This is not to hide what's inside the route object, but to show
// these are the public APIs
const routePublicApi = _helpers.pick(currentRoute, ['name', 'pathDef', 'path']);
routePublicApi.options = _helpers.omit(currentRoute.options, ['triggersEnter', 'triggersExit', 'action', 'subscriptions', 'name']);

this._onRouteCallbacks.forEach((cb) => {
const routePublicApi = _helpers.pick(currentRoute, [
'name',
'pathDef',
'path',
]);
routePublicApi.options = _helpers.omit(currentRoute.options, [
'triggersEnter',
'triggersExit',
'action',
'subscriptions',
'name',
]);

this._onRouteCallbacks.forEach(cb => {
cb(routePublicApi);
});
}


go() {
// client only
}


current() {
// client only
return this._current;
}

middleware() {
// client only
}


getState() {
// client only
}


getAllStates() {
// client only
}

getRouteName() {
return this._current.route ? this._current.route.name : undefined;
}

getQueryParam(key) {
return this._current.query ? this._current.queryParams[key] : undefined;
}

setState() {
// client only
}

setParams(newParams) {}

removeState() {
// client only
}


clearStates() {
// client only
}


ready() {
// client only
}


initialize() {
// client only
}
Expand All @@ -149,7 +208,14 @@ class Router {
// We need to remove the leading base path, or "/", as it will be inserted
// automatically by `Meteor.absoluteUrl` as documented in:
// http://docs.meteor.com/#/full/meteor_absoluteurl
return Meteor.absoluteUrl(this.path.apply(this, arguments).replace(new RegExp('^' + ('/' + (this._basePath || '') + '/').replace(/\/\/+/g, '/')), ''));
return Meteor.absoluteUrl(
this.path
.apply(this, arguments)
.replace(
new RegExp(`^${`/${this._basePath || ''}/`.replace(/\/\/+/g, '/')}`),
''
)
);
}
}

Expand Down