@quiltt/react 2.1.0 → 2.1.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +474 -0
  2. package/README.md +110 -75
  3. package/dist/{QuilttSettingsProvider-6904aa95.d.ts → QuilttSettingsProvider-d1d44353.d.ts} +0 -1
  4. package/dist/components/index.cjs +1 -1
  5. package/dist/components/index.cjs.map +1 -1
  6. package/dist/components/index.js +1 -1
  7. package/dist/components/index.js.map +1 -1
  8. package/dist/index.cjs +1 -1
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/providers/index.cjs +1 -1
  14. package/dist/providers/index.cjs.map +1 -1
  15. package/dist/providers/index.d.ts +1 -1
  16. package/dist/providers/index.js +1 -1
  17. package/dist/providers/index.js.map +1 -1
  18. package/package.json +13 -3
  19. package/src/components/QuilttButton.tsx +24 -0
  20. package/src/components/QuilttContainer.tsx +30 -0
  21. package/src/components/index.ts +2 -0
  22. package/src/hooks/helpers/index.ts +2 -0
  23. package/src/hooks/helpers/useEventListener.ts +84 -0
  24. package/src/hooks/helpers/useIsomorphicLayoutEffect.ts +11 -0
  25. package/src/hooks/index.ts +8 -0
  26. package/src/hooks/session/index.ts +4 -0
  27. package/src/hooks/session/useAuthenticateSession.ts +54 -0
  28. package/src/hooks/session/useIdentifySession.ts +55 -0
  29. package/src/hooks/session/useImportSession.ts +43 -0
  30. package/src/hooks/session/useRevokeSession.ts +27 -0
  31. package/src/hooks/useQuilttClient.ts +5 -0
  32. package/src/hooks/useQuilttConnector.ts +39 -0
  33. package/src/hooks/useQuilttSession.ts +45 -0
  34. package/src/hooks/useQuilttSettings.ts +17 -0
  35. package/src/hooks/useSession.ts +69 -0
  36. package/src/hooks/useStorage.ts +73 -0
  37. package/src/index.ts +4 -0
  38. package/src/providers/QuilttAuthProvider.tsx +43 -0
  39. package/src/providers/QuilttProvider.tsx +19 -0
  40. package/src/providers/QuilttSettingsProvider.tsx +20 -0
  41. package/src/providers/index.ts +3 -0
  42. package/src/types.ts +9 -0
package/README.md CHANGED
@@ -1,137 +1,172 @@
1
1
  # @quiltt/react
2
2
 
3
- ## Overview
3
+ [![npm version](https://badge.fury.io/js/@quiltt%2Fcore.svg)](https://badge.fury.io/js/@quiltt%2Fcore)
4
+ [![CI](https://github.com/quiltt/quiltt-public/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/quiltt/quiltt-public/actions/workflows/ci.yml)
4
5
 
5
- `@quiltt/react` is a comprehensive JavaScript library that provides essential functionality for building applications using React. It extends the capabilities of React by offering various modules, utilities, and types from `@quiltt/core` that can be utilized to enhance your development workflow.
6
+ `@quiltt/react` provides React Components and Hooks for integrating Quiltt into React-based applications.
6
7
 
7
8
  ## Installation
8
9
 
9
- To install `@quiltt/react` in your project, you need to have Node.js and npm (Node Package Manager) installed. Then, you can run the following command:
10
-
11
10
  ```shell
12
- npm install @quiltt/react
11
+ $ npm install @quiltt/react
12
+ # or
13
+ $ yarn add @quiltt/react
14
+ # or
15
+ $ pnpm add @quiltt/react
13
16
  ```
14
17
 
15
- ## Features
18
+ ## Core Modules and Types
16
19
 
17
- `@quiltt/react` provides the following features:
20
+ The `@quiltt/react` library ships with `@quiltt/core`, which provides an Auth API and essential functionality for building Javascript-based applications with Quiltt. See the [Core README](../core/README.md) for more information.
18
21
 
19
- - Integration with `@quiltt/core` library, which includes modules for JSON Web Tokens (JWT), observables, storage management, timeouts, API handling, and TypeScript types.
20
- - Additional React-specific hooks and utilities for session management, storage, and Quiltt client integration.
22
+ ## React Components
21
23
 
22
- ## Available Exports
24
+ All components automatically handle Session token management under the hood, using the `useQuilttSession` hook.
23
25
 
24
- ### Core Modules and Types
26
+ To pre-authenticate the Connector for one of your user profiles, make sure to set your token using the `QuilttProvider` provider or the `useQuilttSession` hook. See the [Authentication guides](https://www.quiltt.dev/guides/authentication) for how to generate a Session.
25
27
 
26
- The following exports are available from `@quiltt/core`:
28
+ ### QuilttButton
27
29
 
28
- - JSON Web Token functionality, observable patterns, storage management, timeouts, API handling, and TypeScript types.
30
+ Launch the [Quiltt Connector](https://www.quiltt.dev/guides/connector) as a pop-out modal.
29
31
 
30
- ### React Hooks
32
+ By default, the rendered component will be a `<button>` element, but you can supply your own component via the `as` prop. You can also pass forward any props to the rendered component.
31
33
 
32
- The following hooks are available from `@quiltt/react/hooks`:
34
+ #### Example
33
35
 
34
- - Helpers for handling Quiltt sessions and client integration.
35
- - `useQuilttConnector`: A hook for connecting to the Quiltt backend.
36
- - `useQuilttSession`: A hook for managing Quiltt sessions.
37
- - `useQuilttSettings`: A hook for accessing Quiltt settings.
36
+ ```tsx
37
+ import { QuilttButton } from '@quiltt/react'
38
38
 
39
- #### Usage
39
+ export const App = () => {
40
+ return (
41
+ <QuilttButton
42
+ connectorId="{YOUR_CONNECTOR_ID}"
43
+ className="my-css-class"
44
+ styles={{ borderWidth: '2px' }}
45
+ >
46
+ Add Account
47
+ </QuilttButton>
48
+ )
49
+ }
50
+ export default App
51
+ ```
40
52
 
41
- ##### useQuilttConnector
53
+ ### QuilttContainer
42
54
 
43
- ```tsx
44
- import { type FC } from 'react'
45
- import { useQuilttConnector } from '@quiltt/react'
55
+ Launch the [Quiltt Connector](https://www.quiltt.dev/guides/connector) inside a container.
46
56
 
47
- type ConnectorLauncherProps = {
48
- connectorId: string
49
- }
57
+ By default, the rendered component will be a `<div>` element, but you can supply your own component via the `as` prop. You can also pass forward any props to the rendered component.
50
58
 
51
- const ConnectorLauncher: FC<ConnectorLauncherProps> = ({ connectorId }) => {
52
- const launcherClass = 'connector-launcher'
59
+ ##### Example
53
60
 
54
- const { ready } = useQuilttConnector({
55
- connectorId,
56
- button: `.${launcherClass}`,
57
- })
61
+ ```tsx
62
+ import { QuilttContainer } from '@quiltt/react'
58
63
 
64
+ export const App = () => {
59
65
  return (
60
- <button type="button" disabled={!ready} className={launcherClass}>
61
- Launch with Component
62
- </button>
66
+ <QuilttContainer
67
+ connectorId="{YOUR_CONNECTOR_ID}"
68
+ className="my-css-class"
69
+ styles={{ height: '100%' }}
70
+ />
63
71
  )
64
72
  }
73
+
74
+ export default App
65
75
  ```
66
76
 
67
- ##### useQuilttSession
77
+ ### QuilttProvider
78
+
79
+ A provider component for passing Session and settings down to the rest of your application.
80
+
81
+ #### Example
68
82
 
69
83
  ```tsx
70
- import { useEffect } from 'react'
71
- import { useQuilttSession } from '@quiltt/react'
84
+ import { QuilttProvider } from '@quiltt/react'
72
85
 
73
- const MyComponent = () => {
74
- const { session, importSession, authenticateSession, revokeSession } = useQuilttSession()
86
+ const Layout = ({ children }) => {
87
+ return <QuilttProvider token="{SESSION_TOKEN}">{children}</QuilttProvider>
88
+ }
75
89
 
76
- useEffect(() => {
77
- // Import session from local storage or any other source
78
- importSession()
90
+ export default Layout
91
+ ```
79
92
 
80
- // Authenticate session with your backend if necessary
81
- authenticateSession()
93
+ ## React Hooks
82
94
 
83
- // Clean up session when component unmounts
84
- return () => {
85
- revokeSession()
86
- }
87
- }, [importSession, authenticateSession, revokeSession])
95
+ For maximum control over the lifecycle of Quiltt Connector and Quiltt Sessions, you can also use hooks directly.
88
96
 
89
- return <div>{session ? <p>Session token: {session.token}</p> : <p>No session available</p>}</div>
90
- }
97
+ ### useQuilttConnector
91
98
 
92
- export default MyComponent
93
- ```
99
+ A hook to manage the lifecycle of [Quiltt Connector](https://www.quiltt.dev/guides/connector).
94
100
 
95
- ##### useQuilttSettings
101
+ #### Example
96
102
 
97
103
  ```tsx
98
- import { QuilttSettings, useQuilttSettings } from '@quiltt/react'
104
+ import { useQuilttConnector } from '@quiltt/react'
99
105
 
100
106
  const App = () => {
101
- // Wrap your application with QuilttSettingsProvider to provide the settings
107
+ useQuilttConnector()
108
+
102
109
  return (
103
- <QuilttSettings.Provider value={{ clientId: 'YOUR_CLIENT_ID' }}>
104
- <MyComponent />
105
- </QuilttSettings.Provider>
110
+ <button quilttButton="{MY_CONNECTOR_ID}" type="button">
111
+ Launch Connector
112
+ </button>
106
113
  )
107
114
  }
115
+ ```
116
+
117
+ ### useQuilttSession
118
+
119
+ A hook to manage the lifecycle of Quiltt Sessions.
108
120
 
109
- const MyComponent = () => {
110
- const { clientId } = useQuilttSettings()
121
+ See the [Authentication guides](https://www.quiltt.dev/guides/authentication) for more information.
122
+
123
+ #### Example
124
+
125
+ ```tsx
126
+ import { useCallback, useEffect } from 'react'
127
+
128
+ import { useQuilttSession } from '@quiltt/react'
129
+
130
+ const App = () => {
131
+ const { session, importSession, revokeSession } = useQuilttSession()
132
+
133
+ useEffect(() => {
134
+ // Import session from API call, local storage, query param, etc.
135
+ importSession('{SESSION_TOKEN}')
136
+ }, [importSession])
137
+
138
+ const logOut = useCallback(() => {
139
+ // Revoke and clear the Quiltt session
140
+ revokeSession()
141
+
142
+ // do other stuff!
143
+ }, [revokeSession])
144
+
145
+ if (session) {
146
+ console.log('Session token: ', session.token)
147
+ } else {
148
+ console.log('No Session available')
149
+ }
111
150
 
112
151
  return (
113
- <div>
114
- <p>Quiltt Client ID: {clientId}</p>
115
- {/* Rest of your component */}
116
- </div>
152
+ <>
153
+ <div>Hello world!</div>
154
+ <button onClick={logOut}>Log Out</button>
155
+ </>
117
156
  )
118
157
  }
119
158
 
120
159
  export default App
121
160
  ```
122
161
 
123
- ### React Providers
124
-
125
- The following providers are available from `@quiltt/react/providers`:
162
+ ## Typescript support
126
163
 
127
- - `QuilttAuthProvider`: A provider component for authenticating with Quiltt.
128
- - `QuilttProvider`: A provider component for initializing Quiltt.
129
- - `QuilttSettingsProvider`: A provider component for managing Quiltt settings.
164
+ `@quiltt/react` is written in Typescript and ships with its own type definitions. It also ships with the type definitions from `@quiltt/core`.
130
165
 
131
166
  ## License
132
167
 
133
168
  This project is licensed under the terms of the MIT license. See the [LICENSE](LICENSE.md) file for more information.
134
169
 
135
- For information on how to contribute to this project, please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file.
170
+ ## Contributing
136
171
 
137
- By contributing to `@quiltt/react`, you can help improve its features and functionality. We appreciate your contributions and thank you for your support!
172
+ For information on how to contribute to this project, please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file.
@@ -1,7 +1,6 @@
1
1
  import { FC, PropsWithChildren } from 'react';
2
2
 
3
3
  type QuilttAuthProviderProps = PropsWithChildren & {
4
- resetOnSessionChange?: boolean;
5
4
  token?: string;
6
5
  };
7
6
  /**
@@ -1,2 +1,2 @@
1
- "use strict";var Ke=Object.create;var g=Object.defineProperty;var qe=Object.getOwnPropertyDescriptor;var Ge=Object.getOwnPropertyNames,w=Object.getOwnPropertySymbols,We=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty,ue=Object.prototype.propertyIsEnumerable;var ae=(e,t,n)=>t in e?g(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,S=(e,t)=>{for(var n in t||(t={}))K.call(t,n)&&ae(e,n,t[n]);if(w)for(var n of w(t))ue.call(t,n)&&ae(e,n,t[n]);return e};var O=(e,t)=>{var n={};for(var r in e)K.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(e!=null&&w)for(var r of w(e))t.indexOf(r)<0&&ue.call(e,r)&&(n[r]=e[r]);return n};var pe=(e,t)=>{for(var n in t)g(e,n,{get:t[n],enumerable:!0})},A=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ge(t))!K.call(e,o)&&o!==n&&g(e,o,{get:()=>t[o],enumerable:!(r=qe(t,o))||r.enumerable});return e},x=(e,t,n)=>(A(e,t,"default"),n&&A(n,t,"default")),ce=(e,t,n)=>(n=e!=null?Ke(We(e)):{},A(t||!e||!e.__esModule?g(n,"default",{value:e,enumerable:!0}):n,e)),Be=e=>A(g({},"__esModule",{value:!0}),e);var l=(e,t,n)=>new Promise((r,o)=>{var i=p=>{try{u(n.next(p))}catch(h){o(h)}},s=p=>{try{u(n.throw(p))}catch(h){o(h)}},u=p=>p.done?r(p.value):Promise.resolve(p.value).then(i,s);u((n=n.apply(e,t)).next())});var at={};pe(at,{QuilttButton:()=>je,QuilttContainer:()=>Ue});module.exports=Be(at);var y={};pe(y,{QuilttAuthProvider:()=>oe,QuilttButton:()=>je,QuilttContainer:()=>Ue,QuilttProvider:()=>st,QuilttSettings:()=>V,QuilttSettingsProvider:()=>ie,useAuthenticateSession:()=>G,useEventListener:()=>He,useIdentifySession:()=>q,useImportSession:()=>W,useIsomorphicLayoutEffect:()=>le,useQuilttClient:()=>ot,useQuilttConnector:()=>E,useQuilttSession:()=>_,useQuilttSettings:()=>te,useRevokeSession:()=>B,useSession:()=>ee,useStorage:()=>Z});x(y,require("@quiltt/core"));var C=require("react");var P=require("react"),le=typeof window!="undefined"?P.useLayoutEffect:P.useEffect,fe=le;function He(e,t,n,r){let o=(0,C.useRef)(t);fe(()=>{o.current=t},[t]),(0,C.useEffect)(()=>{var u;let i=(u=n==null?void 0:n.current)!=null?u:window;if(!(i&&i.addEventListener))return;let s=p=>o.current(p);return i.addEventListener(e,s,r),()=>{i.removeEventListener(e,s,r)}},[e,n,r])}var me=require("react");var q=(e,t)=>(0,me.useCallback)((r,o)=>l(void 0,null,function*(){let i=yield e.identify(r),s=i;switch(i.status){case 201:if(t(i.data.token),o.onSuccess)return o.onSuccess();break;case 202:if(o.onChallenged)return o.onChallenged();break;case 422:if(o.onError)return o.onError(s.data);break;default:throw new Error(`Unexpected auth identify response status: ${i.status}`)}}),[e,t]);var de=require("react");var G=(e,t)=>(0,de.useCallback)((r,o)=>l(void 0,null,function*(){let i=yield e.authenticate(r);switch(i.status){case 201:if(t(i.data.token),o.onSuccess)return o.onSuccess();break;case 401:if(o.onFailure)return o.onFailure();break;case 422:if(o.onError)return o.onError(i.data);break;default:throw new Error(`Unexpected auth authenticate response status: ${i.status}`)}}),[e,t]);var ye=require("react");var W=(e,t,n)=>(0,ye.useCallback)(o=>l(void 0,null,function*(){if(!o)return!!t;if(t&&t.token==o)return!0;let i=yield e.ping(o);switch(i.status){case 200:return n(o),!0;break;case 401:return!1;default:throw new Error(`Unexpected auth ping response status: ${i.status}`)}}),[e,t,n]);var he=require("react");var B=(e,t,n)=>(0,he.useCallback)(()=>l(void 0,null,function*(){t&&(yield e.revoke(t.token),n(null))}),[e,t,n]);var H=function(e,t){return H=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,r){n.__proto__=r}||function(n,r){for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(n[o]=r[o])},H(e,t)};function ve(e,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");H(e,t);function n(){this.constructor=e}e.prototype=t===null?Object.create(t):(n.prototype=t.prototype,new n)}var J="Invariant Violation",Se=Object.setPrototypeOf,Je=Se===void 0?function(e,t){return e.__proto__=t,e}:Se,be=function(e){ve(t,e);function t(n){n===void 0&&(n=J);var r=e.call(this,typeof n=="number"?J+": "+n+" (see https://github.com/apollographql/invariant-packages)":n)||this;return r.framesToPop=1,r.name=J,Je(r,t.prototype),r}return t}(Error);function c(e,t){if(!e)throw new be(t)}var ge=["debug","log","warn","error","silent"],Xe=ge.indexOf("log");function R(e){return function(){if(ge.indexOf(e)>=Xe){var t=console[e]||console.log;return t.apply(console,arguments)}}}(function(e){e.debug=R("debug"),e.log=R("log"),e.warn=R("warn"),e.error=R("error")})(c||(c={}));function a(e){try{return e()}catch(t){}}var L=a(function(){return globalThis})||a(function(){return window})||a(function(){return self})||a(function(){return global})||a(function(){return a.constructor("return this")()});var xe="__",Te=[xe,xe].join("DEV");function $e(){try{return!!__DEV__}catch(e){return Object.defineProperty(L,Te,{value:a(function(){return process.env.NODE_ENV})!=="production",enumerable:!1,configurable:!0,writable:!0}),L[Te]}}var N=$e();function m(e){try{return e()}catch(t){}}var X=m(function(){return globalThis})||m(function(){return window})||m(function(){return self})||m(function(){return global})||m(function(){return m.constructor("return this")()}),$=!1;function Ye(){X&&!m(function(){return process.env.NODE_ENV})&&!m(function(){return process})&&(Object.defineProperty(X,"process",{value:{env:{NODE_ENV:"production"}},configurable:!0,enumerable:!1,writable:!0}),$=!0)}Ye();function Y(){$&&(delete X.process,$=!1)}function I(e,t){if(!!!e)throw new Error(t)}function _e(e){return D(e,[])}function D(e,t){switch(typeof e){case"string":return JSON.stringify(e);case"function":return e.name?`[function ${e.name}]`:"[function]";case"object":return ze(e,t);default:return String(e)}}function ze(e,t){if(e===null)return"null";if(t.includes(e))return"[Circular]";let n=[...t,e];if(Ze(e)){let r=e.toJSON();if(r!==e)return typeof r=="string"?r:D(r,n)}else if(Array.isArray(e))return tt(e,n);return et(e,n)}function Ze(e){return typeof e.toJSON=="function"}function et(e,t){let n=Object.entries(e);return n.length===0?"{}":t.length>2?"["+nt(e)+"]":"{ "+n.map(([o,i])=>o+": "+D(i,t)).join(", ")+" }"}function tt(e,t){if(e.length===0)return"[]";if(t.length>2)return"[Array]";let n=Math.min(10,e.length),r=e.length-n,o=[];for(let i=0;i<n;++i)o.push(D(e[i],t));return r===1?o.push("... 1 more item"):r>1&&o.push(`... ${r} more items`),"["+o.join(", ")+"]"}function nt(e){let t=Object.prototype.toString.call(e).replace(/^\[object /,"").replace(/]$/,"");if(t==="Object"&&typeof e.constructor=="function"){let n=e.constructor.name;if(typeof n=="string"&&n!=="")return n}return t}var b=class{constructor(t,n="GraphQL request",r={line:1,column:1}){typeof t=="string"||I(!1,`Body must be a string. Received: ${_e(t)}.`),this.body=t,this.name=n,this.locationOffset=r,this.locationOffset.line>0||I(!1,"line in locationOffset is 1-indexed and must be positive."),this.locationOffset.column>0||I(!1,"column in locationOffset is 1-indexed and must be positive.")}get[Symbol.toStringTag](){return"Source"}};function Ee(){return typeof b=="function"?Y():Y()}function rt(){__DEV__?c(typeof N=="boolean",N):c(typeof N=="boolean",39)}Ee();rt();var On=typeof WeakMap=="function"&&a(function(){return navigator.product})!=="ReactNative";var we=typeof Symbol=="function"&&typeof Symbol.for=="function";var Pn=typeof a(function(){return window.document.createElement})=="function",Cn=a(function(){return navigator.userAgent.indexOf("jsdom")>=0})||!1;var M=ce(require("react"),1);var Ae=we?Symbol.for("__APOLLO_CONTEXT__"):"__APOLLO_CONTEXT__";function d(){var e=M.createContext[Ae];return e||(Object.defineProperty(M.createContext,Ae,{value:e=M.createContext({}),enumerable:!1,writable:!1,configurable:!0}),e.displayName="ApolloContext"),e}var z=ce(require("react"),1);var Q=function(e){var t=e.client,n=e.children,r=d();return z.createElement(r.Consumer,null,function(o){return o===void 0&&(o={}),t&&o.client!==t&&(o=Object.assign({},o,{client:t})),__DEV__?c(o.client,'ApolloProvider was not passed a client instance. Make sure you pass in your client via the "client" prop.'):c(o.client,29),z.createElement(r.Provider,{value:o},n)})};var Oe=require("react");function Pe(e){var t=(0,Oe.useContext)(d()),n=e||t.client;return __DEV__?c(!!n,'Could not find "client" in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.'):c(!!n,32),n}var ot=Pe;var ne=require("react");var Ne=require("react");var Le=require("react"),k=require("react"),j=require("@quiltt/core");var v=require("react"),T=require("@quiltt/core"),Z=(e,t)=>{let n=(0,v.useCallback)(()=>{let s;return(s=T.GlobalStorage.get(e))!==void 0?s:t},[e,t]),[r,o]=(0,v.useState)(n()),i=(0,v.useCallback)(s=>{let u=s instanceof Function?s(r):s;r!==u&&T.GlobalStorage.set(e,u)},[e,r]);return(0,v.useEffect)(()=>(T.GlobalStorage.subscribe(e,o),o(n()),()=>T.GlobalStorage.unsubscribe(e,o)),[]),[r,i]};var Ce=j.JsonWebTokenParse,Re=new j.Timeoutable,ee=()=>{let[e,t]=Z("session"),n=(0,Le.useMemo)(()=>Ce(e),[e]);(0,k.useEffect)(()=>{if(!n)return;let o=n.claims.exp*1e3,i=()=>t(null);if(Date.now()>=o)i();else return Re.set(i,o-Date.now()),()=>Re.clear(i)},[n,t]);let r=(0,k.useCallback)(o=>{let i=o instanceof Function?o(e):o;e!==i&&(!i||Ce(i))&&t(i)},[e,t]);return[n,r]};var Ie=require("@quiltt/core");var U=require("react");var V=(0,U.createContext)({}),te=()=>{let e=(0,U.useContext)(V);return S({},e)};var _=()=>{let{clientId:e}=te(),[t,n]=ee(),r=new Ie.AuthAPI(e),o=W(r,t,n),i=q(r,n),s=G(r,n),u=B(r,t,n),p=(0,Ne.useCallback)(h=>l(void 0,null,function*(){(!h||t&&h&&h==t.token)&&n(null)}),[t,n]);return{session:t,importSession:o,identifySession:i,authenticateSession:s,revokeSession:u,forgetSession:p}};var it=process.env.QUILTT_CDN_BASE||"https://cdn.quiltt.io",f,E=()=>{let{session:e}=_();(0,ne.useEffect)(()=>{f||(f=document.createElement("script"),f.src=`${it}/v1/connector.js`,e!=null&&e.token&&f.setAttribute("quiltt-token",e.token),document.head.appendChild(f))},[]),(0,ne.useEffect)(()=>{f&&(e!=null&&e.token&&e.token!==f.getAttribute("quiltt-token")?f.setAttribute("quiltt-token",e.token):!(e!=null&&e.token)&&f.getAttribute("quiltt-token")&&f.removeAttribute("quiltt-token"))},[e==null?void 0:e.token])};var re=require("react");var F=require("@quiltt/core");var Me=require("react/jsx-runtime"),De=new F.QuilttClient({cache:new F.InMemoryCache}),oe=({resetOnSessionChange:e,token:t,children:n})=>{let{session:r,importSession:o}=_();return(0,re.useEffect)(()=>{t&&o(t)},[t]),(0,re.useEffect)(()=>{De.resetStore()},[r]),(0,Me.jsx)(Q,{client:De,children:n})};var Qe=require("react");var ke=require("react/jsx-runtime"),ie=({clientId:e,children:t})=>{let[n]=(0,Qe.useState)(e);return(0,ke.jsx)(V.Provider,{value:{clientId:n},children:t})};var se=require("react/jsx-runtime"),st=({clientId:e,token:t,children:n})=>(0,se.jsx)(ie,{clientId:e,children:(0,se.jsx)(oe,{token:t,children:n})});var Ve=require("react/jsx-runtime"),je=o=>{var i=o,{as:e,connectorId:t,connectionId:n}=i,r=O(i,["as","connectorId","connectionId"]);return E(),(0,Ve.jsx)(e||"button",S({"quiltt-button":t,"quiltt-connection":n},r))};var Fe=require("react/jsx-runtime"),Ue=o=>{var i=o,{as:e,connectorId:t,connectionId:n}=i,r=O(i,["as","connectorId","connectionId"]);return E(),(0,Fe.jsx)(e||"div",S({"quiltt-container":t,"quiltt-connection":n},r))};0&&(module.exports={QuilttButton,QuilttContainer});
1
+ "use strict";var Ke=Object.create;var g=Object.defineProperty;var qe=Object.getOwnPropertyDescriptor;var Ge=Object.getOwnPropertyNames,w=Object.getOwnPropertySymbols,We=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty,ue=Object.prototype.propertyIsEnumerable;var ae=(e,t,n)=>t in e?g(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,b=(e,t)=>{for(var n in t||(t={}))K.call(t,n)&&ae(e,n,t[n]);if(w)for(var n of w(t))ue.call(t,n)&&ae(e,n,t[n]);return e};var O=(e,t)=>{var n={};for(var r in e)K.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(e!=null&&w)for(var r of w(e))t.indexOf(r)<0&&ue.call(e,r)&&(n[r]=e[r]);return n};var pe=(e,t)=>{for(var n in t)g(e,n,{get:t[n],enumerable:!0})},A=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ge(t))!K.call(e,o)&&o!==n&&g(e,o,{get:()=>t[o],enumerable:!(r=qe(t,o))||r.enumerable});return e},x=(e,t,n)=>(A(e,t,"default"),n&&A(n,t,"default")),ce=(e,t,n)=>(n=e!=null?Ke(We(e)):{},A(t||!e||!e.__esModule?g(n,"default",{value:e,enumerable:!0}):n,e)),Be=e=>A(g({},"__esModule",{value:!0}),e);var l=(e,t,n)=>new Promise((r,o)=>{var i=p=>{try{u(n.next(p))}catch(h){o(h)}},s=p=>{try{u(n.throw(p))}catch(h){o(h)}},u=p=>p.done?r(p.value):Promise.resolve(p.value).then(i,s);u((n=n.apply(e,t)).next())});var at={};pe(at,{QuilttButton:()=>je,QuilttContainer:()=>Ue});module.exports=Be(at);var y={};pe(y,{QuilttAuthProvider:()=>oe,QuilttButton:()=>je,QuilttContainer:()=>Ue,QuilttProvider:()=>st,QuilttSettings:()=>V,QuilttSettingsProvider:()=>ie,useAuthenticateSession:()=>G,useEventListener:()=>He,useIdentifySession:()=>q,useImportSession:()=>W,useIsomorphicLayoutEffect:()=>le,useQuilttClient:()=>ot,useQuilttConnector:()=>E,useQuilttSession:()=>_,useQuilttSettings:()=>te,useRevokeSession:()=>B,useSession:()=>ee,useStorage:()=>Z});x(y,require("@quiltt/core"));var C=require("react");var P=require("react"),le=typeof window!="undefined"?P.useLayoutEffect:P.useEffect,fe=le;function He(e,t,n,r){let o=(0,C.useRef)(t);fe(()=>{o.current=t},[t]),(0,C.useEffect)(()=>{var u;let i=(u=n==null?void 0:n.current)!=null?u:window;if(!(i&&i.addEventListener))return;let s=p=>o.current(p);return i.addEventListener(e,s,r),()=>{i.removeEventListener(e,s,r)}},[e,n,r])}var me=require("react");var q=(e,t)=>(0,me.useCallback)((r,o)=>l(void 0,null,function*(){let i=yield e.identify(r),s=i;switch(i.status){case 201:if(t(i.data.token),o.onSuccess)return o.onSuccess();break;case 202:if(o.onChallenged)return o.onChallenged();break;case 422:if(o.onError)return o.onError(s.data);break;default:throw new Error(`Unexpected auth identify response status: ${i.status}`)}}),[e,t]);var de=require("react");var G=(e,t)=>(0,de.useCallback)((r,o)=>l(void 0,null,function*(){let i=yield e.authenticate(r);switch(i.status){case 201:if(t(i.data.token),o.onSuccess)return o.onSuccess();break;case 401:if(o.onFailure)return o.onFailure();break;case 422:if(o.onError)return o.onError(i.data);break;default:throw new Error(`Unexpected auth authenticate response status: ${i.status}`)}}),[e,t]);var ye=require("react");var W=(e,t,n)=>(0,ye.useCallback)(o=>l(void 0,null,function*(){if(!o)return!!t;if(t&&t.token==o)return!0;let i=yield e.ping(o);switch(i.status){case 200:return n(o),!0;break;case 401:return!1;default:throw new Error(`Unexpected auth ping response status: ${i.status}`)}}),[e,t,n]);var he=require("react");var B=(e,t,n)=>(0,he.useCallback)(()=>l(void 0,null,function*(){t&&(yield e.revoke(t.token),n(null))}),[e,t,n]);var H=function(e,t){return H=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,r){n.__proto__=r}||function(n,r){for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(n[o]=r[o])},H(e,t)};function ve(e,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");H(e,t);function n(){this.constructor=e}e.prototype=t===null?Object.create(t):(n.prototype=t.prototype,new n)}var J="Invariant Violation",be=Object.setPrototypeOf,Je=be===void 0?function(e,t){return e.__proto__=t,e}:be,Se=function(e){ve(t,e);function t(n){n===void 0&&(n=J);var r=e.call(this,typeof n=="number"?J+": "+n+" (see https://github.com/apollographql/invariant-packages)":n)||this;return r.framesToPop=1,r.name=J,Je(r,t.prototype),r}return t}(Error);function c(e,t){if(!e)throw new Se(t)}var ge=["debug","log","warn","error","silent"],Xe=ge.indexOf("log");function R(e){return function(){if(ge.indexOf(e)>=Xe){var t=console[e]||console.log;return t.apply(console,arguments)}}}(function(e){e.debug=R("debug"),e.log=R("log"),e.warn=R("warn"),e.error=R("error")})(c||(c={}));function a(e){try{return e()}catch(t){}}var L=a(function(){return globalThis})||a(function(){return window})||a(function(){return self})||a(function(){return global})||a(function(){return a.constructor("return this")()});var xe="__",Te=[xe,xe].join("DEV");function $e(){try{return!!__DEV__}catch(e){return Object.defineProperty(L,Te,{value:a(function(){return process.env.NODE_ENV})!=="production",enumerable:!1,configurable:!0,writable:!0}),L[Te]}}var N=$e();function m(e){try{return e()}catch(t){}}var X=m(function(){return globalThis})||m(function(){return window})||m(function(){return self})||m(function(){return global})||m(function(){return m.constructor("return this")()}),$=!1;function Ye(){X&&!m(function(){return process.env.NODE_ENV})&&!m(function(){return process})&&(Object.defineProperty(X,"process",{value:{env:{NODE_ENV:"production"}},configurable:!0,enumerable:!1,writable:!0}),$=!0)}Ye();function Y(){$&&(delete X.process,$=!1)}function I(e,t){if(!!!e)throw new Error(t)}function _e(e){return D(e,[])}function D(e,t){switch(typeof e){case"string":return JSON.stringify(e);case"function":return e.name?`[function ${e.name}]`:"[function]";case"object":return ze(e,t);default:return String(e)}}function ze(e,t){if(e===null)return"null";if(t.includes(e))return"[Circular]";let n=[...t,e];if(Ze(e)){let r=e.toJSON();if(r!==e)return typeof r=="string"?r:D(r,n)}else if(Array.isArray(e))return tt(e,n);return et(e,n)}function Ze(e){return typeof e.toJSON=="function"}function et(e,t){let n=Object.entries(e);return n.length===0?"{}":t.length>2?"["+nt(e)+"]":"{ "+n.map(([o,i])=>o+": "+D(i,t)).join(", ")+" }"}function tt(e,t){if(e.length===0)return"[]";if(t.length>2)return"[Array]";let n=Math.min(10,e.length),r=e.length-n,o=[];for(let i=0;i<n;++i)o.push(D(e[i],t));return r===1?o.push("... 1 more item"):r>1&&o.push(`... ${r} more items`),"["+o.join(", ")+"]"}function nt(e){let t=Object.prototype.toString.call(e).replace(/^\[object /,"").replace(/]$/,"");if(t==="Object"&&typeof e.constructor=="function"){let n=e.constructor.name;if(typeof n=="string"&&n!=="")return n}return t}var S=class{constructor(t,n="GraphQL request",r={line:1,column:1}){typeof t=="string"||I(!1,`Body must be a string. Received: ${_e(t)}.`),this.body=t,this.name=n,this.locationOffset=r,this.locationOffset.line>0||I(!1,"line in locationOffset is 1-indexed and must be positive."),this.locationOffset.column>0||I(!1,"column in locationOffset is 1-indexed and must be positive.")}get[Symbol.toStringTag](){return"Source"}};function Ee(){return typeof S=="function"?Y():Y()}function rt(){__DEV__?c(typeof N=="boolean",N):c(typeof N=="boolean",39)}Ee();rt();var On=typeof WeakMap=="function"&&a(function(){return navigator.product})!=="ReactNative";var we=typeof Symbol=="function"&&typeof Symbol.for=="function";var Pn=typeof a(function(){return window.document.createElement})=="function",Cn=a(function(){return navigator.userAgent.indexOf("jsdom")>=0})||!1;var M=ce(require("react"),1);var Ae=we?Symbol.for("__APOLLO_CONTEXT__"):"__APOLLO_CONTEXT__";function d(){var e=M.createContext[Ae];return e||(Object.defineProperty(M.createContext,Ae,{value:e=M.createContext({}),enumerable:!1,writable:!1,configurable:!0}),e.displayName="ApolloContext"),e}var z=ce(require("react"),1);var Q=function(e){var t=e.client,n=e.children,r=d();return z.createElement(r.Consumer,null,function(o){return o===void 0&&(o={}),t&&o.client!==t&&(o=Object.assign({},o,{client:t})),__DEV__?c(o.client,'ApolloProvider was not passed a client instance. Make sure you pass in your client via the "client" prop.'):c(o.client,29),z.createElement(r.Provider,{value:o},n)})};var Oe=require("react");function Pe(e){var t=(0,Oe.useContext)(d()),n=e||t.client;return __DEV__?c(!!n,'Could not find "client" in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.'):c(!!n,32),n}var ot=Pe;var ne=require("react");var Ne=require("react");var Le=require("react"),k=require("react"),j=require("@quiltt/core");var v=require("react"),T=require("@quiltt/core"),Z=(e,t)=>{let n=(0,v.useCallback)(()=>{let s;return(s=T.GlobalStorage.get(e))!==void 0?s:t},[e,t]),[r,o]=(0,v.useState)(n()),i=(0,v.useCallback)(s=>{let u=s instanceof Function?s(r):s;r!==u&&T.GlobalStorage.set(e,u)},[e,r]);return(0,v.useEffect)(()=>(T.GlobalStorage.subscribe(e,o),o(n()),()=>T.GlobalStorage.unsubscribe(e,o)),[]),[r,i]};var Ce=j.JsonWebTokenParse,Re=new j.Timeoutable,ee=()=>{let[e,t]=Z("session"),n=(0,Le.useMemo)(()=>Ce(e),[e]);(0,k.useEffect)(()=>{if(!n)return;let o=n.claims.exp*1e3,i=()=>t(null);if(Date.now()>=o)i();else return Re.set(i,o-Date.now()),()=>Re.clear(i)},[n,t]);let r=(0,k.useCallback)(o=>{let i=o instanceof Function?o(e):o;e!==i&&(!i||Ce(i))&&t(i)},[e,t]);return[n,r]};var Ie=require("@quiltt/core");var U=require("react");var V=(0,U.createContext)({}),te=()=>{let e=(0,U.useContext)(V);return b({},e)};var _=()=>{let{clientId:e}=te(),[t,n]=ee(),r=new Ie.AuthAPI(e),o=W(r,t,n),i=q(r,n),s=G(r,n),u=B(r,t,n),p=(0,Ne.useCallback)(h=>l(void 0,null,function*(){(!h||t&&h&&h==t.token)&&n(null)}),[t,n]);return{session:t,importSession:o,identifySession:i,authenticateSession:s,revokeSession:u,forgetSession:p}};var it=process.env.QUILTT_CDN_BASE||"https://cdn.quiltt.io",f,E=()=>{let{session:e}=_();(0,ne.useEffect)(()=>{f||(f=document.createElement("script"),f.src=`${it}/v1/connector.js`,e!=null&&e.token&&f.setAttribute("quiltt-token",e.token),document.head.appendChild(f))},[]),(0,ne.useEffect)(()=>{f&&(e!=null&&e.token&&e.token!==f.getAttribute("quiltt-token")?f.setAttribute("quiltt-token",e.token):!(e!=null&&e.token)&&f.getAttribute("quiltt-token")&&f.removeAttribute("quiltt-token"))},[e==null?void 0:e.token])};var re=require("react");var F=require("@quiltt/core");var Me=require("react/jsx-runtime"),De=new F.QuilttClient({cache:new F.InMemoryCache}),oe=({token:e,children:t})=>{let{session:n,importSession:r}=_();return(0,re.useEffect)(()=>{e&&r(e)},[e]),(0,re.useEffect)(()=>{De.resetStore()},[n]),(0,Me.jsx)(Q,{client:De,children:t})};var Qe=require("react");var ke=require("react/jsx-runtime"),ie=({clientId:e,children:t})=>{let[n]=(0,Qe.useState)(e);return(0,ke.jsx)(V.Provider,{value:{clientId:n},children:t})};var se=require("react/jsx-runtime"),st=({clientId:e,token:t,children:n})=>(0,se.jsx)(ie,{clientId:e,children:(0,se.jsx)(oe,{token:t,children:n})});var Ve=require("react/jsx-runtime"),je=o=>{var i=o,{as:e,connectorId:t,connectionId:n}=i,r=O(i,["as","connectorId","connectionId"]);return E(),(0,Ve.jsx)(e||"button",b({"quiltt-button":t,"quiltt-connection":n},r))};var Fe=require("react/jsx-runtime"),Ue=o=>{var i=o,{as:e,connectorId:t,connectionId:n}=i,r=O(i,["as","connectorId","connectionId"]);return E(),(0,Fe.jsx)(e||"div",b({"quiltt-container":t,"quiltt-connection":n},r))};0&&(module.exports={QuilttButton,QuilttContainer});
2
2
  //# sourceMappingURL=index.cjs.map