Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@curi/core

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@curi/core - npm Package Compare versions

Comparing version 1.0.0-beta.6 to 1.0.0-beta.7

17

CHANGELOG.md

@@ -0,1 +1,18 @@

## 1.0.0-beta.7
* Rework how replace works. Routes can now have a `redirect` function. This function should return an object with two properties: `to` which is the string/location object to redirect to and `status` (optional) which is the status code number for the redirect type. `redirect` has been removed from the `LoadModifiers` so that redirecting is only done with `route.redirect`. When a `RedirectResponse` is created, Curi will automatically call `history.replace` with the string/location returned by calling `route.redirect`. Subscribers will still be called, so render functions should detect when the response has a `redirectTo` property and respond properly. Redirect responses now support the `body` property, so while a redirect render should be very short, you _can_ render a redirect.
* Rewrite `SideEffect` as an interface with two properties: `fn` which is a subscriber function and `after` which is a boolean. When `after` is `false`, the subscriber function will be run before any functions subscribed with `config.subscribe`. When `after` is `true`, the function will be called after any `config.subscribe` functions.
```js
createConfig(history, routes, {
sideEffects: [
// run before any subscribers
{ fn: PreSideEffect },
// run after any subscribers
{ fn: PostSideEffect, after: true}
]
})
```
* Don't emit response when there is an error generating a response. The error will be logged (using `console.error`). It is up to the user to make sure that their `load`/`preload` functions catch any errors.
* Fix bug where route with no children and path option `end=false` would match non-exact paths.
## 1.0.0-beta.6

@@ -2,0 +19,0 @@

55

dist/curi.common.js

@@ -59,14 +59,14 @@ 'use strict';

var path = function (pathString, options) {
function path(pathString, options) {
var keys = [];
var re = PathToRegexp(pathString, keys, options);
return { re: re, keys: keys };
};
}
var createRoute = function (options) {
var _a = options || {}, name = _a.name, path$$1 = _a.path, _b = _a.pathOptions, pathOptions = _b === void 0 ? {} : _b, body = _a.body, children = _a.children, preload = _a.preload, load = _a.load, title = _a.title, extra = _a.extra;
var _a = options || {}, name = _a.name, path$$1 = _a.path, _b = _a.pathOptions, pathOptions = _b === void 0 ? {} : _b, body = _a.body, children = _a.children, preload = _a.preload, load = _a.load, title = _a.title, extra = _a.extra, redirect = _a.redirect;
// end defaults to true, so end has to be hardcoded for it to be false
var expectedExact = pathOptions.end == null || pathOptions.end;
// when we have child routes, we need to perform non-end matching
if (children) {
if (children.length) {
pathOptions.end = false;

@@ -82,2 +82,3 @@ }

},
redirect: redirect,
children: children,

@@ -245,3 +246,4 @@ preload: preload ? once(preload) : undefined,

data: this.data,
title: this.generateTitle()
title: this.generateTitle(),
body: this.route && this.route.getBody()
};

@@ -251,3 +253,3 @@ if (this.redirectTo != null) {

}
return __assign({}, sharedResponse, { body: this.route && this.route.getBody(), name: this.route ? this.route.name : undefined, partials: this.partials, params: this.params, error: this.error });
return __assign({}, sharedResponse, { name: this.route ? this.route.name : undefined, partials: this.partials, params: this.params, error: this.error });
};

@@ -260,2 +262,12 @@ return ResponseCreator;

var _a = options, _b = _a.addons, addonFactories = _b === void 0 ? [] : _b, _c = _a.sideEffects, sideEffects = _c === void 0 ? [] : _c, cache = _a.cache;
var beforeSideEffects = [];
var afterSideEffects = [];
sideEffects.forEach(function (se) {
if (se.after) {
afterSideEffects.push(se.fn);
}
else {
beforeSideEffects.push(se.fn);
}
});
// add the pathname addon to the provided addons

@@ -293,13 +305,16 @@ var finalAddons = addonFactories.concat(createPathnameAddon);

function loadRoute(rc) {
if (!rc.route) {
var route = rc.route;
if (!route) {
rc.setStatus(404);
return Promise.resolve(rc);
}
var _a = rc.route, preload = _a.preload, load = _a.load;
if (route.redirect) {
var redirectTo = route.redirect(rc.params, rc.location, registeredAddons);
rc.redirect(redirectTo.to, redirectTo.status);
}
// just want to pass a subset of the ResponseCreator's methods
// to the user
var modifiers = load
var modifiers = route.load
? {
fail: rc.fail.bind(rc),
redirect: rc.redirect.bind(rc),
setData: rc.setData.bind(rc),

@@ -310,7 +325,5 @@ setStatus: rc.setStatus.bind(rc)

return Promise.all([
preload ? preload() : null,
load ? load(rc.params, rc.location, modifiers) : null
])
.catch(function (err) { rc.fail(err); })
.then(function () { return rc; });
route.preload ? route.preload() : null,
route.load ? route.load(rc.params, rc.location, modifiers) : null
]).then(function () { return rc; });
}

@@ -362,3 +375,3 @@

}
sideEffects.forEach(function (fn) {
beforeSideEffects.forEach(function (fn) {
fn(response, action);

@@ -371,2 +384,5 @@ });

});
afterSideEffects.forEach(function (fn) {
fn(response, action);
});
return true;

@@ -379,2 +395,6 @@ }

responseInProgress = prepareResponse(location).then(function (response) {
//
if (response.redirectTo) {
history.replace(response.redirectTo);
}
var emitted = emit(response, action);

@@ -386,2 +406,5 @@ // only store these after we have emitted.

return response;
}, function (err) {
console.error(err);
return null;
});

@@ -388,0 +411,0 @@ }

@@ -55,14 +55,14 @@ import PathToRegexp from 'path-to-regexp';

var path = function (pathString, options) {
function path(pathString, options) {
var keys = [];
var re = PathToRegexp(pathString, keys, options);
return { re: re, keys: keys };
};
}
var createRoute = function (options) {
var _a = options || {}, name = _a.name, path$$1 = _a.path, _b = _a.pathOptions, pathOptions = _b === void 0 ? {} : _b, body = _a.body, children = _a.children, preload = _a.preload, load = _a.load, title = _a.title, extra = _a.extra;
var _a = options || {}, name = _a.name, path$$1 = _a.path, _b = _a.pathOptions, pathOptions = _b === void 0 ? {} : _b, body = _a.body, children = _a.children, preload = _a.preload, load = _a.load, title = _a.title, extra = _a.extra, redirect = _a.redirect;
// end defaults to true, so end has to be hardcoded for it to be false
var expectedExact = pathOptions.end == null || pathOptions.end;
// when we have child routes, we need to perform non-end matching
if (children) {
if (children.length) {
pathOptions.end = false;

@@ -78,2 +78,3 @@ }

},
redirect: redirect,
children: children,

@@ -241,3 +242,4 @@ preload: preload ? once(preload) : undefined,

data: this.data,
title: this.generateTitle()
title: this.generateTitle(),
body: this.route && this.route.getBody()
};

@@ -247,3 +249,3 @@ if (this.redirectTo != null) {

}
return __assign({}, sharedResponse, { body: this.route && this.route.getBody(), name: this.route ? this.route.name : undefined, partials: this.partials, params: this.params, error: this.error });
return __assign({}, sharedResponse, { name: this.route ? this.route.name : undefined, partials: this.partials, params: this.params, error: this.error });
};

@@ -256,2 +258,12 @@ return ResponseCreator;

var _a = options, _b = _a.addons, addonFactories = _b === void 0 ? [] : _b, _c = _a.sideEffects, sideEffects = _c === void 0 ? [] : _c, cache = _a.cache;
var beforeSideEffects = [];
var afterSideEffects = [];
sideEffects.forEach(function (se) {
if (se.after) {
afterSideEffects.push(se.fn);
}
else {
beforeSideEffects.push(se.fn);
}
});
// add the pathname addon to the provided addons

@@ -289,13 +301,16 @@ var finalAddons = addonFactories.concat(createPathnameAddon);

function loadRoute(rc) {
if (!rc.route) {
var route = rc.route;
if (!route) {
rc.setStatus(404);
return Promise.resolve(rc);
}
var _a = rc.route, preload = _a.preload, load = _a.load;
if (route.redirect) {
var redirectTo = route.redirect(rc.params, rc.location, registeredAddons);
rc.redirect(redirectTo.to, redirectTo.status);
}
// just want to pass a subset of the ResponseCreator's methods
// to the user
var modifiers = load
var modifiers = route.load
? {
fail: rc.fail.bind(rc),
redirect: rc.redirect.bind(rc),
setData: rc.setData.bind(rc),

@@ -306,7 +321,5 @@ setStatus: rc.setStatus.bind(rc)

return Promise.all([
preload ? preload() : null,
load ? load(rc.params, rc.location, modifiers) : null
])
.catch(function (err) { rc.fail(err); })
.then(function () { return rc; });
route.preload ? route.preload() : null,
route.load ? route.load(rc.params, rc.location, modifiers) : null
]).then(function () { return rc; });
}

@@ -358,3 +371,3 @@

}
sideEffects.forEach(function (fn) {
beforeSideEffects.forEach(function (fn) {
fn(response, action);

@@ -367,2 +380,5 @@ });

});
afterSideEffects.forEach(function (fn) {
fn(response, action);
});
return true;

@@ -375,2 +391,6 @@ }

responseInProgress = prepareResponse(location).then(function (response) {
//
if (response.redirectTo) {
history.replace(response.redirectTo);
}
var emitted = emit(response, action);

@@ -382,2 +402,5 @@ // only store these after we have emitted.

return response;
}, function (err) {
console.error(err);
return null;
});

@@ -384,0 +407,0 @@ }

@@ -432,14 +432,14 @@ var Curi = (function () {

var path = function (pathString, options) {
function path(pathString, options) {
var keys = [];
var re = pathToRegexp_1(pathString, keys, options);
return { re: re, keys: keys };
};
}
var createRoute = function (options) {
var _a = options || {}, name = _a.name, path$$1 = _a.path, _b = _a.pathOptions, pathOptions = _b === void 0 ? {} : _b, body = _a.body, children = _a.children, preload = _a.preload, load = _a.load, title = _a.title, extra = _a.extra;
var _a = options || {}, name = _a.name, path$$1 = _a.path, _b = _a.pathOptions, pathOptions = _b === void 0 ? {} : _b, body = _a.body, children = _a.children, preload = _a.preload, load = _a.load, title = _a.title, extra = _a.extra, redirect = _a.redirect;
// end defaults to true, so end has to be hardcoded for it to be false
var expectedExact = pathOptions.end == null || pathOptions.end;
// when we have child routes, we need to perform non-end matching
if (children) {
if (children.length) {
pathOptions.end = false;

@@ -455,2 +455,3 @@ }

},
redirect: redirect,
children: children,

@@ -618,3 +619,4 @@ preload: preload ? once(preload) : undefined,

data: this.data,
title: this.generateTitle()
title: this.generateTitle(),
body: this.route && this.route.getBody()
};

@@ -624,3 +626,3 @@ if (this.redirectTo != null) {

}
return __assign({}, sharedResponse, { body: this.route && this.route.getBody(), name: this.route ? this.route.name : undefined, partials: this.partials, params: this.params, error: this.error });
return __assign({}, sharedResponse, { name: this.route ? this.route.name : undefined, partials: this.partials, params: this.params, error: this.error });
};

@@ -633,2 +635,12 @@ return ResponseCreator;

var _a = options, _b = _a.addons, addonFactories = _b === void 0 ? [] : _b, _c = _a.sideEffects, sideEffects = _c === void 0 ? [] : _c, cache = _a.cache;
var beforeSideEffects = [];
var afterSideEffects = [];
sideEffects.forEach(function (se) {
if (se.after) {
afterSideEffects.push(se.fn);
}
else {
beforeSideEffects.push(se.fn);
}
});
// add the pathname addon to the provided addons

@@ -666,13 +678,16 @@ var finalAddons = addonFactories.concat(createPathnameAddon);

function loadRoute(rc) {
if (!rc.route) {
var route = rc.route;
if (!route) {
rc.setStatus(404);
return Promise.resolve(rc);
}
var _a = rc.route, preload = _a.preload, load = _a.load;
if (route.redirect) {
var redirectTo = route.redirect(rc.params, rc.location, registeredAddons);
rc.redirect(redirectTo.to, redirectTo.status);
}
// just want to pass a subset of the ResponseCreator's methods
// to the user
var modifiers = load
var modifiers = route.load
? {
fail: rc.fail.bind(rc),
redirect: rc.redirect.bind(rc),
setData: rc.setData.bind(rc),

@@ -683,7 +698,5 @@ setStatus: rc.setStatus.bind(rc)

return Promise.all([
preload ? preload() : null,
load ? load(rc.params, rc.location, modifiers) : null
])
.catch(function (err) { rc.fail(err); })
.then(function () { return rc; });
route.preload ? route.preload() : null,
route.load ? route.load(rc.params, rc.location, modifiers) : null
]).then(function () { return rc; });
}

@@ -735,3 +748,3 @@

}
sideEffects.forEach(function (fn) {
beforeSideEffects.forEach(function (fn) {
fn(response, action);

@@ -744,2 +757,5 @@ });

});
afterSideEffects.forEach(function (fn) {
fn(response, action);
});
return true;

@@ -752,2 +768,6 @@ }

responseInProgress = prepareResponse(location).then(function (response) {
//
if (response.redirectTo) {
history.replace(response.redirectTo);
}
var emitted = emit(response, action);

@@ -759,2 +779,5 @@ // only store these after we have emitted.

return response;
}, function (err) {
console.error(err);
return null;
});

@@ -761,0 +784,0 @@ }

@@ -1,2 +0,2 @@

var Curi=function(){"use strict";function t(t){var e=null,r=!1;return function(){return r?e:(e=t(),r=!0,e)}}function e(t,e){for(var r,o=[],a=0,u=0,s="",c=e&&e.delimiter||"/",h=e&&e.delimiters||"./",f=!1;null!==(r=j.exec(t));){var p=r[0],l=r[1],d=r.index;if(s+=t.slice(u,d),u=d+p.length,l)s+=l[1],f=!0;else{var m="",v=t[u],y=r[2],g=r[3],b=r[4],x=r[5];if(!f&&s.length){var E=s.length-1;h.indexOf(s[E])>-1&&(m=s[E],s=s.slice(0,E))}s&&(o.push(s),s="",f=!1);var w=""!==m&&void 0!==v&&v!==m,T="+"===x||"*"===x,k="?"===x||"*"===x,A=m||c,O=g||b;o.push({name:y||a++,prefix:m,delimiter:A,optional:k,repeat:T,partial:w,pattern:O?i(O):"[^"+n(A)+"]+?"})}}return(s||u<t.length)&&o.push(s+t.substr(u)),o}function r(t){for(var e=new Array(t.length),r=0;r<t.length;r++)"object"==typeof t[r]&&(e[r]=new RegExp("^(?:"+t[r].pattern+")$"));return function(r,n){for(var i="",o=n&&n.encode||encodeURIComponent,a=0;a<t.length;a++){var u=t[a];if("string"!=typeof u){var s,c=r?r[u.name]:void 0;if(Array.isArray(c)){if(!u.repeat)throw new TypeError('Expected "'+u.name+'" to not repeat, but got array');if(0===c.length){if(u.optional)continue;throw new TypeError('Expected "'+u.name+'" to not be empty')}for(var h=0;h<c.length;h++){if(s=o(c[h]),!e[a].test(s))throw new TypeError('Expected all "'+u.name+'" to match "'+u.pattern+'"');i+=(0===h?u.prefix:u.delimiter)+s}}else if("string"!=typeof c&&"number"!=typeof c&&"boolean"!=typeof c){if(!u.optional)throw new TypeError('Expected "'+u.name+'" to be '+(u.repeat?"an array":"a string"));u.partial&&(i+=u.prefix)}else{if(s=o(String(c)),!e[a].test(s))throw new TypeError('Expected "'+u.name+'" to match "'+u.pattern+'", but got "'+s+'"');i+=u.prefix+s}}else i+=u}return i}}function n(t){return t.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function i(t){return t.replace(/([=!:$/()])/g,"\\$1")}function o(t){return t&&t.sensitive?"":"i"}function a(t,e){if(!e)return t;var r=t.source.match(/\((?!\?)/g);if(r)for(var n=0;n<r.length;n++)e.push({name:n,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,pattern:null});return t}function u(t,e,r){for(var n=[],i=0;i<t.length;i++)n.push(h(t[i],e,r).source);return new RegExp("(?:"+n.join("|")+")",o(r))}function s(t,r,n){return c(e(t,n),r,n)}function c(t,e,r){for(var i=(r=r||{}).strict,a=!1!==r.end,u=n(r.delimiter||"/"),s=[].concat(r.endsWith||[]).map(n).concat("$").join("|"),c="",h=0;h<t.length;h++){var f=t[h];if("string"==typeof f)c+=n(f);else{var p=n(f.prefix),l="(?:"+f.pattern+")";e&&e.push(f),f.repeat&&(l+="(?:"+p+l+")*"),c+=l=f.optional?f.partial?p+"("+l+")?":"(?:"+p+"("+l+"))?":p+"("+l+")"}}return i||(c+="(?:"+u+"(?="+s+"))?"),c+=a?"$"===s?s:"(?="+s+")":"(?="+u+"|"+s+")",new RegExp("^"+c,o(r))}function h(t,e,r){return t instanceof RegExp?a(t,e):Array.isArray(t)?u(t,e,r):s(t,e,r)}function f(t,e){var r=p(t);return l(e,r),r}function p(t){return t.map(function(t){var e=t.children?p(t.children):[];return O(v({},t,{children:e}))})}function l(t,e){t.forEach(function(t){d(e,t)})}function d(t,e,r){t.forEach(function(t){var n=e.register(t,r);t.children&&d(t.children,e,n)})}function m(){var t={},e={};return{name:"pathname",register:function(e,r){var n=e.name,i=e.path;void 0!==t[n]&&console.warn('A pathname with the name "'+n+'" already exists. Each route shouldhave a unique name. By registering a pathname with a name that already exists, you are overwriting the existing pathname. This may break your application.');var o;return r&&t[r]&&(o=t[r]),t[n]=o?x(o,i):i,n},get:function(r,n){if(null!=t[r]){var i=e[r]?e[r]:e[r]=E.compile(t[r]);return y(i(n))}console.error("Could not generate pathname for "+r+" because it is not registered.")}}}var v=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++){e=arguments[r];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},y=function(t){return"/"===t.charAt(0)?t:"/"+t},g=function(t){return"/"===t.charAt(0)?t.slice(1):t},b=function(t){return"/"===t.charAt(t.length-1)?t:t+"/"},x=function(t,e){return b(t)+e},E=h,w=e,T=r,k=c,j=new RegExp(["(\\\\.)","(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?"].join("|"),"g");E.parse=w,E.compile=function(t,n){return r(e(t,n))},E.tokensToFunction=T,E.tokensToRegExp=k;var A=function(t,e){var r=[];return{re:E(t,r,e),keys:r}},O=function(e){var r=e||{},n=r.name,i=r.path,o=r.pathOptions,a=void 0===o?{}:o,u=r.body,s=r.children,c=r.preload,h=r.load,f=r.title,p=r.extra,l=null==a.end||a.end;s&&(a.end=!1);var d=A(i,a);return{name:n,path:i,body:u,getBody:function(){return this.body&&this.body()},children:s,preload:c?t(c):void 0,load:h,keys:d.keys.map(function(t){return t.name}),title:f,extra:p,match:function(t,e,r){var n=g(t),i=d.re.exec(n);if(!i)return!1;var o=i[0],a=i.slice(1),u={};d.keys.forEach(function(t,e){u[t.name]=a[e]});var c=null!=r?x(r,o):y(o);if(e.push(this,u),!s||!s.length)return!0;var h=n.slice(o.length),f=!!h.length,p=s.some(function(t){return t.match(h,e,c)});return!(l&&f&&!p)||(e.pop(),!1)}}},R=function(){function t(t,e){this.key=t,this.location=e,this.status=200,this.matches=[],this.route,this.partials=[],this.params={},this.body}return t.prototype.redirect=function(t,e){void 0===e&&(e=301),this.setStatus(e),this.redirectTo=t},t.prototype.fail=function(t){this.error=t},t.prototype.setStatus=function(t){this.status=t},t.prototype.push=function(t,e){this.matches.push({route:t,params:e})},t.prototype.pop=function(){this.matches.pop()},t.prototype.setData=function(t){this.data=t},t.prototype.freeze=function(){var t=this;if(this.matches.length){var e=this.matches.pop();this.matches.forEach(function(e){t.partials.push(e.route.name),Object.assign(t.params,e.params)}),this.route=e.route,Object.assign(this.params,e.params)}},t.prototype.generateTitle=function(){return this.route&&this.route.title?"function"==typeof this.route.title?this.route.title(this.params,this.data):this.route.title:""},t.prototype.asObject=function(){var t={key:this.key,location:this.location,status:this.status,data:this.data,title:this.generateTitle()};return null!=this.redirectTo?v({},t,{redirectTo:this.redirectTo}):v({},t,{body:this.route&&this.route.getBody(),name:this.route?this.route.name:void 0,partials:this.partials,params:this.params,error:this.error})},t}();return function(t,e,r){function n(e){var r=[];for(var n in w)delete w[n];x.forEach(function(t){var e=t();w[e.name]=e.get,r.push(e)}),E=f(e,r),c(t.location,t.action)}function i(e){return E.some(function(r){return r.match(t.location.pathname,e)}),e.freeze(),Promise.resolve(e)}function o(t){if(!t.route)return t.setStatus(404),Promise.resolve(t);var e=t.route,r=e.preload,n=e.load,i=n?{fail:t.fail.bind(t),redirect:t.redirect.bind(t),setData:t.setData.bind(t),setStatus:t.setStatus.bind(t)}:void 0;return Promise.all([r?r():null,n?n(t.params,t.location,i):null]).catch(function(e){t.fail(e)}).then(function(){return t})}function a(t){var e=t.asObject();return b&&b.set(e),e}function u(t){var e=t.key||Math.random().toString(36).slice(2,8);if(h=e,b){var r=b.get(t);if(null!=r)return Promise.resolve(r)}return i(new R(e,t)).then(o).then(a)}function s(t,e){return t.key===h&&(g.forEach(function(r){r(t,e)}),T.forEach(function(r){null!=r&&r(t,e)}),!0)}function c(t,e){p=u(t).then(function(t){return s(t,e)&&(k=[t,e]),t})}void 0===r&&(r={});var h,p,l=r,d=l.addons,v=void 0===d?[]:d,y=l.sideEffects,g=void 0===y?[]:y,b=l.cache,x=v.concat(m),E=[],w={},T=[],k=[];n(e);t.subscribe(c);return{ready:function(){return p},refresh:n,subscribe:function(t){if("function"!=typeof t)throw new Error("The argument passed to subscribe must be a function");t.apply(null,k);var e=T.push(t);return function(){T[e-1]=null}},addons:w,history:t}}}();
var Curi=function(){"use strict";function t(t){var e=null,r=!1;return function(){return r?e:(e=t(),r=!0,e)}}function e(t,e){for(var r,o=[],a=0,u=0,s="",c=e&&e.delimiter||"/",h=e&&e.delimiters||"./",f=!1;null!==(r=A.exec(t));){var l=r[0],p=r[1],d=r.index;if(s+=t.slice(u,d),u=d+l.length,p)s+=p[1],f=!0;else{var m="",v=t[u],y=r[2],g=r[3],b=r[4],E=r[5];if(!f&&s.length){var x=s.length-1;h.indexOf(s[x])>-1&&(m=s[x],s=s.slice(0,x))}s&&(o.push(s),s="",f=!1);var w=""!==m&&void 0!==v&&v!==m,T="+"===E||"*"===E,k="?"===E||"*"===E,j=m||c,O=g||b;o.push({name:y||a++,prefix:m,delimiter:j,optional:k,repeat:T,partial:w,pattern:O?i(O):"[^"+n(j)+"]+?"})}}return(s||u<t.length)&&o.push(s+t.substr(u)),o}function r(t){for(var e=new Array(t.length),r=0;r<t.length;r++)"object"==typeof t[r]&&(e[r]=new RegExp("^(?:"+t[r].pattern+")$"));return function(r,n){for(var i="",o=n&&n.encode||encodeURIComponent,a=0;a<t.length;a++){var u=t[a];if("string"!=typeof u){var s,c=r?r[u.name]:void 0;if(Array.isArray(c)){if(!u.repeat)throw new TypeError('Expected "'+u.name+'" to not repeat, but got array');if(0===c.length){if(u.optional)continue;throw new TypeError('Expected "'+u.name+'" to not be empty')}for(var h=0;h<c.length;h++){if(s=o(c[h]),!e[a].test(s))throw new TypeError('Expected all "'+u.name+'" to match "'+u.pattern+'"');i+=(0===h?u.prefix:u.delimiter)+s}}else if("string"!=typeof c&&"number"!=typeof c&&"boolean"!=typeof c){if(!u.optional)throw new TypeError('Expected "'+u.name+'" to be '+(u.repeat?"an array":"a string"));u.partial&&(i+=u.prefix)}else{if(s=o(String(c)),!e[a].test(s))throw new TypeError('Expected "'+u.name+'" to match "'+u.pattern+'", but got "'+s+'"');i+=u.prefix+s}}else i+=u}return i}}function n(t){return t.replace(/([.+*?=^!:${}()[\]|/\\])/g,"\\$1")}function i(t){return t.replace(/([=!:$/()])/g,"\\$1")}function o(t){return t&&t.sensitive?"":"i"}function a(t,e){if(!e)return t;var r=t.source.match(/\((?!\?)/g);if(r)for(var n=0;n<r.length;n++)e.push({name:n,prefix:null,delimiter:null,optional:!1,repeat:!1,partial:!1,pattern:null});return t}function u(t,e,r){for(var n=[],i=0;i<t.length;i++)n.push(h(t[i],e,r).source);return new RegExp("(?:"+n.join("|")+")",o(r))}function s(t,r,n){return c(e(t,n),r,n)}function c(t,e,r){for(var i=(r=r||{}).strict,a=!1!==r.end,u=n(r.delimiter||"/"),s=[].concat(r.endsWith||[]).map(n).concat("$").join("|"),c="",h=0;h<t.length;h++){var f=t[h];if("string"==typeof f)c+=n(f);else{var l=n(f.prefix),p="(?:"+f.pattern+")";e&&e.push(f),f.repeat&&(p+="(?:"+l+p+")*"),c+=p=f.optional?f.partial?l+"("+p+")?":"(?:"+l+"("+p+"))?":l+"("+p+")"}}return i||(c+="(?:"+u+"(?="+s+"))?"),c+=a?"$"===s?s:"(?="+s+")":"(?="+u+"|"+s+")",new RegExp("^"+c,o(r))}function h(t,e,r){return t instanceof RegExp?a(t,e):Array.isArray(t)?u(t,e,r):s(t,e,r)}function f(t,e){var r=[];return{re:w(t,r,e),keys:r}}function l(t,e){var r=p(t);return d(e,r),r}function p(t){return t.map(function(t){var e=t.children?p(t.children):[];return O(y({},t,{children:e}))})}function d(t,e){t.forEach(function(t){m(e,t)})}function m(t,e,r){t.forEach(function(t){var n=e.register(t,r);t.children&&m(t.children,e,n)})}function v(){var t={},e={};return{name:"pathname",register:function(e,r){var n=e.name,i=e.path;void 0!==t[n]&&console.warn('A pathname with the name "'+n+'" already exists. Each route shouldhave a unique name. By registering a pathname with a name that already exists, you are overwriting the existing pathname. This may break your application.');var o;return r&&t[r]&&(o=t[r]),t[n]=o?x(o,i):i,n},get:function(r,n){if(null!=t[r]){var i=e[r]?e[r]:e[r]=w.compile(t[r]);return g(i(n))}console.error("Could not generate pathname for "+r+" because it is not registered.")}}}var y=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++){e=arguments[r];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},g=function(t){return"/"===t.charAt(0)?t:"/"+t},b=function(t){return"/"===t.charAt(0)?t.slice(1):t},E=function(t){return"/"===t.charAt(t.length-1)?t:t+"/"},x=function(t,e){return E(t)+e},w=h,T=e,k=r,j=c,A=new RegExp(["(\\\\.)","(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?"].join("|"),"g");w.parse=T,w.compile=function(t,n){return r(e(t,n))},w.tokensToFunction=k,w.tokensToRegExp=j;var O=function(e){var r=e||{},n=r.name,i=r.path,o=r.pathOptions,a=void 0===o?{}:o,u=r.body,s=r.children,c=r.preload,h=r.load,l=r.title,p=r.extra,d=r.redirect,m=null==a.end||a.end;s.length&&(a.end=!1);var v=f(i,a);return{name:n,path:i,body:u,getBody:function(){return this.body&&this.body()},redirect:d,children:s,preload:c?t(c):void 0,load:h,keys:v.keys.map(function(t){return t.name}),title:l,extra:p,match:function(t,e,r){var n=b(t),i=v.re.exec(n);if(!i)return!1;var o=i[0],a=i.slice(1),u={};v.keys.forEach(function(t,e){u[t.name]=a[e]});var c=null!=r?x(r,o):g(o);if(e.push(this,u),!s||!s.length)return!0;var h=n.slice(o.length),f=!!h.length,l=s.some(function(t){return t.match(h,e,c)});return!(m&&f&&!l)||(e.pop(),!1)}}},R=function(){function t(t,e){this.key=t,this.location=e,this.status=200,this.matches=[],this.route,this.partials=[],this.params={},this.body}return t.prototype.redirect=function(t,e){void 0===e&&(e=301),this.setStatus(e),this.redirectTo=t},t.prototype.fail=function(t){this.error=t},t.prototype.setStatus=function(t){this.status=t},t.prototype.push=function(t,e){this.matches.push({route:t,params:e})},t.prototype.pop=function(){this.matches.pop()},t.prototype.setData=function(t){this.data=t},t.prototype.freeze=function(){var t=this;if(this.matches.length){var e=this.matches.pop();this.matches.forEach(function(e){t.partials.push(e.route.name),Object.assign(t.params,e.params)}),this.route=e.route,Object.assign(this.params,e.params)}},t.prototype.generateTitle=function(){return this.route&&this.route.title?"function"==typeof this.route.title?this.route.title(this.params,this.data):this.route.title:""},t.prototype.asObject=function(){var t={key:this.key,location:this.location,status:this.status,data:this.data,title:this.generateTitle(),body:this.route&&this.route.getBody()};return null!=this.redirectTo?y({},t,{redirectTo:this.redirectTo}):y({},t,{name:this.route?this.route.name:void 0,partials:this.partials,params:this.params,error:this.error})},t}();return function(t,e,r){function n(e){var r=[];for(var n in k)delete k[n];w.forEach(function(t){var e=t();k[e.name]=e.get,r.push(e)}),T=l(e,r),c(t.location,t.action)}function i(e){return T.some(function(r){return r.match(t.location.pathname,e)}),e.freeze(),Promise.resolve(e)}function o(t){var e=t.route;if(!e)return t.setStatus(404),Promise.resolve(t);if(e.redirect){var r=e.redirect(t.params,t.location,k);t.redirect(r.to,r.status)}var n=e.load?{fail:t.fail.bind(t),setData:t.setData.bind(t),setStatus:t.setStatus.bind(t)}:void 0;return Promise.all([e.preload?e.preload():null,e.load?e.load(t.params,t.location,n):null]).then(function(){return t})}function a(t){var e=t.asObject();return y&&y.set(e),e}function u(t){var e=t.key||Math.random().toString(36).slice(2,8);if(E=e,y){var r=y.get(t);if(null!=r)return Promise.resolve(r)}return i(new R(e,t)).then(o).then(a)}function s(t,e){return t.key===E&&(g.forEach(function(r){r(t,e)}),j.forEach(function(r){null!=r&&r(t,e)}),b.forEach(function(r){r(t,e)}),!0)}function c(e,r){x=u(e).then(function(e){return e.redirectTo&&t.replace(e.redirectTo),s(e,r)&&(A=[e,r]),e},function(t){return console.error(t),null})}void 0===r&&(r={});var h=r,f=h.addons,p=void 0===f?[]:f,d=h.sideEffects,m=void 0===d?[]:d,y=h.cache,g=[],b=[];m.forEach(function(t){t.after?b.push(t.fn):g.push(t.fn)});var E,x,w=p.concat(v),T=[],k={},j=[],A=[];n(e);t.subscribe(c);return{ready:function(){return x},refresh:n,subscribe:function(t){if("function"!=typeof t)throw new Error("The argument passed to subscribe must be a function");t.apply(null,A);var e=j.push(t);return function(){j[e-1]=null}},addons:k,history:t}}}();
//# sourceMappingURL=curi.min.js.map
{
"name": "@curi/core",
"version": "1.0.0-beta.6",
"version": "1.0.0-beta.7",
"description": "A JavaScript router you can use with anywhere",

@@ -5,0 +5,0 @@ "main": "dist/curi.common.js",

@@ -13,4 +13,7 @@ import { HickoryLocation } from '@hickory/root';

export declare type Subscriber = (response: AnyResponse, action?: string) => void;
export interface SideEffect {
fn: Subscriber;
after?: boolean;
}
export declare type UnsubscribeFn = () => void;
export declare type SideEffect = (response: AnyResponse, action?: string) => void;
export interface Cache {

@@ -20,1 +23,4 @@ set: (response: AnyResponse) => void;

}
export declare type Params = {
[key: string]: string;
};

@@ -1,2 +0,2 @@

import PathToRegexp, { Key } from 'path-to-regexp';
import { RegExpOptions, Key } from 'path-to-regexp';
export interface Path {

@@ -6,3 +6,2 @@ re: RegExp;

}
declare const path: (pathString: string, options?: PathToRegexp.RegExpOptions) => Path;
export default path;
export default function path(pathString: string, options?: RegExpOptions): Path;
import { HickoryLocation } from '@hickory/root';
import { Route } from './createRoute';
import { Params } from '../interface';
export interface BaseResponse {

@@ -9,5 +10,5 @@ key: string;

title: string;
body: any;
}
export interface Response extends BaseResponse {
body: any;
name: string;

@@ -22,5 +23,2 @@ partials: Array<string>;

export declare type AnyResponse = Response | RedirectResponse;
export declare type Params = {
[key: string]: string;
};
export interface Match {

@@ -27,0 +25,0 @@ route: Route;

@@ -1,5 +0,13 @@

import { HickoryLocation } from '@hickory/root';
import { HickoryLocation, ToArgument } from '@hickory/root';
import { RegExpOptions } from 'path-to-regexp';
import ResponseCreator from './createResponse';
import { Params, AddonGet } from '../interface';
export declare type Title = string | ((params?: object, data?: any) => string);
export interface Redirect {
to: ToArgument;
status?: number;
}
export declare type RedirectFn = (params: Params, location: HickoryLocation, addons: {
[key: string]: AddonGet;
}) => Redirect;
export interface RouteDescriptor {

@@ -11,2 +19,3 @@ name: string;

children?: Array<RouteDescriptor>;
redirect?: RedirectFn;
preload?: () => Promise<any>;

@@ -28,2 +37,3 @@ load?: (params?: object, location?: HickoryLocation, modifiers?: LoadModifiers) => Promise<any>;

children: Array<Route>;
redirect: RedirectFn;
preload: () => Promise<any>;

@@ -40,3 +50,2 @@ load: (params?: object, location?: HickoryLocation, modifiers?: LoadModifiers) => Promise<any>;

fail: (err: any) => void;
redirect: (to: any, status?: number) => void;
setData: (data: any) => void;

@@ -43,0 +52,0 @@ setStatus: (status: number) => void;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc