openadminjs 0.1.16 → 0.1.18
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/LICENSE +21 -0
- package/dist/create-project.js +4 -1
- package/dist/index.js +127 -0
- package/package.json +10 -11
- package/template/apps/admin/.storybook/main.ts +12 -0
- package/template/apps/admin/.storybook/preview.ts +10 -0
- package/template/apps/admin/app/dashboard/page.tsx +357 -277
- package/template/apps/admin/app/layout.tsx +5 -1
- package/template/apps/admin/app/login/page.tsx +38 -0
- package/template/apps/admin/app/resources/[resource]/page.tsx +15 -1
- package/template/apps/admin/app/settings/page.tsx +40 -2
- package/template/apps/admin/app/system-logs/page.tsx +212 -0
- package/template/apps/admin/components/api-error-banner.tsx +26 -0
- package/template/apps/admin/components/app-shell.tsx +92 -18
- package/template/apps/admin/components/logo.stories.tsx +24 -0
- package/template/apps/admin/components/resource-form.tsx +47 -0
- package/template/apps/admin/components/resource-table.tsx +417 -44
- package/template/apps/admin/lib/api.ts +44 -1
- package/template/apps/admin/lib/resource-table-prefs.ts +57 -0
- package/template/apps/admin/package.json +8 -2
- package/template/apps/api/package.json +28 -15
- package/template/apps/api/plugins.manifest.example.json +12 -0
- package/template/apps/api/prisma.config.ts +6 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +3 -2
- package/template/apps/api/src/admin/admin-logs.controller.ts +31 -0
- package/template/apps/api/src/admin/admin-plugins.controller.ts +5 -0
- package/template/apps/api/src/admin/admin-plugins.service.ts +11 -0
- package/template/apps/api/src/admin/admin-resource.service.ts +235 -1
- package/template/apps/api/src/admin/admin.controller.ts +35 -0
- package/template/apps/api/src/admin/admin.module.ts +6 -3
- package/template/apps/api/src/admin/dto/resource-bulk.dto.ts +17 -0
- package/template/apps/api/src/admin/dto/resource-reorder.dto.ts +17 -0
- package/template/apps/api/src/admin/dto/resource-upload.dto.ts +20 -0
- package/template/apps/api/src/app.module.ts +45 -5
- package/template/apps/api/src/auth/auth.module.ts +4 -2
- package/template/apps/api/src/auth/auth.service.test.ts +6 -3
- package/template/apps/api/src/auth/auth.service.ts +84 -0
- package/template/apps/api/src/auth/sso-oidc.controller.ts +78 -0
- package/template/apps/api/src/auth/sso-oidc.service.test.ts +84 -0
- package/template/apps/api/src/auth/sso-oidc.service.ts +281 -0
- package/template/apps/api/src/common/app-logger.service.ts +138 -0
- package/template/apps/api/src/common/auth.guard.test.ts +1 -0
- package/template/apps/api/src/common/auth.guard.ts +15 -1
- package/template/apps/api/src/common/current-user.decorator.ts +6 -2
- package/template/apps/api/src/common/logging.module.ts +10 -0
- package/template/apps/api/src/common/request-logging.interceptor.ts +70 -0
- package/template/apps/api/src/graphql/admin-resources.resolver.test.ts +44 -0
- package/template/apps/api/src/graphql/admin-resources.resolver.ts +65 -0
- package/template/apps/api/src/graphql/graphql.module.ts +26 -0
- package/template/apps/api/src/main.ts +2 -0
- package/template/apps/api/src/plugins/bundled/registry.ts +3 -1
- package/template/apps/api/src/plugins/bundled/webhook.plugin.ts +64 -0
- package/template/apps/api/src/resources/categories.resource.ts +1 -0
- package/template/apps/api/src/resources/system.resource.ts +1 -0
- package/template/apps/web/package.json +2 -2
- package/template/package.json +1 -1
- package/template/packages/auth/package.json +1 -1
- package/template/packages/brand/package.json +1 -1
- package/template/packages/brand/src/generate-assets.mjs +12 -5
- package/template/packages/core/package.json +1 -1
- package/template/packages/core/src/index.ts +5 -0
- package/template/packages/core/vitest.config.ts +9 -0
- package/template/packages/mail/package.json +3 -3
- package/template/packages/notifications/package.json +1 -1
- package/template/packages/permissions/package.json +1 -1
- package/template/packages/plugin-sdk/package.json +1 -1
- package/template/packages/queue/package.json +1 -1
- package/template/packages/resource/package.json +12 -1
- package/template/packages/resource/src/index.test.ts +45 -0
- package/template/packages/resource/src/index.ts +63 -0
- package/template/packages/storage/package.json +1 -1
- package/template/prisma/schema.prisma +5 -0
- package/template/packages/core/src/index.js +0 -1
- package/template/packages/permissions/src/blueprints.js +0 -57
- package/template/packages/permissions/src/index.js +0 -11
- package/template/packages/resource/src/index.js +0 -177
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Module } from "@nestjs/common";
|
|
2
|
+
import { GraphQLModule } from "@nestjs/graphql";
|
|
3
|
+
import { ApolloDriver, ApolloDriverConfig } from "@nestjs/apollo";
|
|
4
|
+
import { GraphQLJSON } from "graphql-scalars";
|
|
5
|
+
import { AdminModule } from "../admin/admin.module";
|
|
6
|
+
import { AuthModule } from "../auth/auth.module";
|
|
7
|
+
import { AuthGuard } from "../common/auth.guard";
|
|
8
|
+
import { AdminResourcesResolver } from "./admin-resources.resolver";
|
|
9
|
+
|
|
10
|
+
@Module({
|
|
11
|
+
imports: [
|
|
12
|
+
GraphQLModule.forRoot<ApolloDriverConfig>({
|
|
13
|
+
driver: ApolloDriver,
|
|
14
|
+
autoSchemaFile: true,
|
|
15
|
+
sortSchema: true,
|
|
16
|
+
path: "/graphql",
|
|
17
|
+
context: ({ req, res }: { req: unknown; res: unknown }) => ({ req, res }),
|
|
18
|
+
resolvers: { JSON: GraphQLJSON },
|
|
19
|
+
playground: process.env.NODE_ENV !== "production"
|
|
20
|
+
}),
|
|
21
|
+
AdminModule,
|
|
22
|
+
AuthModule
|
|
23
|
+
],
|
|
24
|
+
providers: [AdminResourcesResolver, AuthGuard]
|
|
25
|
+
})
|
|
26
|
+
export class GraphqlApiModule {}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
+
import cookieParser from "cookie-parser";
|
|
2
3
|
import helmet from "helmet";
|
|
3
4
|
import { NestFactory } from "@nestjs/core";
|
|
4
5
|
import { ValidationPipe } from "@nestjs/common";
|
|
@@ -7,6 +8,7 @@ import { AppModule } from "./app.module";
|
|
|
7
8
|
|
|
8
9
|
async function bootstrap() {
|
|
9
10
|
const app = await NestFactory.create(AppModule);
|
|
11
|
+
app.use(cookieParser());
|
|
10
12
|
app.use(helmet());
|
|
11
13
|
const prodOrigins = (process.env.ADMIN_ORIGIN ?? "")
|
|
12
14
|
.split(",")
|
|
@@ -3,11 +3,13 @@ import { adminWidgetBundledPlugin } from "./admin-widget.plugin";
|
|
|
3
3
|
import { helloBundledPlugin } from "./hello.plugin";
|
|
4
4
|
import { imageOptimizerBundledPlugin } from "./image-optimizer.plugin";
|
|
5
5
|
import { seoBundledPlugin } from "./seo.plugin";
|
|
6
|
+
import { webhookBundledPlugin } from "./webhook.plugin";
|
|
6
7
|
|
|
7
8
|
/** First-party plugins shipped inside the API package (no separate npm install). */
|
|
8
9
|
export const bundledOpenAdminPlugins: Record<string, OpenAdminPlugin> = {
|
|
9
10
|
hello: helloBundledPlugin,
|
|
10
11
|
seo: seoBundledPlugin,
|
|
11
12
|
"image-optimizer": imageOptimizerBundledPlugin,
|
|
12
|
-
"admin-widget": adminWidgetBundledPlugin
|
|
13
|
+
"admin-widget": adminWidgetBundledPlugin,
|
|
14
|
+
webhook: webhookBundledPlugin
|
|
13
15
|
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { OpenAdminPlugin } from "@openadminjs/plugin-sdk";
|
|
2
|
+
import { resources } from "../../resources/registry";
|
|
3
|
+
|
|
4
|
+
type WebhookConfig = {
|
|
5
|
+
url?: string;
|
|
6
|
+
secret?: string;
|
|
7
|
+
resources?: string[];
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const webhookBundledPlugin: OpenAdminPlugin<WebhookConfig> = {
|
|
12
|
+
id: "io.openadminjs.webhook",
|
|
13
|
+
version: "0.1.0",
|
|
14
|
+
displayName: "Webhook bridge",
|
|
15
|
+
register({ config, registerResourceHooks }) {
|
|
16
|
+
const url = typeof config.url === "string" ? config.url.trim() : "";
|
|
17
|
+
if (!url) return;
|
|
18
|
+
const secret = typeof config.secret === "string" ? config.secret : "";
|
|
19
|
+
const timeoutMs = typeof config.timeoutMs === "number" ? Math.max(500, config.timeoutMs) : 8_000;
|
|
20
|
+
const allow = Array.isArray(config.resources) ? new Set(config.resources.map((r) => String(r))) : null;
|
|
21
|
+
|
|
22
|
+
const emit = async (payload: Record<string, unknown>) => {
|
|
23
|
+
const controller = new AbortController();
|
|
24
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
25
|
+
try {
|
|
26
|
+
await fetch(url, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
signal: controller.signal,
|
|
29
|
+
headers: {
|
|
30
|
+
"content-type": "application/json",
|
|
31
|
+
...(secret ? { "x-openadminjs-signature": secret } : {})
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify(payload)
|
|
34
|
+
});
|
|
35
|
+
} catch {
|
|
36
|
+
/* fire-and-forget */
|
|
37
|
+
} finally {
|
|
38
|
+
clearTimeout(timeout);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
for (const resource of resources) {
|
|
43
|
+
if (allow && !allow.has(resource.name)) continue;
|
|
44
|
+
registerResourceHooks(resource.name, {
|
|
45
|
+
afterCreate: async (ctx) => {
|
|
46
|
+
await emit({ event: "resource.created", resource: ctx.resourceName, record: ctx.record, actor: ctx.user });
|
|
47
|
+
},
|
|
48
|
+
afterUpdate: async (ctx) => {
|
|
49
|
+
await emit({
|
|
50
|
+
event: "resource.updated",
|
|
51
|
+
resource: ctx.resourceName,
|
|
52
|
+
id: ctx.id,
|
|
53
|
+
before: ctx.before,
|
|
54
|
+
after: ctx.record,
|
|
55
|
+
actor: ctx.user
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
afterDelete: async (ctx) => {
|
|
59
|
+
await emit({ event: "resource.deleted", resource: ctx.resourceName, id: ctx.id, actor: ctx.user });
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
@@ -16,6 +16,7 @@ export default defineResource({
|
|
|
16
16
|
id: { type: "id", label: "ID", create: false, edit: false, list: false },
|
|
17
17
|
name: { type: "text", label: "Name", required: true, searchable: true, sortable: true },
|
|
18
18
|
slug: { type: "slug", label: "Slug", from: "name", searchable: true },
|
|
19
|
+
order: { type: "number", label: "Order", list: true, sortable: true, min: 0 },
|
|
19
20
|
createdAt: { type: "datetime", label: "Created at", create: false, edit: false, sortable: true }
|
|
20
21
|
}
|
|
21
22
|
});
|
|
@@ -10,6 +10,7 @@ export const filesResource = defineResource({
|
|
|
10
10
|
fields: {
|
|
11
11
|
id: { type: "id", create: false, edit: false, list: false },
|
|
12
12
|
filename: { type: "text", label: "Filename", searchable: true },
|
|
13
|
+
path: { type: "text", label: "Path", list: false },
|
|
13
14
|
mimeType: { type: "text", label: "MIME", filterable: true },
|
|
14
15
|
size: { type: "number", label: "Size" },
|
|
15
16
|
createdAt: { type: "datetime", label: "Created", create: false, edit: false }
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"@openadminjs/core": "workspace:*",
|
|
16
|
-
"next": "^15.
|
|
16
|
+
"next": "^15.5.18",
|
|
17
17
|
"react": "^19.0.0",
|
|
18
18
|
"react-dom": "^19.0.0"
|
|
19
19
|
},
|
|
@@ -25,6 +25,6 @@
|
|
|
25
25
|
"postcss": "^8.4.49",
|
|
26
26
|
"tailwindcss": "^3.4.17",
|
|
27
27
|
"typescript": "^5.7.2",
|
|
28
|
-
"vitest": "^2.
|
|
28
|
+
"vitest": "^3.2.0"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/template/package.json
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
9
9
|
import { join, dirname } from "node:path";
|
|
10
10
|
import { fileURLToPath } from "node:url";
|
|
11
|
-
import sharp from "sharp";
|
|
12
11
|
|
|
13
12
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
14
13
|
/** `packages/brand/src` → monorepo root */
|
|
@@ -42,7 +41,7 @@ function svgRasterImage(href, w, h, label) {
|
|
|
42
41
|
return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 ${w} ${h}" role="img" aria-label="${safe}"><image xlink:href="${href}" href="${href}" width="${w}" height="${h}" preserveAspectRatio="xMidYMid meet"/></svg>`;
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
async function extractLogoMark(logoBuf) {
|
|
44
|
+
async function extractLogoMark(sharp, logoBuf) {
|
|
46
45
|
const { data, info } = await sharp(logoBuf).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
47
46
|
const { width, height, channels } = info;
|
|
48
47
|
const alphaIndex = channels - 1;
|
|
@@ -114,7 +113,7 @@ async function extractLogoMark(logoBuf) {
|
|
|
114
113
|
.toBuffer();
|
|
115
114
|
}
|
|
116
115
|
|
|
117
|
-
async function writeBrandToDir(targetDir) {
|
|
116
|
+
async function writeBrandToDir(sharp, targetDir) {
|
|
118
117
|
mkdirSync(targetDir, { recursive: true });
|
|
119
118
|
|
|
120
119
|
const logoBuf = readFileSync(LOGO_SRC);
|
|
@@ -127,7 +126,7 @@ async function writeBrandToDir(targetDir) {
|
|
|
127
126
|
const monoPng = await sharp(logoBuf).grayscale().png().toBuffer();
|
|
128
127
|
writeFileSync(join(targetDir, "openadminjs-logo-monochrome.png"), monoPng);
|
|
129
128
|
|
|
130
|
-
const iconBuf = await extractLogoMark(logoBuf);
|
|
129
|
+
const iconBuf = await extractLogoMark(sharp, logoBuf);
|
|
131
130
|
writeFileSync(join(targetDir, "openadminjs-icon-new.png"), iconBuf);
|
|
132
131
|
|
|
133
132
|
writeFileSync(join(targetDir, "favicon.svg"), svgRasterImage("openadminjs-icon-new.png", 512, 512, "OpenAdminJS"));
|
|
@@ -168,9 +167,17 @@ async function main() {
|
|
|
168
167
|
throw new Error(`Missing required source logo: ${LOGO_SRC}`);
|
|
169
168
|
}
|
|
170
169
|
|
|
170
|
+
let sharp;
|
|
171
|
+
try {
|
|
172
|
+
const sharpModule = await import("sharp");
|
|
173
|
+
sharp = sharpModule.default;
|
|
174
|
+
} catch {
|
|
175
|
+
throw new Error("Missing optional dependency 'sharp'. Install dependencies before generating brand assets.");
|
|
176
|
+
}
|
|
177
|
+
|
|
171
178
|
for (const rel of targets) {
|
|
172
179
|
const dir = join(repoRoot, rel);
|
|
173
|
-
await writeBrandToDir(dir);
|
|
180
|
+
await writeBrandToDir(sharp, dir);
|
|
174
181
|
console.log("Wrote", rel);
|
|
175
182
|
}
|
|
176
183
|
console.log("Brand assets generated from openadminjs-logo-new.png");
|
|
@@ -2,6 +2,9 @@ export {
|
|
|
2
2
|
buildSeoPagePayload,
|
|
3
3
|
defineResource,
|
|
4
4
|
effectiveLocale,
|
|
5
|
+
formatIcuMessage,
|
|
6
|
+
formatLocalizedIcuMessage,
|
|
7
|
+
formatPlural,
|
|
5
8
|
isSensitiveField,
|
|
6
9
|
listScopeWhereClause,
|
|
7
10
|
mergeWhereClauses,
|
|
@@ -17,7 +20,9 @@ export type {
|
|
|
17
20
|
FieldPermission,
|
|
18
21
|
FieldType,
|
|
19
22
|
ListScopeConfig,
|
|
23
|
+
IcuMessageValues,
|
|
20
24
|
LocalizedLabel,
|
|
25
|
+
PluralFormBundle,
|
|
21
26
|
PermissionMap,
|
|
22
27
|
ResourceAction,
|
|
23
28
|
ResourceActionContext,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
/** Prefer workspace package `development` export (TS sources) when `dist/` is not built yet (CI). */
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
resolve: {
|
|
6
|
+
conditions: ["development", "node", "import", "module", "browser", "default"]
|
|
7
|
+
},
|
|
8
|
+
test: { environment: "node" }
|
|
9
|
+
});
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"test": "vitest run"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@types/nodemailer": "^
|
|
14
|
-
"nodemailer": "^
|
|
13
|
+
"@types/nodemailer": "^8.0.0",
|
|
14
|
+
"nodemailer": "^8.0.5",
|
|
15
15
|
"typescript": "^5.7.2",
|
|
16
|
-
"vitest": "^2.
|
|
16
|
+
"vitest": "^3.2.0"
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -4,6 +4,14 @@
|
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"development": "./src/index.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
7
15
|
"scripts": {
|
|
8
16
|
"build": "tsc -p tsconfig.json",
|
|
9
17
|
"lint": "tsc -p tsconfig.json --noEmit",
|
|
@@ -12,6 +20,9 @@
|
|
|
12
20
|
},
|
|
13
21
|
"devDependencies": {
|
|
14
22
|
"typescript": "^5.7.2",
|
|
15
|
-
"vitest": "^2.
|
|
23
|
+
"vitest": "^3.2.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"intl-messageformat": "^11.2.4"
|
|
16
27
|
}
|
|
17
28
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import {
|
|
3
3
|
defineResource,
|
|
4
|
+
formatIcuMessage,
|
|
5
|
+
formatLocalizedIcuMessage,
|
|
6
|
+
formatPlural,
|
|
4
7
|
isSensitiveField,
|
|
5
8
|
resolveResourceForLocale,
|
|
6
9
|
validateResource,
|
|
@@ -8,6 +11,48 @@ import {
|
|
|
8
11
|
withSafeFields
|
|
9
12
|
} from "./index";
|
|
10
13
|
|
|
14
|
+
describe("formatPlural", () => {
|
|
15
|
+
it("replaces {count} using English plural rules", () => {
|
|
16
|
+
expect(
|
|
17
|
+
formatPlural("en", 1, { one: "{count} item", other: "{count} items" })
|
|
18
|
+
).toBe("1 item");
|
|
19
|
+
expect(
|
|
20
|
+
formatPlural("en", 42, { one: "{count} item", other: "{count} items" })
|
|
21
|
+
).toBe("42 items");
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("formatIcuMessage", () => {
|
|
26
|
+
it("formats ICU plural rules", () => {
|
|
27
|
+
expect(
|
|
28
|
+
formatIcuMessage("en", "{count, plural, one {# file} other {# files}}", { count: 1 })
|
|
29
|
+
).toBe("1 file");
|
|
30
|
+
expect(
|
|
31
|
+
formatIcuMessage("en", "{count, plural, one {# file} other {# files}}", { count: 5 })
|
|
32
|
+
).toBe("5 files");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("formats select/plural complex ICU patterns", () => {
|
|
36
|
+
const output = formatIcuMessage(
|
|
37
|
+
"en",
|
|
38
|
+
"{gender, select, male {{count, plural, one {He has # alert} other {He has # alerts}}} female {{count, plural, one {She has # alert} other {She has # alerts}}} other {{count, plural, one {They have # alert} other {They have # alerts}}}}",
|
|
39
|
+
{ gender: "female", count: 3 }
|
|
40
|
+
);
|
|
41
|
+
expect(output).toBe("She has 3 alerts");
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("formatLocalizedIcuMessage", () => {
|
|
46
|
+
it("resolves locale map and renders ICU", () => {
|
|
47
|
+
const label = {
|
|
48
|
+
en: "{count, plural, one {# draft} other {# drafts}}",
|
|
49
|
+
ru: "{count, plural, one {# черновик} few {# черновика} many {# черновиков} other {# черновика}}"
|
|
50
|
+
};
|
|
51
|
+
expect(formatLocalizedIcuMessage(label, "en", { count: 2 })).toBe("2 drafts");
|
|
52
|
+
expect(formatLocalizedIcuMessage(label, "ru", { count: 5 })).toContain("5 ");
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
11
56
|
describe("defineResource", () => {
|
|
12
57
|
it("hides sensitive fields by default", () => {
|
|
13
58
|
const resource = defineResource({
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import IntlMessageFormat from "intl-messageformat";
|
|
2
|
+
|
|
1
3
|
export type FieldType =
|
|
2
4
|
| "id"
|
|
3
5
|
| "text"
|
|
@@ -218,6 +220,67 @@ export function pickLocalizedLabel(label: LocalizedLabel | undefined, locale: st
|
|
|
218
220
|
return label[locale] ?? label[fallbackLocale] ?? Object.values(label)[0] ?? "";
|
|
219
221
|
}
|
|
220
222
|
|
|
223
|
+
/** ICU-style plural categories for `Intl.PluralRules` (locales like `en`, `ru`). */
|
|
224
|
+
export type PluralFormBundle = {
|
|
225
|
+
zero?: string;
|
|
226
|
+
one: string;
|
|
227
|
+
two?: string;
|
|
228
|
+
few?: string;
|
|
229
|
+
many?: string;
|
|
230
|
+
other: string;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Picks the correct plural template for `count` in `locale`, then replaces `{count}`.
|
|
235
|
+
* Use in resource `i18n.translations` blocks for list summaries, pagination, etc.
|
|
236
|
+
*/
|
|
237
|
+
export function formatPlural(locale: string, count: number, forms: PluralFormBundle): string {
|
|
238
|
+
const pr = new Intl.PluralRules(locale);
|
|
239
|
+
const cat = pr.select(count);
|
|
240
|
+
const raw =
|
|
241
|
+
(cat === "zero" && forms.zero) ||
|
|
242
|
+
(cat === "one" && forms.one) ||
|
|
243
|
+
(cat === "two" && forms.two) ||
|
|
244
|
+
(cat === "few" && forms.few) ||
|
|
245
|
+
(cat === "many" && forms.many) ||
|
|
246
|
+
forms.other ||
|
|
247
|
+
forms.one;
|
|
248
|
+
return raw.replace(/\{count\}/g, String(count));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Full ICU MessageFormat strings (plural/select/rich text per Unicode TR35).
|
|
253
|
+
* Example: `"{n, plural, one {# item} other {# items}}"`.
|
|
254
|
+
*/
|
|
255
|
+
export function formatIcuMessage(
|
|
256
|
+
locale: string,
|
|
257
|
+
pattern: string,
|
|
258
|
+
values?: Record<string, string | number | boolean | Date | null | undefined>
|
|
259
|
+
): string {
|
|
260
|
+
try {
|
|
261
|
+
return new IntlMessageFormat(pattern, locale).format(values ?? {}) as string;
|
|
262
|
+
} catch {
|
|
263
|
+
return pattern;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export type IcuMessageValues = Record<string, string | number | boolean | Date | null | undefined>;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Resolve localized ICU template then render it with `intl-messageformat`.
|
|
271
|
+
* Useful for translation blocks where each locale has its own ICU string.
|
|
272
|
+
*/
|
|
273
|
+
export function formatLocalizedIcuMessage(
|
|
274
|
+
label: LocalizedLabel | undefined,
|
|
275
|
+
locale: string,
|
|
276
|
+
values?: IcuMessageValues,
|
|
277
|
+
fallbackLocale = "en"
|
|
278
|
+
): string {
|
|
279
|
+
const pattern = pickLocalizedLabel(label, locale, fallbackLocale);
|
|
280
|
+
if (!pattern) return "";
|
|
281
|
+
return formatIcuMessage(locale, pattern, values);
|
|
282
|
+
}
|
|
283
|
+
|
|
221
284
|
export function effectiveLocale(resource: ResourceConfig, requested: string | undefined): string {
|
|
222
285
|
const fb = resource.i18n?.defaultLocale ?? "en";
|
|
223
286
|
const locales = resource.i18n?.locales;
|
|
@@ -11,6 +11,8 @@ model User {
|
|
|
11
11
|
id String @id @default(cuid())
|
|
12
12
|
email String @unique
|
|
13
13
|
passwordHash String
|
|
14
|
+
/** Stable IdP subject, format `issuerNormalized::sub` — set when user signs in via OIDC. */
|
|
15
|
+
oidcKey String? @unique
|
|
14
16
|
refreshTokenHash String?
|
|
15
17
|
name String?
|
|
16
18
|
avatarUrl String?
|
|
@@ -70,9 +72,12 @@ model Category {
|
|
|
70
72
|
id String @id @default(cuid())
|
|
71
73
|
name String
|
|
72
74
|
slug String @unique
|
|
75
|
+
order Int @default(0)
|
|
73
76
|
createdAt DateTime @default(now())
|
|
74
77
|
updatedAt DateTime @updatedAt
|
|
75
78
|
posts Post[]
|
|
79
|
+
|
|
80
|
+
@@index([order])
|
|
76
81
|
}
|
|
77
82
|
|
|
78
83
|
model Post {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { buildSeoPagePayload, defineResource, effectiveLocale, isSensitiveField, listScopeWhereClause, mergeWhereClauses, pickLocalizedLabel, resolveResourceForLocale, sensitiveFieldPatterns, shouldApplyListScope, validateResource, visibleFields, withSafeFields } from "@openadminjs/resource";
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
export const DEFAULT_AUTH_REALM = "admin";
|
|
2
|
-
export function realmOf(blueprint) {
|
|
3
|
-
return blueprint.realm ?? DEFAULT_AUTH_REALM;
|
|
4
|
-
}
|
|
5
|
-
export function roleMatrixKey(realm, roleName) {
|
|
6
|
-
return `${realm}:${roleName}`;
|
|
7
|
-
}
|
|
8
|
-
function collectRolePermissions(blueprints, realm, roleName, wildcardSlugs, stack, memo) {
|
|
9
|
-
const memoKey = roleMatrixKey(realm, roleName);
|
|
10
|
-
if (memo.has(memoKey))
|
|
11
|
-
return new Set(memo.get(memoKey));
|
|
12
|
-
if (stack.has(memoKey)) {
|
|
13
|
-
throw new Error(`Circular role inheritance involving "${realm}:${roleName}"`);
|
|
14
|
-
}
|
|
15
|
-
const bp = blueprints.find((b) => b.name === roleName && realmOf(b) === realm);
|
|
16
|
-
if (!bp) {
|
|
17
|
-
throw new Error(`Unknown role blueprint: "${roleName}" in realm "${realm}"`);
|
|
18
|
-
}
|
|
19
|
-
stack.add(memoKey);
|
|
20
|
-
const out = new Set();
|
|
21
|
-
for (const parent of bp.inherits ?? []) {
|
|
22
|
-
for (const p of collectRolePermissions(blueprints, realm, parent, wildcardSlugs, stack, memo)) {
|
|
23
|
-
out.add(p);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
for (const p of bp.permissions ?? []) {
|
|
27
|
-
if (p === "*") {
|
|
28
|
-
out.add("*");
|
|
29
|
-
for (const s of wildcardSlugs)
|
|
30
|
-
out.add(s);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
out.add(p);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
stack.delete(memoKey);
|
|
37
|
-
memo.set(memoKey, out);
|
|
38
|
-
return out;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Resolves inherits + wildcard expansion within one realm.
|
|
42
|
-
* When `slugsByRealm` is set, `"*"` expands only that realm's slug list (so admin superusers do not silently gain storefront-only slugs unless you assign them).
|
|
43
|
-
*/
|
|
44
|
-
export function expandRolePermissions(blueprints, realm, roleName, allPermissionSlugs, slugsByRealm) {
|
|
45
|
-
const wildcardSlugs = slugsByRealm?.[realm] ?? allPermissionSlugs;
|
|
46
|
-
return collectRolePermissions(blueprints, realm, roleName, wildcardSlugs, new Set(), new Map());
|
|
47
|
-
}
|
|
48
|
-
/** Every blueprint gets a frozen permission array keyed as `realm:roleName`. */
|
|
49
|
-
export function materializeAllRoles(blueprints, allPermissionSlugs, slugsByRealm) {
|
|
50
|
-
const map = new Map();
|
|
51
|
-
for (const bp of blueprints) {
|
|
52
|
-
const r = realmOf(bp);
|
|
53
|
-
const set = expandRolePermissions(blueprints, r, bp.name, allPermissionSlugs, slugsByRealm);
|
|
54
|
-
map.set(roleMatrixKey(r, bp.name), [...set].sort());
|
|
55
|
-
}
|
|
56
|
-
return map;
|
|
57
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export { DEFAULT_AUTH_REALM, expandRolePermissions, materializeAllRoles, realmOf, roleMatrixKey } from "./blueprints";
|
|
2
|
-
export function can(permissions, permission) {
|
|
3
|
-
return permissions.includes(permission) || permissions.includes("*");
|
|
4
|
-
}
|
|
5
|
-
export function requirePermission(permissions, permission) {
|
|
6
|
-
if (!can(permissions, permission)) {
|
|
7
|
-
const error = new Error(`Missing permission: ${permission}`);
|
|
8
|
-
error.name = "ForbiddenError";
|
|
9
|
-
throw error;
|
|
10
|
-
}
|
|
11
|
-
}
|