@noxickon/onyx 3.0.0 → 4.0.0

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.
package/AI-README.md CHANGED
@@ -69,8 +69,12 @@ import { useForm, useFilterQuery, useFormState } from '@noxickon/onyx/hooks';
69
69
  // Layouts
70
70
  import { OxAppLayout, OxSidebar, OxMainContent } from '@noxickon/onyx/layouts';
71
71
 
72
- // Route Guards
73
- import { OxProtectedRoute, OxGuestRoute, OxRoleRoute } from '@noxickon/onyx/routes';
72
+ // Route Guard Factories
73
+ import {
74
+ createOxProtectedRoute,
75
+ createOxGuestRoute,
76
+ createOxRoleRoute,
77
+ } from '@noxickon/onyx/routes';
74
78
 
75
79
  // Utilities
76
80
  import { cn, buildUrl, validateEmail } from '@noxickon/onyx/utils';
@@ -638,9 +642,7 @@ interface OxDroppableProps extends Omit<ComponentPropsWithRef<'div'>, 'id'> {
638
642
  }}
639
643
  >
640
644
  <OxDndContext.Droppable id="drop-zone">
641
- <OxDndContext.Draggable id="item-1">
642
- Drag me
643
- </OxDndContext.Draggable>
645
+ <OxDndContext.Draggable id="item-1">Drag me</OxDndContext.Draggable>
644
646
  </OxDndContext.Droppable>
645
647
  </OxDndContext>
646
648
  ```
@@ -856,9 +858,7 @@ interface OxEndlessListErrorProps extends Omit<HTMLAttributes<HTMLDivElement>, '
856
858
  onLoadMore={fetchMore}
857
859
  >
858
860
  <OxEndlessList.Items>
859
- <OxEndlessList.Item>
860
- {(item, index) => <div>{item.name}</div>}
861
- </OxEndlessList.Item>
861
+ <OxEndlessList.Item>{(item, index) => <div>{item.name}</div>}</OxEndlessList.Item>
862
862
  </OxEndlessList.Items>
863
863
  <OxEndlessList.Loader>Loading more...</OxEndlessList.Loader>
864
864
  <OxEndlessList.Empty>No items found</OxEndlessList.Empty>
@@ -1327,11 +1327,21 @@ interface OxPageOutlineItemProps extends ComponentPropsWithRef<'a'> {
1327
1327
  <OxPageOutline scrollOffset={80}>
1328
1328
  <OxPageOutline.Header>On this page</OxPageOutline.Header>
1329
1329
  <OxPageOutline.Content>
1330
- <OxPageOutline.Item href="#introduction" level={1}>Introduction</OxPageOutline.Item>
1331
- <OxPageOutline.Item href="#getting-started" level={1}>Getting Started</OxPageOutline.Item>
1332
- <OxPageOutline.Item href="#installation" level={2}>Installation</OxPageOutline.Item>
1333
- <OxPageOutline.Item href="#configuration" level={2}>Configuration</OxPageOutline.Item>
1334
- <OxPageOutline.Item href="#api" level={1}>API Reference</OxPageOutline.Item>
1330
+ <OxPageOutline.Item href="#introduction" level={1}>
1331
+ Introduction
1332
+ </OxPageOutline.Item>
1333
+ <OxPageOutline.Item href="#getting-started" level={1}>
1334
+ Getting Started
1335
+ </OxPageOutline.Item>
1336
+ <OxPageOutline.Item href="#installation" level={2}>
1337
+ Installation
1338
+ </OxPageOutline.Item>
1339
+ <OxPageOutline.Item href="#configuration" level={2}>
1340
+ Configuration
1341
+ </OxPageOutline.Item>
1342
+ <OxPageOutline.Item href="#api" level={1}>
1343
+ API Reference
1344
+ </OxPageOutline.Item>
1335
1345
  </OxPageOutline.Content>
1336
1346
  </OxPageOutline>
1337
1347
  ```
@@ -3154,61 +3164,106 @@ interface OxLegendProps extends ComponentPropsWithRef<'legend'> {
3154
3164
 
3155
3165
  ## OxAuthContext
3156
3166
 
3157
- User authentication and authorization.
3167
+ Factory-based authentication context with custom fetcher pattern (backend-agnostic).
3158
3168
 
3159
- **Exports**: `OxAuthProvider`, `useOxAuth`, `OxAuthContext`, `DEFAULT_GET_CURRENT_USER`
3169
+ **Exports**: `createOxAuth`
3160
3170
 
3161
- ### Props
3171
+ ### Factory Pattern
3162
3172
 
3163
3173
  ```tsx
3164
- interface OxAuthProviderProps {
3165
- adminRole?: string; // Default: 'ADMIN'
3166
- children: ReactNode;
3167
- getCurrentUserQuery?: DocumentNode; // Custom GraphQL query
3168
- options?: Parameters<typeof useQuery>[1]; // Apollo options
3169
- pollInterval?: number; // Default: 300000 (5 minutes)
3174
+ type OxCheckFunction<T> = (user: T | null) => boolean;
3175
+ type OxChecks<T> = Record<string, OxCheckFunction<T>>;
3176
+
3177
+ interface CreateOxAuthConfig<T, C extends OxChecks<T>> {
3178
+ checks?: C; // Custom check functions defined at factory level
3170
3179
  }
3171
3180
 
3172
- type OxUser = {
3173
- id: string;
3174
- role: string;
3175
- email?: string;
3176
- username?: string;
3177
- } | null;
3181
+ interface OxAuthProviderProps<T> {
3182
+ children: ReactNode;
3183
+ fetcher: () => Promise<T | null>; // Custom fetcher (REST, GraphQL, etc.)
3184
+ pollInterval?: number; // Default: 300000 (5 minutes), 0 to disable
3185
+ }
3178
3186
 
3179
- type OxAuthContextType = {
3180
- error: ErrorLike | null;
3181
- isAdmin: boolean; // Computed from user.role === adminRole
3187
+ type OxAuthContextValue<T, C extends OxChecks<T>> = {
3188
+ checks: { [K in keyof C]: boolean }; // Evaluated check results
3189
+ error: Error | null;
3182
3190
  isLoggedIn: boolean; // Computed from !!user
3183
3191
  loading: boolean;
3184
- refreshUser: RefetchFunction; // Apollo refetch
3185
- user: OxUser;
3192
+ refreshUser: () => Promise<void>;
3193
+ user: T | null;
3194
+ };
3195
+
3196
+ // Factory returns typed Provider and Hook
3197
+ function createOxAuth<T, C extends OxChecks<T>>(
3198
+ config: CreateOxAuthConfig<T, C>
3199
+ ): {
3200
+ OxAuthProvider: (props: OxAuthProviderProps<T>) => ReactNode;
3201
+ useOxAuth: () => OxAuthContextValue<T, C>;
3186
3202
  };
3187
3203
  ```
3188
3204
 
3189
3205
  ### Example
3190
3206
 
3191
3207
  ```tsx
3192
- // Wrap app
3193
- <OxAuthProvider adminRole="ADMIN" pollInterval={300000}>
3208
+ // 1. Define user type and create auth context (src/lib/auth.ts)
3209
+ interface MyUser {
3210
+ id: string;
3211
+ email: string;
3212
+ role: 'admin' | 'user';
3213
+ emailVerified: boolean;
3214
+ }
3215
+
3216
+ export const { OxAuthProvider, useOxAuth } = createOxAuth<MyUser>({
3217
+ checks: {
3218
+ isAdmin: (user) => user?.role === 'admin',
3219
+ isVerified: (user) => user?.emailVerified === true,
3220
+ },
3221
+ });
3222
+
3223
+ // 2. Wrap app with provider (REST example)
3224
+ import { OxAuthProvider } from '@/lib/auth';
3225
+
3226
+ <OxAuthProvider
3227
+ fetcher={async () => {
3228
+ const res = await fetch('/api/auth/me', { credentials: 'include' });
3229
+ if (!res.ok) return null;
3230
+ return res.json();
3231
+ }}
3232
+ pollInterval={300000}
3233
+ >
3194
3234
  <App />
3195
- </OxAuthProvider>;
3235
+ </OxAuthProvider>
3236
+
3237
+ // 2. Wrap app with provider (GraphQL example)
3238
+ <OxAuthProvider
3239
+ fetcher={async () => {
3240
+ const { data } = await apolloClient.query({ query: GET_ME });
3241
+ return data.me;
3242
+ }}
3243
+ >
3244
+ <App />
3245
+ </OxAuthProvider>
3246
+
3247
+ // 3. Use hook in components (no generics needed - already typed!)
3248
+ import { useOxAuth } from '@/lib/auth';
3196
3249
 
3197
- // Use in component
3198
3250
  function MyComponent() {
3199
- const { user, isLoggedIn, isAdmin, loading, refreshUser } = useOxAuth();
3251
+ const { user, isLoggedIn, checks, loading, refreshUser } = useOxAuth();
3200
3252
 
3201
3253
  if (loading) return <Spinner />;
3202
3254
  if (!isLoggedIn) return <Login />;
3255
+ if (!checks.isVerified) return <VerifyEmail />;
3203
3256
 
3204
- return <Dashboard user={user} />;
3257
+ return <Dashboard user={user} isAdmin={checks.isAdmin} />;
3205
3258
  }
3206
3259
  ```
3207
3260
 
3208
3261
  **Notes**:
3209
3262
 
3210
- - Apollo GraphQL integration with auto-polling
3211
- - Default query fetches: id, username, email, role
3263
+ - Factory pattern: checks defined once, full TypeScript inference
3264
+ - Backend-agnostic: works with REST, GraphQL, or any async fetcher
3265
+ - No generics at call site: hook is pre-typed from factory
3266
+ - No external dependencies (no Apollo required)
3212
3267
  - Throws error if hook used outside provider
3213
3268
 
3214
3269
  ---
@@ -3529,7 +3584,11 @@ function ProductList() {
3529
3584
  <option value="">All</option>
3530
3585
  <option value="electronics">Electronics</option>
3531
3586
  </select>
3532
- {isLoading ? <Spinner /> : data?.map(product => <ProductCard key={product.id} {...product} />)}
3587
+ {isLoading ? (
3588
+ <Spinner />
3589
+ ) : (
3590
+ data?.map((product) => <ProductCard key={product.id} {...product} />)
3591
+ )}
3533
3592
  </div>
3534
3593
  );
3535
3594
  }
@@ -4019,12 +4078,8 @@ function TableOfContents() {
4019
4078
 
4020
4079
  return (
4021
4080
  <nav>
4022
- {sectionIds.map(id => (
4023
- <a
4024
- key={id}
4025
- href={`#${id}`}
4026
- className={activeId === id ? 'active' : ''}
4027
- >
4081
+ {sectionIds.map((id) => (
4082
+ <a key={id} href={`#${id}`} className={activeId === id ? 'active' : ''}>
4028
4083
  {id}
4029
4084
  </a>
4030
4085
  ))}
@@ -4066,7 +4121,9 @@ function ResponsiveComponent() {
4066
4121
  return (
4067
4122
  <div ref={setRef}>
4068
4123
  {size && (
4069
- <p>Width: {size.width}px, Height: {size.height}px</p>
4124
+ <p>
4125
+ Width: {size.width}px, Height: {size.height}px
4126
+ </p>
4070
4127
  )}
4071
4128
  {size && size.width < 400 ? <CompactView /> : <FullView />}
4072
4129
  </div>
@@ -4113,11 +4170,7 @@ interface UseThemeReturn {
4113
4170
  function ThemeToggle() {
4114
4171
  const { theme, toggleTheme } = useTheme();
4115
4172
 
4116
- return (
4117
- <button onClick={toggleTheme}>
4118
- {theme === 'light' ? '🌙' : '☀️'}
4119
- </button>
4120
- );
4173
+ return <button onClick={toggleTheme}>{theme === 'light' ? '🌙' : '☀️'}</button>;
4121
4174
  }
4122
4175
 
4123
4176
  // With custom storage key
@@ -4137,9 +4190,40 @@ const { theme, setTheme } = useTheme({
4137
4190
 
4138
4191
  # Route Guards (3)
4139
4192
 
4140
- ## OxGuestRoute
4193
+ Factory functions that create typed route components bound to your auth hook.
4141
4194
 
4142
- Protects routes for unauthenticated users only (e.g., login, signup).
4195
+ ## Setup
4196
+
4197
+ ```tsx
4198
+ // src/lib/auth.ts - Create routes once with your auth hook
4199
+ import { createOxAuth } from '@noxickon/onyx/contexts';
4200
+ import {
4201
+ createOxProtectedRoute,
4202
+ createOxGuestRoute,
4203
+ createOxRoleRoute,
4204
+ } from '@noxickon/onyx/routes';
4205
+
4206
+ interface User {
4207
+ id: string;
4208
+ role: 'admin' | 'user' | 'moderator';
4209
+ }
4210
+
4211
+ const { OxAuthProvider, useOxAuth } = createOxAuth<User>({
4212
+ checks: { isAdmin: (u) => u?.role === 'admin' },
4213
+ });
4214
+
4215
+ // Create typed route components
4216
+ export const OxProtectedRoute = createOxProtectedRoute(useOxAuth);
4217
+ export const OxGuestRoute = createOxGuestRoute(useOxAuth);
4218
+ export const OxRoleRoute = createOxRoleRoute(useOxAuth);
4219
+ export { OxAuthProvider, useOxAuth };
4220
+ ```
4221
+
4222
+ ---
4223
+
4224
+ ## createOxGuestRoute
4225
+
4226
+ Factory for routes accessible only to unauthenticated users (e.g., login, signup).
4143
4227
 
4144
4228
  ### Props
4145
4229
 
@@ -4154,16 +4238,18 @@ interface OxGuestRouteProps {
4154
4238
  ### Example
4155
4239
 
4156
4240
  ```tsx
4241
+ import { OxGuestRoute } from '@/lib/auth';
4242
+
4157
4243
  <OxGuestRoute onLoading={<Spinner />} onAuthenticated={<Navigate to="/dashboard" />}>
4158
4244
  <LoginPage />
4159
- </OxGuestRoute>
4245
+ </OxGuestRoute>;
4160
4246
  ```
4161
4247
 
4162
4248
  ---
4163
4249
 
4164
- ## OxProtectedRoute
4250
+ ## createOxProtectedRoute
4165
4251
 
4166
- Protects routes requiring authentication.
4252
+ Factory for routes requiring authentication.
4167
4253
 
4168
4254
  ### Props
4169
4255
 
@@ -4178,24 +4264,27 @@ interface OxProtectedRouteProps {
4178
4264
  ### Example
4179
4265
 
4180
4266
  ```tsx
4267
+ import { OxProtectedRoute } from '@/lib/auth';
4268
+
4181
4269
  <OxProtectedRoute onLoading={<Spinner />} onUnauthenticated={<Navigate to="/login" />}>
4182
4270
  <Dashboard />
4183
- </OxProtectedRoute>
4271
+ </OxProtectedRoute>;
4184
4272
  ```
4185
4273
 
4186
4274
  ---
4187
4275
 
4188
- ## OxRoleRoute
4276
+ ## createOxRoleRoute
4189
4277
 
4190
- Protects routes requiring specific user roles.
4278
+ Factory for routes requiring specific user roles.
4191
4279
 
4192
4280
  ### Props
4193
4281
 
4194
4282
  ```tsx
4195
- interface OxRoleRouteProps {
4283
+ interface OxRoleRouteProps<T extends { role: string }> {
4196
4284
  children: ReactNode; // Required - Shown when user has role
4197
4285
  onLoading?: ReactNode; // Fallback during auth check
4198
- onUnauthorized?: ReactNode; // Fallback when unauthorized or not authenticated
4286
+ onUnauthorized?: ReactNode; // Fallback when unauthorized
4287
+ roleExtractor?: (user: T) => string; // Default: (u) => u.role
4199
4288
  roles?: string[]; // Default: ['admin'] - Allowed roles (OR logic)
4200
4289
  }
4201
4290
  ```
@@ -4203,10 +4292,12 @@ interface OxRoleRouteProps {
4203
4292
  ### Example
4204
4293
 
4205
4294
  ```tsx
4295
+ import { OxRoleRoute } from '@/lib/auth';
4296
+
4206
4297
  {
4207
- /* Admin only */
4298
+ /* Admin only (default) */
4208
4299
  }
4209
- <OxRoleRoute>
4300
+ <OxRoleRoute onUnauthorized={<AccessDenied />}>
4210
4301
  <AdminPanel />
4211
4302
  </OxRoleRoute>;
4212
4303
 
@@ -4220,9 +4311,12 @@ interface OxRoleRouteProps {
4220
4311
 
4221
4312
  **Notes**:
4222
4313
 
4314
+ - Factory pattern: no prop drilling, hook called internally
4315
+ - User type must have a `role` property (TypeScript enforced)
4223
4316
  - User must have at least ONE of the specified roles
4224
4317
  - Empty roles array always shows `onUnauthorized`
4225
4318
  - Default role: `'admin'`
4319
+ - Custom `roleExtractor` for nested role properties
4226
4320
 
4227
4321
  ---
4228
4322
 
@@ -4766,9 +4860,12 @@ const { data, validate } = useFormState({
4766
4860
 
4767
4861
  ## Route Protection
4768
4862
 
4769
- Layer route guards for complex authorization:
4863
+ Layer route guards for complex authorization (routes created via factory pattern):
4770
4864
 
4771
4865
  ```tsx
4866
+ // Import from your auth setup file
4867
+ import { OxProtectedRoute, OxGuestRoute, OxRoleRoute } from '@/lib/auth';
4868
+
4772
4869
  {
4773
4870
  /* Guest route wrapping protected content */
4774
4871
  }
@@ -4843,13 +4940,8 @@ interface OxFormState {
4843
4940
  setError: (field: string, error: string | string[]) => void;
4844
4941
  }
4845
4942
 
4846
- // User type (AuthContext)
4847
- type OxUser = {
4848
- id: string;
4849
- role: string;
4850
- email?: string;
4851
- username?: string;
4852
- } | null;
4943
+ // AuthContext uses generics - define your own user type
4944
+ // See OxAuthContext section for examples
4853
4945
  ```
4854
4946
 
4855
4947
  ---
@@ -1,5 +1,2 @@
1
1
  export type * from './src/AuthContext.types';
2
- export { DEFAULT_GET_CURRENT_USER } from './src/query';
3
- export { OxAuthProvider } from './src/AuthProvider';
4
- export { OxAuthContext } from './src/AuthContext';
5
- export { useOxAuth } from './src/useAuth';
2
+ export { createOxAuth } from './src/createOxAuth';
@@ -1,27 +1,33 @@
1
1
  import { ReactNode } from 'react';
2
- import { DocumentNode, ErrorLike } from '@apollo/client';
3
- import { useQuery } from '@apollo/client/react';
4
- export type OxUser = {
5
- email?: string;
6
- id: string;
7
- role: string;
8
- username?: string;
9
- } | null;
10
- export type OxGetCurrentUserResponse = {
11
- me: OxUser;
12
- };
13
- export interface OxAuthProviderProps {
14
- adminRole?: string;
2
+ export type OxCheckFunction<T> = (user: T | null) => boolean;
3
+ export type OxChecks<T> = Record<string, OxCheckFunction<T>>;
4
+ export interface OxAuthContextValue<T, C extends OxChecks<T> = OxChecks<T>> {
5
+ checks: {
6
+ [K in keyof C]: boolean;
7
+ };
8
+ error: Error | null;
9
+ isLoggedIn: boolean;
10
+ loading: boolean;
11
+ refreshUser: () => Promise<void>;
12
+ user: T | null;
13
+ }
14
+ export interface OxAuthProviderProps<T> {
15
15
  children: ReactNode;
16
- getCurrentUserQuery?: DocumentNode;
17
- options?: Omit<Parameters<typeof useQuery>[1], 'pollInterval'>;
16
+ fetcher: () => Promise<T | null>;
18
17
  pollInterval?: number;
19
18
  }
20
- export type OxAuthContextType = {
21
- error: ErrorLike | null;
22
- isAdmin: boolean;
19
+ export interface CreateOxAuthConfig<T, C extends OxChecks<T>> {
20
+ checks?: C;
21
+ }
22
+ export interface CreateOxAuthReturn<T, C extends OxChecks<T>> {
23
+ OxAuthProvider: (props: OxAuthProviderProps<T>) => ReactNode;
24
+ useOxAuth: () => OxAuthContextValue<T, C>;
25
+ }
26
+ export interface OxAuthState<T = unknown> {
27
+ error: Error | null;
23
28
  isLoggedIn: boolean;
24
29
  loading: boolean;
25
- refreshUser: ReturnType<typeof useQuery<OxGetCurrentUserResponse>>['refetch'];
26
- user: OxUser;
27
- };
30
+ refreshUser: () => Promise<void>;
31
+ user: T | null;
32
+ }
33
+ export type UseAuthHook<T = unknown> = () => OxAuthState<T>;
@@ -0,0 +1,5 @@
1
+ import { CreateOxAuthConfig, OxAuthContextValue, OxAuthProviderProps, OxChecks } from './AuthContext.types';
2
+ export declare function createOxAuth<T, C extends OxChecks<T> = OxChecks<T>>(config?: CreateOxAuthConfig<T, C>): {
3
+ OxAuthProvider: ({ children, fetcher, pollInterval, }: OxAuthProviderProps<T>) => import("react/jsx-runtime").JSX.Element;
4
+ useOxAuth: () => OxAuthContextValue<T, C>;
5
+ };
@@ -1,14 +1,5 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("../chunks/ui-B0Ma5GdK.js"),C=require("@apollo/client"),g=require("react/jsx-runtime"),T=require("../chunks/useAuth-DxoDe3YA.js"),m=require("../chunks/hooks-CNgb50x1.js"),O=require("react"),w=C.gql`
2
- query getCurrentUser {
3
- me {
4
- id
5
- username
6
- email
7
- role
8
- }
9
- }
10
- `;function L({adminRole:t="ADMIN",children:e,getCurrentUserQuery:a=w,options:l={},pollInterval:f=300*1e3}){const{data:u,error:r,loading:d,refetch:o}=m.useQuery(a,{fetchPolicy:"cache-and-network",nextFetchPolicy:"cache-first",errorPolicy:"all",pollInterval:f,...l}),s=!!u?.me,c=u?.me?.role===t;return g.jsx(T.OxAuthContext.Provider,{value:{user:u?.me||null,loading:d,error:r??null,isLoggedIn:s,isAdmin:c,refreshUser:o},children:e})}const R=C.gql`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A=require("../chunks/ui-B0Ma5GdK.js"),y=require("react/jsx-runtime"),s=require("react"),j=require("@apollo/client"),w=require("../chunks/hooks-CNgb50x1.js");function T(r={}){const{checks:e={}}=r,d=s.createContext(void 0);function l({children:a,fetcher:o,pollInterval:f=300*1e3}){const[n,i]=s.useState(null),[u,_]=s.useState(!0),[v,p]=s.useState(null),O=s.useRef(null),D=s.useRef(!0),E=s.useRef(o);E.current=o;const m=s.useCallback(async()=>{try{const t=await E.current();D.current&&(i(t),p(null))}catch(t){D.current&&(p(t instanceof Error?t:new Error("Unknown error")),i(null))}finally{D.current&&_(!1)}},[]);s.useEffect(()=>(D.current=!0,m(),f>0&&(O.current=setInterval(m,f)),()=>{D.current=!1,O.current&&clearInterval(O.current)}),[m,f]);const h=s.useMemo(()=>{const t={};for(const[g,b]of Object.entries(e))t[g]=b(n);return t},[n]),F=!!n,c=s.useMemo(()=>({checks:h,error:v,isLoggedIn:F,loading:u,refreshUser:m,user:n}),[h,v,F,u,m,n]);return y.jsx(d.Provider,{value:c,children:a})}function x(){const a=s.useContext(d);if(!a)throw new Error("useOxAuth must be used within an OxAuthProvider");return a}return{OxAuthProvider:l,useOxAuth:x}}const k=j.gql`
11
2
  query getFeatures {
12
3
  features
13
4
  }
14
- `,U=O.createContext(void 0);function k({children:t,enableDevMode:e=!1,getFeaturesQuery:a=R,options:l={},pollInterval:f=300*1e3}){const{data:u,error:r,loading:d,refetch:o}=m.useQuery(a,{fetchPolicy:"cache-and-network",nextFetchPolicy:"cache-first",errorPolicy:"all",pollInterval:f,skip:e,...l}),s=u?.features||{},c=_=>e?!0:s[_]??!1;return g.jsx(U.Provider,{value:{features:s,isEnabled:c,loading:d,error:r??null,refreshFeatures:o,enableDevMode:e},children:t})}function q(){const t=O.useContext(U);if(!t)throw new Error("useOxFeature must be used within an OxFeatureProvider");return t}const N=t=>{const e=m.compilerRuntimeExports.c(8),{children:a,className:l,fallback:f,name:u}=t,r=f===void 0?null:f,{isEnabled:d}=q();if(!d(u)){let c;return e[0]!==l||e[1]!==r?(c=r?g.jsx("div",{className:b.cn("contents",l),children:r}):null,e[0]=l,e[1]=r,e[2]=c):c=e[2],c}let o;e[3]!==l?(o=b.cn("contents",l),e[3]=l,e[4]=o):o=e[4];let s;return e[5]!==a||e[6]!==o?(s=g.jsx("div",{className:o,children:a}),e[5]=a,e[6]=o,e[7]=s):s=e[7],s},S=O.createContext(null),G=t=>{const e=m.compilerRuntimeExports.c(17),{children:a,maxDialogs:l}=t,f=l===void 0?3:l;let u;e[0]===Symbol.for("react.memo_cache_sentinel")?(u=[],e[0]=u):u=e[0];const[r,d]=O.useState(u);let o,s;e[1]!==r?(o=()=>{const n=r.some(I);return document.body.classList.toggle("overflow-hidden",n),M},s=[r],e[1]=r,e[2]=o,e[3]=s):(o=e[2],s=e[3]),O.useEffect(o,s);let c;e[4]===Symbol.for("react.memo_cache_sentinel")?(c=()=>{let n=null;d(i=>{const h=i.filter(Q),p=m.head(h);return p?(n=p.id,i.map(E=>E.id===p.id?{...E,isOpen:!1}:E)):i}),n&&setTimeout(()=>{d(i=>m.reject(i,h=>h.id===n))},200)},e[4]=c):c=e[4];const _=c;let D;e[5]!==f?(D=(n,i)=>{const h=m.uniqueId(),p={id:h,content:n,isOpen:!0,options:i};return d(E=>[p,...E].slice(0,f)),h},e[5]=f,e[6]=D):D=e[6];const v=D;let F;e[7]===Symbol.for("react.memo_cache_sentinel")?(F=()=>{d(z),setTimeout(()=>{d([])},200)},e[7]=F):F=e[7];const j=F;let y;e[8]!==v?(y=Object.assign((n,i)=>v(n,i),{open:v,dismiss:_,dismissAll:j}),e[8]=v,e[9]=y):y=e[9];const P=y;let x;if(e[10]!==r){let n;e[12]===Symbol.for("react.memo_cache_sentinel")?(n=i=>g.jsx(b.OxDialog,{isOpen:i.isOpen,onClose:_,...i.options,children:i.content},i.id),e[12]=n):n=e[12],x=r.map(n),e[10]=r,e[11]=x}else x=e[11];let A;return e[13]!==a||e[14]!==P||e[15]!==x?(A=g.jsxs(S.Provider,{value:P,children:[a,x]}),e[13]=a,e[14]=P,e[15]=x,e[16]=A):A=e[16],A};function I(t){return t.isOpen}function M(){document.body.classList.remove("overflow-hidden")}function Q(t){return t.isOpen}function $(t){return{...t,isOpen:!1}}function z(t){return t.map($)}const B=()=>{const t=O.useContext(S);if(!t)throw new Error("useOxDialog must be used within a DialogProvider");return t};exports.OxAuthContext=T.OxAuthContext;exports.useOxAuth=T.useOxAuth;exports.DEFAULT_GET_CURRENT_USER=w;exports.DEFAULT_GET_FEATURES=R;exports.OxAuthProvider=L;exports.OxDialogProvider=G;exports.OxFeatureFlag=N;exports.OxFeatureProvider=k;exports.useOxDialog=B;exports.useOxFeature=q;
5
+ `,C=s.createContext(void 0);function q({children:r,enableDevMode:e=!1,getFeaturesQuery:d=k,options:l={},pollInterval:x=300*1e3}){const{data:a,error:o,loading:f,refetch:n}=w.useQuery(d,{fetchPolicy:"cache-and-network",nextFetchPolicy:"cache-first",errorPolicy:"all",pollInterval:x,skip:e,...l}),i=a?.features||{},u=_=>e?!0:i[_]??!1;return y.jsx(C.Provider,{value:{features:i,isEnabled:u,loading:f,error:o??null,refreshFeatures:n,enableDevMode:e},children:r})}function R(){const r=s.useContext(C);if(!r)throw new Error("useOxFeature must be used within an OxFeatureProvider");return r}const U=r=>{const e=w.compilerRuntimeExports.c(8),{children:d,className:l,fallback:x,name:a}=r,o=x===void 0?null:x,{isEnabled:f}=R();if(!f(a)){let u;return e[0]!==l||e[1]!==o?(u=o?y.jsx("div",{className:A.cn("contents",l),children:o}):null,e[0]=l,e[1]=o,e[2]=u):u=e[2],u}let n;e[3]!==l?(n=A.cn("contents",l),e[3]=l,e[4]=n):n=e[4];let i;return e[5]!==d||e[6]!==n?(i=y.jsx("div",{className:n,children:d}),e[5]=d,e[6]=n,e[7]=i):i=e[7],i},S=s.createContext(null),L=r=>{const e=w.compilerRuntimeExports.c(17),{children:d,maxDialogs:l}=r,x=l===void 0?3:l;let a;e[0]===Symbol.for("react.memo_cache_sentinel")?(a=[],e[0]=a):a=e[0];const[o,f]=s.useState(a);let n,i;e[1]!==o?(n=()=>{const c=o.some(M);return document.body.classList.toggle("overflow-hidden",c),I},i=[o],e[1]=o,e[2]=n,e[3]=i):(n=e[2],i=e[3]),s.useEffect(n,i);let u;e[4]===Symbol.for("react.memo_cache_sentinel")?(u=()=>{let c=null;f(t=>{const g=t.filter(N),b=w.head(g);return b?(c=b.id,t.map(P=>P.id===b.id?{...P,isOpen:!1}:P)):t}),c&&setTimeout(()=>{f(t=>w.reject(t,g=>g.id===c))},200)},e[4]=u):u=e[4];const _=u;let v;e[5]!==x?(v=(c,t)=>{const g=w.uniqueId(),b={id:g,content:c,isOpen:!0,options:t};return f(P=>[b,...P].slice(0,x)),g},e[5]=x,e[6]=v):v=e[6];const p=v;let O;e[7]===Symbol.for("react.memo_cache_sentinel")?(O=()=>{f($),setTimeout(()=>{f([])},200)},e[7]=O):O=e[7];const D=O;let E;e[8]!==p?(E=Object.assign((c,t)=>p(c,t),{open:p,dismiss:_,dismissAll:D}),e[8]=p,e[9]=E):E=e[9];const m=E;let h;if(e[10]!==o){let c;e[12]===Symbol.for("react.memo_cache_sentinel")?(c=t=>y.jsx(A.OxDialog,{isOpen:t.isOpen,onClose:_,...t.options,children:t.content},t.id),e[12]=c):c=e[12],h=o.map(c),e[10]=o,e[11]=h}else h=e[11];let F;return e[13]!==d||e[14]!==m||e[15]!==h?(F=y.jsxs(S.Provider,{value:m,children:[d,h]}),e[13]=d,e[14]=m,e[15]=h,e[16]=F):F=e[16],F};function M(r){return r.isOpen}function I(){document.body.classList.remove("overflow-hidden")}function N(r){return r.isOpen}function G(r){return{...r,isOpen:!1}}function $(r){return r.map(G)}const Q=()=>{const r=s.useContext(S);if(!r)throw new Error("useOxDialog must be used within a DialogProvider");return r};exports.DEFAULT_GET_FEATURES=k;exports.OxDialogProvider=L;exports.OxFeatureFlag=U;exports.OxFeatureProvider=q;exports.createOxAuth=T;exports.useOxDialog=Q;exports.useOxFeature=R;
@@ -1,202 +1,209 @@
1
- import { af as P, y as L } from "../chunks/ui-DrKTwQ0M.js";
2
- import { gql as b } from "@apollo/client";
3
- import { jsx as g, jsxs as N } from "react/jsx-runtime";
4
- import { O as R } from "../chunks/useAuth-BFgXzu6Q.js";
5
- import { u as ue } from "../chunks/useAuth-BFgXzu6Q.js";
6
- import { B as w, d as A, C as j, w as q, y as I } from "../chunks/hooks-6tQOArBm.js";
7
- import { createContext as C, useContext as T, useState as G, useEffect as $ } from "react";
8
- const B = b`
9
- query getCurrentUser {
10
- me {
11
- id
12
- username
13
- email
14
- role
15
- }
16
- }
17
- `;
18
- function se({
19
- adminRole: t = "ADMIN",
20
- children: e,
21
- getCurrentUserQuery: u = B,
22
- options: l = {},
23
- pollInterval: m = 300 * 1e3
24
- }) {
1
+ import { af as C, y as T } from "../chunks/ui-DrKTwQ0M.js";
2
+ import { jsx as E, jsxs as q } from "react/jsx-runtime";
3
+ import { createContext as k, useContext as A, useState as F, useRef as P, useCallback as I, useEffect as S, useMemo as R } from "react";
4
+ import { gql as N } from "@apollo/client";
5
+ import { B as M, d as U, C as $, w as B, y as G } from "../chunks/hooks-6tQOArBm.js";
6
+ function ne(r = {}) {
25
7
  const {
26
- data: a,
27
- error: o,
28
- loading: f,
29
- refetch: s
30
- } = w(u, {
31
- fetchPolicy: "cache-and-network",
32
- nextFetchPolicy: "cache-first",
33
- errorPolicy: "all",
34
- pollInterval: m,
35
- ...l
36
- }), r = !!a?.me, c = a?.me?.role === t;
37
- return /* @__PURE__ */ g(R.Provider, { value: {
38
- user: a?.me || null,
39
- loading: f,
40
- error: o ?? null,
41
- isLoggedIn: r,
42
- isAdmin: c,
43
- refreshUser: s
44
- }, children: e });
8
+ checks: e = {}
9
+ } = r, f = k(void 0);
10
+ function l({
11
+ children: u,
12
+ fetcher: s,
13
+ pollInterval: a = 300 * 1e3
14
+ }) {
15
+ const [n, i] = F(null), [c, b] = F(!0), [O, p] = F(null), x = P(null), v = P(!0), w = P(s);
16
+ w.current = s;
17
+ const m = I(async () => {
18
+ try {
19
+ const t = await w.current();
20
+ v.current && (i(t), p(null));
21
+ } catch (t) {
22
+ v.current && (p(t instanceof Error ? t : new Error("Unknown error")), i(null));
23
+ } finally {
24
+ v.current && b(!1);
25
+ }
26
+ }, []);
27
+ S(() => (v.current = !0, m(), a > 0 && (x.current = setInterval(m, a)), () => {
28
+ v.current = !1, x.current && clearInterval(x.current);
29
+ }), [m, a]);
30
+ const h = R(() => {
31
+ const t = {};
32
+ for (const [g, D] of Object.entries(e))
33
+ t[g] = D(n);
34
+ return t;
35
+ }, [n]), y = !!n, o = R(() => ({
36
+ checks: h,
37
+ error: O,
38
+ isLoggedIn: y,
39
+ loading: c,
40
+ refreshUser: m,
41
+ user: n
42
+ }), [h, O, y, c, m, n]);
43
+ return /* @__PURE__ */ E(f.Provider, { value: o, children: u });
44
+ }
45
+ function d() {
46
+ const u = A(f);
47
+ if (!u)
48
+ throw new Error("useOxAuth must be used within an OxAuthProvider");
49
+ return u;
50
+ }
51
+ return {
52
+ OxAuthProvider: l,
53
+ useOxAuth: d
54
+ };
45
55
  }
46
- const M = b`
56
+ const Q = N`
47
57
  query getFeatures {
48
58
  features
49
59
  }
50
- `, S = C(void 0);
51
- function re({
52
- children: t,
60
+ `, j = k(void 0);
61
+ function se({
62
+ children: r,
53
63
  enableDevMode: e = !1,
54
- getFeaturesQuery: u = M,
64
+ getFeaturesQuery: f = Q,
55
65
  options: l = {},
56
- pollInterval: m = 300 * 1e3
66
+ pollInterval: d = 300 * 1e3
57
67
  }) {
58
68
  const {
59
- data: a,
60
- error: o,
61
- loading: f,
62
- refetch: s
63
- } = w(u, {
69
+ data: u,
70
+ error: s,
71
+ loading: a,
72
+ refetch: n
73
+ } = M(f, {
64
74
  fetchPolicy: "cache-and-network",
65
75
  nextFetchPolicy: "cache-first",
66
76
  errorPolicy: "all",
67
- pollInterval: m,
77
+ pollInterval: d,
68
78
  skip: e,
69
79
  ...l
70
- }), r = a?.features || {}, c = (O) => e ? !0 : r[O] ?? !1;
71
- return /* @__PURE__ */ g(S.Provider, { value: {
72
- features: r,
80
+ }), i = u?.features || {}, c = (b) => e ? !0 : i[b] ?? !1;
81
+ return /* @__PURE__ */ E(j.Provider, { value: {
82
+ features: i,
73
83
  isEnabled: c,
74
- loading: f,
75
- error: o ?? null,
76
- refreshFeatures: s,
84
+ loading: a,
85
+ error: s ?? null,
86
+ refreshFeatures: n,
77
87
  enableDevMode: e
78
- }, children: t });
88
+ }, children: r });
79
89
  }
80
- function Q() {
81
- const t = T(S);
82
- if (!t)
90
+ function z() {
91
+ const r = A(j);
92
+ if (!r)
83
93
  throw new Error("useOxFeature must be used within an OxFeatureProvider");
84
- return t;
94
+ return r;
85
95
  }
86
- const ne = (t) => {
87
- const e = A.c(8), {
88
- children: u,
96
+ const oe = (r) => {
97
+ const e = U.c(8), {
98
+ children: f,
89
99
  className: l,
90
- fallback: m,
91
- name: a
92
- } = t, o = m === void 0 ? null : m, {
93
- isEnabled: f
94
- } = Q();
95
- if (!f(a)) {
100
+ fallback: d,
101
+ name: u
102
+ } = r, s = d === void 0 ? null : d, {
103
+ isEnabled: a
104
+ } = z();
105
+ if (!a(u)) {
96
106
  let c;
97
- return e[0] !== l || e[1] !== o ? (c = o ? /* @__PURE__ */ g("div", { className: P("contents", l), children: o }) : null, e[0] = l, e[1] = o, e[2] = c) : c = e[2], c;
107
+ return e[0] !== l || e[1] !== s ? (c = s ? /* @__PURE__ */ E("div", { className: C("contents", l), children: s }) : null, e[0] = l, e[1] = s, e[2] = c) : c = e[2], c;
98
108
  }
99
- let s;
100
- e[3] !== l ? (s = P("contents", l), e[3] = l, e[4] = s) : s = e[4];
101
- let r;
102
- return e[5] !== u || e[6] !== s ? (r = /* @__PURE__ */ g("div", { className: s, children: u }), e[5] = u, e[6] = s, e[7] = r) : r = e[7], r;
103
- }, U = C(null), ie = (t) => {
104
- const e = A.c(17), {
105
- children: u,
109
+ let n;
110
+ e[3] !== l ? (n = C("contents", l), e[3] = l, e[4] = n) : n = e[4];
111
+ let i;
112
+ return e[5] !== f || e[6] !== n ? (i = /* @__PURE__ */ E("div", { className: n, children: f }), e[5] = f, e[6] = n, e[7] = i) : i = e[7], i;
113
+ }, L = k(null), ie = (r) => {
114
+ const e = U.c(17), {
115
+ children: f,
106
116
  maxDialogs: l
107
- } = t, m = l === void 0 ? 3 : l;
108
- let a;
109
- e[0] === Symbol.for("react.memo_cache_sentinel") ? (a = [], e[0] = a) : a = e[0];
110
- const [o, f] = G(a);
111
- let s, r;
112
- e[1] !== o ? (s = () => {
113
- const n = o.some(z);
114
- return document.body.classList.toggle("overflow-hidden", n), H;
115
- }, r = [o], e[1] = o, e[2] = s, e[3] = r) : (s = e[2], r = e[3]), $(s, r);
117
+ } = r, d = l === void 0 ? 3 : l;
118
+ let u;
119
+ e[0] === Symbol.for("react.memo_cache_sentinel") ? (u = [], e[0] = u) : u = e[0];
120
+ const [s, a] = F(u);
121
+ let n, i;
122
+ e[1] !== s ? (n = () => {
123
+ const o = s.some(H);
124
+ return document.body.classList.toggle("overflow-hidden", o), J;
125
+ }, i = [s], e[1] = s, e[2] = n, e[3] = i) : (n = e[2], i = e[3]), S(n, i);
116
126
  let c;
117
127
  e[4] === Symbol.for("react.memo_cache_sentinel") ? (c = () => {
118
- let n = null;
119
- f((i) => {
120
- const h = i.filter(J), x = j(h);
121
- return x ? (n = x.id, i.map((p) => p.id === x.id ? {
122
- ...p,
128
+ let o = null;
129
+ a((t) => {
130
+ const g = t.filter(K), D = $(g);
131
+ return D ? (o = D.id, t.map((_) => _.id === D.id ? {
132
+ ..._,
123
133
  isOpen: !1
124
- } : p)) : i;
125
- }), n && setTimeout(() => {
126
- f((i) => q(i, (h) => h.id === n));
134
+ } : _)) : t;
135
+ }), o && setTimeout(() => {
136
+ a((t) => B(t, (g) => g.id === o));
127
137
  }, 200);
128
138
  }, e[4] = c) : c = e[4];
129
- const O = c;
130
- let D;
131
- e[5] !== m ? (D = (n, i) => {
132
- const h = I(), x = {
133
- id: h,
134
- content: n,
139
+ const b = c;
140
+ let O;
141
+ e[5] !== d ? (O = (o, t) => {
142
+ const g = G(), D = {
143
+ id: g,
144
+ content: o,
135
145
  isOpen: !0,
136
- options: i
146
+ options: t
137
147
  };
138
- return f((p) => [x, ...p].slice(0, m)), h;
139
- }, e[5] = m, e[6] = D) : D = e[6];
140
- const _ = D;
141
- let v;
142
- e[7] === Symbol.for("react.memo_cache_sentinel") ? (v = () => {
143
- f(V), setTimeout(() => {
144
- f([]);
148
+ return a((_) => [D, ..._].slice(0, d)), g;
149
+ }, e[5] = d, e[6] = O) : O = e[6];
150
+ const p = O;
151
+ let x;
152
+ e[7] === Symbol.for("react.memo_cache_sentinel") ? (x = () => {
153
+ a(W), setTimeout(() => {
154
+ a([]);
145
155
  }, 200);
146
- }, e[7] = v) : v = e[7];
147
- const k = v;
148
- let y;
149
- e[8] !== _ ? (y = Object.assign((n, i) => _(n, i), {
150
- open: _,
151
- dismiss: O,
152
- dismissAll: k
153
- }), e[8] = _, e[9] = y) : y = e[9];
154
- const F = y;
155
- let d;
156
- if (e[10] !== o) {
157
- let n;
158
- e[12] === Symbol.for("react.memo_cache_sentinel") ? (n = (i) => /* @__PURE__ */ g(L, { isOpen: i.isOpen, onClose: O, ...i.options, children: i.content }, i.id), e[12] = n) : n = e[12], d = o.map(n), e[10] = o, e[11] = d;
156
+ }, e[7] = x) : x = e[7];
157
+ const v = x;
158
+ let w;
159
+ e[8] !== p ? (w = Object.assign((o, t) => p(o, t), {
160
+ open: p,
161
+ dismiss: b,
162
+ dismissAll: v
163
+ }), e[8] = p, e[9] = w) : w = e[9];
164
+ const m = w;
165
+ let h;
166
+ if (e[10] !== s) {
167
+ let o;
168
+ e[12] === Symbol.for("react.memo_cache_sentinel") ? (o = (t) => /* @__PURE__ */ E(T, { isOpen: t.isOpen, onClose: b, ...t.options, children: t.content }, t.id), e[12] = o) : o = e[12], h = s.map(o), e[10] = s, e[11] = h;
159
169
  } else
160
- d = e[11];
161
- let E;
162
- return e[13] !== u || e[14] !== F || e[15] !== d ? (E = /* @__PURE__ */ N(U.Provider, { value: F, children: [
163
- u,
164
- d
165
- ] }), e[13] = u, e[14] = F, e[15] = d, e[16] = E) : E = e[16], E;
170
+ h = e[11];
171
+ let y;
172
+ return e[13] !== f || e[14] !== m || e[15] !== h ? (y = /* @__PURE__ */ q(L.Provider, { value: m, children: [
173
+ f,
174
+ h
175
+ ] }), e[13] = f, e[14] = m, e[15] = h, e[16] = y) : y = e[16], y;
166
176
  };
167
- function z(t) {
168
- return t.isOpen;
177
+ function H(r) {
178
+ return r.isOpen;
169
179
  }
170
- function H() {
180
+ function J() {
171
181
  document.body.classList.remove("overflow-hidden");
172
182
  }
173
- function J(t) {
174
- return t.isOpen;
183
+ function K(r) {
184
+ return r.isOpen;
175
185
  }
176
- function K(t) {
186
+ function V(r) {
177
187
  return {
178
- ...t,
188
+ ...r,
179
189
  isOpen: !1
180
190
  };
181
191
  }
182
- function V(t) {
183
- return t.map(K);
192
+ function W(r) {
193
+ return r.map(V);
184
194
  }
185
- const le = () => {
186
- const t = T(U);
187
- if (!t)
195
+ const ce = () => {
196
+ const r = A(L);
197
+ if (!r)
188
198
  throw new Error("useOxDialog must be used within a DialogProvider");
189
- return t;
199
+ return r;
190
200
  };
191
201
  export {
192
- B as DEFAULT_GET_CURRENT_USER,
193
- M as DEFAULT_GET_FEATURES,
194
- R as OxAuthContext,
195
- se as OxAuthProvider,
202
+ Q as DEFAULT_GET_FEATURES,
196
203
  ie as OxDialogProvider,
197
- ne as OxFeatureFlag,
198
- re as OxFeatureProvider,
199
- ue as useOxAuth,
200
- le as useOxDialog,
201
- Q as useOxFeature
204
+ oe as OxFeatureFlag,
205
+ se as OxFeatureProvider,
206
+ ne as createOxAuth,
207
+ ce as useOxDialog,
208
+ z as useOxFeature
202
209
  };
@@ -1,2 +1,2 @@
1
- export type * from './src/GuestRoute.types';
2
- export { OxGuestRoute } from './src/GuestRoute';
1
+ export { createOxGuestRoute } from './src/createOxGuestRoute';
2
+ export * from './src/GuestRoute.types';
@@ -0,0 +1,4 @@
1
+ import { ReactNode } from 'react';
2
+ import { OxAuthState } from '../../../contexts';
3
+ import { OxGuestRouteProps } from './GuestRoute.types';
4
+ export declare function createOxGuestRoute<T>(useAuth: () => OxAuthState<T>): ({ children, onAuthenticated, onLoading, }: OxGuestRouteProps) => ReactNode;
@@ -1,2 +1,2 @@
1
- export type { OxProtectedRouteProps } from './src/ProtectedRoute.types';
2
- export { OxProtectedRoute } from './src/ProtectedRoute';
1
+ export { createOxProtectedRoute } from './src/createOxProtectedRoute';
2
+ export * from './src/ProtectedRoute.types';
@@ -0,0 +1,4 @@
1
+ import { ReactNode } from 'react';
2
+ import { OxAuthState } from '../../../contexts';
3
+ import { OxProtectedRouteProps } from './ProtectedRoute.types';
4
+ export declare function createOxProtectedRoute<T>(useAuth: () => OxAuthState<T>): ({ children, onLoading, onUnauthenticated, }: OxProtectedRouteProps) => ReactNode;
@@ -1,2 +1,2 @@
1
- export type { OxRoleRouteProps } from './src/RoleRoute.types';
2
- export { OxRoleRoute } from './src/RoleRoute';
1
+ export { createOxRoleRoute } from './src/createOxRoleRoute';
2
+ export * from './src/RoleRoute.types';
@@ -1,7 +1,12 @@
1
1
  import { ReactNode } from 'react';
2
- export interface OxRoleRouteProps {
2
+ export interface OxRoleRouteProps<T extends {
3
+ role: string;
4
+ } = {
5
+ role: string;
6
+ }> {
3
7
  children: ReactNode;
4
8
  onLoading?: ReactNode;
5
9
  onUnauthorized?: ReactNode;
10
+ roleExtractor?: (user: T) => string;
6
11
  roles?: string[];
7
12
  }
@@ -0,0 +1,8 @@
1
+ import { ReactNode } from 'react';
2
+ import { OxAuthState } from '../../../contexts';
3
+ import { OxRoleRouteProps } from './RoleRoute.types';
4
+ export declare function createOxRoleRoute<T extends {
5
+ role: string;
6
+ } = {
7
+ role: string;
8
+ }>(useAuth: () => OxAuthState<T>): ({ children, onLoading, onUnauthorized, roleExtractor, roles, }: OxRoleRouteProps<T>) => ReactNode;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("../chunks/ui-B0Ma5GdK.js");const d=require("../chunks/useAuth-DxoDe3YA.js"),a=require("../chunks/hooks-CNgb50x1.js"),g=u=>{const{children:i,onAuthenticated:n,onLoading:t}=u,o=n===void 0?null:n,r=t===void 0?null:t,{isLoggedIn:s,loading:e}=d.useOxAuth();return e?r:s?o:i},h=u=>{const{children:i,onLoading:n,onUnauthenticated:t}=u,o=n===void 0?null:n,r=t===void 0?null:t,{isLoggedIn:s,loading:e}=d.useOxAuth();return e?o:s?i:r},O=u=>{const{children:i,onLoading:n,onUnauthorized:t,roles:o}=u,r=n===void 0?null:n,s=t===void 0?null:t,e=o===void 0?["admin"]:o,{loading:l,user:c}=d.useOxAuth();return l?r:!c||a.isEmpty(e)||!e.includes(c.role)?s:i};exports.OxGuestRoute=g;exports.OxProtectedRoute=h;exports.OxRoleRoute=O;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("../chunks/hooks-CNgb50x1.js");function a(e){return function({children:n,onAuthenticated:u=null,onLoading:r=null}){const{isLoggedIn:o,loading:t}=e();return t?r:o?u:n}}function f(e){return function({children:n,onLoading:u=null,onUnauthenticated:r=null}){const{isLoggedIn:o,loading:t}=e();return t?u:o?n:r}}const d=e=>e.role;function R(e){return function({children:n,onLoading:u=null,onUnauthorized:r=null,roleExtractor:o=d,roles:t=["admin"]}){const{loading:l,user:i}=e();return l?u:!i||s.isEmpty(t)||!t.includes(o(i))?r:n}}exports.createOxGuestRoute=a;exports.createOxProtectedRoute=f;exports.createOxRoleRoute=R;
@@ -1,40 +1,48 @@
1
- import "../chunks/ui-DrKTwQ0M.js";
2
- import { u as s } from "../chunks/useAuth-BFgXzu6Q.js";
3
- import { A as a } from "../chunks/hooks-6tQOArBm.js";
4
- const L = (e) => {
5
- const {
6
- children: r,
7
- onAuthenticated: n,
8
- onLoading: o
9
- } = e, t = n === void 0 ? null : n, d = o === void 0 ? null : o, {
10
- isLoggedIn: u,
11
- loading: i
12
- } = s();
13
- return i ? d : u ? t : r;
14
- }, m = (e) => {
15
- const {
16
- children: r,
17
- onLoading: n,
18
- onUnauthenticated: o
19
- } = e, t = n === void 0 ? null : n, d = o === void 0 ? null : o, {
20
- isLoggedIn: u,
21
- loading: i
22
- } = s();
23
- return i ? t : u ? r : d;
24
- }, v = (e) => {
25
- const {
26
- children: r,
27
- onLoading: n,
28
- onUnauthorized: o,
29
- roles: t
30
- } = e, d = n === void 0 ? null : n, u = o === void 0 ? null : o, i = t === void 0 ? ["admin"] : t, {
31
- loading: l,
32
- user: c
33
- } = s();
34
- return l ? d : !c || a(i) || !i.includes(c.role) ? u : r;
35
- };
1
+ import { A as f } from "../chunks/hooks-6tQOArBm.js";
2
+ function d(t) {
3
+ return function({
4
+ children: e,
5
+ onAuthenticated: r = null,
6
+ onLoading: u = null
7
+ }) {
8
+ const {
9
+ isLoggedIn: o,
10
+ loading: n
11
+ } = t();
12
+ return n ? u : o ? r : e;
13
+ };
14
+ }
15
+ function R(t) {
16
+ return function({
17
+ children: e,
18
+ onLoading: r = null,
19
+ onUnauthenticated: u = null
20
+ }) {
21
+ const {
22
+ isLoggedIn: o,
23
+ loading: n
24
+ } = t();
25
+ return n ? r : o ? e : u;
26
+ };
27
+ }
28
+ const s = (t) => t.role;
29
+ function x(t) {
30
+ return function({
31
+ children: e,
32
+ onLoading: r = null,
33
+ onUnauthorized: u = null,
34
+ roleExtractor: o = s,
35
+ roles: n = ["admin"]
36
+ }) {
37
+ const {
38
+ loading: c,
39
+ user: l
40
+ } = t();
41
+ return c ? r : !l || f(n) || !n.includes(o(l)) ? u : e;
42
+ };
43
+ }
36
44
  export {
37
- L as OxGuestRoute,
38
- m as OxProtectedRoute,
39
- v as OxRoleRoute
45
+ d as createOxGuestRoute,
46
+ R as createOxProtectedRoute,
47
+ x as createOxRoleRoute
40
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noxickon/onyx",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "main": "./dist/onyx.umd.js",
6
6
  "module": "./dist/onyx.es.js",
@@ -1,12 +0,0 @@
1
- import { createContext as e, useContext as o } from "react";
2
- const n = e(void 0);
3
- function u() {
4
- const t = o(n);
5
- if (!t)
6
- throw new Error("useOxAuth must be used within an AuthProvider");
7
- return t;
8
- }
9
- export {
10
- n as O,
11
- u
12
- };
@@ -1 +0,0 @@
1
- "use strict";const e=require("react"),u=e.createContext(void 0);function n(){const t=e.useContext(u);if(!t)throw new Error("useOxAuth must be used within an AuthProvider");return t}exports.OxAuthContext=u;exports.useOxAuth=n;
@@ -1,2 +0,0 @@
1
- import { OxAuthContextType } from './AuthContext.types';
2
- export declare const OxAuthContext: import('react').Context<OxAuthContextType | undefined>;
@@ -1,2 +0,0 @@
1
- import { OxAuthProviderProps } from './AuthContext.types';
2
- export declare function OxAuthProvider({ adminRole, children, getCurrentUserQuery, options, pollInterval, }: OxAuthProviderProps): import("react/jsx-runtime").JSX.Element;
@@ -1 +0,0 @@
1
- export declare const DEFAULT_GET_CURRENT_USER: import('graphql').DocumentNode;
@@ -1 +0,0 @@
1
- export declare function useOxAuth(): import('./AuthContext.types').OxAuthContextType;
@@ -1,2 +0,0 @@
1
- import { OxGuestRouteProps } from './GuestRoute.types';
2
- export declare const OxGuestRoute: ({ children, onAuthenticated, onLoading, }: OxGuestRouteProps) => import('react').ReactNode;
@@ -1,2 +0,0 @@
1
- import { OxProtectedRouteProps } from './ProtectedRoute.types';
2
- export declare const OxProtectedRoute: ({ children, onLoading, onUnauthenticated, }: OxProtectedRouteProps) => import('react').ReactNode;
@@ -1,2 +0,0 @@
1
- import { OxRoleRouteProps } from './RoleRoute.types';
2
- export declare const OxRoleRoute: ({ children, onLoading, onUnauthorized, roles, }: OxRoleRouteProps) => import('react').ReactNode;