openadminjs 0.1.7 → 0.1.9
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 +114 -18
- package/dist/create-project.js +27 -4
- package/dist/create-project.test.js +57 -41
- package/dist/index.js +16 -5
- package/package.json +4 -2
- package/template/.env.example +2 -2
- package/template/README.md +24 -5
- package/template/apps/admin/app/api/auth/login/route.ts +51 -0
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +46 -0
- package/template/apps/admin/app/api-tokens/page.tsx +253 -0
- package/template/apps/admin/app/audit-logs/page.tsx +5 -0
- package/template/apps/admin/app/dashboard/page.tsx +392 -0
- package/template/apps/admin/app/files/page.tsx +5 -0
- package/template/apps/admin/app/globals.css +206 -0
- package/template/apps/admin/app/jobs/page.tsx +19 -0
- package/template/apps/admin/app/layout.tsx +57 -0
- package/template/apps/admin/app/login/page.test.tsx +18 -0
- package/template/apps/admin/app/login/page.tsx +161 -0
- package/template/apps/admin/app/login/safe-next-param.ts +4 -0
- package/template/apps/admin/app/notifications/page.tsx +146 -0
- package/template/apps/admin/app/page.tsx +5 -0
- package/template/apps/admin/app/plugins/page.tsx +407 -0
- package/template/apps/admin/app/profile/page.tsx +116 -0
- package/template/apps/admin/app/resources/[resource]/[id]/edit/page.tsx +45 -0
- package/template/apps/admin/app/resources/[resource]/[id]/page.tsx +449 -0
- package/template/apps/admin/app/resources/[resource]/create/page.tsx +34 -0
- package/template/apps/admin/app/resources/[resource]/page.tsx +100 -0
- package/template/apps/admin/app/settings/page.tsx +233 -0
- package/template/apps/admin/components/ai-assistant-widget.tsx +684 -0
- package/template/apps/admin/components/app-shell.tsx +305 -0
- package/template/apps/admin/components/auth-guard.test.tsx +55 -0
- package/template/apps/admin/components/auth-guard.tsx +39 -0
- package/template/apps/admin/components/jobs-panel.tsx +103 -0
- package/template/apps/admin/components/logo.tsx +30 -0
- package/template/apps/admin/components/resource-form.tsx +349 -0
- package/template/apps/admin/components/resource-table.tsx +277 -0
- package/template/apps/admin/lib/ai-events.ts +23 -0
- package/template/apps/admin/lib/api.test.ts +118 -0
- package/template/apps/admin/lib/api.ts +135 -0
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +79 -0
- package/template/apps/admin/lib/brand.ts +14 -0
- package/template/apps/admin/lib/locale.ts +49 -0
- package/template/apps/admin/lib/tokens.ts +93 -0
- package/template/apps/admin/middleware.ts +58 -0
- package/template/apps/admin/next-env.d.ts +6 -0
- package/template/apps/admin/next.config.mjs +7 -0
- package/template/apps/admin/package.json +44 -0
- package/template/apps/admin/postcss.config.mjs +6 -0
- package/template/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/template/apps/admin/public/brand/favicon.svg +1 -0
- package/template/apps/admin/public/brand/og-image.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/template/apps/admin/tailwind.config.ts +37 -0
- package/template/apps/admin/tsconfig.json +23 -0
- package/template/apps/admin/vitest.config.ts +7 -0
- package/template/apps/api/package.json +56 -0
- package/template/apps/api/plugins.manifest.example.json +34 -0
- package/template/apps/api/plugins.manifest.json +4 -0
- package/template/apps/api/src/access/catalog.ts +112 -0
- package/template/apps/api/src/access/derive-permissions.test.ts +26 -0
- package/template/apps/api/src/access/derive-permissions.ts +36 -0
- package/template/apps/api/src/access/public-catalog.ts +28 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +125 -0
- package/template/apps/api/src/admin/admin-ai.events.ts +23 -0
- package/template/apps/api/src/admin/admin-ai.service.ts +1271 -0
- package/template/apps/api/src/admin/admin-analytics.service.ts +164 -0
- package/template/apps/api/src/admin/admin-plugins.controller.ts +32 -0
- package/template/apps/api/src/admin/admin-plugins.service.ts +267 -0
- package/template/apps/api/src/admin/admin-resource.service.test.ts +118 -0
- package/template/apps/api/src/admin/admin-resource.service.ts +375 -0
- package/template/apps/api/src/admin/admin.controller.ts +72 -0
- package/template/apps/api/src/admin/admin.module.ts +30 -0
- package/template/apps/api/src/admin/dto/plugins.dto.ts +55 -0
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +44 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +103 -0
- package/template/apps/api/src/app.module.ts +45 -0
- package/template/apps/api/src/auth/auth.controller.ts +70 -0
- package/template/apps/api/src/auth/auth.dto.ts +49 -0
- package/template/apps/api/src/auth/auth.module.ts +30 -0
- package/template/apps/api/src/auth/auth.service.test.ts +75 -0
- package/template/apps/api/src/auth/auth.service.ts +231 -0
- package/template/apps/api/src/common/auth-realm.decorator.ts +6 -0
- package/template/apps/api/src/common/auth.guard.test.ts +61 -0
- package/template/apps/api/src/common/auth.guard.ts +78 -0
- package/template/apps/api/src/common/casl-ability.factory.test.ts +33 -0
- package/template/apps/api/src/common/casl-ability.factory.ts +41 -0
- package/template/apps/api/src/common/current-user.decorator.ts +6 -0
- package/template/apps/api/src/common/permission.decorator.ts +7 -0
- package/template/apps/api/src/common/permissions.guard.ts +31 -0
- package/template/apps/api/src/common/prisma.service.ts +13 -0
- package/template/apps/api/src/health/health.controller.ts +25 -0
- package/template/apps/api/src/mail/mail.module.ts +9 -0
- package/template/apps/api/src/mail/mail.service.ts +115 -0
- package/template/apps/api/src/main.ts +37 -0
- package/template/apps/api/src/plugins/bootstrap.ts +4 -0
- package/template/apps/api/src/plugins/bundled/admin-widget.plugin.ts +25 -0
- package/template/apps/api/src/plugins/bundled/hello.plugin.ts +19 -0
- package/template/apps/api/src/plugins/bundled/image-optimizer.plugin.ts +22 -0
- package/template/apps/api/src/plugins/bundled/registry.ts +13 -0
- package/template/apps/api/src/plugins/bundled/seo.plugin.ts +25 -0
- package/template/apps/api/src/plugins/manifest-path.ts +35 -0
- package/template/apps/api/src/plugins/manifest.schema.test.ts +36 -0
- package/template/apps/api/src/plugins/manifest.schema.ts +58 -0
- package/template/apps/api/src/plugins/plugin-api.interceptor.ts +47 -0
- package/template/apps/api/src/plugins/plugin-loader.ts +134 -0
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +24 -0
- package/template/apps/api/src/plugins/plugin-runtime.ts +85 -0
- package/template/apps/api/src/queue/queue.controller.ts +28 -0
- package/template/apps/api/src/queue/queue.module.ts +23 -0
- package/template/apps/api/src/queue/queue.processor.ts +47 -0
- package/template/apps/api/src/queue/queue.service.ts +50 -0
- package/template/apps/api/src/resources/categories.resource.ts +21 -0
- package/template/apps/api/src/resources/commerce.resource.ts +157 -0
- package/template/apps/api/src/resources/operations.resource.ts +111 -0
- package/template/apps/api/src/resources/posts.resource.ts +88 -0
- package/template/apps/api/src/resources/registry.ts +27 -0
- package/template/apps/api/src/resources/resource-hooks.install.ts +18 -0
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +72 -0
- package/template/apps/api/src/resources/resource-hooks.registry.ts +66 -0
- package/template/apps/api/src/resources/resource-hooks.runner.ts +93 -0
- package/template/apps/api/src/resources/system.resource.ts +51 -0
- package/template/apps/api/src/resources/users.resource.ts +19 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +98 -0
- package/template/apps/api/src/store/payment/payment.module.ts +14 -0
- package/template/apps/api/src/store/payment/payment.provider.ts +38 -0
- package/template/apps/api/src/store/payment/payment.service.ts +235 -0
- package/template/apps/api/src/store/store.controller.ts +26 -0
- package/template/apps/api/src/store/store.module.ts +12 -0
- package/template/apps/api/src/test-setup.ts +1 -0
- package/template/apps/api/tsconfig.json +11 -0
- package/template/apps/api/vitest.config.ts +21 -0
- package/template/apps/web/lib/openadmin-client.ts +11 -0
- package/template/apps/web/lib/seo.test.ts +41 -0
- package/template/apps/web/lib/seo.ts +24 -13
- package/template/apps/web/next-env.d.ts +4 -0
- package/template/apps/web/package.json +2 -1
- package/template/apps/web/test/next-stub.ts +7 -0
- package/template/apps/web/vitest.config.ts +19 -0
- package/template/package.json +18 -11
- package/template/packages/auth/package.json +17 -0
- package/template/packages/auth/src/index.test.ts +10 -0
- package/template/packages/auth/src/index.ts +17 -0
- package/template/packages/auth/tsconfig.json +8 -0
- package/template/packages/brand/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/favicon.svg +1 -0
- package/template/packages/brand/apps/admin/public/brand/og-image.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/template/packages/brand/apps/web/public/brand/apple-touch-icon.png +0 -0
- package/template/packages/brand/apps/web/public/brand/favicon.svg +1 -0
- package/template/packages/brand/apps/web/public/brand/og-image.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-icon-new.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-logo-new.png +0 -0
- package/template/packages/brand/package.json +19 -0
- package/template/packages/brand/site/assets/brand/favicon.svg +1 -0
- package/template/packages/brand/site/assets/brand/og-image.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-icon.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-dark.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-horizontal.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-monochrome.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo.svg +1 -0
- package/template/packages/brand/src/generate-assets.mjs +182 -0
- package/template/packages/brand/src/index.test.ts +9 -0
- package/template/packages/brand/src/index.ts +7 -0
- package/template/packages/brand/tsconfig.json +7 -0
- package/template/packages/core/package.json +20 -0
- package/template/packages/core/src/index.js +1 -0
- package/template/packages/core/src/index.test.ts +27 -0
- package/template/packages/core/src/index.ts +61 -0
- package/template/packages/core/tsconfig.json +8 -0
- package/template/packages/mail/package.json +18 -0
- package/template/packages/mail/src/index.test.ts +23 -0
- package/template/packages/mail/src/index.ts +246 -0
- package/template/packages/mail/tsconfig.json +5 -0
- package/template/packages/notifications/package.json +16 -0
- package/template/packages/notifications/src/index.test.ts +9 -0
- package/template/packages/notifications/src/index.ts +5 -0
- package/template/packages/notifications/tsconfig.json +5 -0
- package/template/packages/payments/package.json +15 -0
- package/template/packages/payments/src/index.ts +256 -0
- package/template/packages/payments/tsconfig.json +9 -0
- package/template/packages/permissions/package.json +17 -0
- package/template/packages/permissions/src/blueprints.js +57 -0
- package/template/packages/permissions/src/blueprints.test.ts +62 -0
- package/template/packages/permissions/src/blueprints.ts +89 -0
- package/template/packages/permissions/src/index.js +11 -0
- package/template/packages/permissions/src/index.test.ts +34 -0
- package/template/packages/permissions/src/index.ts +22 -0
- package/template/packages/permissions/tsconfig.json +8 -0
- package/template/packages/plugin-sdk/package.json +26 -0
- package/template/packages/plugin-sdk/src/hooks.ts +135 -0
- package/template/packages/plugin-sdk/src/index.ts +42 -0
- package/template/packages/plugin-sdk/src/sdk.test.ts +13 -0
- package/template/packages/plugin-sdk/tsconfig.json +8 -0
- package/template/packages/plugin-sdk/vitest.config.ts +5 -0
- package/template/packages/queue/package.json +16 -0
- package/template/packages/queue/src/index.test.ts +12 -0
- package/template/packages/queue/src/index.ts +6 -0
- package/template/packages/queue/tsconfig.json +5 -0
- package/template/packages/resource/package.json +17 -0
- package/template/packages/resource/src/index.js +177 -0
- package/template/packages/resource/src/index.test.ts +151 -0
- package/template/packages/resource/src/index.ts +344 -0
- package/template/packages/resource/tsconfig.json +8 -0
- package/template/packages/storage/package.json +16 -0
- package/template/packages/storage/src/index.test.ts +10 -0
- package/template/packages/storage/src/index.ts +23 -0
- package/template/packages/storage/tsconfig.json +5 -0
- package/template/prisma/migrations/20260505151751_baseline_init_all_tables/migration.sql +371 -0
- package/template/prisma/migrations/migration_lock.toml +3 -0
- package/template/prisma/schema.prisma +291 -0
- package/template/prisma/seed.ts +217 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { ResourceConfig } from "@openadminjs/core";
|
|
2
|
+
|
|
3
|
+
/** Mirrors `apps/api` resource hook user shape — keep in sync when extending auth context. */
|
|
4
|
+
export type ResourceHookUser = { id: string; email: string; permissions: string[] };
|
|
5
|
+
|
|
6
|
+
export type BeforeCreateCtx = {
|
|
7
|
+
resource: ResourceConfig;
|
|
8
|
+
resourceName: string;
|
|
9
|
+
user: ResourceHookUser;
|
|
10
|
+
prisma: unknown;
|
|
11
|
+
data: Record<string, unknown>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type AfterCreateCtx = BeforeCreateCtx & { record: unknown };
|
|
15
|
+
|
|
16
|
+
export type BeforeUpdateCtx = {
|
|
17
|
+
resource: ResourceConfig;
|
|
18
|
+
resourceName: string;
|
|
19
|
+
user: ResourceHookUser;
|
|
20
|
+
prisma: unknown;
|
|
21
|
+
id: string;
|
|
22
|
+
data: Record<string, unknown>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type AfterUpdateCtx = BeforeUpdateCtx & { before: unknown; record: unknown };
|
|
26
|
+
|
|
27
|
+
export type BeforeDeleteCtx = {
|
|
28
|
+
resource: ResourceConfig;
|
|
29
|
+
resourceName: string;
|
|
30
|
+
user: ResourceHookUser;
|
|
31
|
+
prisma: unknown;
|
|
32
|
+
id: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type AfterDeleteCtx = BeforeDeleteCtx;
|
|
36
|
+
|
|
37
|
+
export type ResourceLifecycleHooks = {
|
|
38
|
+
beforeCreate?: (ctx: BeforeCreateCtx) => Promise<void> | void;
|
|
39
|
+
afterCreate?: (ctx: AfterCreateCtx) => Promise<void> | void;
|
|
40
|
+
beforeUpdate?: (ctx: BeforeUpdateCtx) => Promise<void> | void;
|
|
41
|
+
afterUpdate?: (ctx: AfterUpdateCtx) => Promise<void> | void;
|
|
42
|
+
beforeDelete?: (ctx: BeforeDeleteCtx) => Promise<void> | void;
|
|
43
|
+
afterDelete?: (ctx: AfterDeleteCtx) => Promise<void> | void;
|
|
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
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { PluginCapability, PluginSurfaceRegistry, ResourceLifecycleHooks } from "./hooks";
|
|
2
|
+
|
|
3
|
+
export type * from "./hooks";
|
|
4
|
+
|
|
5
|
+
export type PluginRegistrationContext<Config extends Record<string, unknown> = Record<string, unknown>> = {
|
|
6
|
+
readonly pluginId: string;
|
|
7
|
+
readonly config: Config;
|
|
8
|
+
readonly trustMode: "trusted" | "sandboxed";
|
|
9
|
+
readonly capabilities: PluginCapability[];
|
|
10
|
+
/**
|
|
11
|
+
* Register server-only lifecycle hooks for a resource. Multiple plugins (and core code) stack in load order.
|
|
12
|
+
* Use `{ replace: true }` only when a plugin must fully override prior hooks for that resource.
|
|
13
|
+
*/
|
|
14
|
+
registerResourceHooks: (
|
|
15
|
+
resourceName: string,
|
|
16
|
+
hooks: ResourceLifecycleHooks,
|
|
17
|
+
options?: { replace?: boolean }
|
|
18
|
+
) => void;
|
|
19
|
+
registerSurface: (surface: PluginSurfaceRegistry) => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Published plugins should export this (named `openAdminPlugin` or default). */
|
|
23
|
+
export type OpenAdminPlugin<Config extends Record<string, unknown> = Record<string, unknown>> = {
|
|
24
|
+
/** Stable id, e.g. reverse-DNS `com.vendor.billing` */
|
|
25
|
+
id: string;
|
|
26
|
+
displayName?: string;
|
|
27
|
+
version: string;
|
|
28
|
+
/** Reserved for marketplace / license flows */
|
|
29
|
+
marketplace?: {
|
|
30
|
+
listingUrl?: string;
|
|
31
|
+
productId?: string;
|
|
32
|
+
licenseKey?: string;
|
|
33
|
+
};
|
|
34
|
+
register: (ctx: PluginRegistrationContext<Config>) => void | Promise<void>;
|
|
35
|
+
onStart?: (ctx: PluginRegistrationContext<Config>) => void | Promise<void>;
|
|
36
|
+
onShutdown?: (ctx: PluginRegistrationContext<Config>) => void | Promise<void>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type OpenAdminPluginModule<Config extends Record<string, unknown> = Record<string, unknown>> = {
|
|
40
|
+
openAdminPlugin?: OpenAdminPlugin<Config>;
|
|
41
|
+
default?: OpenAdminPlugin<Config>;
|
|
42
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type { OpenAdminPlugin } from "./index";
|
|
3
|
+
|
|
4
|
+
describe("plugin-sdk", () => {
|
|
5
|
+
it("OpenAdminPlugin shape is structural", () => {
|
|
6
|
+
const p: OpenAdminPlugin = {
|
|
7
|
+
id: "test",
|
|
8
|
+
version: "0.0.0",
|
|
9
|
+
register() {}
|
|
10
|
+
};
|
|
11
|
+
expect(p.id).toBe("test");
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openadminjs/queue",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc -p tsconfig.json",
|
|
8
|
+
"lint": "tsc -p tsconfig.json --noEmit",
|
|
9
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
10
|
+
"test": "vitest run"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"typescript": "^5.7.2",
|
|
14
|
+
"vitest": "^2.1.8"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { JobPayload, QueueDriver } from "./index";
|
|
3
|
+
|
|
4
|
+
describe("queue types", () => {
|
|
5
|
+
it("dispatch returns job id", async () => {
|
|
6
|
+
const driver: QueueDriver = {
|
|
7
|
+
dispatch: vi.fn(async () => "job-1")
|
|
8
|
+
};
|
|
9
|
+
const id = await driver.dispatch("notify", { userId: "1" } satisfies JobPayload);
|
|
10
|
+
expect(id).toBe("job-1");
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openadminjs/resource",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -p tsconfig.json",
|
|
9
|
+
"lint": "tsc -p tsconfig.json --noEmit",
|
|
10
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
11
|
+
"test": "vitest run"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"typescript": "^5.7.2",
|
|
15
|
+
"vitest": "^2.1.8"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
export const sensitiveFieldPatterns = [
|
|
2
|
+
"password",
|
|
3
|
+
"passwordHash",
|
|
4
|
+
"token",
|
|
5
|
+
"accessToken",
|
|
6
|
+
"refreshToken",
|
|
7
|
+
"secret",
|
|
8
|
+
"apiKey",
|
|
9
|
+
"privateKey",
|
|
10
|
+
"resetToken",
|
|
11
|
+
"verificationToken",
|
|
12
|
+
"otp",
|
|
13
|
+
"twoFactorSecret"
|
|
14
|
+
];
|
|
15
|
+
export function defineResource(config) {
|
|
16
|
+
validateResource(config);
|
|
17
|
+
return withSafeFields(config);
|
|
18
|
+
}
|
|
19
|
+
export function isSensitiveField(name, field) {
|
|
20
|
+
if (field?.sensitive || field?.type === "password" || field?.type === "hidden")
|
|
21
|
+
return true;
|
|
22
|
+
const lower = name.toLowerCase();
|
|
23
|
+
return sensitiveFieldPatterns.some((pattern) => lower.includes(pattern.toLowerCase()));
|
|
24
|
+
}
|
|
25
|
+
export function withSafeFields(config) {
|
|
26
|
+
const fields = Object.fromEntries(Object.entries(config.fields).map(([name, field]) => {
|
|
27
|
+
if (!isSensitiveField(name, field))
|
|
28
|
+
return [name, field];
|
|
29
|
+
return [
|
|
30
|
+
name,
|
|
31
|
+
{
|
|
32
|
+
...field,
|
|
33
|
+
list: false,
|
|
34
|
+
detail: false,
|
|
35
|
+
create: field.type === "password" ? field.create : false,
|
|
36
|
+
edit: field.type === "password" ? field.edit : false,
|
|
37
|
+
sensitive: true
|
|
38
|
+
}
|
|
39
|
+
];
|
|
40
|
+
}));
|
|
41
|
+
return { ...config, fields };
|
|
42
|
+
}
|
|
43
|
+
export function validateResource(config) {
|
|
44
|
+
if (!config.name || !/^[a-z][a-z0-9-]*$/.test(config.name)) {
|
|
45
|
+
throw new Error(`Invalid resource name "${config.name}". Use kebab-case.`);
|
|
46
|
+
}
|
|
47
|
+
if (!config.model || !/^[A-Za-z][A-Za-z0-9_]*$/.test(config.model)) {
|
|
48
|
+
throw new Error(`Invalid Prisma model name for resource "${config.name}".`);
|
|
49
|
+
}
|
|
50
|
+
if (!config.permissions.read) {
|
|
51
|
+
throw new Error(`Resource "${config.name}" must define permissions.read.`);
|
|
52
|
+
}
|
|
53
|
+
if (!Object.keys(config.fields).length) {
|
|
54
|
+
throw new Error(`Resource "${config.name}" must define at least one field.`);
|
|
55
|
+
}
|
|
56
|
+
const ls = config.listScope;
|
|
57
|
+
if (ls && "field" in ls && ls.type === "userOwns") {
|
|
58
|
+
if (!ls.field?.trim()) {
|
|
59
|
+
throw new Error(`Resource "${config.name}" listScope.userOwns requires a non-empty field.`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function visibleFields(resource, mode, permissions) {
|
|
64
|
+
return Object.fromEntries(Object.entries(resource.fields).filter(([name, field]) => {
|
|
65
|
+
if (isSensitiveField(name, field))
|
|
66
|
+
return false;
|
|
67
|
+
if (field[mode] === false)
|
|
68
|
+
return false;
|
|
69
|
+
const permissionKey = mode === "detail" || mode === "list" ? "read" : mode === "edit" ? "update" : "create";
|
|
70
|
+
const permission = field.permissions?.[permissionKey];
|
|
71
|
+
return permission ? permissions.includes(permission) : true;
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
export function pickLocalizedLabel(label, locale, fallbackLocale = "en") {
|
|
75
|
+
if (label == null || label === "")
|
|
76
|
+
return "";
|
|
77
|
+
if (typeof label === "string")
|
|
78
|
+
return label;
|
|
79
|
+
return label[locale] ?? label[fallbackLocale] ?? Object.values(label)[0] ?? "";
|
|
80
|
+
}
|
|
81
|
+
export function effectiveLocale(resource, requested) {
|
|
82
|
+
const fb = resource.i18n?.defaultLocale ?? "en";
|
|
83
|
+
const locales = resource.i18n?.locales;
|
|
84
|
+
if (!requested)
|
|
85
|
+
return fb;
|
|
86
|
+
if (!locales?.length)
|
|
87
|
+
return requested;
|
|
88
|
+
return locales.includes(requested) ? requested : fb;
|
|
89
|
+
}
|
|
90
|
+
export function resolveResourceForLocale(resource, locale) {
|
|
91
|
+
const fb = resource.i18n?.defaultLocale ?? "en";
|
|
92
|
+
const loc = effectiveLocale(resource, locale);
|
|
93
|
+
const fields = Object.fromEntries(Object.entries(resource.fields).map(([key, field]) => [
|
|
94
|
+
key,
|
|
95
|
+
{
|
|
96
|
+
...field,
|
|
97
|
+
label: pickLocalizedLabel(field.label, loc, fb) || field.label
|
|
98
|
+
}
|
|
99
|
+
]));
|
|
100
|
+
const actions = resource.actions
|
|
101
|
+
? Object.fromEntries(Object.entries(resource.actions).map(([key, action]) => [
|
|
102
|
+
key,
|
|
103
|
+
{
|
|
104
|
+
...action,
|
|
105
|
+
label: pickLocalizedLabel(action.label, loc, fb) || action.label
|
|
106
|
+
}
|
|
107
|
+
]))
|
|
108
|
+
: undefined;
|
|
109
|
+
return {
|
|
110
|
+
...resource,
|
|
111
|
+
label: pickLocalizedLabel(resource.label, loc, fb),
|
|
112
|
+
fields,
|
|
113
|
+
actions
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
export function shouldApplyListScope(resource, userPermissions) {
|
|
117
|
+
const ls = resource.listScope;
|
|
118
|
+
if (!ls || ls.type === "none" || !("field" in ls))
|
|
119
|
+
return false;
|
|
120
|
+
if (userPermissions.includes("*"))
|
|
121
|
+
return false;
|
|
122
|
+
for (const p of ls.bypassPermissions ?? []) {
|
|
123
|
+
if (userPermissions.includes(p))
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
export function listScopeWhereClause(resource, userId) {
|
|
129
|
+
const ls = resource.listScope;
|
|
130
|
+
if (!ls || ls.type === "none" || !("field" in ls))
|
|
131
|
+
return undefined;
|
|
132
|
+
return { [ls.field]: userId };
|
|
133
|
+
}
|
|
134
|
+
export function mergeWhereClauses(...parts) {
|
|
135
|
+
const clauses = parts.filter(Boolean);
|
|
136
|
+
if (!clauses.length)
|
|
137
|
+
return undefined;
|
|
138
|
+
if (clauses.length === 1)
|
|
139
|
+
return clauses[0];
|
|
140
|
+
return { AND: clauses };
|
|
141
|
+
}
|
|
142
|
+
export function buildSeoPagePayload(opts) {
|
|
143
|
+
const { siteUrl, basePath, seo, record, resourceLabel } = opts;
|
|
144
|
+
const titleField = seo?.titleField ?? "title";
|
|
145
|
+
const descField = seo?.descriptionField ?? "description";
|
|
146
|
+
const slugField = seo?.slugField ?? "slug";
|
|
147
|
+
const title = String(record[titleField] ?? resourceLabel);
|
|
148
|
+
const description = String(record[descField] ?? record["excerpt"] ?? `${resourceLabel} page`);
|
|
149
|
+
const slug = String(record[slugField] ?? record["id"]);
|
|
150
|
+
const path = (seo?.pathPattern ?? `${basePath}/:slug`).replace(":slug", encodeURIComponent(slug));
|
|
151
|
+
const canonicalUrl = new URL(path.startsWith("/") ? path : `/${path}`, siteUrl).toString();
|
|
152
|
+
const ogImageField = seo?.ogImageField;
|
|
153
|
+
const imageUrl = ogImageField && record[ogImageField] ? String(record[ogImageField]) : undefined;
|
|
154
|
+
const languages = {};
|
|
155
|
+
if (seo?.alternateSlugFields) {
|
|
156
|
+
for (const [loc, field] of Object.entries(seo.alternateSlugFields)) {
|
|
157
|
+
const altSlug = record[field];
|
|
158
|
+
if (altSlug != null && String(altSlug)) {
|
|
159
|
+
const altPath = (seo.pathPattern ?? `${basePath}/:slug`).replace(":slug", encodeURIComponent(String(altSlug)));
|
|
160
|
+
languages[loc] = new URL(altPath.startsWith("/") ? altPath : `/${altPath}`, siteUrl).toString();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
title,
|
|
166
|
+
description,
|
|
167
|
+
canonicalUrl,
|
|
168
|
+
openGraph: {
|
|
169
|
+
title,
|
|
170
|
+
description,
|
|
171
|
+
url: canonicalUrl,
|
|
172
|
+
type: "article",
|
|
173
|
+
...(imageUrl ? { images: [{ url: imageUrl }] } : {})
|
|
174
|
+
},
|
|
175
|
+
...(Object.keys(languages).length ? { alternates: { languages } } : {})
|
|
176
|
+
};
|
|
177
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
defineResource,
|
|
4
|
+
isSensitiveField,
|
|
5
|
+
resolveResourceForLocale,
|
|
6
|
+
validateResource,
|
|
7
|
+
visibleFields,
|
|
8
|
+
withSafeFields
|
|
9
|
+
} from "./index";
|
|
10
|
+
|
|
11
|
+
describe("defineResource", () => {
|
|
12
|
+
it("hides sensitive fields by default", () => {
|
|
13
|
+
const resource = defineResource({
|
|
14
|
+
name: "users",
|
|
15
|
+
label: "Users",
|
|
16
|
+
model: "User",
|
|
17
|
+
permissions: { read: "users.read" },
|
|
18
|
+
fields: {
|
|
19
|
+
id: { type: "id" },
|
|
20
|
+
email: { type: "email" },
|
|
21
|
+
passwordHash: { type: "text" }
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
expect(isSensitiveField("passwordHash", resource.fields.passwordHash)).toBe(true);
|
|
26
|
+
expect(visibleFields(resource, "list", [])).not.toHaveProperty("passwordHash");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("requires read permission", () => {
|
|
30
|
+
expect(() =>
|
|
31
|
+
defineResource({
|
|
32
|
+
name: "posts",
|
|
33
|
+
label: "Posts",
|
|
34
|
+
model: "Post",
|
|
35
|
+
permissions: {},
|
|
36
|
+
fields: { id: { type: "id" } }
|
|
37
|
+
})
|
|
38
|
+
).toThrow("permissions.read");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("rejects invalid resource names", () => {
|
|
42
|
+
expect(() =>
|
|
43
|
+
defineResource({
|
|
44
|
+
name: "BadName",
|
|
45
|
+
label: "X",
|
|
46
|
+
model: "X",
|
|
47
|
+
permissions: { read: "x.read" },
|
|
48
|
+
fields: { id: { type: "id" } }
|
|
49
|
+
})
|
|
50
|
+
).toThrow("Invalid resource name");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("rejects invalid prisma model names", () => {
|
|
54
|
+
expect(() =>
|
|
55
|
+
defineResource({
|
|
56
|
+
name: "items",
|
|
57
|
+
label: "Items",
|
|
58
|
+
model: "9bad",
|
|
59
|
+
permissions: { read: "items.read" },
|
|
60
|
+
fields: { id: { type: "id" } }
|
|
61
|
+
})
|
|
62
|
+
).toThrow("Invalid Prisma model name");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("rejects empty fields", () => {
|
|
66
|
+
expect(() =>
|
|
67
|
+
validateResource({
|
|
68
|
+
name: "items",
|
|
69
|
+
label: "Items",
|
|
70
|
+
model: "Item",
|
|
71
|
+
permissions: { read: "items.read" },
|
|
72
|
+
fields: {}
|
|
73
|
+
})
|
|
74
|
+
).toThrow("at least one field");
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe("visibleFields", () => {
|
|
79
|
+
it("respects field-level read permission", () => {
|
|
80
|
+
const resource = defineResource({
|
|
81
|
+
name: "posts",
|
|
82
|
+
label: "Posts",
|
|
83
|
+
model: "Post",
|
|
84
|
+
permissions: { read: "posts.read" },
|
|
85
|
+
fields: {
|
|
86
|
+
id: { type: "id", list: true },
|
|
87
|
+
title: { type: "text", list: true, permissions: { read: "posts.title" } },
|
|
88
|
+
body: { type: "textarea", list: true }
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const list = visibleFields(resource, "list", ["posts.read"]);
|
|
93
|
+
expect(list).toHaveProperty("id");
|
|
94
|
+
expect(list).not.toHaveProperty("title");
|
|
95
|
+
expect(list).toHaveProperty("body");
|
|
96
|
+
|
|
97
|
+
const withTitle = visibleFields(resource, "list", ["posts.read", "posts.title"]);
|
|
98
|
+
expect(withTitle).toHaveProperty("title");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("hides fields with list: false", () => {
|
|
102
|
+
const resource = defineResource({
|
|
103
|
+
name: "posts",
|
|
104
|
+
label: "Posts",
|
|
105
|
+
model: "Post",
|
|
106
|
+
permissions: { read: "posts.read" },
|
|
107
|
+
fields: {
|
|
108
|
+
id: { type: "id", list: true },
|
|
109
|
+
secret: { type: "text", list: false }
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
expect(visibleFields(resource, "list", ["posts.read"])).not.toHaveProperty("secret");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("resolveResourceForLocale", () => {
|
|
117
|
+
it("resolves labels for requested locale", () => {
|
|
118
|
+
const resource = defineResource({
|
|
119
|
+
name: "items",
|
|
120
|
+
label: { en: "Items", ru: "Элементы" },
|
|
121
|
+
model: "Item",
|
|
122
|
+
i18n: { defaultLocale: "en", locales: ["en", "ru"] },
|
|
123
|
+
permissions: { read: "items.read" },
|
|
124
|
+
fields: {
|
|
125
|
+
id: { type: "id", list: true },
|
|
126
|
+
title: { type: "text", list: true, label: { en: "Title", ru: "Название" } }
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
const ru = resolveResourceForLocale(resource, "ru");
|
|
130
|
+
expect(ru.label).toBe("Элементы");
|
|
131
|
+
expect(ru.fields.title?.label).toBe("Название");
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe("withSafeFields", () => {
|
|
136
|
+
it("strips list/detail for sensitive fields but keeps password fields configurable", () => {
|
|
137
|
+
const raw = {
|
|
138
|
+
name: "users",
|
|
139
|
+
label: "Users",
|
|
140
|
+
model: "User",
|
|
141
|
+
permissions: { read: "users.read" },
|
|
142
|
+
fields: {
|
|
143
|
+
id: { type: "id" },
|
|
144
|
+
password: { type: "password", create: true, edit: true }
|
|
145
|
+
}
|
|
146
|
+
} satisfies Parameters<typeof withSafeFields>[0];
|
|
147
|
+
const safe = withSafeFields(raw);
|
|
148
|
+
expect(safe.fields.password?.list).toBe(false);
|
|
149
|
+
expect(safe.fields.password?.detail).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
});
|