本文整理汇总了TypeScript中path-to-regexp.default方法的典型用法代码示例。如果您正苦于以下问题:TypeScript path-to-regexp.default方法的具体用法?TypeScript path-to-regexp.default怎么用?TypeScript path-to-regexp.default使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类path-to-regexp
的用法示例。
在下文中一共展示了path-to-regexp.default方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: route
route(path: string, handler: RouteHandler) {
if (path === '*') {
this.defaultHandler = handler;
return;
}
const keys = [];
const regexp = pathToRegexp(path, keys);
this.routes.push({
regexp,
keys,
handler,
});
}
示例2: function
let fty:Route = (path, action:(ctx, args)=> Promise<any> ,opts?) : AppMiddleware => {
const re = pathToRegexp(path, opts);
debug('%s %s -> %s', method || 'ALL', path, re);
return async function (ctx, next:(x?) => Promise<any> ) {
// match method
if (!matches(ctx, method)) {
next();
return
};
// match path
const m = re.exec(ctx.path);
if (m) {
//collect route segments
const args = m.slice(1).map(decode);
debug('%s %s matches %s %j', ctx.method, path, ctx.path, args);
return await action(ctx, args)
}
// miss
return next(true);
}
}
示例3: getRegexp
export function getRegexp(pattern: string) {
if (!REGEXP_CACHE.has(pattern)) {
const keys = [];
const regexp = pathToRegexp(pattern, keys, { end: false });
REGEXP_CACHE.set(pattern, { keys, regexp });
}
return REGEXP_CACHE.get(pattern);
}
示例4:
/*
Add an endpoint.
@param {string} method - Endpoint method
@param {string} url - Endopint url
@param {Array.<string>} stores - Stores used by endpoint
@param {function} endopint - Function for computing the HTTP response
*/
add_endpoint(method : string, url: string, stores: Array<string>, endpoint: DbEndpointGen){
// Add to list of endpoints; compile path
this.endpoints.push({
url: path2regexp(url),
stores: stores,
method: method,
endpoint: endpoint
});
}
示例5:
/*
Setup database.
@param {ng.ItthpBackendService} $httpBackend - Angular mocks $httpBackend service
*/
setup($httpBackend : ng.IHttpBackendService) : void{
const api_match = path2regexp(this.apiUrl + "*")
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'JSONP', 'PATCH'];
methods.forEach(method => {$httpBackend['when' + method](api_match).respond(this.manager.mock_request.bind(this.manager))});
methods.forEach(method => {$httpBackend['when' + method](/.*/).passThrough()});
}
示例6: async
get(path: string, action: (...args: any[]) => Promise<any>): (ctx, next) => Promise<any> {
let re = pathToRegexp(path);
return async (ctx: Koa.Context, next): Promise<any> => {
let method = ctx.request.method;
let url = ctx.request.url;
let ok = re.test(url);
debug('%s, %s, %s, %s -> %s', method, path, url, ok, re);
if (method == 'GET' && ok) {
action.apply(ctx, []);
return next();
}
next();
}
}
示例7: next
return (path: string, callback: Function) => {
const re = pathToRegexp(path);
const normalizedPath = path === '/*' ? '' : path;
this.instance.use(normalizedPath, (req, res, next) => {
if (!re.exec(req.originalUrl + '/')) {
return next();
}
if (
requestMethod === RequestMethod.ALL ||
req.method === RequestMethod[requestMethod]
) {
return callback(req, res, next);
}
next();
});
};
示例8: pathToRegexp
const compile = (route: Route): CompiledRoute => {
const keys: any[] = [];
const regexp = pathToRegexp(route.path, keys);
return { keys, regexp, route };
};
示例9: TypeError
}
let routing = {
set(path: string, handler: Handler) {
if (typeof path !== "string") {
throw new TypeError("expecting path to be a string");
}
if (path === "/" || path === "") {
// home
homeHandler = handler;
return;
}
let keys: any[] = [];
let re = pRegex(path, keys);
// append to object list
objectList[objectList.length] = routeObject(re, keys, handler);
},
get(path: string) {
if (typeof path !== "string") {
throw new TypeError("expecting path to be a string");
}
if (path === "/" || path === "") {
if (homeHandler) {
return {
handler: homeHandler
};
}
示例10: decodeURI
ProuterPathExp,
ProuterRequestProcessor,
ProuterRequest,
ProuterParsedHandler,
ProuterPathKey
} from './entity';
export const routerHelper = {
getPath() {
return decodeURI(location.pathname + location.search);
},
stringToRegexp(str: string) {
const keys: ProuterPathKey[] = [];
const resp = pathToRegexp(str, keys) as ProuterPathExp;
resp.keys = keys;
return resp;
},
parseQuery(str: string) {
const searchObj: { [key: string]: string } = {};
if (str === '') {
return searchObj;
}
const qs = str.slice(1);
const params = qs.split('&');