@croz/nrich-notification-core
Version:
Contains core utilities related to the nrich-notification module
87 lines (82 loc) • 3.1 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
// src/api/notification-type-guards.ts
var isNotificationResponse = (body) => body && "notification" in body;
// src/store/notification-store.ts
import { create } from "zustand";
var store = create((set) => ({
notifications: [],
add: (notification) => set((state) => ({
notifications: [...state.notifications, __spreadProps(__spreadValues({}, notification), { timestamp: new Date(notification.timestamp) || new Date() })]
})),
remove: (notification) => set((state) => ({
notifications: state.notifications.filter((currentNotification) => currentNotification !== notification)
}))
}));
var useNotificationStore = store;
var addNotification = store.getState().add;
var removeNotification = store.getState().remove;
// src/interceptor/fetch-notification-interceptor.ts
var fetchNotificationInterceptor = () => {
window.fetch = new Proxy(window.fetch, {
apply(fetch, that, request) {
const result = fetch.apply(that, request);
result.then((response) => response.clone().json()).then((body) => {
if (isNotificationResponse(body)) {
useNotificationStore.getState().add(body.notification);
}
});
return result;
}
});
};
// src/interceptor/xhr-notification-interceptor.ts
var xhrNotificationInterceptor = () => {
const old = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(...args) {
this.addEventListener("readystatechange", function() {
if (this.readyState === 4) {
try {
const body = JSON.parse(this.responseText);
if (isNotificationResponse(body)) {
useNotificationStore.getState().add(body.notification);
}
} catch (e) {
}
}
}, false);
old.apply(this, args);
};
};
// src/hook/use-notifications.ts
var useNotifications = () => {
const notifications = useNotificationStore((state) => state.notifications);
const add = useNotificationStore((state) => state.add);
const remove = useNotificationStore((state) => state.remove);
return { notifications, add, remove };
};
export {
addNotification,
fetchNotificationInterceptor,
removeNotification,
useNotifications,
xhrNotificationInterceptor
};
//# sourceMappingURL=index.mjs.map