- App.tsx: full navigation (Auth stack + Main tabs with 5 screens) - Auth: LoginScreen, RegisterScreen, ForgotPasswordScreen - HomeScreen: dashboard with IoT metrics, weather widget, alerts, quick actions, sensors - MapScreen: interactive map with layer toggles (6 layers) - MarketplaceScreen: categories (6), products (5), search - ChatScreen: AI chat with quick prompts (4), bot responses - ProfileScreen: user info, stats, menu (9 items), logout - AlertsScreen: alert list with severity, acknowledge - SensorsScreen: sensor list with type filters (6 types), search - ZonesScreen: zone cards with stats - SettingsScreen: language picker (FR/EN/ES/DE), privacy, about - Stores: iotStore (sensors, zones, alerts), notificationStore, uiStore + i18n - Hooks: useSensors, useAlerts, useNotifications, useLocation - Components: Card, Button, LoadingSpinner, ErrorBoundary, Header - Services: iotService, notificationService (with axios API client) - Utils: formatters (temp, AQI, noise, dates), validators (email, password, IBAN) - Theme: colors.ts with full design system (Blue Ocean palette) - Ditto: fixed MongoDB connection, new JWT secrets, official gateway image
89 lines
3.1 KiB
JavaScript
89 lines
3.1 KiB
JavaScript
import { argsert } from './argsert.js';
|
|
import { isPromise } from './utils/is-promise.js';
|
|
export class GlobalMiddleware {
|
|
constructor(yargs) {
|
|
this.globalMiddleware = [];
|
|
this.frozens = [];
|
|
this.yargs = yargs;
|
|
}
|
|
addMiddleware(callback, applyBeforeValidation, global = true, mutates = false) {
|
|
argsert('<array|function> [boolean] [boolean] [boolean]', [callback, applyBeforeValidation, global], arguments.length);
|
|
if (Array.isArray(callback)) {
|
|
for (let i = 0; i < callback.length; i++) {
|
|
if (typeof callback[i] !== 'function') {
|
|
throw Error('middleware must be a function');
|
|
}
|
|
const m = callback[i];
|
|
m.applyBeforeValidation = applyBeforeValidation;
|
|
m.global = global;
|
|
}
|
|
Array.prototype.push.apply(this.globalMiddleware, callback);
|
|
}
|
|
else if (typeof callback === 'function') {
|
|
const m = callback;
|
|
m.applyBeforeValidation = applyBeforeValidation;
|
|
m.global = global;
|
|
m.mutates = mutates;
|
|
this.globalMiddleware.push(callback);
|
|
}
|
|
return this.yargs;
|
|
}
|
|
addCoerceMiddleware(callback, option) {
|
|
const aliases = this.yargs.getAliases();
|
|
this.globalMiddleware = this.globalMiddleware.filter(m => {
|
|
const toCheck = [...(aliases[option] || []), option];
|
|
if (!m.option)
|
|
return true;
|
|
else
|
|
return !toCheck.includes(m.option);
|
|
});
|
|
callback.option = option;
|
|
return this.addMiddleware(callback, true, true, true);
|
|
}
|
|
getMiddleware() {
|
|
return this.globalMiddleware;
|
|
}
|
|
freeze() {
|
|
this.frozens.push([...this.globalMiddleware]);
|
|
}
|
|
unfreeze() {
|
|
const frozen = this.frozens.pop();
|
|
if (frozen !== undefined)
|
|
this.globalMiddleware = frozen;
|
|
}
|
|
reset() {
|
|
this.globalMiddleware = this.globalMiddleware.filter(m => m.global);
|
|
}
|
|
}
|
|
export function commandMiddlewareFactory(commandMiddleware) {
|
|
if (!commandMiddleware)
|
|
return [];
|
|
return commandMiddleware.map(middleware => {
|
|
middleware.applyBeforeValidation = false;
|
|
return middleware;
|
|
});
|
|
}
|
|
export function applyMiddleware(argv, yargs, middlewares, beforeValidation) {
|
|
return middlewares.reduce((acc, middleware) => {
|
|
if (middleware.applyBeforeValidation !== beforeValidation) {
|
|
return acc;
|
|
}
|
|
if (middleware.mutates) {
|
|
if (middleware.applied)
|
|
return acc;
|
|
middleware.applied = true;
|
|
}
|
|
if (isPromise(acc)) {
|
|
return acc
|
|
.then(initialObj => Promise.all([initialObj, middleware(initialObj, yargs)]))
|
|
.then(([initialObj, middlewareObj]) => Object.assign(initialObj, middlewareObj));
|
|
}
|
|
else {
|
|
const result = middleware(acc, yargs);
|
|
return isPromise(result)
|
|
? result.then(middlewareObj => Object.assign(acc, middlewareObj))
|
|
: Object.assign(acc, result);
|
|
}
|
|
}, argv);
|
|
}
|