@rsc-kit/mcp 0.30.38 → 0.30.39
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/recipes.js +164 -1
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +109 -0
- package/guides/admin.md +297 -0
- package/guides/index.json +10 -0
- package/guides/server-actions.md +47 -0
- package/package.json +2 -2
package/dist/recipes.js
CHANGED
|
@@ -216,7 +216,17 @@ the same selector showing a #results-skeleton.
|
|
|
216
216
|
Do NOT patch around a skeleton on an in-page search with your own useState,
|
|
217
217
|
router.replace or a Suspense key: the form already does this. It is the form
|
|
218
218
|
only - a <Link> or visit() to the same path with another query is a page of its
|
|
219
|
-
own. A search to ANOTHER page still shows that page's shell first
|
|
219
|
+
own. A search to ANOTHER page still shows that page's shell first.
|
|
220
|
+
|
|
221
|
+
A MESSAGE FOR THE NEXT PAGE ("Saved." after a save that redirects): the return
|
|
222
|
+
value goes to the form, which the redirect replaced. Call
|
|
223
|
+
await flash('Product saved.') from @rsc-kit/core/flash in the action before
|
|
224
|
+
redirect(), and render ONE client component in the root layout:
|
|
225
|
+
useFlash(({ message, kind }) => toast(message)) from @rsc-kit/core/useFlash.
|
|
226
|
+
kind: 'success' (default) | 'error' | 'info' | 'warning'. flash() sets a cookie:
|
|
227
|
+
an action, middleware or route.ts, NEVER a page. The browser takes it after the
|
|
228
|
+
navigation (or when the cookie changes) and clears it, so a stored page stays
|
|
229
|
+
cacheable. Do NOT read it in a server component, and render it as text.`,
|
|
220
230
|
},
|
|
221
231
|
{
|
|
222
232
|
topic: 'prefetch',
|
|
@@ -2639,6 +2649,159 @@ That limit is narrower than Next's: the action's logic is a unit test here.
|
|
|
2639
2649
|
Do NOT start a dev server, spawn a process or pick a port in a test. Do NOT
|
|
2640
2650
|
add a second runner. The one in tests/ goes through the real build already.`,
|
|
2641
2651
|
},
|
|
2652
|
+
{
|
|
2653
|
+
topic: 'admin',
|
|
2654
|
+
summary: 'An admin panel with @rsc-kit/admin: resources over any data source, server or client tables, Standard Schema forms, auth, tenants, plugins',
|
|
2655
|
+
body: `@rsc-kit/admin draws a shadcn admin panel (sidebar, data table, dialogs)
|
|
2656
|
+
from resources. The app keeps what is its own: where the data lives, who may
|
|
2657
|
+
see it, whose it is. Full guides: admin, admin-plugins.
|
|
2658
|
+
|
|
2659
|
+
SETUP: the app needs Tailwind v4 + shadcn (create-rsc-kit --tailwind --shadcn).
|
|
2660
|
+
In the app's stylesheet, after tailwind and shadcn:
|
|
2661
|
+
@import "@rsc-kit/admin/styles.css";
|
|
2662
|
+
Without it the panel renders unstyled - the line is Tailwind's @source for the
|
|
2663
|
+
panel's components.
|
|
2664
|
+
|
|
2665
|
+
A RESOURCE:
|
|
2666
|
+
\`\`\`ts
|
|
2667
|
+
import * as z from 'zod'
|
|
2668
|
+
import { defineResource } from '@rsc-kit/admin'
|
|
2669
|
+
|
|
2670
|
+
export const products = defineResource<Product>()({
|
|
2671
|
+
name: 'products', key: 'id', title: 'name',
|
|
2672
|
+
icon: Package, // lucide-react, optional
|
|
2673
|
+
source: productsSource, // a DataSource, below
|
|
2674
|
+
mode: 'server', // or 'client'; server is the default
|
|
2675
|
+
table: (col) => [
|
|
2676
|
+
col.text('name').searchable().sortable(),
|
|
2677
|
+
col.money('price').sortable(),
|
|
2678
|
+
col.badge('status'),
|
|
2679
|
+
col.boolean('featured').hidden(), // off in the Columns menu at first
|
|
2680
|
+
col.date('createdAt'),
|
|
2681
|
+
// col.text('image').cell((value, row) => <Avatar .../>) - a server component
|
|
2682
|
+
],
|
|
2683
|
+
filters: ['status'],
|
|
2684
|
+
form: z.object({ name: z.string().min(2), price: z.number().positive(), status: z.enum(['draft', 'active']), featured: z.boolean() }),
|
|
2685
|
+
fields: { description: { type: 'textarea', description: 'Help text' } },
|
|
2686
|
+
perPage: 10,
|
|
2687
|
+
})
|
|
2688
|
+
\`\`\`
|
|
2689
|
+
|
|
2690
|
+
THE FORM IS ANY STANDARD SCHEMA - zod, valibot, arktype, Effect Schema. Use
|
|
2691
|
+
the one the app already has; do NOT add zod for the panel. The schema checks
|
|
2692
|
+
every save on the server, errors under each field. Fields come from the
|
|
2693
|
+
schema's Standard JSON Schema (zod 4.2+ has it). A schema without it MUST list
|
|
2694
|
+
every field in fields: { title: { type: 'text' } } - the panel throws saying
|
|
2695
|
+
so. A number field arrives as a number: write z.number(), NOT z.coerce.number().
|
|
2696
|
+
A switch that is off is false; an empty optional field is absent.
|
|
2697
|
+
BROWSER CHECK: the same schema's constraints become attributes (required,
|
|
2698
|
+
min/max, minlength/maxlength, an anchored pattern, type=email, step=1 for an
|
|
2699
|
+
integer); a wrong field is caught before submit and the browser's message
|
|
2700
|
+
shows in the field's FieldError. The schema never ships to the browser, so it
|
|
2701
|
+
works the same when the rules live in Laravel/Go. A courtesy only: custom
|
|
2702
|
+
messages and uniqueness come from the server. Do NOT add a client-side
|
|
2703
|
+
schema run.
|
|
2704
|
+
THEME: the header menu is Light/Dark/System (System default, follows the OS);
|
|
2705
|
+
it sets class="dark" on <html> before paint, so the root layout's <html> gets
|
|
2706
|
+
suppressHydrationWarning. modeToggle: false hides it.
|
|
2707
|
+
|
|
2708
|
+
A DATA SOURCE is five functions, and the panel calls nothing else - a
|
|
2709
|
+
database, ORM, REST API or rpc() into Laravel/Go alike:
|
|
2710
|
+
\`\`\`ts
|
|
2711
|
+
import type { DataSource } from '@rsc-kit/admin'
|
|
2712
|
+
|
|
2713
|
+
const source: DataSource<Customer, Values> = {
|
|
2714
|
+
async list({ q, sort, dir, page, perPage, filters, searchable }, { tenant }) { return { rows, total } },
|
|
2715
|
+
find: (id, { tenant }) => ..., // null -> 404, for the page AND a save/delete of that id
|
|
2716
|
+
create: (values, { tenant }) => ...,
|
|
2717
|
+
update: (id, values, { tenant }) => ...,
|
|
2718
|
+
delete: (id, { tenant }) => ...,
|
|
2719
|
+
}
|
|
2720
|
+
\`\`\`
|
|
2721
|
+
The last argument of every call is the context { tenant } (null without
|
|
2722
|
+
tenancy). On a Laravel/Go backend the functions call rpc() and the backend
|
|
2723
|
+
validates again - the panel's check is the renderer's, the backend's counts.
|
|
2724
|
+
A BACKEND 422 (unique:products,sku, a stock check) shows under the same fields
|
|
2725
|
+
as the schema's, values kept: do NOT catch it in create/update - let the rpc()
|
|
2726
|
+
call throw, and the panel's save answers it as field errors.
|
|
2727
|
+
memorySource(rows, key, make) is for tests and demos; perTenant((tenantId) =>
|
|
2728
|
+
source) keeps one source per tenant.
|
|
2729
|
+
|
|
2730
|
+
OPTIONS: an enum's values are shown humanized ('in_stock' reads "In stock")
|
|
2731
|
+
and saved as they are; fields: { status: { options: ['draft', { value:
|
|
2732
|
+
'active', label: 'Live' }] } } labels them yourself. An optional select starts
|
|
2733
|
+
on "None", which saves nothing.
|
|
2734
|
+
|
|
2735
|
+
RELATIONS (belongsTo): fields: { categoryId: { relation: 'categories' } } is a
|
|
2736
|
+
select of the categories resource's rows - its key saved, its title shown -
|
|
2737
|
+
and col.relation('categoryId', 'categories') shows the title, not the id.
|
|
2738
|
+
The choices are that resource's list() with perPage: null AS THE VISITOR
|
|
2739
|
+
(tenant-scoped), so relate to a resource with a select's worth of rows. The
|
|
2740
|
+
save checks the posted id with the related source's find() as the visitor: an
|
|
2741
|
+
id from another tenant is refused under the field ("Choose one of the listed
|
|
2742
|
+
categories."). A z.number() relation arrives as a number. A relation to a
|
|
2743
|
+
resource the panel does not have throws when the panel is defined.
|
|
2744
|
+
|
|
2745
|
+
BULK ACTIONS: a resource that is not readOnly ticks rows and offers the
|
|
2746
|
+
panel's own Delete below the table. bulkActions: [{ name: 'publish', label:
|
|
2747
|
+
'Publish', destructive?: true, run: (rows, context) => ... }] adds more. run
|
|
2748
|
+
gets ROWS, each found with the source's find() as the visitor - an id that is
|
|
2749
|
+
not theirs never reaches it - after authorize. destructive asks first. The
|
|
2750
|
+
toast says "Publish: 3 products." unless run returns a string. 'delete' is
|
|
2751
|
+
reserved, and a readOnly resource with bulkActions throws.
|
|
2752
|
+
|
|
2753
|
+
TABLE MODE: 'server' - each search/filter/sort/page is a request, list() gets
|
|
2754
|
+
one page (perPage a number), state is in the url (links, back button, works
|
|
2755
|
+
without JS); for data that grows. 'client' - list() is called once with
|
|
2756
|
+
perPage: null and returns EVERY row; search, filters, sort and paging run in
|
|
2757
|
+
the browser; for small data (hundreds of rows). A source must handle
|
|
2758
|
+
perPage: null if any resource using it is client mode.
|
|
2759
|
+
|
|
2760
|
+
THE PANEL:
|
|
2761
|
+
\`\`\`ts
|
|
2762
|
+
import { definePanel } from '@rsc-kit/admin'
|
|
2763
|
+
|
|
2764
|
+
export const panel = definePanel({
|
|
2765
|
+
path: '/admin',
|
|
2766
|
+
resources: [products, customers],
|
|
2767
|
+
authorize: async () => (await currentUser())?.isAdmin === true,
|
|
2768
|
+
user: currentUser, // { name, email?, avatar? } for the sidebar footer
|
|
2769
|
+
signOut: () => endSession(), // runs in a server action, lands on /
|
|
2770
|
+
tenancy: { label: 'Teams', tenants: () => teamsOfUser(), current: () => teamFromCookie(), switch: (id) => setTeamCookie(id) },
|
|
2771
|
+
plugins: [auditLog()],
|
|
2772
|
+
})
|
|
2773
|
+
\`\`\`
|
|
2774
|
+
AUTH: the panel has no login of its own; authorize asks the app's. It runs on
|
|
2775
|
+
every page AND every save, delete and tenant switch (actions are public
|
|
2776
|
+
endpoints). A refusal is a 404. Do NOT guard only the pages.
|
|
2777
|
+
TENANTS: current() is read from the session; the panel refuses a current
|
|
2778
|
+
tenant that is not in tenants() (a forged cookie). switch() runs in a server
|
|
2779
|
+
action, so cookies().set works there.
|
|
2780
|
+
|
|
2781
|
+
MOUNTING - seven route files, because the engine finds routes, url schemas and
|
|
2782
|
+
sections by their files. Each re-exports from the panel:
|
|
2783
|
+
src/app/admin/middleware.ts export default panel.middleware
|
|
2784
|
+
src/app/admin/layout.tsx export default panel.Layout
|
|
2785
|
+
src/app/admin/page.tsx export default panel.Dashboard
|
|
2786
|
+
src/app/admin/[resource]/page.tsx export const params = resourceParams
|
|
2787
|
+
export const searchParams = listSearch
|
|
2788
|
+
export default panel.Index
|
|
2789
|
+
src/app/admin/[resource]/admin-table.section.tsx export default panel.Table
|
|
2790
|
+
src/app/admin/[resource]/new/page.tsx export const params = resourceParams
|
|
2791
|
+
export default panel.Create
|
|
2792
|
+
src/app/admin/[resource]/[id]/page.tsx export const params = recordParams
|
|
2793
|
+
export default panel.Edit
|
|
2794
|
+
resourceParams, recordParams and listSearch come from '@rsc-kit/admin'. The
|
|
2795
|
+
section MUST be named admin-table.section.tsx: a delete refreshes it by name.
|
|
2796
|
+
|
|
2797
|
+
PLUGINS: definePlugin({ name, resources?, widgets?, onChange? }). A widget is
|
|
2798
|
+
{ title, description?, component } - a server component given { tenant }.
|
|
2799
|
+
onChange(change) runs after a save/delete is written, with { panel, resource,
|
|
2800
|
+
id, change, values?, tenant }; a plugin that throws is logged and never fails
|
|
2801
|
+
the save. Export a function of options returning the plugin. Publish with
|
|
2802
|
+
@rsc-kit/admin and react as PEER dependencies and the npm keyword
|
|
2803
|
+
rsc-kit-admin-plugin.`,
|
|
2804
|
+
},
|
|
2642
2805
|
];
|
|
2643
2806
|
/** Every topic, with one line each — what a caller reads before choosing. */
|
|
2644
2807
|
export function listTopics() {
|