Multifunctional JavaScript router solves the problem of routing your application, providing full control over the route.
const router = Pilot.create({
'#route-id': {
url: '/:type(/:detail)?', // route pattern
model: {
user: (req) => fetch(`/api/${req.params.type}`).then(r => r.json()),
},
onroute(/**Pilot.Request*/req) {
console.log(this.model.user);
}
}
});
// Запускаем перехват ссылок и history api
router.listenFrom(document, {autoStart: true});
// Где-то в коде
router.go('#route-id').then(() => ...);
router.getUrl('#route-id', {type: 'user'}); // '/user';
router.route.getUrl({type: 'user'}); // '/user';
router.route.getUrl({type: 'user', detail: 123}); // '/user/123';
Object): Pilotstring[, base: string]]) — see Native URL andstring)string|RegExp)#propertiesstringstringstringstringstringstringstringstringstringstringstring or pathnamestringstringstringobjectobjectstring#methodsobject|string|null)string[])object|string|null[, remove: string[])string): objectobject): stringPilot lifecyclePilot.RequestPilot.RequestPilot.RoutePilot.RequestPilot.RouteErrorPilot.RequestPilot.RoutePilot methods and propertiesObjectList of all models
Pilot.RequestCurrent Request.
URLActive/Current URL.
Pilot.RouteCurrent route.
stringstring — route idobject — route parametrs (optional)object|inherit — route GET-query parametrs (optional)Promisestring — route idobject — route parameters (optional)object|inherit — route GET-query parameters (optional)object - route navigation details (options)Promisestringobject - route navigation details (options)PromiseEmits beforereload and reload events. if a handler to beforereload returns false, does not
perform actual reload and returns a resolved promise instead.
Pilot.Route methods and propertiesObjectLocal models (inherit global models).
Protected method.
stringObject (optional)object|inherit — route GET-query parametrs (optional)booleanid:string — route id or space-separated list
Pilot.Loaderconst modelLoader = new Pilot.Loader({
user: ({params:{id}}) => fetch(`/api/user/${id}`).then(r => r.json()),
status: () => fetch(`/api/status`).then(r => r.json()),
}, {
// неважно сколько раз вызвать `fetch`,
// если уже есть запрос на сервер, новый не последует
persist: true,
// Обработку данных загруженной модели
processingModel(modelName, modelData, req, models) {
return {...modelData, pathed: true}; // or Promise
},
// Обработка ошибки при загрузки модели
processingModelError(modelName, error, req, models) {
return Promise.resolve({defaultData: 123}); // или undefined для reject
},
// Финальная обработка полученных данных
processing(req, models) {
return {...models, patched: true};
},
});
// Используем `modelLoader`
const router = Pilot.create({
model: modelLoader,
});
// Где-то в коде
modelLoader.fetch().then(model => {
console.log(model.user);
console.log(model.status);
});
$ claude mcp add Pilot \
-- python -m otcore.mcp_server <graph>