openadminjs 0.1.3 → 0.1.4

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.
Files changed (32) hide show
  1. package/README.md +36 -4
  2. package/apps/admin/app/plugins/page.tsx +60 -1
  3. package/apps/api/plugins.manifest.example.json +12 -0
  4. package/apps/api/src/admin/admin-ai.service.ts +36 -3
  5. package/apps/api/src/admin/admin-plugins.service.ts +18 -4
  6. package/apps/api/src/admin/dto/plugins.dto.ts +19 -1
  7. package/apps/api/src/app.module.ts +9 -1
  8. package/apps/api/src/plugins/bootstrap.ts +2 -2
  9. package/apps/api/src/plugins/bundled/admin-widget.plugin.ts +25 -0
  10. package/apps/api/src/plugins/bundled/image-optimizer.plugin.ts +22 -0
  11. package/apps/api/src/plugins/bundled/registry.ts +7 -1
  12. package/apps/api/src/plugins/bundled/seo.plugin.ts +25 -0
  13. package/apps/api/src/plugins/manifest.schema.test.ts +9 -1
  14. package/apps/api/src/plugins/manifest.schema.ts +20 -0
  15. package/apps/api/src/plugins/plugin-api.interceptor.ts +47 -0
  16. package/apps/api/src/plugins/plugin-loader.ts +34 -9
  17. package/apps/api/src/plugins/plugin-runtime.test.ts +24 -0
  18. package/apps/api/src/plugins/plugin-runtime.ts +85 -0
  19. package/apps/api/src/resources/resource-hooks.runner.ts +13 -6
  20. package/examples/README.md +1 -1
  21. package/examples/basic-admin/README.md +1 -1
  22. package/package.json +1 -1
  23. package/packages/cli/src/create-project.ts +99 -10
  24. package/packages/cli/src/index.ts +35 -8
  25. package/packages/cli/template/.env.example +5 -3
  26. package/packages/create-openadminjs/src/index.ts +56 -4
  27. package/packages/create-openadminjs/src/render-files.ts +1 -1
  28. package/packages/create-openadminjs/template/.env.example +5 -2
  29. package/packages/plugin-sdk/src/hooks.ts +91 -0
  30. package/packages/plugin-sdk/src/index.ts +6 -1
  31. package/site/docs.html +51 -41
  32. package/site/index.html +1 -1
@@ -42,3 +42,94 @@ export type ResourceLifecycleHooks = {
42
42
  beforeDelete?: (ctx: BeforeDeleteCtx) => Promise<void> | void;
43
43
  afterDelete?: (ctx: AfterDeleteCtx) => Promise<void> | void;
44
44
  };
45
+
46
+ export type ApiRouteExtension = {
47
+ method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
48
+ path: string;
49
+ handler: (ctx: {
50
+ body?: unknown;
51
+ query?: Record<string, unknown>;
52
+ params?: Record<string, string>;
53
+ user?: ResourceHookUser;
54
+ prisma: unknown;
55
+ }) => Promise<unknown> | unknown;
56
+ };
57
+
58
+ export type ApiHooks = {
59
+ beforeRequest?: (ctx: {
60
+ method: string;
61
+ path: string;
62
+ body?: unknown;
63
+ query?: Record<string, unknown>;
64
+ user?: ResourceHookUser;
65
+ }) => Promise<void> | void;
66
+ afterRequest?: (ctx: {
67
+ method: string;
68
+ path: string;
69
+ statusCode: number;
70
+ durationMs: number;
71
+ user?: ResourceHookUser;
72
+ }) => Promise<void> | void;
73
+ routes?: ApiRouteExtension[];
74
+ };
75
+
76
+ export type MediaHooks = {
77
+ beforeStore?: (ctx: {
78
+ filename: string;
79
+ mimeType: string;
80
+ size: number;
81
+ contentBase64?: string;
82
+ user?: ResourceHookUser;
83
+ }) => Promise<void> | void;
84
+ transform?: (ctx: {
85
+ filename: string;
86
+ mimeType: string;
87
+ contentBase64: string;
88
+ user?: ResourceHookUser;
89
+ }) => Promise<{ filename?: string; mimeType?: string; contentBase64: string }> | { filename?: string; mimeType?: string; contentBase64: string };
90
+ afterStore?: (ctx: {
91
+ fileId: string;
92
+ filename: string;
93
+ mimeType: string;
94
+ size: number;
95
+ user?: ResourceHookUser;
96
+ }) => Promise<void> | void;
97
+ };
98
+
99
+ export type SeoHooks = {
100
+ metadata?: (ctx: { resourceName: string; record: Record<string, unknown> }) => Promise<Record<string, unknown>> | Record<string, unknown>;
101
+ sitemapEntry?: (ctx: { resourceName: string; record: Record<string, unknown> }) => Promise<{ url: string; lastModified?: string } | null> | { url: string; lastModified?: string } | null;
102
+ };
103
+
104
+ export type JobHandler = {
105
+ name: string;
106
+ handler: (ctx: { payload: unknown; user?: ResourceHookUser; prisma: unknown }) => Promise<unknown> | unknown;
107
+ };
108
+
109
+ export type AdminUiExtension = {
110
+ kind: "menu" | "page" | "widget" | "action";
111
+ id: string;
112
+ title: string;
113
+ route?: string;
114
+ config?: Record<string, unknown>;
115
+ };
116
+
117
+ export type PluginCapability =
118
+ | "db.read"
119
+ | "db.write"
120
+ | "resource.hooks"
121
+ | "api.hooks"
122
+ | "api.routes"
123
+ | "media.pipeline"
124
+ | "seo.extend"
125
+ | "jobs.run"
126
+ | "admin.ui.extend";
127
+
128
+ export type PluginSurfaceRegistry = {
129
+ resource?: ResourceLifecycleHooks;
130
+ api?: ApiHooks;
131
+ media?: MediaHooks;
132
+ seo?: SeoHooks;
133
+ jobs?: JobHandler[];
134
+ adminUi?: AdminUiExtension[];
135
+ };
@@ -1,10 +1,12 @@
1
- import type { ResourceLifecycleHooks } from "./hooks";
1
+ import type { PluginCapability, PluginSurfaceRegistry, ResourceLifecycleHooks } from "./hooks";
2
2
 
3
3
  export type * from "./hooks";
4
4
 
5
5
  export type PluginRegistrationContext<Config extends Record<string, unknown> = Record<string, unknown>> = {
6
6
  readonly pluginId: string;
7
7
  readonly config: Config;
8
+ readonly trustMode: "trusted" | "sandboxed";
9
+ readonly capabilities: PluginCapability[];
8
10
  /**
9
11
  * Register server-only lifecycle hooks for a resource. Multiple plugins (and core code) stack in load order.
10
12
  * Use `{ replace: true }` only when a plugin must fully override prior hooks for that resource.
@@ -14,6 +16,7 @@ export type PluginRegistrationContext<Config extends Record<string, unknown> = R
14
16
  hooks: ResourceLifecycleHooks,
15
17
  options?: { replace?: boolean }
16
18
  ) => void;
19
+ registerSurface: (surface: PluginSurfaceRegistry) => void;
17
20
  };
18
21
 
19
22
  /** Published plugins should export this (named `openAdminPlugin` or default). */
@@ -29,6 +32,8 @@ export type OpenAdminPlugin<Config extends Record<string, unknown> = Record<stri
29
32
  licenseKey?: string;
30
33
  };
31
34
  register: (ctx: PluginRegistrationContext<Config>) => void | Promise<void>;
35
+ onStart?: (ctx: PluginRegistrationContext<Config>) => void | Promise<void>;
36
+ onShutdown?: (ctx: PluginRegistrationContext<Config>) => void | Promise<void>;
32
37
  };
33
38
 
34
39
  export type OpenAdminPluginModule<Config extends Record<string, unknown> = Record<string, unknown>> = {
package/site/docs.html CHANGED
@@ -43,7 +43,7 @@
43
43
  <span>Quick install</span>
44
44
  <div class="docs-code-block docs-code-block--inline" data-language="bash">
45
45
  <div class="docs-code-toolbar"><span class="docs-code-lang">bash</span><button type="button" class="docs-code-copy" data-copy-code>Copy</button></div>
46
- <pre><code>npm create openadminjs@latest my-app</code></pre>
46
+ <pre><code>npx openadminjs create my-app</code></pre>
47
47
  </div>
48
48
  </div>
49
49
  </section>
@@ -159,11 +159,11 @@
159
159
  <h2>Installation</h2>
160
160
  <p>
161
161
  Prerequisites: <strong>Node.js 20+</strong>, <strong>pnpm</strong> (recommended) or npm/yarn, and the OpenAdminJS CLI.
162
- The flow mirrors a typical Nest.js workspace bootstrap: scaffold, migrate, seed, run. Dependency installation happens automatically when the project is created.
162
+ The flow mirrors a typical Nest.js workspace bootstrap: scaffold, migrate, seed, run. Dependency installation and core env prompts (DB/Redis/JWT/origins) happen automatically during project creation.
163
163
  </p>
164
164
  <div class="docs-code-block" data-language="bash">
165
165
  <div class="docs-code-toolbar"><span class="docs-code-lang">bash</span><button type="button" class="docs-code-copy" data-copy-code>Copy</button></div>
166
- <pre><code>npm create openadminjs@latest my-app
166
+ <pre><code>npx openadminjs create my-app
167
167
  cd my-app
168
168
  pnpm db:migrate
169
169
  pnpm db:seed
@@ -207,6 +207,7 @@ prisma/
207
207
  <section id="configuration">
208
208
  <h2>Configuration</h2>
209
209
  <p>
210
+ During <code>openadminjs create</code>, the wizard asks for DB credentials and key env values, then writes them into <code>.env.example</code>.
210
211
  Copy <code>.env.example</code> to <code>.env</code> at the repo root. Values are read by the Nest API and Next apps through <code>@nestjs/config</code> and
211
212
  <code>NEXT_PUBLIC_*</code> variables—treat secrets like you would in a Nest <code>ConfigModule</code> setup.
212
213
  </p>
@@ -251,7 +252,7 @@ pnpm --filter @openadminjs/api build</code></pre>
251
252
  <p>Scaffolds a monorepo with <code>apps/api</code>, <code>apps/admin</code>, <code>apps/web</code>, shared packages, Prisma schema, and automatically installs dependencies with the selected package manager.</p>
252
253
  <div class="docs-code-block" data-language="bash">
253
254
  <div class="docs-code-toolbar"><span class="docs-code-lang">bash</span><button type="button" class="docs-code-copy" data-copy-code>Copy</button></div>
254
- <pre><code>npm create openadminjs@latest my-app
255
+ <pre><code>npx openadminjs create my-app
255
256
  cd my-app</code></pre>
256
257
  </div>
257
258
 
@@ -909,61 +910,70 @@ export const LOCALE_LABELS: Record&lt;string, string&gt; = {
909
910
  <section id="plugins">
910
911
  <h2>Plugin system</h2>
911
912
  <p>
912
- OpenAdminJS exposes a lightweight plugin registry so you can extend resource behavior — add computed fields, inject audit log entries,
913
- attach webhooks, or wrap any action — without forking the core library.
914
- Plugins live in <code>apps/api/src/plugins/</code> and are bootstrapped before the Nest app starts.
913
+ OpenAdminJS Plugin Platform is a broad extension system (WordPress-style) with explicit capability gating.
914
+ Plugins can register resource hooks, API hooks/routes, media pipeline transforms, SEO contributors, job handlers, and admin UI extensions.
915
+ Plugins are loaded from <code>plugins.manifest.json</code> and enforced by trust mode + capabilities.
915
916
  </p>
916
917
  <div class="docs-code-block" data-language="typescript">
917
918
  <div class="docs-code-toolbar"><span class="docs-code-lang">TypeScript</span><button type="button" class="docs-code-copy" data-copy-code>Copy</button></div>
918
- <pre><code>// apps/api/src/plugins/webhook.plugin.ts
919
- import { definePlugin } from "@openadminjs/core";
919
+ <pre><code>import type { OpenAdminPlugin } from "@openadminjs/plugin-sdk";
920
920
 
921
- export default definePlugin({
922
- name: "webhook-notifier",
921
+ export const seoAndMediaPlugin: OpenAdminPlugin = {
922
+ id: "com.example.seo-media",
923
923
  version: "1.0.0",
924
- description: "Send a POST after every resource create/update/delete.",
925
- hooks: {
926
- afterCreate: async ({ resource, record, user }) => {
927
- await fetch(process.env.WEBHOOK_URL!, {
928
- method: "POST",
929
- headers: { "content-type": "application/json" },
930
- body: JSON.stringify({ event: "create", resource: resource.name, record, user: user.email })
931
- });
932
- }
924
+ async register({ registerSurface }) {
925
+ registerSurface({
926
+ seo: {
927
+ metadata({ resourceName, record }) {
928
+ if (resourceName !== "posts") return {};
929
+ return { title: record.title ?? "Untitled" };
930
+ }
931
+ },
932
+ media: {
933
+ transform({ filename, mimeType, contentBase64 }) {
934
+ if (!mimeType.startsWith("image/")) return { filename, mimeType, contentBase64 };
935
+ return { filename: filename.replace(/\\.(png|jpe?g)$/i, ".webp"), mimeType: "image/webp", contentBase64 };
936
+ }
937
+ }
938
+ });
933
939
  }
934
- });</code></pre>
940
+ };</code></pre>
935
941
  </div>
936
- <p>Register plugins in <code>apps/api/src/plugins/bootstrap.ts</code>:</p>
942
+ <p>Register plugin entries in <code>apps/api/plugins.manifest.json</code> with explicit capabilities:</p>
937
943
  <div class="docs-code-block" data-language="typescript">
938
944
  <div class="docs-code-toolbar"><span class="docs-code-lang">TypeScript</span><button type="button" class="docs-code-copy" data-copy-code>Copy</button></div>
939
- <pre><code>import { pluginRegistry } from "@openadminjs/core";
940
- import webhookPlugin from "./webhook.plugin";
941
-
942
- pluginRegistry.register(webhookPlugin);</code></pre>
945
+ <pre><code>{
946
+ "version": 1,
947
+ "plugins": [
948
+ {
949
+ "id": "com.example.seo-media",
950
+ "enabled": true,
951
+ "package": "@example/openadmin-seo-media",
952
+ "trustMode": "sandboxed",
953
+ "capabilities": ["seo.extend", "media.pipeline"]
954
+ }
955
+ ]
956
+ }</code></pre>
943
957
  </div>
958
+ <p class="mt-3">
959
+ Capability matrix: <code>resource.hooks</code>, <code>api.hooks</code>, <code>api.routes</code>,
960
+ <code>media.pipeline</code>, <code>seo.extend</code>, <code>jobs.run</code>, <code>admin.ui.extend</code>.
961
+ Use <code>sandboxed</code> trust mode and request only minimal capabilities by default.
962
+ </p>
944
963
  </section>
945
964
 
946
965
  <section id="hooks">
947
966
  <h2>Resource hooks</h2>
948
967
  <p>
949
- Resource-level hooks let you intercept the CRUD lifecycle per resource, e.g. to hash passwords before a create or invalidate a cache after an update.
950
- Hooks declared in <code>resource-hooks.install.ts</code> are attached to the resource registry at startup.
968
+ Resource lifecycle hooks are now one surface within Plugin Platform. Use capability
969
+ <code>resource.hooks</code> and register through plugin SDK context.
951
970
  </p>
952
971
  <div class="docs-code-block" data-language="typescript">
953
972
  <div class="docs-code-toolbar"><span class="docs-code-lang">TypeScript</span><button type="button" class="docs-code-copy" data-copy-code>Copy</button></div>
954
- <pre><code>// apps/api/src/resources/resource-hooks.install.ts
955
- import bcrypt from "bcryptjs";
956
- import { resourceHooks } from "@openadminjs/core";
957
-
958
- resourceHooks.on("users", "beforeCreate", async (data) => {
959
- if (data.password) {
960
- data.password = await bcrypt.hash(data.password, 12);
973
+ <pre><code>registerResourceHooks("users", {
974
+ async beforeCreate({ data }) {
975
+ if (typeof data.email === "string") data.email = data.email.trim().toLowerCase();
961
976
  }
962
- return data;
963
- });
964
-
965
- resourceHooks.on("posts", "afterUpdate", async (_data, context) => {
966
- await context.cache?.invalidate(`post:${context.id}`);
967
977
  });</code></pre>
968
978
  </div>
969
979
  <p>Available lifecycle events: <code>beforeCreate</code>, <code>afterCreate</code>, <code>beforeUpdate</code>, <code>afterUpdate</code>, <code>beforeDelete</code>, <code>afterDelete</code>.</p>
@@ -1424,7 +1434,7 @@ GET /store/orders/:id/status</code></pre>
1424
1434
 
1425
1435
  <h3>Developer experience</h3>
1426
1436
  <ul class="docs-list">
1427
- <li>🟢 CLI scaffold with <code>npm create openadminjs@latest</code></li>
1437
+ <li>🟢 CLI scaffold with <code>npx openadminjs create</code></li>
1428
1438
  <li>🟡 VS Code extension — resource file type support, schema validation</li>
1429
1439
  <li>⚪ <code>openadminjs generate field</code> — append a field to an existing resource and emit a Prisma migration fragment</li>
1430
1440
  <li>⚪ Storybook integration — isolated UI component development</li>
package/site/index.html CHANGED
@@ -53,7 +53,7 @@
53
53
  </ul>
54
54
  <div class="command-chip command-chip-glow">
55
55
  <span>Quick start</span>
56
- <code>npm create openadminjs@latest my-app</code>
56
+ <code>npx openadminjs create my-app</code>
57
57
  </div>
58
58
  </div>
59
59