@alepha/react 0.6.0 → 0.6.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.
- package/dist/index.browser.cjs +0 -0
- package/dist/index.browser.js +0 -0
- package/dist/index.cjs +11 -14
- package/dist/index.d.ts +2 -2
- package/dist/index.js +11 -14
- package/package.json +4 -4
- package/src/components/Link.tsx +0 -22
- package/src/components/NestedView.tsx +0 -36
- package/src/constants/SSID.ts +0 -1
- package/src/contexts/RouterContext.ts +0 -15
- package/src/contexts/RouterLayerContext.ts +0 -10
- package/src/descriptors/$auth.ts +0 -28
- package/src/descriptors/$page.ts +0 -144
- package/src/errors/RedirectionError.ts +0 -7
- package/src/hooks/RouterHookApi.ts +0 -154
- package/src/hooks/useActive.ts +0 -57
- package/src/hooks/useAuth.ts +0 -29
- package/src/hooks/useClient.ts +0 -6
- package/src/hooks/useInject.ts +0 -12
- package/src/hooks/useQueryParams.ts +0 -59
- package/src/hooks/useRouter.ts +0 -28
- package/src/hooks/useRouterEvents.ts +0 -43
- package/src/hooks/useRouterState.ts +0 -23
- package/src/index.browser.ts +0 -21
- package/src/index.shared.ts +0 -28
- package/src/index.ts +0 -46
- package/src/providers/PageDescriptorProvider.ts +0 -52
- package/src/providers/ReactAuthProvider.ts +0 -410
- package/src/providers/ReactBrowserProvider.ts +0 -250
- package/src/providers/ReactServerProvider.ts +0 -301
- package/src/services/Auth.ts +0 -45
- package/src/services/Router.ts +0 -855
package/src/hooks/useAuth.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { UserAccountToken } from "@alepha/security";
|
|
2
|
-
import { useContext } from "react";
|
|
3
|
-
import { RouterContext } from "../contexts/RouterContext";
|
|
4
|
-
import { Auth } from "../services/Auth";
|
|
5
|
-
|
|
6
|
-
export const useAuth = (): AuthHook => {
|
|
7
|
-
const ctx = useContext(RouterContext);
|
|
8
|
-
if (!ctx) {
|
|
9
|
-
throw new Error("useAuth must be used within a RouterContext");
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const args = ctx.args ?? {};
|
|
13
|
-
|
|
14
|
-
return {
|
|
15
|
-
user: args.user,
|
|
16
|
-
logout: () => {
|
|
17
|
-
ctx.alepha.get(Auth).logout();
|
|
18
|
-
},
|
|
19
|
-
login: (provider?: string) => {
|
|
20
|
-
ctx.alepha.get(Auth).login();
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export interface AuthHook {
|
|
26
|
-
user?: UserAccountToken;
|
|
27
|
-
logout: () => void;
|
|
28
|
-
login: (provider?: string) => void;
|
|
29
|
-
}
|
package/src/hooks/useClient.ts
DELETED
package/src/hooks/useInject.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Class } from "@alepha/core";
|
|
2
|
-
import { useContext } from "react";
|
|
3
|
-
import { RouterContext } from "../contexts/RouterContext";
|
|
4
|
-
|
|
5
|
-
export const useInject = <T extends object>(clazz: Class<T>): T => {
|
|
6
|
-
const ctx = useContext(RouterContext);
|
|
7
|
-
if (!ctx) {
|
|
8
|
-
throw new Error("useRouter must be used within a <RouterProvider>");
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
return ctx.alepha.get(clazz);
|
|
12
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type { Alepha, Static, TObject } from "@alepha/core";
|
|
2
|
-
import { useContext, useEffect, useState } from "react";
|
|
3
|
-
import { RouterContext } from "../contexts/RouterContext";
|
|
4
|
-
import { useRouter } from "./useRouter";
|
|
5
|
-
|
|
6
|
-
export interface UseQueryParamsHookOptions {
|
|
7
|
-
format?: "base64" | "querystring";
|
|
8
|
-
key?: string;
|
|
9
|
-
push?: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const useQueryParams = <T extends TObject>(
|
|
13
|
-
schema: T,
|
|
14
|
-
options: UseQueryParamsHookOptions = {},
|
|
15
|
-
): [Static<T>, (data: Static<T>) => void] => {
|
|
16
|
-
const ctx = useContext(RouterContext);
|
|
17
|
-
if (!ctx) {
|
|
18
|
-
throw new Error("useQueryParams must be used within a RouterProvider");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const key = options.key ?? "q";
|
|
22
|
-
const router = useRouter();
|
|
23
|
-
const querystring = router.query[key];
|
|
24
|
-
|
|
25
|
-
const [queryParams, setQueryParams] = useState(
|
|
26
|
-
decode(ctx.alepha, schema, router.query[key]),
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
useEffect(() => {
|
|
30
|
-
setQueryParams(decode(ctx.alepha, schema, querystring));
|
|
31
|
-
}, [querystring]);
|
|
32
|
-
|
|
33
|
-
return [
|
|
34
|
-
queryParams,
|
|
35
|
-
(queryParams: Static<T>) => {
|
|
36
|
-
setQueryParams(queryParams);
|
|
37
|
-
router.setQueryParams(
|
|
38
|
-
{ [key]: encode(ctx.alepha, schema, queryParams) },
|
|
39
|
-
{
|
|
40
|
-
merge: true,
|
|
41
|
-
},
|
|
42
|
-
);
|
|
43
|
-
},
|
|
44
|
-
];
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
// ---------------------------------------------------------------------------------------------------------------------
|
|
48
|
-
|
|
49
|
-
const encode = (alepha: Alepha, schema: TObject, data: any) => {
|
|
50
|
-
return btoa(JSON.stringify(alepha.parse(schema, data)));
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const decode = (alepha: Alepha, schema: TObject, data: any) => {
|
|
54
|
-
try {
|
|
55
|
-
return alepha.parse(schema, JSON.parse(atob(decodeURIComponent(data))));
|
|
56
|
-
} catch (error) {
|
|
57
|
-
return {};
|
|
58
|
-
}
|
|
59
|
-
};
|
package/src/hooks/useRouter.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { useContext, useMemo } from "react";
|
|
2
|
-
import { RouterContext } from "../contexts/RouterContext";
|
|
3
|
-
import { RouterLayerContext } from "../contexts/RouterLayerContext";
|
|
4
|
-
import { ReactBrowserProvider } from "../providers/ReactBrowserProvider";
|
|
5
|
-
import { RouterHookApi } from "./RouterHookApi";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
*/
|
|
10
|
-
export const useRouter = (): RouterHookApi => {
|
|
11
|
-
const ctx = useContext(RouterContext);
|
|
12
|
-
const layer = useContext(RouterLayerContext);
|
|
13
|
-
if (!ctx || !layer) {
|
|
14
|
-
throw new Error("useRouter must be used within a RouterProvider");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return useMemo(
|
|
18
|
-
() =>
|
|
19
|
-
new RouterHookApi(
|
|
20
|
-
ctx.state,
|
|
21
|
-
layer,
|
|
22
|
-
ctx.alepha.isBrowser()
|
|
23
|
-
? ctx.alepha.get(ReactBrowserProvider)
|
|
24
|
-
: undefined,
|
|
25
|
-
),
|
|
26
|
-
[ctx.router, layer],
|
|
27
|
-
);
|
|
28
|
-
};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { useContext, useEffect } from "react";
|
|
2
|
-
import { RouterContext } from "../contexts/RouterContext";
|
|
3
|
-
import { RouterLayerContext } from "../contexts/RouterLayerContext";
|
|
4
|
-
import type { RouterState } from "../services/Router";
|
|
5
|
-
|
|
6
|
-
export const useRouterEvents = (
|
|
7
|
-
opts: {
|
|
8
|
-
onBegin?: () => void;
|
|
9
|
-
onEnd?: (it: RouterState) => void;
|
|
10
|
-
onError?: (it: Error) => void;
|
|
11
|
-
} = {},
|
|
12
|
-
) => {
|
|
13
|
-
const ctx = useContext(RouterContext);
|
|
14
|
-
const layer = useContext(RouterLayerContext);
|
|
15
|
-
if (!ctx || !layer) {
|
|
16
|
-
throw new Error("useRouter must be used within a RouterProvider");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
useEffect(() => {
|
|
20
|
-
const subs: Function[] = [];
|
|
21
|
-
const onBegin = opts.onBegin;
|
|
22
|
-
const onEnd = opts.onEnd;
|
|
23
|
-
const onError = opts.onError;
|
|
24
|
-
|
|
25
|
-
if (onBegin) {
|
|
26
|
-
subs.push(ctx.router.on("begin", onBegin));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (onEnd) {
|
|
30
|
-
subs.push(ctx.router.on("end", onEnd));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (onError) {
|
|
34
|
-
subs.push(ctx.router.on("error", onError));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return () => {
|
|
38
|
-
for (const sub of subs) {
|
|
39
|
-
sub();
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
}, []);
|
|
43
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { useContext, useEffect, useState } from "react";
|
|
2
|
-
import { RouterContext } from "../contexts/RouterContext";
|
|
3
|
-
import { RouterLayerContext } from "../contexts/RouterLayerContext";
|
|
4
|
-
import type { RouterState } from "../services/Router";
|
|
5
|
-
|
|
6
|
-
export const useRouterState = (): RouterState => {
|
|
7
|
-
const ctx = useContext(RouterContext);
|
|
8
|
-
const layer = useContext(RouterLayerContext);
|
|
9
|
-
if (!ctx || !layer) {
|
|
10
|
-
throw new Error("useRouter must be used within a RouterProvider");
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const [state, setState] = useState(ctx.state);
|
|
14
|
-
useEffect(
|
|
15
|
-
() =>
|
|
16
|
-
ctx.router.on("end", (it) => {
|
|
17
|
-
setState({ ...it });
|
|
18
|
-
}),
|
|
19
|
-
[],
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
return state;
|
|
23
|
-
};
|
package/src/index.browser.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { $inject, Alepha, autoInject } from "@alepha/core";
|
|
2
|
-
import { $page } from "./descriptors/$page";
|
|
3
|
-
import { PageDescriptorProvider } from "./providers/PageDescriptorProvider";
|
|
4
|
-
import { ReactBrowserProvider } from "./providers/ReactBrowserProvider";
|
|
5
|
-
import { Auth } from "./services/Auth";
|
|
6
|
-
|
|
7
|
-
export * from "./index.shared";
|
|
8
|
-
export * from "./providers/ReactBrowserProvider";
|
|
9
|
-
|
|
10
|
-
export class ReactModule {
|
|
11
|
-
protected readonly alepha = $inject(Alepha);
|
|
12
|
-
|
|
13
|
-
constructor() {
|
|
14
|
-
this.alepha //
|
|
15
|
-
.with(PageDescriptorProvider)
|
|
16
|
-
.with(ReactBrowserProvider)
|
|
17
|
-
.with(Auth);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
autoInject($page, ReactModule);
|
package/src/index.shared.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export { default as NestedView } from "./components/NestedView";
|
|
2
|
-
export { default as Link } from "./components/Link";
|
|
3
|
-
|
|
4
|
-
export * from "./contexts/RouterContext";
|
|
5
|
-
export * from "./contexts/RouterLayerContext";
|
|
6
|
-
|
|
7
|
-
export * from "./services/Auth";
|
|
8
|
-
|
|
9
|
-
export * from "./descriptors/$page";
|
|
10
|
-
export * from "./descriptors/$auth";
|
|
11
|
-
|
|
12
|
-
export * from "./hooks/RouterHookApi";
|
|
13
|
-
|
|
14
|
-
// --- Hooks
|
|
15
|
-
// - core
|
|
16
|
-
export * from "./hooks/useInject";
|
|
17
|
-
// - http
|
|
18
|
-
export * from "./hooks/useClient";
|
|
19
|
-
// - router
|
|
20
|
-
export * from "./hooks/useQueryParams";
|
|
21
|
-
export * from "./hooks/useRouter";
|
|
22
|
-
export * from "./hooks/useRouterEvents";
|
|
23
|
-
export * from "./hooks/useRouterState";
|
|
24
|
-
export * from "./hooks/useActive";
|
|
25
|
-
// - auth
|
|
26
|
-
export * from "./hooks/useAuth";
|
|
27
|
-
|
|
28
|
-
export * from "./services/Router";
|
package/src/index.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { $inject, Alepha, type Static, autoInject, t } from "@alepha/core";
|
|
2
|
-
import { ServerLinksProvider, ServerModule } from "@alepha/server";
|
|
3
|
-
import { $auth } from "./descriptors/$auth";
|
|
4
|
-
import { $page } from "./descriptors/$page";
|
|
5
|
-
import { PageDescriptorProvider } from "./providers/PageDescriptorProvider";
|
|
6
|
-
import { ReactAuthProvider } from "./providers/ReactAuthProvider";
|
|
7
|
-
import { ReactServerProvider } from "./providers/ReactServerProvider";
|
|
8
|
-
import { Auth } from "./services/Auth";
|
|
9
|
-
export { default as NestedView } from "./components/NestedView";
|
|
10
|
-
|
|
11
|
-
export * from "./index.shared";
|
|
12
|
-
export * from "./providers/PageDescriptorProvider";
|
|
13
|
-
export * from "./providers/ReactBrowserProvider";
|
|
14
|
-
export * from "./providers/ReactServerProvider";
|
|
15
|
-
export * from "./providers/ReactAuthProvider";
|
|
16
|
-
export * from "./services/Router";
|
|
17
|
-
export * from "./errors/RedirectionError";
|
|
18
|
-
|
|
19
|
-
const envSchema = t.object({
|
|
20
|
-
REACT_AUTH_ENABLED: t.boolean({ default: false }),
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
declare module "@alepha/core" {
|
|
24
|
-
interface Env extends Partial<Static<typeof envSchema>> {}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class ReactModule {
|
|
28
|
-
protected readonly env = $inject(envSchema);
|
|
29
|
-
protected readonly alepha = $inject(Alepha);
|
|
30
|
-
|
|
31
|
-
constructor() {
|
|
32
|
-
this.alepha //
|
|
33
|
-
.with(ServerModule)
|
|
34
|
-
.with(ServerLinksProvider)
|
|
35
|
-
.with(PageDescriptorProvider)
|
|
36
|
-
.with(ReactServerProvider);
|
|
37
|
-
|
|
38
|
-
if (this.env.REACT_AUTH_ENABLED) {
|
|
39
|
-
this.alepha.with(ReactAuthProvider);
|
|
40
|
-
this.alepha.with(Auth);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
autoInject($page, ReactModule);
|
|
46
|
-
autoInject($auth, ReactAuthProvider, Auth);
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { $hook, $inject, Alepha } from "@alepha/core";
|
|
2
|
-
import type { PageDescriptorOptions } from "../descriptors/$page";
|
|
3
|
-
import { $page } from "../descriptors/$page";
|
|
4
|
-
import type { PageRoute, PageRouteEntry } from "../services/Router";
|
|
5
|
-
import { Router } from "../services/Router";
|
|
6
|
-
|
|
7
|
-
export class PageDescriptorProvider {
|
|
8
|
-
protected readonly alepha = $inject(Alepha);
|
|
9
|
-
protected readonly router = $inject(Router);
|
|
10
|
-
|
|
11
|
-
protected readonly configure = $hook({
|
|
12
|
-
name: "configure",
|
|
13
|
-
handler: () => {
|
|
14
|
-
const pages = this.alepha.getDescriptorValues($page);
|
|
15
|
-
for (const { value, key } of pages) {
|
|
16
|
-
value.options.name ??= key;
|
|
17
|
-
|
|
18
|
-
// skip children, we only want root pages
|
|
19
|
-
if (pages.find((it) => it.value.options.children?.().includes(value))) {
|
|
20
|
-
continue;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
this.router.add(this.map(pages, value));
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Transform
|
|
30
|
-
* @param pages
|
|
31
|
-
* @param target
|
|
32
|
-
* @protected
|
|
33
|
-
*/
|
|
34
|
-
protected map(
|
|
35
|
-
pages: Array<{ value: { options: PageDescriptorOptions } }>,
|
|
36
|
-
target: { options: PageDescriptorOptions },
|
|
37
|
-
): PageRouteEntry {
|
|
38
|
-
const children = target.options.children?.() ?? [];
|
|
39
|
-
|
|
40
|
-
for (const it of pages) {
|
|
41
|
-
if (it.value.options.parent === target) {
|
|
42
|
-
children.push(it.value);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
...target.options,
|
|
48
|
-
parent: undefined,
|
|
49
|
-
children: children.map((it) => this.map(pages, it)),
|
|
50
|
-
} as PageRoute;
|
|
51
|
-
}
|
|
52
|
-
}
|