@shivaedev/admin 0.3.0 → 0.5.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/README.md +32 -8
- package/dist/client/UsersTable.d.ts +11 -0
- package/dist/client/UsersTable.d.ts.map +1 -0
- package/dist/client/UsersTable.js +25 -0
- package/dist/client/UsersTable.js.map +1 -0
- package/dist/client/UsersView.d.ts +13 -0
- package/dist/client/UsersView.d.ts.map +1 -0
- package/dist/client/UsersView.js +35 -0
- package/dist/client/UsersView.js.map +1 -0
- package/dist/jobs/page.d.ts +6 -6
- package/dist/jobs/retryPage.d.ts +22 -22
- package/dist/page.d.ts +2 -2
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +2 -2
- package/dist/page.js.map +1 -1
- package/dist/permissions.d.ts +15 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/permissions.js +6 -0
- package/dist/permissions.js.map +1 -0
- package/dist/server/usersHandlers.d.ts +2 -0
- package/dist/server/usersHandlers.d.ts.map +1 -0
- package/dist/server/usersHandlers.js +14 -0
- package/dist/server/usersHandlers.js.map +1 -0
- package/dist/users/page.d.ts +80 -0
- package/dist/users/page.d.ts.map +1 -0
- package/dist/users/page.js +31 -0
- package/dist/users/page.js.map +1 -0
- package/package.json +18 -6
- package/src/client/UsersTable.tsx +102 -0
- package/src/client/UsersView.ts +71 -0
- package/src/page.ts +3 -3
- package/src/permissions.ts +5 -0
- package/src/server/usersHandlers.ts +15 -0
- package/src/users/page.ts +32 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @shivaedev/admin
|
|
2
2
|
|
|
3
|
-
Admin tooling starts with an explicit list of pages and the operations each page needs. This package collects native Effect contracts behind
|
|
3
|
+
Admin tooling starts with an explicit list of pages and the operations each page needs. This package collects native Effect contracts behind a declared permission, so every page operation shares the same boundary.
|
|
4
4
|
|
|
5
5
|
## Declare the tools operators can use
|
|
6
6
|
|
|
@@ -13,11 +13,11 @@ const Jobs = adminPage({
|
|
|
13
13
|
const Admin = adminRegistry(Jobs);
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
The page exposes `admin.jobs.failed` and the generated `admin.jobs.$changes` subscription. Both require `
|
|
16
|
+
The page exposes `admin.jobs.failed` and the generated `admin.jobs.$changes` subscription. Both require `AdminPermission`, which checks the `OpenAdmin` permission (`admin.open`). A query-only contract has no command. The application grants this permission to its chosen roles.
|
|
17
17
|
|
|
18
18
|
## Use native contracts and Layers
|
|
19
19
|
|
|
20
|
-
Install `@shivaedev/admin` with `effect`. Declare each page's queries, commands and streams with `@shivaedev/effect-contract`, then pass its native contract to `adminPage`. The stable id identifies the page at server registration; route metadata belongs to the application's view. Author RPC namespaces in the contract name, such as `admin.jobs`.
|
|
20
|
+
Install `@shivaedev/admin` with `effect`, `react` and `@effect/atom-react`. Declare each page's queries, commands and streams with `@shivaedev/effect-contract`, then pass its native contract to `adminPage`. The stable id identifies the page at server registration; route metadata belongs to the application's view. Author RPC namespaces in the contract name, such as `admin.jobs`.
|
|
21
21
|
|
|
22
22
|
```ts
|
|
23
23
|
import { Schema } from "effect";
|
|
@@ -45,7 +45,8 @@ Register server handler Layers by page id:
|
|
|
45
45
|
```ts
|
|
46
46
|
import { Effect, Layer } from "effect";
|
|
47
47
|
import { registerAdmin } from "@shivaedev/admin/server/registerAdmin.ts";
|
|
48
|
-
import {
|
|
48
|
+
import { AdminPermission, OpenAdmin } from "@shivaedev/admin/permissions.ts";
|
|
49
|
+
import { permissionLayer } from "@shivaedev/platform/server/rpc-server/permissions.ts";
|
|
49
50
|
|
|
50
51
|
const registration = registerAdmin(Admin, {
|
|
51
52
|
jobs: Jobs.contract.application.toLayer({
|
|
@@ -53,13 +54,15 @@ const registration = registerAdmin(Admin, {
|
|
|
53
54
|
}),
|
|
54
55
|
});
|
|
55
56
|
const handlers = registration.handlers.pipe(
|
|
56
|
-
Layer.provideMerge(
|
|
57
|
+
Layer.provideMerge(permissionLayer(AdminPermission, sessions)),
|
|
57
58
|
);
|
|
58
59
|
```
|
|
59
60
|
|
|
61
|
+
Declare application roles with `@shivaedev/access`, grant `OpenAdmin` explicitly, and supply that role set through Platform's session or auth configuration.
|
|
62
|
+
|
|
60
63
|
Pass `registration.group` and `handlers` to the existing application's RPC host on `/rpc`, merging them with its other groups and Layers as needed. The Platform host supplies generated change handlers; registration requires the page's application handlers. Handler Layer errors and service requirements remain inferred. Missing or extra page ids fail at registration; TypeScript also checks each Layer provides the handlers for its page.
|
|
61
64
|
|
|
62
|
-
Page declarations and the registry are browser-safe. Server registration lives under `/server`;
|
|
65
|
+
Page declarations and the registry are browser-safe. Server registration lives under `/server`; applications own route composition and styling. The built-in Users view uses React; server declarations add no transport. The package's stories prove permission enforcement through native in-process RPC, with an already resolved session as a precondition.
|
|
63
66
|
|
|
64
67
|
## Built-in Jobs page
|
|
65
68
|
|
|
@@ -90,7 +93,7 @@ The queries are `admin.jobs.health`, `admin.jobs.counts`, `admin.jobs.failed`, `
|
|
|
90
93
|
|
|
91
94
|
Health filters the runtime's monitor snapshot to the page's queues and recomputes totals from those queues. It has the same refresh timing as effect-pg-boss health. Unregistered names, including dead-letter queue names passed directly, fail with `JobsQueueUnregistered`. Stored payload decoding failures become `JobsPayloadInvalid`; jobs operation failures become `JobsUnavailable`, carrying the operation name. These declared failures do not expose the original database error.
|
|
92
95
|
|
|
93
|
-
The default `jobsPage` declares no commands, and its bound client has no retry operation. Handler registration performs no jobs read, and a failed jobs request leaves other pages available. PostgreSQL stories exercise these queries through serialized `/rpc` requests, including pagination, transformed payloads, missing jobs, queue restrictions and
|
|
96
|
+
The default `jobsPage` declares no commands, and its bound client has no retry operation. Handler registration performs no jobs read, and a failed jobs request leaves other pages available. PostgreSQL stories exercise these queries through serialized `/rpc` requests, including pagination, transformed payloads, missing jobs, queue restrictions and permission enforcement. The application supplies the Jobs view and mounts the registration on its existing RPC host.
|
|
94
97
|
|
|
95
98
|
### Declare retry explicitly
|
|
96
99
|
|
|
@@ -107,6 +110,27 @@ const registration = registerAdmin(Admin, {
|
|
|
107
110
|
});
|
|
108
111
|
```
|
|
109
112
|
|
|
110
|
-
This variant adds only `admin.jobs.retry`, taking `{ queue, id }` and returning void. It requires
|
|
113
|
+
This variant adds only `admin.jobs.retry`, taking `{ queue, id }` and returning void. It requires `OpenAdmin`, through the same page middleware. The handler uses effect-pg-boss's existing retry for a failed job in a declared queue. Missing jobs and jobs in other states fail with `JobsNotFailed`; unregistered queue names, including dead-letter queues, fail with `JobsQueueUnregistered`. A dead-letter copy's id cannot retry its original.
|
|
111
114
|
|
|
112
115
|
A successful bound retry invalidates health, the affected queue's counts and both listings, and the original job's detail. PostgreSQL stories mount these query atoms, retry through authenticated serialized RPC, and observe refetching and a second worker execution. Health retains the runtime monitor's refresh timing.
|
|
116
|
+
|
|
117
|
+
## Built-in Users page
|
|
118
|
+
|
|
119
|
+
`UsersPage` from `@shivaedev/admin/users/page.ts` declares `/admin/users`. Add it to the same registry as Jobs and register `users: usersHandlers` from `@shivaedev/admin/server/usersHandlers.ts`. The handlers use the application's existing Effect SQL client against Better Auth's default PostgreSQL `user` table, with the admin plugin's `role` column. They do not create another user store. Custom Better Auth table or column names are not supported by these handlers.
|
|
120
|
+
|
|
121
|
+
Bind `UsersPage.contract` to the registry's native AtomRpc client, then mount the package view in the application's admin shell. The view uses semantic HTML and Tailwind utility classes; Tailwind applications include `@source "../../node_modules/@shivaedev/admin/src/client";` in `src/client/entry.css`, as the starter does:
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
import { UsersView } from "@shivaedev/admin/client/UsersView.ts";
|
|
125
|
+
|
|
126
|
+
const users = bind(UsersPage.contract, Client);
|
|
127
|
+
const changeRole = Client.runtime.fn(users.changeRole.run);
|
|
128
|
+
|
|
129
|
+
<UsersView list={users.list.query} changeRole={changeRole} />;
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`admin.users.list` takes `{ limit, cursor? }` (1–100 users), returns `{ users, next, canChangeRoles }`, and orders pages by user id. Each user has id, name, email, roles and a UTC sign-up time. All reads and subscriptions require `OpenAdmin`. Callers granted `ChangeUserRoles` (`users.roles.change`) from Platform's `rpc/userRoles.ts` see role controls. `admin.users.changeRole` takes `{ userId, role, grant }` for Platform's `user`, `admin` and `owner` roles. It returns typed `Forbidden` without that permission, `LastOwner` when revoking the final owner, and `NotFound` for a missing user. Role changes preserve other roles, invalidate the list after commit, and emit an Effect log with actor id, target id, role, action and timestamp. There is no durable audit-history store.
|
|
133
|
+
|
|
134
|
+
Keep Better Auth cookie caching disabled so requests see current roles. Applications must route role writes through this operation to retain its permission and last-owner policy. The starter disables Better Auth's `/admin/set-role`, `/admin/update-user` and `/admin/create-user` browser endpoints; its server-side bootstrap API remains available.
|
|
135
|
+
|
|
136
|
+
The starter mounts Users beside Jobs and bootstraps new administrators with both `admin` and `owner`. Existing generated apps receive this wiring through `pnpm dlx @shivaedev/create-app@latest update` after release; review conflicts and install dependencies. For an existing database with no owner, bootstrap a new administrator with `pnpm admin:create`.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ChangeRoleInput } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
2
|
+
import type { UsersList } from "#users/page.ts";
|
|
3
|
+
interface RoleControls {
|
|
4
|
+
readonly dispatch: (input: typeof ChangeRoleInput.Type) => void;
|
|
5
|
+
readonly pending: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function UsersTable({ data, ...controls }: RoleControls & {
|
|
8
|
+
readonly data: typeof UsersList.Type;
|
|
9
|
+
}): import("react").JSX.Element;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=UsersTable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UsersTable.d.ts","sourceRoot":"","sources":["../../src/client/UsersTable.tsx"],"names":[],"mappings":"AACA,OAAO,EAAgB,KAAK,eAAe,EAAY,MAAM,sCAAsC,CAAC;AACpG,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,UAAU,YAAY;IACrB,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,eAAe,CAAC,IAAI,KAAK,IAAI,CAAC;IAChE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC1B;AAuFD,wBAAgB,UAAU,CAAC,EAAE,IAAI,EAAE,GAAG,QAAQ,EAAE,EAAE,YAAY,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,SAAS,CAAC,IAAI,CAAA;CAAE,+BAOxG"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createElement as _createElement } from "react";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { DateTime } from "effect";
|
|
4
|
+
import { UserRole } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
5
|
+
function UserHeaders() {
|
|
6
|
+
return (_jsx("tr", { children: ["Name", "Email", "Roles", "Signed up"].map((label) => (_jsx("th", { className: "whitespace-nowrap p-3", scope: "col", children: label }, label))) }));
|
|
7
|
+
}
|
|
8
|
+
function RoleCheckbox({ user, role, pending, dispatch }) {
|
|
9
|
+
return (_jsxs("label", { className: "mr-3 inline-flex gap-1", children: [_jsx("input", { "aria-label": `${role} for ${user.email}`, checked: user.roles.includes(role), disabled: pending, onChange: (event) => dispatch({ grant: event.target.checked, role, userId: user.id }), type: "checkbox" }), role] }));
|
|
10
|
+
}
|
|
11
|
+
function UserRoles({ user, canChangeRoles, dispatch, pending, }) {
|
|
12
|
+
return (_jsxs("div", { children: [_jsx("p", { children: user.roles.join(", ") || "No roles" }), canChangeRoles
|
|
13
|
+
? UserRole.literals.map((role) => _jsx(RoleCheckbox, { dispatch: dispatch, pending: pending, role: role, user: user }, role))
|
|
14
|
+
: null] }));
|
|
15
|
+
}
|
|
16
|
+
function UserRow({ user, ...controls }) {
|
|
17
|
+
return (_jsxs("tr", { className: "border-b", children: [_jsx("td", { className: "p-3", children: user.name }), _jsx("td", { className: "p-3", children: user.email }), _jsx("td", { className: "p-3", children: _jsx(UserRoles, { ...controls, user: user }) }), _jsx("td", { className: "whitespace-nowrap p-3", children: _jsx("time", { dateTime: DateTime.formatIso(user.createdAt), children: DateTime.formatIso(user.createdAt) }) })] }));
|
|
18
|
+
}
|
|
19
|
+
function UserRows({ data, ...controls }) {
|
|
20
|
+
return (_jsx("div", { className: "overflow-x-auto rounded-md border", children: _jsxs("table", { className: "w-full text-left text-sm", children: [_jsx("caption", { className: "sr-only", children: "App users and roles" }), _jsx("thead", { className: "bg-muted", children: _jsx(UserHeaders, {}) }), _jsx("tbody", { children: data.users.map((user) => (_createElement(UserRow, { ...controls, canChangeRoles: data.canChangeRoles, key: user.id, user: user }))) })] }) }));
|
|
21
|
+
}
|
|
22
|
+
export function UsersTable({ data, ...controls }) {
|
|
23
|
+
return (_jsxs("div", { className: "space-y-4", children: [data.canChangeRoles ? null : _jsx("p", { children: "Only owners can change roles." }), data.users.length === 0 ? _jsx("p", { children: "No users." }) : _jsx(UserRows, { ...controls, data: data })] }));
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=UsersTable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UsersTable.js","sourceRoot":"","sources":["../../src/client/UsersTable.tsx"],"names":[],"mappings":";;AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAsC,QAAQ,EAAE,MAAM,sCAAsC,CAAC;AAQpG,SAAS,WAAW;IACnB,OAAO,CACN,uBACE,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CACvD,aAAI,SAAS,EAAC,uBAAuB,EAAa,KAAK,EAAC,KAAK,YAC3D,KAAK,IADoC,KAAK,CAE3C,CACL,CAAC,GACE,CACL,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CAAC,EAAE,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put, UserListInput } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
4
|
+
import type { UsersList } from "#users/page.ts";
|
|
5
|
+
export interface UsersViewProps {
|
|
6
|
+
readonly changeRole: Atom.AtomResultFn<typeof ChangeRoleInput.Type, void, unknown>;
|
|
7
|
+
readonly list: (input: typeof UserListInput.Type) => Atom.Atom<AsyncResult.AsyncResult<typeof UsersList.Type, unknown>>;
|
|
8
|
+
}
|
|
9
|
+
export declare function UsersView({ list, changeRole }: UsersViewProps): import("react").DetailedReactHTMLElement<{
|
|
10
|
+
"aria-label": string;
|
|
11
|
+
className: string;
|
|
12
|
+
}, HTMLElement>;
|
|
13
|
+
//# sourceMappingURL=UsersView.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UsersView.d.ts","sourceRoot":"","sources":["../../src/client/UsersView.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAGpD,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAC3F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,MAAM,WAAW,cAAc;IAC9B,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,YAAY,CAAC,OAAO,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACnF,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,aAAa,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,OAAO,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;CACxH;AAaD,wBAAgB,SAAS,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,cAAc;;;gBA6C7D"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Cause, Option, Predicate } from "effect";
|
|
2
|
+
import { createElement, useState } from "react";
|
|
3
|
+
import { useAction, useQuery } from "@shivaedev/effect-react/client/resultState.ts";
|
|
4
|
+
import { UsersTable } from "./UsersTable.js";
|
|
5
|
+
function failure(cause) {
|
|
6
|
+
const error = Option.getOrUndefined(Cause.findErrorOption(cause));
|
|
7
|
+
if (Predicate.isTagged(error, "LastOwner")) {
|
|
8
|
+
return "The last owner cannot be demoted. Grant owner to another user first.";
|
|
9
|
+
}
|
|
10
|
+
if (Predicate.isTagged(error, "Forbidden")) {
|
|
11
|
+
return "Owner role required to change roles.";
|
|
12
|
+
}
|
|
13
|
+
return "Could not change the role. Try again.";
|
|
14
|
+
}
|
|
15
|
+
export function UsersView({ list, changeRole }) {
|
|
16
|
+
const [cursors, setCursors] = useState([]);
|
|
17
|
+
const cursor = cursors.at(-1);
|
|
18
|
+
const state = useQuery(list({ ...(cursor === undefined ? {} : { cursor }), limit: 20 }));
|
|
19
|
+
const action = useAction(changeRole);
|
|
20
|
+
const data = Option.getOrUndefined(state.data);
|
|
21
|
+
return createElement("section", { "aria-label": "App users", className: "space-y-4" }, createElement("button", { className: "rounded-md border px-3 py-2 disabled:opacity-50", disabled: state.pending, onClick: state.refresh, type: "button" }, "Refresh users"), state.pending ? createElement("p", { role: "status" }, "Loading users…") : null, Option.isSome(state.cause) ? createElement("p", { role: "alert" }, "Could not load users. Try refreshing.") : null, Option.isSome(action.cause) ? createElement("p", { role: "alert" }, failure(action.cause.value)) : null, data === undefined ? null : createElement(UsersTable, { data, dispatch: action.dispatch, pending: action.pending }), data === undefined
|
|
22
|
+
? null
|
|
23
|
+
: createElement("nav", { "aria-label": "User pages", className: "flex gap-4" }, createElement("button", {
|
|
24
|
+
className: "rounded-md border px-3 py-2 disabled:opacity-50",
|
|
25
|
+
disabled: cursors.length === 0 || state.pending,
|
|
26
|
+
onClick: () => setCursors(cursors.slice(0, -1)),
|
|
27
|
+
type: "button",
|
|
28
|
+
}, "Previous users"), createElement("button", {
|
|
29
|
+
className: "rounded-md border px-3 py-2 disabled:opacity-50",
|
|
30
|
+
disabled: Option.isNone(data.next) || state.pending,
|
|
31
|
+
onClick: () => Option.map(data.next, (next) => setCursors([...cursors, next])),
|
|
32
|
+
type: "button",
|
|
33
|
+
}, "Next users")));
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=UsersView.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UsersView.js","sourceRoot":"","sources":["../../src/client/UsersView.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAGlD,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,+CAA+C,CAAC;AAGpF,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAkB,CAAC;AAO9C,SAAS,OAAO,CAAC,KAA2B;IAC3C,MAAM,KAAK,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC;IAClE,IAAI,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,EAAE,CAAC;QAC5C,OAAO,sEAAsE,CAAC;IAC/E,CAAC;IACD,IAAI,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,EAAE,CAAC;QAC5C,OAAO,sCAAsC,CAAC;IAC/C,CAAC;IACD,OAAO,uCAAuC,CAAC;AAChD,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,EAAE,IAAI,EAAE,UAAU,EAAkB;IAC7D,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAoB,EAAE,CAAC,CAAC;IAC9D,MAAM,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9B,MAAM,KAAK,GAAG,QAAQ,C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EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,EACnH,IAAI,KAAK,SAAS;QACjB,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,aAAa,CACb,KAAK,EACL,EAAE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,YAAY,EAAE,EACvD,aAAa,CACZ,QAAQ,EACR;YACC,SAAS,EAAE,iDAAiD;YAC5D,QAAQ,EAAE,OAAO,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO;YAC/C,OAAO,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YAC/C,IAAI,EAAE,QAAQ;SACd,EACD,gBAAgB,CAChB,EACD,aAAa,CACZ,QAAQ,EACR;YACC,SAAS,EAAE,iDAAiD;YAC5D,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO;YACnD,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;YAC9E,IAAI,EAAE,QAAQ;SACd,EACD,YAAY,CACZ,CACD,CACH,CAAC;AACH,CAAC"}
|
package/dist/jobs/page.d.ts
CHANGED
|
@@ -140,14 +140,14 @@ export declare function jobsPage<const TQueues extends readonly JobsQueue[]>(...
|
|
|
140
140
|
}>]>>;
|
|
141
141
|
}>, Schema.Struct<{
|
|
142
142
|
readonly _tag: Schema.Literal<"Resync">;
|
|
143
|
-
}>]>, Schema.Never>, Schema.Never, typeof import("
|
|
143
|
+
}>]>, Schema.Never>, Schema.Never, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.counts", Schema.Struct<{
|
|
144
144
|
queue: Schema.String;
|
|
145
145
|
}>, Schema.Struct<{
|
|
146
146
|
readonly activeCount: Schema.Number;
|
|
147
147
|
readonly failedCount: Schema.Number;
|
|
148
148
|
readonly queuedCount: Schema.Number;
|
|
149
149
|
readonly totalCount: Schema.Number;
|
|
150
|
-
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
150
|
+
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.deadLettered", Schema.Struct<{
|
|
151
151
|
cursor: Schema.optionalKey<Schema.String>;
|
|
152
152
|
limit: Schema.Number;
|
|
153
153
|
queue: Schema.String;
|
|
@@ -168,7 +168,7 @@ export declare function jobsPage<const TQueues extends readonly JobsQueue[]>(...
|
|
|
168
168
|
readonly sourceOutput: Schema.Unknown;
|
|
169
169
|
}>[]>>;
|
|
170
170
|
readonly next: Schema.OptionFromNullOr<Schema.String>;
|
|
171
|
-
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
171
|
+
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.failed", Schema.Struct<{
|
|
172
172
|
cursor: Schema.optionalKey<Schema.String>;
|
|
173
173
|
limit: Schema.Number;
|
|
174
174
|
queue: Schema.String;
|
|
@@ -187,7 +187,7 @@ export declare function jobsPage<const TQueues extends readonly JobsQueue[]>(...
|
|
|
187
187
|
readonly queue: Schema.Literal<string>;
|
|
188
188
|
}>[]>>;
|
|
189
189
|
readonly next: Schema.OptionFromNullOr<Schema.String>;
|
|
190
|
-
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
190
|
+
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.health", Schema.Void, Schema.Struct<{
|
|
191
191
|
readonly activeTotal: Schema.Number;
|
|
192
192
|
readonly deadLetteredTotal: Schema.Number;
|
|
193
193
|
readonly failedTotal: Schema.Number;
|
|
@@ -201,7 +201,7 @@ export declare function jobsPage<const TQueues extends readonly JobsQueue[]>(...
|
|
|
201
201
|
}>>;
|
|
202
202
|
readonly queuedTotal: Schema.Number;
|
|
203
203
|
readonly readyTotal: Schema.Number;
|
|
204
|
-
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
204
|
+
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.job", Schema.Struct<{
|
|
205
205
|
id: Schema.String;
|
|
206
206
|
queue: Schema.String;
|
|
207
207
|
}>, Schema.OptionFromNullOr<Schema.Union<Schema.Struct<{
|
|
@@ -216,7 +216,7 @@ export declare function jobsPage<const TQueues extends readonly JobsQueue[]>(...
|
|
|
216
216
|
readonly state: Schema.Literals<readonly ["created", "retry", "active", "completed", "cancelled", "failed"]>;
|
|
217
217
|
readonly data: TQueues[number]["schema"];
|
|
218
218
|
readonly queue: Schema.Literal<string>;
|
|
219
|
-
}>[]>>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
219
|
+
}>[]>>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never>, readonly [], undefined>;
|
|
220
220
|
} & {
|
|
221
221
|
queues: TQueues;
|
|
222
222
|
};
|
package/dist/jobs/retryPage.d.ts
CHANGED
|
@@ -154,14 +154,14 @@ export declare function jobsPageWithRetry<const TQueues extends readonly JobsQue
|
|
|
154
154
|
}>]>>;
|
|
155
155
|
}>, Schema.Struct<{
|
|
156
156
|
readonly _tag: Schema.Literal<"Resync">;
|
|
157
|
-
}>]>, Schema.Never>, Schema.Never, typeof import("
|
|
157
|
+
}>]>, Schema.Never>, Schema.Never, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.counts", Schema.Struct<{
|
|
158
158
|
queue: Schema.String;
|
|
159
159
|
}>, Schema.Struct<{
|
|
160
160
|
readonly activeCount: Schema.Number;
|
|
161
161
|
readonly failedCount: Schema.Number;
|
|
162
162
|
readonly queuedCount: Schema.Number;
|
|
163
163
|
readonly totalCount: Schema.Number;
|
|
164
|
-
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
164
|
+
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.deadLettered", Schema.Struct<{
|
|
165
165
|
cursor: Schema.optionalKey<Schema.String>;
|
|
166
166
|
limit: Schema.Number;
|
|
167
167
|
queue: Schema.String;
|
|
@@ -178,9 +178,11 @@ export declare function jobsPageWithRetry<const TQueues extends readonly JobsQue
|
|
|
178
178
|
readonly state: Schema.Literals<readonly ["created", "retry", "active", "completed", "cancelled", "failed"]>;
|
|
179
179
|
readonly data: [...TQueues][number]["schema"];
|
|
180
180
|
readonly queue: Schema.Literal<string>;
|
|
181
|
+
readonly sourceId: Schema.OptionFromNullOr<Schema.String>;
|
|
182
|
+
readonly sourceOutput: Schema.Unknown;
|
|
181
183
|
}>[]>>;
|
|
182
184
|
readonly next: Schema.OptionFromNullOr<Schema.String>;
|
|
183
|
-
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
185
|
+
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.failed", Schema.Struct<{
|
|
184
186
|
cursor: Schema.optionalKey<Schema.String>;
|
|
185
187
|
limit: Schema.Number;
|
|
186
188
|
queue: Schema.String;
|
|
@@ -197,11 +199,23 @@ export declare function jobsPageWithRetry<const TQueues extends readonly JobsQue
|
|
|
197
199
|
readonly state: Schema.Literals<readonly ["created", "retry", "active", "completed", "cancelled", "failed"]>;
|
|
198
200
|
readonly data: [...TQueues][number]["schema"];
|
|
199
201
|
readonly queue: Schema.Literal<string>;
|
|
200
|
-
readonly sourceId: Schema.OptionFromNullOr<Schema.String>;
|
|
201
|
-
readonly sourceOutput: Schema.Unknown;
|
|
202
202
|
}>[]>>;
|
|
203
203
|
readonly next: Schema.OptionFromNullOr<Schema.String>;
|
|
204
|
-
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
204
|
+
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.health", Schema.Void, Schema.Struct<{
|
|
205
|
+
readonly activeTotal: Schema.Number;
|
|
206
|
+
readonly deadLetteredTotal: Schema.Number;
|
|
207
|
+
readonly failedTotal: Schema.Number;
|
|
208
|
+
readonly jobs: Schema.$Array<Schema.Struct<{
|
|
209
|
+
readonly activeCount: Schema.Number;
|
|
210
|
+
readonly deadLetteredCount: Schema.Number;
|
|
211
|
+
readonly failedCount: Schema.Number;
|
|
212
|
+
readonly name: Schema.String;
|
|
213
|
+
readonly queuedCount: Schema.Number;
|
|
214
|
+
readonly readyCount: Schema.Number;
|
|
215
|
+
}>>;
|
|
216
|
+
readonly queuedTotal: Schema.Number;
|
|
217
|
+
readonly readyTotal: Schema.Number;
|
|
218
|
+
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.job", Schema.Struct<{
|
|
205
219
|
id: Schema.String;
|
|
206
220
|
queue: Schema.String;
|
|
207
221
|
}>, Schema.OptionFromNullOr<Schema.Union<Schema.Struct<{
|
|
@@ -216,24 +230,10 @@ export declare function jobsPageWithRetry<const TQueues extends readonly JobsQue
|
|
|
216
230
|
readonly state: Schema.Literals<readonly ["created", "retry", "active", "completed", "cancelled", "failed"]>;
|
|
217
231
|
readonly data: [...TQueues][number]["schema"];
|
|
218
232
|
readonly queue: Schema.Literal<string>;
|
|
219
|
-
}>[]>>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("
|
|
233
|
+
}>[]>>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.jobs.retry", Schema.Struct<{
|
|
220
234
|
id: Schema.String;
|
|
221
235
|
queue: Schema.String;
|
|
222
|
-
}>, Schema.Void, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered | typeof JobsNotFailed)[]>, typeof import("
|
|
223
|
-
readonly activeTotal: Schema.Number;
|
|
224
|
-
readonly deadLetteredTotal: Schema.Number;
|
|
225
|
-
readonly failedTotal: Schema.Number;
|
|
226
|
-
readonly jobs: Schema.$Array<Schema.Struct<{
|
|
227
|
-
readonly activeCount: Schema.Number;
|
|
228
|
-
readonly deadLetteredCount: Schema.Number;
|
|
229
|
-
readonly failedCount: Schema.Number;
|
|
230
|
-
readonly name: Schema.String;
|
|
231
|
-
readonly queuedCount: Schema.Number;
|
|
232
|
-
readonly readyCount: Schema.Number;
|
|
233
|
-
}>>;
|
|
234
|
-
readonly queuedTotal: Schema.Number;
|
|
235
|
-
readonly readyTotal: Schema.Number;
|
|
236
|
-
}>, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered)[]>, typeof import("@shivaedev/platform/rpc/middleware.ts").AuthenticatedAdmin, never>, readonly [], undefined>;
|
|
236
|
+
}>, Schema.Void, Schema.Union<readonly (typeof import("./schemas.ts").JobsUnavailable | typeof import("./schemas.ts").JobsPayloadInvalid | typeof import("./schemas.ts").JobsQueueUnregistered | typeof JobsNotFailed)[]>, typeof import("../permissions.ts").AdminPermission, never>, readonly [], undefined>;
|
|
237
237
|
} & {
|
|
238
238
|
queues: TQueues;
|
|
239
239
|
};
|
package/dist/page.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
2
2
|
import type { Contract } from "@shivaedev/effect-contract/contract.ts";
|
|
3
3
|
import type { CommandShape, QueryShape, StreamShape } from "@shivaedev/effect-contract/operation.ts";
|
|
4
|
-
import {
|
|
4
|
+
import { AdminPermission } from "#permissions.ts";
|
|
5
5
|
export interface AdminRoute {
|
|
6
6
|
readonly path: string;
|
|
7
7
|
readonly title: string;
|
|
@@ -22,7 +22,7 @@ export declare function adminPage<const TId extends string, const TName extends
|
|
|
22
22
|
readonly contract: Contract<TName, TQueries, TCommands, TRpcs, TStreams>;
|
|
23
23
|
}): AdminPage & {
|
|
24
24
|
readonly id: TId;
|
|
25
|
-
readonly contract: Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs, typeof
|
|
25
|
+
readonly contract: Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs, typeof AdminPermission>, TStreams>;
|
|
26
26
|
};
|
|
27
27
|
export {};
|
|
28
28
|
//# sourceMappingURL=page.d.ts.map
|
package/dist/page.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,wCAAwC,CAAC;AACvE,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,yCAAyC,CAAC;AACrG,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../src/page.ts"],"names":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AC,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;CACzE,GAAG,SAAS,GAAG;IACf,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC;IACjB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,CAAC,aAAa,CAAC,KAAK,EAAE,OAAO,eAAe,CAAC,EAAE,QAAQ,CAAC,CAAC;CACpH,CAEA"}
|
package/dist/page.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AdminPermission } from "#permissions.ts";
|
|
2
2
|
const AdminPageTypeId = Symbol.for("@shivaedev/admin/Page");
|
|
3
3
|
export function adminPage(page) {
|
|
4
|
-
return { ...page, [AdminPageTypeId]: true, contract: page.contract.middleware(
|
|
4
|
+
return { ...page, [AdminPageTypeId]: true, contract: page.contract.middleware(AdminPermission) };
|
|
5
5
|
}
|
|
6
6
|
//# sourceMappingURL=page.js.map
|
package/dist/page.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page.js","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAOlD,MAAM,eAAe,GAAG,MAAM,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;AAY5D,MAAM,UAAU,SAAS,CAOvB,IAID;IAIA,OAAO,EAAE,GAAG,IAAI,EAAE,CAAC,eAAe,CAAC,EAAE,IAAa,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,eAAe,CAAC,EAAE,CAAC;AAC3G,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const OpenAdmin: {
|
|
2
|
+
readonly any: {
|
|
3
|
+
readonly name: "admin.open";
|
|
4
|
+
readonly strength: "any";
|
|
5
|
+
};
|
|
6
|
+
readonly name: "admin.open";
|
|
7
|
+
readonly strength: "own";
|
|
8
|
+
};
|
|
9
|
+
declare const AdminPermission_base: import("effect/rpc/RpcMiddleware").ServiceClass<AdminPermission, "@shivaedev/admin/Permission", import("@shivaedev/platform/rpc/identity.ts").Identity | import("@shivaedev/platform/rpc/identity.ts").SessionUser, import("effect/Schema").Union<readonly [typeof import("@shivaedev/effect-contract/errors/taxonomy.ts").Unauthorized, typeof import("@shivaedev/effect-contract/errors/taxonomy.ts").AuthUnavailable, typeof import("@shivaedev/effect-contract/errors/taxonomy.ts").Forbidden]>, never, never, false> & {
|
|
10
|
+
permission: import("@shivaedev/access/permission.ts").Permission;
|
|
11
|
+
};
|
|
12
|
+
export declare class AdminPermission extends AdminPermission_base {
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=permissions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permissions.d.ts","sourceRoot":"","sources":["../src/permissions.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,SAAS;;;;;;;CAA2B,CAAC;;;;AAClD,qBAAa,eAAgB,SAAQ,oBAAiF;CAAG"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { permission } from "@shivaedev/access/permission.ts";
|
|
2
|
+
import { permissionMiddleware } from "@shivaedev/platform/rpc/permission.ts";
|
|
3
|
+
export const OpenAdmin = permission("admin.open");
|
|
4
|
+
export class AdminPermission extends permissionMiddleware()("@shivaedev/admin/Permission", OpenAdmin) {
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=permissions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"permissions.js","sourceRoot":"","sources":["../src/permissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAE7E,MAAM,CAAC,MAAM,SAAS,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;AAClD,MAAM,OAAO,eAAgB,SAAQ,oBAAoB,EAAmB,CAAC,6BAA6B,EAAE,SAAS,CAAC;CAAG"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const usersHandlers: import("effect/Layer").Layer<import("effect/rpc/Rpc").Handler<"admin.users.changeRole"> | import("effect/rpc/Rpc").Handler<"admin.users.list">, never, import("effect/reactivity/Reactivity").Reactivity | import("effect/sql/SqlClient").SqlClient>;
|
|
2
|
+
//# sourceMappingURL=usersHandlers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usersHandlers.d.ts","sourceRoot":"","sources":["../../src/server/usersHandlers.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,aAAa,sPAOxB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { SessionUser } from "@shivaedev/platform/rpc/identity.ts";
|
|
3
|
+
import { hasPermission } from "@shivaedev/platform/rpc/permission.ts";
|
|
4
|
+
import { ChangeUserRoles } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
5
|
+
import { changeUserRole, listUsers } from "@shivaedev/platform/server/better-auth/users.ts";
|
|
6
|
+
import { UsersPage } from "#users/page.ts";
|
|
7
|
+
export const usersHandlers = UsersPage.contract.application.toLayer({
|
|
8
|
+
"admin.users.changeRole": changeUserRole,
|
|
9
|
+
"admin.users.list": (input) => Effect.gen(function* () {
|
|
10
|
+
const user = yield* SessionUser;
|
|
11
|
+
return { ...(yield* listUsers(input)), canChangeRoles: hasPermission(user, ChangeUserRoles) };
|
|
12
|
+
}),
|
|
13
|
+
});
|
|
14
|
+
//# sourceMappingURL=usersHandlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usersHandlers.js","sourceRoot":"","sources":["../../src/server/usersHandlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,WAAW,EAAE,MAAM,qCAAqC,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,iDAAiD,CAAC;AAC5F,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,CAAC,MAAM,aAAa,GAAG,SAAS,CAAC,QAAQ,CAAC,WAAW,CAAC,OAAO,CAAC;IACnE,wBAAwB,EAAE,cAAc;IACxC,kBAAkB,EAAE,CAAC,KAAK,EAAE,EAAE,CAC7B,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnB,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,WAAW,CAAC;QAChC,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,cAAc,EAAE,aAAa,CAAC,IAAI,EAAE,eAAe,CAAC,EAAE,CAAC;IAC/F,CAAC,CAAC;CACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { AuthUnavailable, Forbidden, NotFound } from "@shivaedev/effect-contract/errors/taxonomy.ts";
|
|
3
|
+
import { LastOwner } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
4
|
+
export declare const UsersList: Schema.Struct<{
|
|
5
|
+
readonly next: Schema.OptionFromNullOr<Schema.String>;
|
|
6
|
+
readonly users: Schema.$Array<Schema.Struct<{
|
|
7
|
+
readonly createdAt: Schema.DateTimeUtcFromString;
|
|
8
|
+
readonly email: Schema.String;
|
|
9
|
+
readonly id: Schema.String;
|
|
10
|
+
readonly name: Schema.String;
|
|
11
|
+
readonly roles: Schema.$Array<Schema.String>;
|
|
12
|
+
}>>;
|
|
13
|
+
readonly canChangeRoles: Schema.Boolean;
|
|
14
|
+
}>;
|
|
15
|
+
export declare const UsersPage: import("#page.ts").AdminPage & {
|
|
16
|
+
readonly id: "users";
|
|
17
|
+
readonly contract: import("@shivaedev/effect-contract/contract.ts").Contract<"admin.users", readonly [NoInfer<import("@shivaedev/effect-contract/operation.ts").Query<"list", Schema.Struct<{
|
|
18
|
+
readonly cursor: Schema.optionalKey<Schema.String>;
|
|
19
|
+
readonly limit: Schema.Number;
|
|
20
|
+
}>, Schema.Struct<{
|
|
21
|
+
readonly next: Schema.OptionFromNullOr<Schema.String>;
|
|
22
|
+
readonly users: Schema.$Array<Schema.Struct<{
|
|
23
|
+
readonly createdAt: Schema.DateTimeUtcFromString;
|
|
24
|
+
readonly email: Schema.String;
|
|
25
|
+
readonly id: Schema.String;
|
|
26
|
+
readonly name: Schema.String;
|
|
27
|
+
readonly roles: Schema.$Array<Schema.String>;
|
|
28
|
+
}>>;
|
|
29
|
+
readonly canChangeRoles: Schema.Boolean;
|
|
30
|
+
}>, {
|
|
31
|
+
readonly AuthUnavailable: typeof AuthUnavailable;
|
|
32
|
+
}>>], readonly [NoInfer<import("@shivaedev/effect-contract/operation.ts").Command<"changeRole", Schema.Struct<{
|
|
33
|
+
readonly grant: Schema.Boolean;
|
|
34
|
+
readonly role: Schema.Literals<readonly ["user", "admin", "owner"]>;
|
|
35
|
+
readonly userId: Schema.String;
|
|
36
|
+
}>, Schema.Void, {
|
|
37
|
+
readonly AuthUnavailable: typeof AuthUnavailable;
|
|
38
|
+
readonly Forbidden: typeof Forbidden;
|
|
39
|
+
readonly LastOwner: typeof LastOwner;
|
|
40
|
+
readonly NotFound: typeof NotFound;
|
|
41
|
+
}>>], import("effect/rpc/Rpc").Rpc<"admin.users.$changes", Schema.Struct<{
|
|
42
|
+
queries: Schema.$Array<Schema.Union<readonly Schema.Struct<{
|
|
43
|
+
query: Schema.Literal<"list">;
|
|
44
|
+
payload: Schema.Struct<{
|
|
45
|
+
readonly cursor: Schema.optionalKey<Schema.String>;
|
|
46
|
+
readonly limit: Schema.Number;
|
|
47
|
+
}>;
|
|
48
|
+
}>[]>>;
|
|
49
|
+
}>, import("effect/rpc/RpcSchema").Stream<Schema.Union<readonly [Schema.Struct<{
|
|
50
|
+
readonly _tag: Schema.Literal<"Changed">;
|
|
51
|
+
readonly keys: Schema.$Array<Schema.Union<readonly [Schema.Struct<{
|
|
52
|
+
readonly _tag: Schema.Literal<"List">;
|
|
53
|
+
readonly collection: Schema.String;
|
|
54
|
+
}>, Schema.Struct<{
|
|
55
|
+
readonly _tag: Schema.Literal<"Item">;
|
|
56
|
+
readonly collection: Schema.String;
|
|
57
|
+
readonly id: Schema.Union<readonly [Schema.String, Schema.Number]>;
|
|
58
|
+
}>]>>;
|
|
59
|
+
}>, Schema.Struct<{
|
|
60
|
+
readonly _tag: Schema.Literal<"Resync">;
|
|
61
|
+
}>]>, Schema.Never>, Schema.Never, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.users.changeRole", Schema.Struct<{
|
|
62
|
+
readonly grant: Schema.Boolean;
|
|
63
|
+
readonly role: Schema.Literals<readonly ["user", "admin", "owner"]>;
|
|
64
|
+
readonly userId: Schema.String;
|
|
65
|
+
}>, Schema.Void, Schema.Union<readonly (typeof NotFound | typeof Forbidden | typeof AuthUnavailable | typeof LastOwner)[]>, typeof import("../permissions.ts").AdminPermission, never> | import("effect/rpc/Rpc").Rpc<"admin.users.list", Schema.Struct<{
|
|
66
|
+
readonly cursor: Schema.optionalKey<Schema.String>;
|
|
67
|
+
readonly limit: Schema.Number;
|
|
68
|
+
}>, Schema.Struct<{
|
|
69
|
+
readonly next: Schema.OptionFromNullOr<Schema.String>;
|
|
70
|
+
readonly users: Schema.$Array<Schema.Struct<{
|
|
71
|
+
readonly createdAt: Schema.DateTimeUtcFromString;
|
|
72
|
+
readonly email: Schema.String;
|
|
73
|
+
readonly id: Schema.String;
|
|
74
|
+
readonly name: Schema.String;
|
|
75
|
+
readonly roles: Schema.$Array<Schema.String>;
|
|
76
|
+
}>>;
|
|
77
|
+
readonly canChangeRoles: Schema.Boolean;
|
|
78
|
+
}>, Schema.Union<readonly (typeof AuthUnavailable)[]>, typeof import("../permissions.ts").AdminPermission, never>, readonly [], undefined>;
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=page.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../../src/users/page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,+CAA+C,CAAC;AAGrG,OAAO,EAAmB,SAAS,EAA2B,MAAM,sCAAsC,CAAC;AAK3G,eAAO,MAAM,SAAS;;;;;;;;;;EAAwE,CAAC;AAC/F,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoBpB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { contract } from "@shivaedev/effect-contract/contract.ts";
|
|
3
|
+
import { AuthUnavailable, Forbidden, NotFound } from "@shivaedev/effect-contract/errors/taxonomy.ts";
|
|
4
|
+
import { collection } from "@shivaedev/effect-contract/keys.ts";
|
|
5
|
+
import { command, query } from "@shivaedev/effect-contract/operation.ts";
|
|
6
|
+
import { ChangeRoleInput, LastOwner, UserList, UserListInput } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
7
|
+
import { adminPage } from "#page.ts";
|
|
8
|
+
const users = collection("admin.users", Schema.String);
|
|
9
|
+
export const UsersList = Schema.Struct({ ...UserList.fields, canChangeRoles: Schema.Boolean });
|
|
10
|
+
export const UsersPage = adminPage({
|
|
11
|
+
contract: contract("admin.users", {
|
|
12
|
+
commands: [
|
|
13
|
+
command("changeRole", {
|
|
14
|
+
invalidates: () => [users.list],
|
|
15
|
+
payload: ChangeRoleInput.fields,
|
|
16
|
+
rejections: { AuthUnavailable, Forbidden, LastOwner, NotFound },
|
|
17
|
+
}),
|
|
18
|
+
],
|
|
19
|
+
queries: [
|
|
20
|
+
query("list", {
|
|
21
|
+
payload: UserListInput.fields,
|
|
22
|
+
reads: () => [users.list],
|
|
23
|
+
rejections: { AuthUnavailable },
|
|
24
|
+
success: UsersList,
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
}),
|
|
28
|
+
id: "users",
|
|
29
|
+
route: { path: "/admin/users", title: "Users" },
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=page.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../../src/users/page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,wCAAwC,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,+CAA+C,CAAC;AACrG,OAAO,EAAE,UAAU,EAAE,MAAM,oCAAoC,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,yCAAyC,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAC3G,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,MAAM,KAAK,GAAG,UAAU,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;AAEvD,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,QAAQ,CAAC,MAAM,EAAE,cAAc,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;AAC/F,MAAM,CAAC,MAAM,SAAS,GAAG,SAAS,CAAC;IAClC,QAAQ,EAAE,QAAQ,CAAC,aAAa,EAAE;QACjC,QAAQ,EAAE;YACT,OAAO,CAAC,YAAY,EAAE;gBACrB,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;gBAC/B,OAAO,EAAE,eAAe,CAAC,MAAM;gBAC/B,UAAU,EAAE,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE;aAC/D,CAAC;SACF;QACD,OAAO,EAAE;YACR,KAAK,CAAC,MAAM,EAAE;gBACb,OAAO,EAAE,aAAa,CAAC,MAAM;gBAC7B,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;gBACzB,UAAU,EAAE,EAAE,eAAe,EAAE;gBAC/B,OAAO,EAAE,SAAS;aAClB,CAAC;SACF;KACD,CAAC;IACF,EAAE,EAAE,OAAO;IACX,KAAK,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE;CAC/C,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/admin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Explicit admin page contracts and a protected native Effect RPC registry",
|
|
5
5
|
"homepage": "https://github.com/ShivaeDev/platform/tree/main/packages/admin#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -34,26 +34,38 @@
|
|
|
34
34
|
"dist",
|
|
35
35
|
"src",
|
|
36
36
|
"!src/**/*.test.ts",
|
|
37
|
+
"!src/**/*.test.tsx",
|
|
37
38
|
"!src/**/*.spec.ts",
|
|
39
|
+
"!src/**/*.spec.tsx",
|
|
38
40
|
"!src/**/test-support",
|
|
39
41
|
"README.md"
|
|
40
42
|
],
|
|
41
43
|
"dependencies": {
|
|
42
|
-
"@shivaedev/
|
|
44
|
+
"@shivaedev/access": "0.1.0",
|
|
45
|
+
"@shivaedev/effect-contract": "0.16.0",
|
|
43
46
|
"@shivaedev/effect-pg-boss": "0.19.0",
|
|
44
|
-
"@shivaedev/
|
|
47
|
+
"@shivaedev/effect-react": "0.18.1",
|
|
48
|
+
"@shivaedev/platform": "0.34.0"
|
|
45
49
|
},
|
|
46
50
|
"devDependencies": {
|
|
51
|
+
"@effect/atom-react": "4.0.5",
|
|
47
52
|
"@shivaedev/effect-test-sql": "0.9.3",
|
|
48
|
-
"@shivaedev/quality": "0.
|
|
49
|
-
"@shivaedev/test-story": "0.20.
|
|
53
|
+
"@shivaedev/quality": "0.41.1",
|
|
54
|
+
"@shivaedev/test-story": "0.20.1",
|
|
50
55
|
"@types/node": "24.19.2",
|
|
56
|
+
"@types/react": "19.3.0",
|
|
57
|
+
"@types/react-dom": "19.3.0",
|
|
51
58
|
"effect": "4.0.5",
|
|
59
|
+
"happy-dom": "21.0.3",
|
|
60
|
+
"react": "19.3.1",
|
|
61
|
+
"react-dom": "19.3.1",
|
|
52
62
|
"typescript": "7.0.2",
|
|
53
63
|
"vitest": "5.0.5"
|
|
54
64
|
},
|
|
55
65
|
"peerDependencies": {
|
|
56
|
-
"effect": "4.0.5"
|
|
66
|
+
"@effect/atom-react": "4.0.5",
|
|
67
|
+
"effect": "4.0.5",
|
|
68
|
+
"react": "19.3.1"
|
|
57
69
|
},
|
|
58
70
|
"engines": {
|
|
59
71
|
"node": ">=24"
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { DateTime } from "effect";
|
|
2
|
+
import { type AppUser, type ChangeRoleInput, UserRole } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
3
|
+
import type { UsersList } from "#users/page.ts";
|
|
4
|
+
|
|
5
|
+
interface RoleControls {
|
|
6
|
+
readonly dispatch: (input: typeof ChangeRoleInput.Type) => void;
|
|
7
|
+
readonly pending: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function UserHeaders() {
|
|
11
|
+
return (
|
|
12
|
+
<tr>
|
|
13
|
+
{["Name", "Email", "Roles", "Signed up"].map((label) => (
|
|
14
|
+
<th className="whitespace-nowrap p-3" key={label} scope="col">
|
|
15
|
+
{label}
|
|
16
|
+
</th>
|
|
17
|
+
))}
|
|
18
|
+
</tr>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function RoleCheckbox({ user, role, pending, dispatch }: RoleControls & { readonly user: typeof AppUser.Type; readonly role: typeof UserRole.Type }) {
|
|
23
|
+
return (
|
|
24
|
+
<label className="mr-3 inline-flex gap-1">
|
|
25
|
+
<input
|
|
26
|
+
aria-label={`${role} for ${user.email}`}
|
|
27
|
+
checked={user.roles.includes(role)}
|
|
28
|
+
disabled={pending}
|
|
29
|
+
onChange={(event) => dispatch({ grant: event.target.checked, role, userId: user.id })}
|
|
30
|
+
type="checkbox"
|
|
31
|
+
/>
|
|
32
|
+
{role}
|
|
33
|
+
</label>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function UserRoles({
|
|
38
|
+
user,
|
|
39
|
+
canChangeRoles,
|
|
40
|
+
dispatch,
|
|
41
|
+
pending,
|
|
42
|
+
}: RoleControls & {
|
|
43
|
+
readonly user: typeof AppUser.Type;
|
|
44
|
+
readonly canChangeRoles: boolean;
|
|
45
|
+
}) {
|
|
46
|
+
return (
|
|
47
|
+
<div>
|
|
48
|
+
<p>{user.roles.join(", ") || "No roles"}</p>
|
|
49
|
+
{canChangeRoles
|
|
50
|
+
? UserRole.literals.map((role) => <RoleCheckbox dispatch={dispatch} key={role} pending={pending} role={role} user={user} />)
|
|
51
|
+
: null}
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function UserRow({
|
|
57
|
+
user,
|
|
58
|
+
...controls
|
|
59
|
+
}: RoleControls & {
|
|
60
|
+
readonly user: typeof AppUser.Type;
|
|
61
|
+
readonly canChangeRoles: boolean;
|
|
62
|
+
}) {
|
|
63
|
+
return (
|
|
64
|
+
<tr className="border-b">
|
|
65
|
+
<td className="p-3">{user.name}</td>
|
|
66
|
+
<td className="p-3">{user.email}</td>
|
|
67
|
+
<td className="p-3">
|
|
68
|
+
<UserRoles {...controls} user={user} />
|
|
69
|
+
</td>
|
|
70
|
+
<td className="whitespace-nowrap p-3">
|
|
71
|
+
<time dateTime={DateTime.formatIso(user.createdAt)}>{DateTime.formatIso(user.createdAt)}</time>
|
|
72
|
+
</td>
|
|
73
|
+
</tr>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function UserRows({ data, ...controls }: RoleControls & { readonly data: typeof UsersList.Type }) {
|
|
78
|
+
return (
|
|
79
|
+
<div className="overflow-x-auto rounded-md border">
|
|
80
|
+
<table className="w-full text-left text-sm">
|
|
81
|
+
<caption className="sr-only">App users and roles</caption>
|
|
82
|
+
<thead className="bg-muted">
|
|
83
|
+
<UserHeaders />
|
|
84
|
+
</thead>
|
|
85
|
+
<tbody>
|
|
86
|
+
{data.users.map((user) => (
|
|
87
|
+
<UserRow {...controls} canChangeRoles={data.canChangeRoles} key={user.id} user={user} />
|
|
88
|
+
))}
|
|
89
|
+
</tbody>
|
|
90
|
+
</table>
|
|
91
|
+
</div>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function UsersTable({ data, ...controls }: RoleControls & { readonly data: typeof UsersList.Type }) {
|
|
96
|
+
return (
|
|
97
|
+
<div className="space-y-4">
|
|
98
|
+
{data.canChangeRoles ? null : <p>Only owners can change roles.</p>}
|
|
99
|
+
{data.users.length === 0 ? <p>No users.</p> : <UserRows {...controls} data={data} />}
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Cause, Option, Predicate } from "effect";
|
|
2
|
+
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
import { createElement, useState } from "react";
|
|
5
|
+
import { useAction, useQuery } from "@shivaedev/effect-react/client/resultState.ts";
|
|
6
|
+
import type { ChangeRoleInput, UserListInput } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
7
|
+
import type { UsersList } from "#users/page.ts";
|
|
8
|
+
import { UsersTable } from "./UsersTable.tsx";
|
|
9
|
+
|
|
10
|
+
export interface UsersViewProps {
|
|
11
|
+
readonly changeRole: Atom.AtomResultFn<typeof ChangeRoleInput.Type, void, unknown>;
|
|
12
|
+
readonly list: (input: typeof UserListInput.Type) => Atom.Atom<AsyncResult.AsyncResult<typeof UsersList.Type, unknown>>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function failure(cause: Cause.Cause<unknown>): string {
|
|
16
|
+
const error = Option.getOrUndefined(Cause.findErrorOption(cause));
|
|
17
|
+
if (Predicate.isTagged(error, "LastOwner")) {
|
|
18
|
+
return "The last owner cannot be demoted. Grant owner to another user first.";
|
|
19
|
+
}
|
|
20
|
+
if (Predicate.isTagged(error, "Forbidden")) {
|
|
21
|
+
return "Owner role required to change roles.";
|
|
22
|
+
}
|
|
23
|
+
return "Could not change the role. Try again.";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function UsersView({ list, changeRole }: UsersViewProps) {
|
|
27
|
+
const [cursors, setCursors] = useState<readonly string[]>([]);
|
|
28
|
+
const cursor = cursors.at(-1);
|
|
29
|
+
const state = useQuery(list({ ...(cursor === undefined ? {} : { cursor }), limit: 20 }));
|
|
30
|
+
const action = useAction(changeRole);
|
|
31
|
+
const data = Option.getOrUndefined(state.data);
|
|
32
|
+
return createElement(
|
|
33
|
+
"section",
|
|
34
|
+
{ "aria-label": "App users", className: "space-y-4" },
|
|
35
|
+
createElement(
|
|
36
|
+
"button",
|
|
37
|
+
{ className: "rounded-md border px-3 py-2 disabled:opacity-50", disabled: state.pending, onClick: state.refresh, type: "button" },
|
|
38
|
+
"Refresh users",
|
|
39
|
+
),
|
|
40
|
+
state.pending ? createElement("p", { role: "status" }, "Loading users…") : null,
|
|
41
|
+
Option.isSome(state.cause) ? createElement("p", { role: "alert" }, "Could not load users. Try refreshing.") : null,
|
|
42
|
+
Option.isSome(action.cause) ? createElement("p", { role: "alert" }, failure(action.cause.value)) : null,
|
|
43
|
+
data === undefined ? null : createElement(UsersTable, { data, dispatch: action.dispatch, pending: action.pending }),
|
|
44
|
+
data === undefined
|
|
45
|
+
? null
|
|
46
|
+
: createElement(
|
|
47
|
+
"nav",
|
|
48
|
+
{ "aria-label": "User pages", className: "flex gap-4" },
|
|
49
|
+
createElement(
|
|
50
|
+
"button",
|
|
51
|
+
{
|
|
52
|
+
className: "rounded-md border px-3 py-2 disabled:opacity-50",
|
|
53
|
+
disabled: cursors.length === 0 || state.pending,
|
|
54
|
+
onClick: () => setCursors(cursors.slice(0, -1)),
|
|
55
|
+
type: "button",
|
|
56
|
+
},
|
|
57
|
+
"Previous users",
|
|
58
|
+
),
|
|
59
|
+
createElement(
|
|
60
|
+
"button",
|
|
61
|
+
{
|
|
62
|
+
className: "rounded-md border px-3 py-2 disabled:opacity-50",
|
|
63
|
+
disabled: Option.isNone(data.next) || state.pending,
|
|
64
|
+
onClick: () => Option.map(data.next, (next) => setCursors([...cursors, next])),
|
|
65
|
+
type: "button",
|
|
66
|
+
},
|
|
67
|
+
"Next users",
|
|
68
|
+
),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
}
|
package/src/page.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
2
2
|
import type { Contract } from "@shivaedev/effect-contract/contract.ts";
|
|
3
3
|
import type { CommandShape, QueryShape, StreamShape } from "@shivaedev/effect-contract/operation.ts";
|
|
4
|
-
import {
|
|
4
|
+
import { AdminPermission } from "#permissions.ts";
|
|
5
5
|
|
|
6
6
|
export interface AdminRoute {
|
|
7
7
|
readonly path: string;
|
|
@@ -33,7 +33,7 @@ export function adminPage<
|
|
|
33
33
|
readonly contract: Contract<TName, TQueries, TCommands, TRpcs, TStreams>;
|
|
34
34
|
}): AdminPage & {
|
|
35
35
|
readonly id: TId;
|
|
36
|
-
readonly contract: Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs, typeof
|
|
36
|
+
readonly contract: Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs, typeof AdminPermission>, TStreams>;
|
|
37
37
|
} {
|
|
38
|
-
return { ...page, [AdminPageTypeId]: true as const, contract: page.contract.middleware(
|
|
38
|
+
return { ...page, [AdminPageTypeId]: true as const, contract: page.contract.middleware(AdminPermission) };
|
|
39
39
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { permission } from "@shivaedev/access/permission.ts";
|
|
2
|
+
import { permissionMiddleware } from "@shivaedev/platform/rpc/permission.ts";
|
|
3
|
+
|
|
4
|
+
export const OpenAdmin = permission("admin.open");
|
|
5
|
+
export class AdminPermission extends permissionMiddleware<AdminPermission>()("@shivaedev/admin/Permission", OpenAdmin) {}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { SessionUser } from "@shivaedev/platform/rpc/identity.ts";
|
|
3
|
+
import { hasPermission } from "@shivaedev/platform/rpc/permission.ts";
|
|
4
|
+
import { ChangeUserRoles } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
5
|
+
import { changeUserRole, listUsers } from "@shivaedev/platform/server/better-auth/users.ts";
|
|
6
|
+
import { UsersPage } from "#users/page.ts";
|
|
7
|
+
|
|
8
|
+
export const usersHandlers = UsersPage.contract.application.toLayer({
|
|
9
|
+
"admin.users.changeRole": changeUserRole,
|
|
10
|
+
"admin.users.list": (input) =>
|
|
11
|
+
Effect.gen(function* () {
|
|
12
|
+
const user = yield* SessionUser;
|
|
13
|
+
return { ...(yield* listUsers(input)), canChangeRoles: hasPermission(user, ChangeUserRoles) };
|
|
14
|
+
}),
|
|
15
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { contract } from "@shivaedev/effect-contract/contract.ts";
|
|
3
|
+
import { AuthUnavailable, Forbidden, NotFound } from "@shivaedev/effect-contract/errors/taxonomy.ts";
|
|
4
|
+
import { collection } from "@shivaedev/effect-contract/keys.ts";
|
|
5
|
+
import { command, query } from "@shivaedev/effect-contract/operation.ts";
|
|
6
|
+
import { ChangeRoleInput, LastOwner, UserList, UserListInput } from "@shivaedev/platform/rpc/userRoles.ts";
|
|
7
|
+
import { adminPage } from "#page.ts";
|
|
8
|
+
|
|
9
|
+
const users = collection("admin.users", Schema.String);
|
|
10
|
+
|
|
11
|
+
export const UsersList = Schema.Struct({ ...UserList.fields, canChangeRoles: Schema.Boolean });
|
|
12
|
+
export const UsersPage = adminPage({
|
|
13
|
+
contract: contract("admin.users", {
|
|
14
|
+
commands: [
|
|
15
|
+
command("changeRole", {
|
|
16
|
+
invalidates: () => [users.list],
|
|
17
|
+
payload: ChangeRoleInput.fields,
|
|
18
|
+
rejections: { AuthUnavailable, Forbidden, LastOwner, NotFound },
|
|
19
|
+
}),
|
|
20
|
+
],
|
|
21
|
+
queries: [
|
|
22
|
+
query("list", {
|
|
23
|
+
payload: UserListInput.fields,
|
|
24
|
+
reads: () => [users.list],
|
|
25
|
+
rejections: { AuthUnavailable },
|
|
26
|
+
success: UsersList,
|
|
27
|
+
}),
|
|
28
|
+
],
|
|
29
|
+
}),
|
|
30
|
+
id: "users",
|
|
31
|
+
route: { path: "/admin/users", title: "Users" },
|
|
32
|
+
});
|