@croz/nrich-notification-core
Version:
Contains core utilities related to the nrich-notification module
114 lines (108 loc) • 4.27 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
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));
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
addNotification: () => addNotification,
fetchNotificationInterceptor: () => fetchNotificationInterceptor,
removeNotification: () => removeNotification,
useNotifications: () => useNotifications,
xhrNotificationInterceptor: () => xhrNotificationInterceptor
});
module.exports = __toCommonJS(src_exports);
// src/api/notification-type-guards.ts
var isNotificationResponse = (body) => body && "notification" in body;
// src/store/notification-store.ts
var import_zustand = require("zustand");
var store = (0, import_zustand.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 };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
addNotification,
fetchNotificationInterceptor,
removeNotification,
useNotifications,
xhrNotificationInterceptor
});
//# sourceMappingURL=index.js.map