m2m-components 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,8 @@ var _m2mNotifications = require("../endpoints/m2m-notifications.v1");
13
13
 
14
14
  var _ServiceIdProvider = require("./ServiceIdProvider");
15
15
 
16
+ var _jsxRuntime = require("react/jsx-runtime");
17
+
16
18
  const NotificationPermissionContext = /*#__PURE__*/(0, _react.createContext)(undefined);
17
19
  const NotificationsContext = /*#__PURE__*/(0, _react.createContext)(undefined); // ServiceIdProviderの中で使うこと
18
20
 
@@ -61,11 +63,13 @@ const NotificationsProvider = ({
61
63
  refetch: refetchNotifications
62
64
  };
63
65
  }, [refetchNotifications, notifications?.data]);
64
- return /*#__PURE__*/React.createElement(NotificationPermissionContext.Provider, {
65
- value: permissionContextState
66
- }, /*#__PURE__*/React.createElement(NotificationsContext.Provider, {
67
- value: notificationsContextState
68
- }, children));
66
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(NotificationPermissionContext.Provider, {
67
+ value: permissionContextState,
68
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(NotificationsContext.Provider, {
69
+ value: notificationsContextState,
70
+ children: children
71
+ })
72
+ });
69
73
  };
70
74
 
71
75
  exports.NotificationsProvider = NotificationsProvider;
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationProvider.js","names":["NotificationPermissionContext","createContext","undefined","NotificationsContext","NotificationsProvider","token","children","serviceId","useServiceId","data","subscriptions","refetch","refetchSubscriptions","useAuthFetch","m2mNotifications_v1","findMySubscriptions","checkPermission","useCallback","notificationType","subscription","find","item","resourceSubscription","resourceSelectType","status","notifications","error","refetchNotifications","findNotificationsByServiceId","id","swrConfig","refreshInterval","console","permissionContextState","useMemo","notificationsContextState","isAllRead","every","n","useNotifications","ctx","useContext","Error","useNotificationPermission"],"sources":["../../src/notification/NotificationProvider.tsx"],"sourcesContent":["import {\n createContext,\n PropsWithChildren,\n useCallback,\n useContext,\n} from \"react\";\nimport { useAuthFetch } from \"matsuri-hooks\";\nimport { useMemo } from \"react\";\nimport { m2mNotifications_v1 } from \"../endpoints/m2m-notifications.v1\";\nimport { useServiceId } from \"./ServiceIdProvider\";\nimport { SubscriptionStatus, Subscription, Notification } from \"./domain\";\n\nconst NotificationPermissionContext = createContext<\n | {\n checkPermission: (\n notificationType: string\n ) => SubscriptionStatus | undefined;\n refetch: () => void;\n }\n | undefined\n>(undefined);\n\nconst NotificationsContext = createContext<\n | { notifications: Notification[]; isAllRead: boolean; refetch: () => void }\n | undefined\n>(undefined);\n\n// ServiceIdProviderの中で使うこと\nexport const NotificationsProvider = ({\n token,\n children,\n}: PropsWithChildren<{ token: string }>) => {\n const serviceId = useServiceId();\n\n const { data: subscriptions, refetch: refetchSubscriptions } = useAuthFetch<\n Subscription[]\n >(token, m2mNotifications_v1.findMySubscriptions(), {});\n const checkPermission = useCallback(\n (notificationType: string) => {\n const subscription = subscriptions?.find((item) => {\n return (\n item.serviceId === serviceId &&\n item.notificationType === notificationType &&\n item.resourceSubscription.resourceSelectType === \"any\"\n );\n });\n\n return subscription?.status;\n },\n [serviceId, subscriptions]\n );\n\n const {\n data: notifications,\n error,\n refetch: refetchNotifications,\n } = useAuthFetch<{ data: Notification[] }>(\n token,\n m2mNotifications_v1.findNotificationsByServiceId({ id: serviceId }),\n { swrConfig: { refreshInterval: 1000 * 60 * 5 } }\n );\n if (error) {\n // エラーが出てもユーザーは特に困らないので、ユーザーに伝える必要はないが、監視は必要。\n console.error(error);\n }\n\n const permissionContextState = useMemo(() => {\n return {\n checkPermission,\n refetch: refetchSubscriptions,\n };\n }, [checkPermission, refetchSubscriptions]);\n\n const notificationsContextState = useMemo(() => {\n return {\n notifications: notifications?.data ?? [],\n isAllRead: notifications?.data.every((n) => n.status === \"read\") ?? true,\n refetch: refetchNotifications,\n };\n }, [refetchNotifications, notifications?.data]);\n\n return (\n <NotificationPermissionContext.Provider value={permissionContextState}>\n <NotificationsContext.Provider value={notificationsContextState}>\n {children}\n </NotificationsContext.Provider>\n </NotificationPermissionContext.Provider>\n );\n};\n\nexport const useNotifications = () => {\n const ctx = useContext(NotificationsContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n\nexport const useNotificationPermission = () => {\n const ctx = useContext(NotificationPermissionContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n"],"mappings":";;;;;;;AAAA;;AAMA;;AAEA;;AACA;;AAGA,MAAMA,6BAA6B,gBAAG,IAAAC,oBAAA,EAQpCC,SARoC,CAAtC;AAUA,MAAMC,oBAAoB,gBAAG,IAAAF,oBAAA,EAG3BC,SAH2B,CAA7B,C,CAKA;;AACO,MAAME,qBAAqB,GAAG,CAAC;EACpCC,KADoC;EAEpCC;AAFoC,CAAD,KAGO;EAC1C,MAAMC,SAAS,GAAG,IAAAC,+BAAA,GAAlB;EAEA,MAAM;IAAEC,IAAI,EAAEC,aAAR;IAAuBC,OAAO,EAAEC;EAAhC,IAAyD,IAAAC,0BAAA,EAE7DR,KAF6D,EAEtDS,qCAAA,CAAoBC,mBAApB,EAFsD,EAEX,EAFW,CAA/D;EAGA,MAAMC,eAAe,GAAG,IAAAC,kBAAA,EACrBC,gBAAD,IAA8B;IAC5B,MAAMC,YAAY,GAAGT,aAAa,EAAEU,IAAf,CAAqBC,IAAD,IAAU;MACjD,OACEA,IAAI,CAACd,SAAL,KAAmBA,SAAnB,IACAc,IAAI,CAACH,gBAAL,KAA0BA,gBAD1B,IAEAG,IAAI,CAACC,oBAAL,CAA0BC,kBAA1B,KAAiD,KAHnD;IAKD,CANoB,CAArB;IAQA,OAAOJ,YAAY,EAAEK,MAArB;EACD,CAXqB,EAYtB,CAACjB,SAAD,EAAYG,aAAZ,CAZsB,CAAxB;EAeA,MAAM;IACJD,IAAI,EAAEgB,aADF;IAEJC,KAFI;IAGJf,OAAO,EAAEgB;EAHL,IAIF,IAAAd,0BA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,IAAAC,iBAAA,EAAWzC,6BAAX,CAAZ;;EACA,IAAI,CAACwC,GAAL,EAAU;IACR,MAAM,IAAIE,KAAJ,CAAU,mCAAV,CAAN;EACD;;EAED,OAAOF,GAAP;AACD,CAPM"}
1
+ {"version":3,"file":"NotificationProvider.js","names":["NotificationPermissionContext","createContext","undefined","NotificationsContext","NotificationsProvider","token","children","serviceId","useServiceId","data","subscriptions","refetch","refetchSubscriptions","useAuthFetch","m2mNotifications_v1","findMySubscriptions","checkPermission","useCallback","notificationType","subscription","find","item","resourceSubscription","resourceSelectType","status","notifications","error","refetchNotifications","findNotificationsByServiceId","id","swrConfig","refreshInterval","console","permissionContextState","useMemo","notificationsContextState","isAllRead","every","n","useNotifications","ctx","useContext","Error","useNotificationPermission"],"sources":["../../src/notification/NotificationProvider.tsx"],"sourcesContent":["import {\n createContext,\n PropsWithChildren,\n useCallback,\n useContext,\n} from \"react\";\nimport { useAuthFetch } from \"matsuri-hooks\";\nimport { useMemo } from \"react\";\nimport { m2mNotifications_v1 } from \"../endpoints/m2m-notifications.v1\";\nimport { useServiceId } from \"./ServiceIdProvider\";\nimport { SubscriptionStatus, Subscription, Notification } from \"./domain\";\n\nconst NotificationPermissionContext = createContext<\n | {\n checkPermission: (\n notificationType: string\n ) => SubscriptionStatus | undefined;\n refetch: () => void;\n }\n | undefined\n>(undefined);\n\nconst NotificationsContext = createContext<\n | { notifications: Notification[]; isAllRead: boolean; refetch: () => void }\n | undefined\n>(undefined);\n\n// ServiceIdProviderの中で使うこと\nexport const NotificationsProvider = ({\n token,\n children,\n}: PropsWithChildren<{ token: string }>) => {\n const serviceId = useServiceId();\n\n const { data: subscriptions, refetch: refetchSubscriptions } = useAuthFetch<\n Subscription[]\n >(token, m2mNotifications_v1.findMySubscriptions(), {});\n const checkPermission = useCallback(\n (notificationType: string) => {\n const subscription = subscriptions?.find((item) => {\n return (\n item.serviceId === serviceId &&\n item.notificationType === notificationType &&\n item.resourceSubscription.resourceSelectType === \"any\"\n );\n });\n\n return subscription?.status;\n },\n [serviceId, subscriptions]\n );\n\n const {\n data: notifications,\n error,\n refetch: refetchNotifications,\n } = useAuthFetch<{ data: Notification[] }>(\n token,\n m2mNotifications_v1.findNotificationsByServiceId({ id: serviceId }),\n { swrConfig: { refreshInterval: 1000 * 60 * 5 } }\n );\n if (error) {\n // エラーが出てもユーザーは特に困らないので、ユーザーに伝える必要はないが、監視は必要。\n console.error(error);\n }\n\n const permissionContextState = useMemo(() => {\n return {\n checkPermission,\n refetch: refetchSubscriptions,\n };\n }, [checkPermission, refetchSubscriptions]);\n\n const notificationsContextState = useMemo(() => {\n return {\n notifications: notifications?.data ?? [],\n isAllRead: notifications?.data.every((n) => n.status === \"read\") ?? true,\n refetch: refetchNotifications,\n };\n }, [refetchNotifications, notifications?.data]);\n\n return (\n <NotificationPermissionContext.Provider value={permissionContextState}>\n <NotificationsContext.Provider value={notificationsContextState}>\n {children}\n </NotificationsContext.Provider>\n </NotificationPermissionContext.Provider>\n );\n};\n\nexport const useNotifications = () => {\n const ctx = useContext(NotificationsContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n\nexport const useNotificationPermission = () => {\n const ctx = useContext(NotificationPermissionContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n"],"mappings":";;;;;;;AAAA;;AAMA;;AAEA;;AACA;;;;AAGA,MAAMA,6BAA6B,gBAAG,IAAAC,oBAAA,EAQpCC,SARoC,CAAtC;AAUA,MAAMC,oBAAoB,gBAAG,IAAAF,oBAAA,EAG3BC,SAH2B,CAA7B,C,CAKA;;AACO,MAAME,qBAAqB,GAAG,CAAC;EACpCC,KADoC;EAEpCC;AAFoC,CAAD,KAGO;EAC1C,MAAMC,SAAS,GAAG,IAAAC,+BAAA,GAAlB;EAEA,MAAM;IAAEC,IAAI,EAAEC,aAAR;IAAuBC,OAAO,EAAEC;EAAhC,IAAyD,IAAAC,0BAAA,EAE7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GLb,OAAO,EAAEgB;IAHJ,CAAP;EAKD,CANiC,EAM/B,CAACA,oBAAD,EAAuBF,aAAa,EAAEhB,IAAtC,CAN+B,CAAlC;EAQA,oBACE,qBAAC,6BAAD,CAA+B,QAA/B;IAAwC,KAAK,EAAEwB,sBAA/C;IAAA,uBACE,qBAAC,oBAAD,CAAsB,QAAtB;MAA+B,KAAK,EAAEE,yBAAtC;MAAA,UACG7B;IADH;EADF,EADF;AAOD,CA5DM;;;;AA8DA,MAAMiC,gBAAgB,GAAG,MAAM;EACpC,MAAMC,GAAG,GAAG,IAAAC,iBAAA,EAAWtC,oBAAX,CAAZ;;EACA,IAAI,CAACqC,GAAL,EAAU;IACR,MAAM,IAAIE,KAAJ,CAAU,mCAAV,CAAN;EACD;;EAED,OAAOF,GAAP;AACD,CAPM;;;;AASA,MAAMG,yBAAyB,GAAG,MAAM;EAC7C,MAAMH,GAAG,GAAG,IAAAC,iBAAA,EAAWzC,6BAAX,CAAZ;;EACA,IAAI,CAACwC,GAAL,EAAU;IACR,MAAM,IAAIE,KAAJ,CAAU,mCAAV,CAAN;EACD;;EAED,OAAOF,GAAP;AACD,CAPM"}
@@ -7,6 +7,8 @@ exports.useServiceId = exports.ServiceIdProvider = void 0;
7
7
 
8
8
  var _react = _interopRequireWildcard(require("react"));
9
9
 
10
+ var _jsxRuntime = require("react/jsx-runtime");
11
+
10
12
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
13
 
12
14
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -19,10 +21,11 @@ const ServiceIdProvider = ({
19
21
  }) => {
20
22
  const state = (0, _react.useMemo)(() => ({
21
23
  serviceId
22
- }), []);
23
- return /*#__PURE__*/_react.default.createElement(ServiceIdContext.Provider, {
24
- value: state
25
- }, children);
24
+ }), [serviceId]);
25
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(ServiceIdContext.Provider, {
26
+ value: state,
27
+ children: children
28
+ });
26
29
  };
27
30
 
28
31
  exports.ServiceIdProvider = ServiceIdProvider;
@@ -1 +1 @@
1
- {"version":3,"file":"ServiceIdProvider.js","names":["ServiceIdContext","createContext","undefined","ServiceIdProvider","serviceId","children","state","useMemo","useServiceId","context","React","useContext","Error"],"sources":["../../src/notification/ServiceIdProvider.tsx"],"sourcesContent":["import React, { createContext, useMemo } from \"react\";\n\ninterface ServiceIdContextState {\n serviceId: string;\n}\n\nconst ServiceIdContext = createContext<ServiceIdContextState | undefined>(\n undefined\n);\n\nexport const ServiceIdProvider = ({\n serviceId,\n children,\n}: {\n serviceId: string;\n children: React.ReactNode;\n}) => {\n const state = useMemo(() => ({ serviceId }), []);\n\n return (\n <ServiceIdContext.Provider value={state}>\n {children}\n </ServiceIdContext.Provider>\n );\n};\n\nexport const useServiceId = () => {\n const context = React.useContext(ServiceIdContext);\n if (context === undefined) {\n throw new Error(\"useServiceId must be used within a ServiceIdProvider\");\n }\n return context.serviceId;\n};\n"],"mappings":";;;;;;;AAAA;;;;;;AAMA,MAAMA,gBAAgB,gBAAG,IAAAC,oBAAA,EACvBC,SADuB,CAAzB;;AAIO,MAAMC,iBAAiB,GAAG,CAAC;EAChCC,SADgC;EAEhCC;AAFgC,CAAD,KAM3B;EACJ,MAAMC,KAAK,GAAG,IAAAC,cAAA,EAAQ,OAAO;IAAEH;EAAF,CAAP,CAAR,EAA+B,EAA/B,CAAd;EAEA,oBACE,6BAAC,gBAAD,CAAkB,QAAlB;IAA2B,KAAK,EAAEE;EAAlC,GACGD,QADH,CADF;AAKD,CAdM;;;;AAgBA,MAAMG,YAAY,GAAG,MAAM;EAChC,MAAMC,OAAO,GAAGC,cAAA,CAAMC,UAAN,CAAiBX,gBAAjB,CAAhB;;EACA,IAAIS,OAAO,KAAKP,SAAhB,EAA2B;IACzB,MAAM,IAAIU,KAAJ,CAAU,sDAAV,CAAN;EACD;;EACD,OAAOH,OAAO,CAACL,SAAf;AACD,CANM"}
1
+ {"version":3,"file":"ServiceIdProvider.js","names":["ServiceIdContext","createContext","undefined","ServiceIdProvider","serviceId","children","state","useMemo","useServiceId","context","React","useContext","Error"],"sources":["../../src/notification/ServiceIdProvider.tsx"],"sourcesContent":["import React, { createContext, useMemo } from \"react\";\n\ninterface ServiceIdContextState {\n serviceId: string;\n}\n\nconst ServiceIdContext = createContext<ServiceIdContextState | undefined>(\n undefined\n);\n\nexport const ServiceIdProvider = ({\n serviceId,\n children,\n}: {\n serviceId: string;\n children: React.ReactNode;\n}) => {\n const state = useMemo(() => ({ serviceId }), [serviceId]);\n\n return (\n <ServiceIdContext.Provider value={state}>\n {children}\n </ServiceIdContext.Provider>\n );\n};\n\nexport const useServiceId = () => {\n const context = React.useContext(ServiceIdContext);\n if (context === undefined) {\n throw new Error(\"useServiceId must be used within a ServiceIdProvider\");\n }\n return context.serviceId;\n};\n"],"mappings":";;;;;;;AAAA;;;;;;;;AAMA,MAAMA,gBAAgB,gBAAG,IAAAC,oBAAA,EACvBC,SADuB,CAAzB;;AAIO,MAAMC,iBAAiB,GAAG,CAAC;EAChCC,SADgC;EAEhCC;AAFgC,CAAD,KAM3B;EACJ,MAAMC,KAAK,GAAG,IAAAC,cAAA,EAAQ,OAAO;IAAEH;EAAF,CAAP,CAAR,EAA+B,CAACA,SAAD,CAA/B,CAAd;EAEA,oBACE,qBAAC,gBAAD,CAAkB,QAAlB;IAA2B,KAAK,EAAEE,KAAlC;IAAA,UACGD;EADH,EADF;AAKD,CAdM;;;;AAgBA,MAAMG,YAAY,GAAG,MAAM;EAChC,MAAMC,OAAO,GAAGC,cAAA,CAAMC,UAAN,CAAiBX,gBAAjB,CAAhB;;EACA,IAAIS,OAAO,KAAKP,SAAhB,EAA2B;IACzB,MAAM,IAAIU,KAAJ,CAAU,sDAAV,CAAN;EACD;;EACD,OAAOH,OAAO,CAACL,SAAf;AACD,CANM"}
@@ -13,6 +13,8 @@ var _client = require("./client");
13
13
 
14
14
  var _m2mUsers = require("../endpoints/m2m-users.v1");
15
15
 
16
+ var _jsxRuntime = require("react/jsx-runtime");
17
+
16
18
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17
19
 
18
20
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -80,9 +82,10 @@ const M2mAuthenticationProvider = ({
80
82
  authenticated
81
83
  };
82
84
  }, [authenticated, login, logout, token]);
83
- return /*#__PURE__*/_react.default.createElement(AuthenticationContext.Provider, {
84
- value: state
85
- }, children);
85
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(AuthenticationContext.Provider, {
86
+ value: state,
87
+ children: children
88
+ });
86
89
  };
87
90
 
88
91
  exports.M2mAuthenticationProvider = M2mAuthenticationProvider;
@@ -1 +1 @@
1
- {"version":3,"file":"useM2mAuth.js","names":["AuthenticationContext","createContext","undefined","checkJwtExpiration","jwt","splitted","split","length","payload","exp","JSON","parse","atob","Date","M2mAuthenticationProvider","children","token","setToken","useState","m2mAuthTokenApi","getCache","authenticated","useMemo","tryCheckAuth","useCallback","authToken","get","clear","set","useEffect","login","input","data","fetcherResult","fetcher","m2mUsers_v1_login","method","body","stringify","accessToken","logout","window","location","reload","state","useAuth","ctx","useContext","Error"],"sources":["../../src/storage/useM2mAuth.tsx"],"sourcesContent":["import React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useState,\n} from \"react\";\nimport { fetcher } from \"matsuri-hooks\";\nimport { m2mAuthTokenApi } from \"./client\";\nimport { login as m2mUsers_v1_login } from \"../endpoints/m2m-users.v1\";\n\ninterface LoginInput {\n email: string;\n password: string;\n}\n\ntype AuthenticationState = {\n token: string;\n authenticated: boolean;\n login: (input: LoginInput) => Promise<{ error?: Error }>;\n logout: () => void;\n};\n\nconst AuthenticationContext = createContext<AuthenticationState | undefined>(\n undefined\n);\n\nconst checkJwtExpiration = (jwt: string): boolean => {\n const splitted = jwt.split(\".\");\n if (splitted.length !== 3) return false;\n\n const [, payload] = splitted;\n const exp = JSON.parse(atob(payload))[\"exp\"];\n return new Date() < new Date(exp * 1000);\n};\n\nexport const M2mAuthenticationProvider = ({\n children,\n}: {\n children: React.ReactNode;\n}) => {\n const [token, setToken] = useState(m2mAuthTokenApi.getCache() || \"\");\n\n const authenticated = useMemo(() => !!token, [token]);\n\n const tryCheckAuth = useCallback(async () => {\n const authToken = await m2mAuthTokenApi.get();\n if (!(authToken && checkJwtExpiration(authToken))) {\n m2mAuthTokenApi.clear();\n setToken(\"\");\n } else {\n m2mAuthTokenApi.set(authToken);\n setToken(authToken);\n }\n }, []);\n\n useEffect(() => {\n tryCheckAuth();\n }, [tryCheckAuth]);\n\n const login = useCallback(async (input: LoginInput) => {\n const { data, ...fetcherResult } = await fetcher<{\n accessToken: string;\n }>(m2mUsers_v1_login(), {\n method: \"POST\",\n body: JSON.stringify(input),\n });\n if (data) {\n const authToken = data.accessToken;\n\n m2mAuthTokenApi.set(authToken);\n setToken(authToken);\n }\n return fetcherResult;\n }, []);\n\n const logout = useCallback(() => {\n m2mAuthTokenApi.clear();\n window.location.reload();\n }, []);\n\n const state = useMemo(() => {\n return {\n login,\n logout,\n token,\n authenticated,\n };\n }, [authenticated, login, logout, token]);\n return (\n <AuthenticationContext.Provider value={state}>\n {children}\n </AuthenticationContext.Provider>\n );\n};\n\nexport const useAuth = () => {\n const ctx = useContext(AuthenticationContext);\n if (!ctx) {\n throw new Error(\"AuthenticationProvider is not found\");\n }\n return ctx;\n};\n"],"mappings":";;;;;;;AAAA;;AAQA;;AACA;;AACA;;;;;;AAcA,MAAMA,qBAAqB,gBAAG,IAAAC,oBAAA,EAC5BC,SAD4B,CAA9B;;AAIA,MAAMC,kBAAkB,GAAIC,GAAD,IAA0B;EACnD,MAAMC,QAAQ,GAAGD,GAAG,CAACE,KAAJ,CAAU,GAAV,CAAjB;EACA,IAAID,QAAQ,CAACE,MAAT,KAAoB,CAAxB,EAA2B,OAAO,KAAP;EAE3B,MAAM,GAAGC,OAAH,IAAcH,QAApB;EACA,MAAMI,GAAG,GAAGC,IAAI,CAACC,KAAL,CAAWC,IAAI,CAACJ,OAAD,CAAf,EAA0B,KAA1B,CAAZ;EACA,OAAO,IAAIK,IAAJ,KAAa,IAAIA,IAAJ,CAASJ,GAAG,GAAG,IAAf,CAApB;AACD,CAPD;;AASO,MAAMK,yBAAyB,GAAG,CAAC;EACxCC;AADwC,CAAD,KAInC;EAC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KAAhB;;IACAc,MAAM,CAACC,QAAP,CAAgBC,MAAhB;EACD,CAHc,EAGZ,EAHY,CAAf;EAKA,MAAMC,KAAK,GAAG,IAAAtB,cAAA,EAAQ,MAAM;IAC1B,OAAO;MACLQ,KADK;MAELU,MAFK;MAGLxB,KAHK;MAILK;IAJK,CAAP;EAMD,CAPa,EAOX,CAACA,aAAD,EAAgBS,KAAhB,EAAuBU,MAAvB,EAA+BxB,KAA/B,CAPW,CAAd;EAQA,oBACE,6BAAC,qBAAD,CAAuB,QAAvB;IAAgC,KAAK,EAAE4B;EAAvC,GACG7B,QADH,CADF;AAKD,CA1DM;;;;AA4DA,MAAM8B,OAAO,GAAG,MAAM;EAC3B,MAAMC,GAAG,GAAG,IAAAC,iBAAA,EAAW/C,qBAAX,CAAZ;;EACA,IAAI,CAAC8C,GAAL,EAAU;IACR,MAAM,IAAIE,KAAJ,CAAU,qCAAV,CAAN;EACD;;EACD,OAAOF,GAAP;AACD,CANM"}
1
+ {"version":3,"file":"useM2mAuth.js","names":["AuthenticationContext","createContext","undefined","checkJwtExpiration","jwt","splitted","split","length","payload","exp","JSON","parse","atob","Date","M2mAuthenticationProvider","children","token","setToken","useState","m2mAuthTokenApi","getCache","authenticated","useMemo","tryCheckAuth","useCallback","authToken","get","clear","set","useEffect","login","input","data","fetcherResult","fetcher","m2mUsers_v1_login","method","body","stringify","accessToken","logout","window","location","reload","state","useAuth","ctx","useContext","Error"],"sources":["../../src/storage/useM2mAuth.tsx"],"sourcesContent":["import React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useState,\n} from \"react\";\nimport { fetcher } from \"matsuri-hooks\";\nimport { m2mAuthTokenApi } from \"./client\";\nimport { login as m2mUsers_v1_login } from \"../endpoints/m2m-users.v1\";\n\ninterface LoginInput {\n email: string;\n password: string;\n}\n\ntype AuthenticationState = {\n token: string;\n authenticated: boolean;\n login: (input: LoginInput) => Promise<{ error?: Error }>;\n logout: () => void;\n};\n\nconst AuthenticationContext = createContext<AuthenticationState | undefined>(\n undefined\n);\n\nconst checkJwtExpiration = (jwt: string): boolean => {\n const splitted = jwt.split(\".\");\n if (splitted.length !== 3) return false;\n\n const [, payload] = splitted;\n const exp = JSON.parse(atob(payload))[\"exp\"];\n return new Date() < new Date(exp * 1000);\n};\n\nexport const M2mAuthenticationProvider = ({\n children,\n}: {\n children: React.ReactNode;\n}) => {\n const [token, setToken] = useState(m2mAuthTokenApi.getCache() || \"\");\n\n const authenticated = useMemo(() => !!token, [token]);\n\n const tryCheckAuth = useCallback(async () => {\n const authToken = await m2mAuthTokenApi.get();\n if (!(authToken && checkJwtExpiration(authToken))) {\n m2mAuthTokenApi.clear();\n setToken(\"\");\n } else {\n m2mAuthTokenApi.set(authToken);\n setToken(authToken);\n }\n }, []);\n\n useEffect(() => {\n tryCheckAuth();\n }, [tryCheckAuth]);\n\n const login = useCallback(async (input: LoginInput) => {\n const { data, ...fetcherResult } = await fetcher<{\n accessToken: string;\n }>(m2mUsers_v1_login(), {\n method: \"POST\",\n body: JSON.stringify(input),\n });\n if (data) {\n const authToken = data.accessToken;\n\n m2mAuthTokenApi.set(authToken);\n setToken(authToken);\n }\n return fetcherResult;\n }, []);\n\n const logout = useCallback(() => {\n m2mAuthTokenApi.clear();\n window.location.reload();\n }, []);\n\n const state = useMemo(() => {\n return {\n login,\n logout,\n token,\n authenticated,\n };\n }, [authenticated, login, logout, token]);\n return (\n <AuthenticationContext.Provider value={state}>\n {children}\n </AuthenticationContext.Provider>\n );\n};\n\nexport const useAuth = () => {\n const ctx = useContext(AuthenticationContext);\n if (!ctx) {\n throw new Error(\"AuthenticationProvider is not found\");\n }\n return ctx;\n};\n"],"mappings":";;;;;;;AAAA;;AAQA;;AACA;;AACA;;;;;;;;AAcA,MAAMA,qBAAqB,gBAAG,IAAAC,oBAAA,EAC5BC,SAD4B,CAA9B;;AAIA,MAAMC,kBAAkB,GAAIC,GAAD,IAA0B;EACnD,MAAMC,QAAQ,GAAGD,GAAG,CAACE,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AM,EAAE,MADc;MAEtBC,IAAI,EAAE3B,IAAI,CAAC4B,SAAL,CAAeP,KAAf;IAFgB,CAFiB,CAAzC;;IAMA,IAAIC,IAAJ,EAAU;MACR,MAAMP,SAAS,GAAGO,IAAI,CAACO,WAAvB;;MAEApB,uBAAA,CAAgBS,GAAhB,CAAoBH,SAApB;;MACAR,QAAQ,CAACQ,SAAD,CAAR;IACD;;IACD,OAAOQ,aAAP;EACD,CAda,EAcX,EAdW,CAAd;EAgBA,MAAMO,MAAM,GAAG,IAAAhB,kBAAA,EAAY,MAAM;IAC/BL,uBAAA,CAAgBQ,KAAhB;;IACAc,MAAM,CAACC,QAAP,CAAgBC,MAAhB;EACD,CAHc,EAGZ,EAHY,CAAf;EAKA,MAAMC,KAAK,GAAG,IAAAtB,cAAA,EAAQ,MAAM;IAC1B,OAAO;MACLQ,KADK;MAELU,MAFK;MAGLxB,KAHK;MAILK;IAJK,CAAP;EAMD,CAPa,EAOX,CAACA,aAAD,EAAgBS,KAAhB,EAAuBU,MAAvB,EAA+BxB,KAA/B,CAPW,CAAd;EAQA,oBACE,qBAAC,qBAAD,CAAuB,QAAvB;IAAgC,KAAK,EAAE4B,KAAvC;IAAA,UACG7B;EADH,EADF;AAKD,CA1DM;;;;AA4DA,MAAM8B,OAAO,GAAG,MAAM;EAC3B,MAAMC,GAAG,GAAG,IAAAC,iBAAA,EAAW/C,qBAAX,CAAZ;;EACA,IAAI,CAAC8C,GAAL,EAAU;IACR,MAAM,IAAIE,KAAJ,CAAU,qCAAV,CAAN;EACD;;EACD,OAAOF,GAAP;AACD,CANM"}
@@ -3,6 +3,7 @@ import { useAuthFetch } from "matsuri-hooks";
3
3
  import { useMemo } from "react";
4
4
  import { m2mNotifications_v1 } from "../endpoints/m2m-notifications.v1";
5
5
  import { useServiceId } from "./ServiceIdProvider";
6
+ import { jsx as _jsx } from "react/jsx-runtime";
6
7
  var NotificationPermissionContext = /*#__PURE__*/createContext(undefined);
7
8
  var NotificationsContext = /*#__PURE__*/createContext(undefined); // ServiceIdProviderの中で使うこと
8
9
 
@@ -54,11 +55,13 @@ export var NotificationsProvider = _ref => {
54
55
  refetch: refetchNotifications
55
56
  };
56
57
  }, [refetchNotifications, notifications === null || notifications === void 0 ? void 0 : notifications.data]);
57
- return /*#__PURE__*/React.createElement(NotificationPermissionContext.Provider, {
58
- value: permissionContextState
59
- }, /*#__PURE__*/React.createElement(NotificationsContext.Provider, {
60
- value: notificationsContextState
61
- }, children));
58
+ return /*#__PURE__*/_jsx(NotificationPermissionContext.Provider, {
59
+ value: permissionContextState,
60
+ children: /*#__PURE__*/_jsx(NotificationsContext.Provider, {
61
+ value: notificationsContextState,
62
+ children: children
63
+ })
64
+ });
62
65
  };
63
66
  export var useNotifications = () => {
64
67
  var ctx = useContext(NotificationsContext);
@@ -1 +1 @@
1
- {"version":3,"file":"NotificationProvider.js","names":["createContext","useCallback","useContext","useAuthFetch","useMemo","m2mNotifications_v1","useServiceId","NotificationPermissionContext","undefined","NotificationsContext","NotificationsProvider","token","children","serviceId","data","subscriptions","refetch","refetchSubscriptions","findMySubscriptions","checkPermission","notificationType","subscription","find","item","resourceSubscription","resourceSelectType","status","notifications","error","refetchNotifications","findNotificationsByServiceId","id","swrConfig","refreshInterval","console","permissionContextState","notificationsContextState","isAllRead","every","n","useNotifications","ctx","Error","useNotificationPermission"],"sources":["../src/notification/NotificationProvider.tsx"],"sourcesContent":["import {\n createContext,\n PropsWithChildren,\n useCallback,\n useContext,\n} from \"react\";\nimport { useAuthFetch } from \"matsuri-hooks\";\nimport { useMemo } from \"react\";\nimport { m2mNotifications_v1 } from \"../endpoints/m2m-notifications.v1\";\nimport { useServiceId } from \"./ServiceIdProvider\";\nimport { SubscriptionStatus, Subscription, Notification } from \"./domain\";\n\nconst NotificationPermissionContext = createContext<\n | {\n checkPermission: (\n notificationType: string\n ) => SubscriptionStatus | undefined;\n refetch: () => void;\n }\n | undefined\n>(undefined);\n\nconst NotificationsContext = createContext<\n | { notifications: Notification[]; isAllRead: boolean; refetch: () => void }\n | undefined\n>(undefined);\n\n// ServiceIdProviderの中で使うこと\nexport const NotificationsProvider = ({\n token,\n children,\n}: PropsWithChildren<{ token: string }>) => {\n const serviceId = useServiceId();\n\n const { data: subscriptions, refetch: refetchSubscriptions } = useAuthFetch<\n Subscription[]\n >(token, m2mNotifications_v1.findMySubscriptions(), {});\n const checkPermission = useCallback(\n (notificationType: string) => {\n const subscription = subscriptions?.find((item) => {\n return (\n item.serviceId === serviceId &&\n item.notificationType === notificationType &&\n item.resourceSubscription.resourceSelectType === \"any\"\n );\n });\n\n return subscription?.status;\n },\n [serviceId, subscriptions]\n );\n\n const {\n data: notifications,\n error,\n refetch: refetchNotifications,\n } = useAuthFetch<{ data: Notification[] }>(\n token,\n m2mNotifications_v1.findNotificationsByServiceId({ id: serviceId }),\n { swrConfig: { refreshInterval: 1000 * 60 * 5 } }\n );\n if (error) {\n // エラーが出てもユーザーは特に困らないので、ユーザーに伝える必要はないが、監視は必要。\n console.error(error);\n }\n\n const permissionContextState = useMemo(() => {\n return {\n checkPermission,\n refetch: refetchSubscriptions,\n };\n }, [checkPermission, refetchSubscriptions]);\n\n const notificationsContextState = useMemo(() => {\n return {\n notifications: notifications?.data ?? [],\n isAllRead: notifications?.data.every((n) => n.status === \"read\") ?? true,\n refetch: refetchNotifications,\n };\n }, [refetchNotifications, notifications?.data]);\n\n return (\n <NotificationPermissionContext.Provider value={permissionContextState}>\n <NotificationsContext.Provider value={notificationsContextState}>\n {children}\n </NotificationsContext.Provider>\n </NotificationPermissionContext.Provider>\n );\n};\n\nexport const useNotifications = () => {\n const ctx = useContext(NotificationsContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n\nexport const useNotificationPermission = () => {\n const ctx = useContext(NotificationPermissionContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n"],"mappings":"AAAA,SACEA,aADF,EAGEC,WAHF,EAIE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AM,IAAIC,KAAJ,CAAU,mCAAV,CAAN;EACD;;EAED,OAAOD,GAAP;AACD,CAPM"}
1
+ {"version":3,"file":"NotificationProvider.js","names":["createContext","useCallback","useContext","useAuthFetch","useMemo","m2mNotifications_v1","useServiceId","NotificationPermissionContext","undefined","NotificationsContext","NotificationsProvider","token","children","serviceId","data","subscriptions","refetch","refetchSubscriptions","findMySubscriptions","checkPermission","notificationType","subscription","find","item","resourceSubscription","resourceSelectType","status","notifications","error","refetchNotifications","findNotificationsByServiceId","id","swrConfig","refreshInterval","console","permissionContextState","notificationsContextState","isAllRead","every","n","useNotifications","ctx","Error","useNotificationPermission"],"sources":["../src/notification/NotificationProvider.tsx"],"sourcesContent":["import {\n createContext,\n PropsWithChildren,\n useCallback,\n useContext,\n} from \"react\";\nimport { useAuthFetch } from \"matsuri-hooks\";\nimport { useMemo } from \"react\";\nimport { m2mNotifications_v1 } from \"../endpoints/m2m-notifications.v1\";\nimport { useServiceId } from \"./ServiceIdProvider\";\nimport { SubscriptionStatus, Subscription, Notification } from \"./domain\";\n\nconst NotificationPermissionContext = createContext<\n | {\n checkPermission: (\n notificationType: string\n ) => SubscriptionStatus | undefined;\n refetch: () => void;\n }\n | undefined\n>(undefined);\n\nconst NotificationsContext = createContext<\n | { notifications: Notification[]; isAllRead: boolean; refetch: () => void }\n | undefined\n>(undefined);\n\n// ServiceIdProviderの中で使うこと\nexport const NotificationsProvider = ({\n token,\n children,\n}: PropsWithChildren<{ token: string }>) => {\n const serviceId = useServiceId();\n\n const { data: subscriptions, refetch: refetchSubscriptions } = useAuthFetch<\n Subscription[]\n >(token, m2mNotifications_v1.findMySubscriptions(), {});\n const checkPermission = useCallback(\n (notificationType: string) => {\n const subscription = subscriptions?.find((item) => {\n return (\n item.serviceId === serviceId &&\n item.notificationType === notificationType &&\n item.resourceSubscription.resourceSelectType === \"any\"\n );\n });\n\n return subscription?.status;\n },\n [serviceId, subscriptions]\n );\n\n const {\n data: notifications,\n error,\n refetch: refetchNotifications,\n } = useAuthFetch<{ data: Notification[] }>(\n token,\n m2mNotifications_v1.findNotificationsByServiceId({ id: serviceId }),\n { swrConfig: { refreshInterval: 1000 * 60 * 5 } }\n );\n if (error) {\n // エラーが出てもユーザーは特に困らないので、ユーザーに伝える必要はないが、監視は必要。\n console.error(error);\n }\n\n const permissionContextState = useMemo(() => {\n return {\n checkPermission,\n refetch: refetchSubscriptions,\n };\n }, [checkPermission, refetchSubscriptions]);\n\n const notificationsContextState = useMemo(() => {\n return {\n notifications: notifications?.data ?? [],\n isAllRead: notifications?.data.every((n) => n.status === \"read\") ?? true,\n refetch: refetchNotifications,\n };\n }, [refetchNotifications, notifications?.data]);\n\n return (\n <NotificationPermissionContext.Provider value={permissionContextState}>\n <NotificationsContext.Provider value={notificationsContextState}>\n {children}\n </NotificationsContext.Provider>\n </NotificationPermissionContext.Provider>\n );\n};\n\nexport const useNotifications = () => {\n const ctx = useContext(NotificationsContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n\nexport const useNotificationPermission = () => {\n const ctx = useContext(NotificationPermissionContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n"],"mappings":"AAAA,SACEA,aADF,EAGEC,WAHF,EAIEC,UAJF,QAKO,OALP;AAMA,SAASC,YAAT,QAA6B,eAA7B;AACA,SAASC,OAAT,QAAwB,OAAxB;AACA,SAASC,mBAAT,QAAoC,mCAApC;AACA,SAASC,YAAT,QAA6B,qBAA7B;;AAGA,IAAMC,6BAA6B,gBAAGP,aAAa,CAQjDQ,SARiD,CAAnD;AAUA,IAAMC,oBAAoB,gBAAGT,aAAa,CAGxCQ,SAHwC,CAA1C,C,CAKA;;AACA,OAAO,IAAME,qBAAqB,GAAG,QAGO;EAAA,IAHN;IACpCC,KADoC;IAEpCC;EAFoC,CAGM;EAC1C,IAAMC,SAAS,GAAGP,YAAY,EAA9B;EAEA,IAAM;IAAEQ,IAAI,EAAEC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SAAS,2BAAEV,aAAF,aAAEA,aAAF,uBAAEA,aAAa,CAAEb,IAAf,CAAoBwB,KAApB,CAA2BC,CAAD,IAAOA,CAAC,CAACb,MAAF,KAAa,MAA9C,CAAF,yEAA2D,IAF/D;MAGLV,OAAO,EAAEa;IAHJ,CAAP;EAKD,CANwC,EAMtC,CAACA,oBAAD,EAAuBF,aAAvB,aAAuBA,aAAvB,uBAAuBA,aAAa,CAAEb,IAAtC,CANsC,CAAzC;EAQA,oBACE,KAAC,6BAAD,CAA+B,QAA/B;IAAwC,KAAK,EAAEqB,sBAA/C;IAAA,uBACE,KAAC,oBAAD,CAAsB,QAAtB;MAA+B,KAAK,EAAEC,yBAAtC;MAAA,UACGxB;IADH;EADF,EADF;AAOD,CA5DM;AA8DP,OAAO,IAAM4B,gBAAgB,GAAG,MAAM;EACpC,IAAMC,GAAG,GAAGvC,UAAU,CAACO,oBAAD,CAAtB;;EACA,IAAI,CAACgC,GAAL,EAAU;IACR,MAAM,IAAIC,KAAJ,CAAU,mCAAV,CAAN;EACD;;EAED,OAAOD,GAAP;AACD,CAPM;AASP,OAAO,IAAME,yBAAyB,GAAG,MAAM;EAC7C,IAAMF,GAAG,GAAGvC,UAAU,CAACK,6BAAD,CAAtB;;EACA,IAAI,CAACkC,GAAL,EAAU;IACR,MAAM,IAAIC,KAAJ,CAAU,mCAAV,CAAN;EACD;;EAED,OAAOD,GAAP;AACD,CAPM"}
@@ -1,4 +1,5 @@
1
1
  import React, { createContext, useMemo } from "react";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
2
3
  var ServiceIdContext = /*#__PURE__*/createContext(undefined);
3
4
  export var ServiceIdProvider = _ref => {
4
5
  var {
@@ -7,10 +8,11 @@ export var ServiceIdProvider = _ref => {
7
8
  } = _ref;
8
9
  var state = useMemo(() => ({
9
10
  serviceId
10
- }), []);
11
- return /*#__PURE__*/React.createElement(ServiceIdContext.Provider, {
12
- value: state
13
- }, children);
11
+ }), [serviceId]);
12
+ return /*#__PURE__*/_jsx(ServiceIdContext.Provider, {
13
+ value: state,
14
+ children: children
15
+ });
14
16
  };
15
17
  export var useServiceId = () => {
16
18
  var context = React.useContext(ServiceIdContext);
@@ -1 +1 @@
1
- {"version":3,"file":"ServiceIdProvider.js","names":["React","createContext","useMemo","ServiceIdContext","undefined","ServiceIdProvider","serviceId","children","state","useServiceId","context","useContext","Error"],"sources":["../src/notification/ServiceIdProvider.tsx"],"sourcesContent":["import React, { createContext, useMemo } from \"react\";\n\ninterface ServiceIdContextState {\n serviceId: string;\n}\n\nconst ServiceIdContext = createContext<ServiceIdContextState | undefined>(\n undefined\n);\n\nexport const ServiceIdProvider = ({\n serviceId,\n children,\n}: {\n serviceId: string;\n children: React.ReactNode;\n}) => {\n const state = useMemo(() => ({ serviceId }), []);\n\n return (\n <ServiceIdContext.Provider value={state}>\n {children}\n </ServiceIdContext.Provider>\n );\n};\n\nexport const useServiceId = () => {\n const context = React.useContext(ServiceIdContext);\n if (context === undefined) {\n throw new Error(\"useServiceId must be used within a ServiceIdProvider\");\n }\n return context.serviceId;\n};\n"],"mappings":"AAAA,OAAOA,KAAP,IAAgBC,aAAhB,EAA+BC,OAA/B,QAA8C,OAA9C;AAMA,IAAMC,gBAAgB,gBAAGF,aAAa,CACpCG,SADoC,CAAtC;AAIA,OAAO,IAAMC,iBAAiB,GAAG,QAM3B;EAAA,IAN4B;IAChCC,SADgC;IAEhCC;EAFgC,CAM5B;EACJ,IAAMC,KAAK,GAAGN,OAAO,CAAC,OAAO;IAAEI;EAAF,CAAP,CAAD,EAAwB,EAAxB,CAArB;EAEA,oBACE,oBAAC,gBAAD,CAAkB,QAAlB;IAA2B,KAAK,EAAEE;EAAlC,GACGD,QADH,CADF;AAKD,CAdM;AAgBP,OAAO,IAAME,YAAY,GAAG,MAAM;EAChC,IAAMC,OAAO,GAAGV,KAAK,CAACW,UAAN,CAAiBR,gBAAjB,CAAhB;;EACA,IAAIO,OAAO,KAAKN,SAAhB,EAA2B;IACzB,MAAM,IAAIQ,KAAJ,CAAU,sDAAV,CAAN;EACD;;EACD,OAAOF,OAAO,CAACJ,SAAf;AACD,CANM"}
1
+ {"version":3,"file":"ServiceIdProvider.js","names":["React","createContext","useMemo","ServiceIdContext","undefined","ServiceIdProvider","serviceId","children","state","useServiceId","context","useContext","Error"],"sources":["../src/notification/ServiceIdProvider.tsx"],"sourcesContent":["import React, { createContext, useMemo } from \"react\";\n\ninterface ServiceIdContextState {\n serviceId: string;\n}\n\nconst ServiceIdContext = createContext<ServiceIdContextState | undefined>(\n undefined\n);\n\nexport const ServiceIdProvider = ({\n serviceId,\n children,\n}: {\n serviceId: string;\n children: React.ReactNode;\n}) => {\n const state = useMemo(() => ({ serviceId }), [serviceId]);\n\n return (\n <ServiceIdContext.Provider value={state}>\n {children}\n </ServiceIdContext.Provider>\n );\n};\n\nexport const useServiceId = () => {\n const context = React.useContext(ServiceIdContext);\n if (context === undefined) {\n throw new Error(\"useServiceId must be used within a ServiceIdProvider\");\n }\n return context.serviceId;\n};\n"],"mappings":"AAAA,OAAOA,KAAP,IAAgBC,aAAhB,EAA+BC,OAA/B,QAA8C,OAA9C;;AAMA,IAAMC,gBAAgB,gBAAGF,aAAa,CACpCG,SADoC,CAAtC;AAIA,OAAO,IAAMC,iBAAiB,GAAG,QAM3B;EAAA,IAN4B;IAChCC,SADgC;IAEhCC;EAFgC,CAM5B;EACJ,IAAMC,KAAK,GAAGN,OAAO,CAAC,OAAO;IAAEI;EAAF,CAAP,CAAD,EAAwB,CAACA,SAAD,CAAxB,CAArB;EAEA,oBACE,KAAC,gBAAD,CAAkB,QAAlB;IAA2B,KAAK,EAAEE,KAAlC;IAAA,UACGD;EADH,EADF;AAKD,CAdM;AAgBP,OAAO,IAAME,YAAY,GAAG,MAAM;EAChC,IAAMC,OAAO,GAAGV,KAAK,CAACW,UAAN,CAAiBR,gBAAjB,CAAhB;;EACA,IAAIO,OAAO,KAAKN,SAAhB,EAA2B;IACzB,MAAM,IAAIQ,KAAJ,CAAU,sDAAV,CAAN;EACD;;EACD,OAAOF,OAAO,CAACJ,SAAf;AACD,CANM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "m2m-components",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "main": "cjs/index.js",
5
5
  "module": "index.js",
6
6
  "typings": "index.d.ts",
@@ -12,6 +12,7 @@ import React, { createContext, useCallback, useContext, useEffect, useMemo, useS
12
12
  import { fetcher } from "matsuri-hooks";
13
13
  import { m2mAuthTokenApi } from "./client";
14
14
  import { login as m2mUsers_v1_login } from "../endpoints/m2m-users.v1";
15
+ import { jsx as _jsx } from "react/jsx-runtime";
15
16
  var AuthenticationContext = /*#__PURE__*/createContext(undefined);
16
17
 
17
18
  var checkJwtExpiration = jwt => {
@@ -78,9 +79,10 @@ export var M2mAuthenticationProvider = _ref => {
78
79
  authenticated
79
80
  };
80
81
  }, [authenticated, login, logout, token]);
81
- return /*#__PURE__*/React.createElement(AuthenticationContext.Provider, {
82
- value: state
83
- }, children);
82
+ return /*#__PURE__*/_jsx(AuthenticationContext.Provider, {
83
+ value: state,
84
+ children: children
85
+ });
84
86
  };
85
87
  export var useAuth = () => {
86
88
  var ctx = useContext(AuthenticationContext);
@@ -1 +1 @@
1
- {"version":3,"file":"useM2mAuth.js","names":["React","createContext","useCallback","useContext","useEffect","useMemo","useState","fetcher","m2mAuthTokenApi","login","m2mUsers_v1_login","AuthenticationContext","undefined","checkJwtExpiration","jwt","splitted","split","length","payload","exp","JSON","parse","atob","Date","M2mAuthenticationProvider","children","token","setToken","getCache","authenticated","tryCheckAuth","authToken","get","clear","set","input","method","body","stringify","data","fetcherResult","accessToken","logout","window","location","reload","state","useAuth","ctx","Error"],"sources":["../src/storage/useM2mAuth.tsx"],"sourcesContent":["import React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useState,\n} from \"react\";\nimport { fetcher } from \"matsuri-hooks\";\nimport { m2mAuthTokenApi } from \"./client\";\nimport { login as m2mUsers_v1_login } from \"../endpoints/m2m-users.v1\";\n\ninterface LoginInput {\n email: string;\n password: string;\n}\n\ntype AuthenticationState = {\n token: string;\n authenticated: boolean;\n login: (input: LoginInput) => Promise<{ error?: Error }>;\n logout: () => void;\n};\n\nconst AuthenticationContext = createContext<AuthenticationState | undefined>(\n undefined\n);\n\nconst checkJwtExpiration = (jwt: string): boolean => {\n const splitted = jwt.split(\".\");\n if (splitted.length !== 3) return false;\n\n const [, payload] = splitted;\n const exp = JSON.parse(atob(payload))[\"exp\"];\n return new Date() < new Date(exp * 1000);\n};\n\nexport const M2mAuthenticationProvider = ({\n children,\n}: {\n children: React.ReactNode;\n}) => {\n const [token, setToken] = useState(m2mAuthTokenApi.getCache() || \"\");\n\n const authenticated = useMemo(() => !!token, [token]);\n\n const tryCheckAuth = useCallback(async () => {\n const authToken = await m2mAuthTokenApi.get();\n if (!(authToken && checkJwtExpiration(authToken))) {\n m2mAuthTokenApi.clear();\n setToken(\"\");\n } else {\n m2mAuthTokenApi.set(authToken);\n setToken(authToken);\n }\n }, []);\n\n useEffect(() => {\n tryCheckAuth();\n }, [tryCheckAuth]);\n\n const login = useCallback(async (input: LoginInput) => {\n const { data, ...fetcherResult } = await fetcher<{\n accessToken: string;\n }>(m2mUsers_v1_login(), {\n method: \"POST\",\n body: JSON.stringify(input),\n });\n if (data) {\n const authToken = data.accessToken;\n\n m2mAuthTokenApi.set(authToken);\n setToken(authToken);\n }\n return fetcherResult;\n }, []);\n\n const logout = useCallback(() => {\n m2mAuthTokenApi.clear();\n window.location.reload();\n }, []);\n\n const state = useMemo(() => {\n return {\n login,\n logout,\n token,\n authenticated,\n };\n }, [authenticated, login, logout, token]);\n return (\n <AuthenticationContext.Provider value={state}>\n {children}\n </AuthenticationContext.Provider>\n );\n};\n\nexport const useAuth = () => {\n const ctx = useContext(AuthenticationContext);\n if (!ctx) {\n throw new Error(\"AuthenticationProvider is not found\");\n }\n return ctx;\n};\n"],"mappings":";;;;;;;;;;AA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,IAAI,CAACqC,GAAL,EAAU;IACR,MAAM,IAAIC,KAAJ,CAAU,qCAAV,CAAN;EACD;;EACD,OAAOD,GAAP;AACD,CANM"}
1
+ {"version":3,"file":"useM2mAuth.js","names":["React","createContext","useCallback","useContext","useEffect","useMemo","useState","fetcher","m2mAuthTokenApi","login","m2mUsers_v1_login","AuthenticationContext","undefined","checkJwtExpiration","jwt","splitted","split","length","payload","exp","JSON","parse","atob","Date","M2mAuthenticationProvider","children","token","setToken","getCache","authenticated","tryCheckAuth","authToken","get","clear","set","input","method","body","stringify","data","fetcherResult","accessToken","logout","window","location","reload","state","useAuth","ctx","Error"],"sources":["../src/storage/useM2mAuth.tsx"],"sourcesContent":["import React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useState,\n} from \"react\";\nimport { fetcher } from \"matsuri-hooks\";\nimport { m2mAuthTokenApi } from \"./client\";\nimport { login as m2mUsers_v1_login } from \"../endpoints/m2m-users.v1\";\n\ninterface LoginInput {\n email: string;\n password: string;\n}\n\ntype AuthenticationState = {\n token: string;\n authenticated: boolean;\n login: (input: LoginInput) => Promise<{ error?: Error }>;\n logout: () => void;\n};\n\nconst AuthenticationContext = createContext<AuthenticationState | undefined>(\n undefined\n);\n\nconst checkJwtExpiration = (jwt: string): boolean => {\n const splitted = jwt.split(\".\");\n if (splitted.length !== 3) return false;\n\n const [, payload] = splitted;\n const exp = JSON.parse(atob(payload))[\"exp\"];\n return new Date() < new Date(exp * 1000);\n};\n\nexport const M2mAuthenticationProvider = ({\n children,\n}: {\n children: React.ReactNode;\n}) => {\n const [token, setToken] = useState(m2mAuthTokenApi.getCache() || \"\");\n\n const authenticated = useMemo(() => !!token, [token]);\n\n const tryCheckAuth = useCallback(async () => {\n const authToken = await m2mAuthTokenApi.get();\n if (!(authToken && checkJwtExpiration(authToken))) {\n m2mAuthTokenApi.clear();\n setToken(\"\");\n } else {\n m2mAuthTokenApi.set(authToken);\n setToken(authToken);\n }\n }, []);\n\n useEffect(() => {\n tryCheckAuth();\n }, [tryCheckAuth]);\n\n const login = useCallback(async (input: LoginInput) => {\n const { data, ...fetcherResult } = await fetcher<{\n accessToken: string;\n }>(m2mUsers_v1_login(), {\n method: \"POST\",\n body: JSON.stringify(input),\n });\n if (data) {\n const authToken = data.accessToken;\n\n m2mAuthTokenApi.set(authToken);\n setToken(authToken);\n }\n return fetcherResult;\n }, []);\n\n const logout = useCallback(() => {\n m2mAuthTokenApi.clear();\n window.location.reload();\n }, []);\n\n const state = useMemo(() => {\n return {\n login,\n logout,\n token,\n authenticated,\n };\n }, [authenticated, login, logout, token]);\n return (\n <AuthenticationContext.Provider value={state}>\n {children}\n </AuthenticationContext.Provider>\n );\n};\n\nexport const useAuth = () => {\n const ctx = useContext(AuthenticationContext);\n if (!ctx) {\n throw new Error(\"AuthenticationProvider is not found\");\n }\n return ctx;\n};\n"],"mappings":";;;;;;;;;;AAAA,OAAOA,KAAP,IACEC,aADF,EAEEC,WAFF,EAGEC,UAHF,EAIEC,SAJF,EAKEC,OALF,EAMEC,QANF,QAOO,OAPP;AAQA,SAASC,OAAT,QAAwB,eAAxB;AACA,SAASC,eAAT,QAAgC,UAAhC;AACA,SAASC,KAAK,IAAIC,iBAAlB,QAA2C,2BAA3C;;AAcA,IAAMC,qBAAqB,gBAAGV,aAAa,CACzCW,SADyC,CAA3C;;AAIA,IAAMC,kBAAkB,GAAIC,GAAD,IAA0B;EACnD,IAAMC,QAAQ,GAAGD,GAAG,CAACE,KAAJ,CAAU,GAAV,CAAjB;EACA,IAAID,QAAQ,CAACE,MAAT,KAAoB,CAAxB,EAA2B,OAAO,KAAP;EAE3B,IAAM,GAAGC,OAAH,IAAcH,QAApB;EACA,IAAMI,GAAG,GAAGC,IAAI,CAACC,KAAL,CAAWC,IAAI,CAACJ,OAAD,CAAf,EAA0B,KAA1B,CAAZ;EACA,OAAO,IAAIK,IAAJ,KAAa,IAAIA,IAAJ,CAASJ,GAAG,GAAG,IAAf,CAApB;AACD,CAPD;;A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