build: fix build and add examples
This commit is contained in:
47
src/adapters/react/index.ts
Normal file
47
src/adapters/react/index.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { InjectionToken, Injector, Type } from '@outposts/injection-js';
|
||||
import {
|
||||
type PropsWithChildren,
|
||||
createContext,
|
||||
createElement,
|
||||
useContext,
|
||||
useMemo,
|
||||
} from 'react';
|
||||
import { OidcSecurityService } from '../..';
|
||||
|
||||
export const InjectorContextVoidInjector: Injector = {
|
||||
get: <T>(_token: Type<T> | InjectionToken<T>, _notFoundValue?: T): T => {
|
||||
throw new Error('Please wrap with a InjectorContext.Provider first');
|
||||
},
|
||||
};
|
||||
|
||||
export const InjectorContext = createContext<Injector>(
|
||||
InjectorContextVoidInjector
|
||||
);
|
||||
|
||||
export function InjectorProvider({
|
||||
injector,
|
||||
...props
|
||||
}: PropsWithChildren<{ injector: Injector }>) {
|
||||
return createElement(InjectorContext, {
|
||||
...props,
|
||||
value: injector,
|
||||
});
|
||||
}
|
||||
|
||||
export function useInjector() {
|
||||
return useContext(InjectorContext);
|
||||
}
|
||||
|
||||
export function useOidcClient() {
|
||||
const injector = useInjector();
|
||||
|
||||
const oidcSecurityService = useMemo(
|
||||
() => injector.get(OidcSecurityService),
|
||||
[injector]
|
||||
);
|
||||
|
||||
return {
|
||||
injector,
|
||||
oidcSecurityService,
|
||||
};
|
||||
}
|
||||
45
src/adapters/tanstack-router/index.ts
Normal file
45
src/adapters/tanstack-router/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { InjectionToken, inject } from '@outposts/injection-js';
|
||||
import type {
|
||||
AnyRoute,
|
||||
Router,
|
||||
TrailingSlashOption,
|
||||
} from '@tanstack/react-router';
|
||||
import { AbstractRouter } from 'src/router';
|
||||
import type { AuthFeature } from '../../provide-auth';
|
||||
|
||||
export type TanStackRouter = Router<AnyRoute, TrailingSlashOption, boolean>;
|
||||
|
||||
export const TANSTACK_ROUTER = new InjectionToken<TanStackRouter>(
|
||||
'TANSTACK_ROUTER'
|
||||
);
|
||||
|
||||
export class TanStackRouterAdapter implements AbstractRouter<string> {
|
||||
private router = inject(TANSTACK_ROUTER);
|
||||
|
||||
navigateByUrl(url: string): void {
|
||||
this.router.navigate({
|
||||
href: url,
|
||||
});
|
||||
}
|
||||
|
||||
getCurrentNavigation() {
|
||||
return {
|
||||
extractedUrl: this.router.state.location.href,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function withTanstackRouter(router: TanStackRouter): AuthFeature {
|
||||
return {
|
||||
ɵproviders: [
|
||||
{
|
||||
provide: TANSTACK_ROUTER,
|
||||
useValue: router,
|
||||
},
|
||||
{
|
||||
provide: AbstractRouter,
|
||||
useClass: TanStackRouterAdapter,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user