openadminjs 0.1.20 → 0.1.21
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/package.json +1 -1
- package/template/apps/api/nest-cli.json +11 -0
- package/template/apps/api/package.json +6 -4
- package/template/apps/api/src/admin/admin-plugins.service.ts +2 -2
- package/template/apps/api/src/graphql/admin-resources.args.ts +35 -0
- package/template/apps/api/src/graphql/admin-resources.resolver.test.ts +11 -10
- package/template/apps/api/src/graphql/admin-resources.resolver.ts +11 -13
- package/template/apps/api/src/graphql/ensure-gql-paramtypes.test.ts +25 -0
- package/template/apps/api/src/graphql/ensure-gql-paramtypes.ts +15 -0
- package/template/apps/api/src/graphql/graphql-schema.test.ts +18 -0
- package/template/apps/api/src/plugins/manifest-path.ts +4 -3
- package/template/apps/api/tsconfig.build.json +4 -0
- package/template/apps/api/webpack.config.cjs +13 -0
package/package.json
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/nest-cli",
|
|
3
|
+
"collection": "@nestjs/schematics",
|
|
4
|
+
"sourceRoot": "src",
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"deleteOutDir": true,
|
|
7
|
+
"builder": "webpack",
|
|
8
|
+
"tsConfigPath": "tsconfig.build.json",
|
|
9
|
+
"webpackConfigPath": "webpack.config.cjs"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
"name": "@openadminjs/api",
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"private": true,
|
|
5
|
-
"type": "module",
|
|
6
5
|
"scripts": {
|
|
7
6
|
"dev": "tsx watch src/main.ts",
|
|
8
7
|
"prisma:generate": "prisma generate --schema ../../prisma/schema.prisma",
|
|
9
8
|
"prebuild": "pnpm run prisma:generate",
|
|
10
9
|
"build": "nest build",
|
|
11
|
-
"start": "node dist/
|
|
10
|
+
"start": "node dist/main.js",
|
|
12
11
|
"prelint": "pnpm run prisma:generate",
|
|
13
12
|
"lint": "tsc -p tsconfig.json --noEmit",
|
|
14
13
|
"pretypecheck": "pnpm run prisma:generate",
|
|
@@ -57,14 +56,17 @@
|
|
|
57
56
|
"zod": "^3.24.1"
|
|
58
57
|
},
|
|
59
58
|
"devDependencies": {
|
|
60
|
-
"dotenv": "^16.4.7",
|
|
61
59
|
"@nestjs/cli": "^11.0.21",
|
|
62
60
|
"@types/bcryptjs": "^2.4.6",
|
|
63
61
|
"@types/cookie-parser": "^1.4.10",
|
|
64
62
|
"@types/nodemailer": "^8.0.0",
|
|
63
|
+
"dotenv": "^16.4.7",
|
|
65
64
|
"prisma": "^6.1.0",
|
|
65
|
+
"ts-loader": "^9.5.7",
|
|
66
66
|
"tsx": "4.19.2",
|
|
67
67
|
"typescript": "^5.7.2",
|
|
68
|
-
"vitest": "^3.2.0"
|
|
68
|
+
"vitest": "^3.2.0",
|
|
69
|
+
"webpack": "^5.106.2",
|
|
70
|
+
"webpack-node-externals": "^3.0.0"
|
|
69
71
|
}
|
|
70
72
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
2
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { dirname, join } from "node:path";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
4
|
import { promisify } from "node:util";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
import { BadRequestException, Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
|
|
@@ -275,7 +275,7 @@ export class AdminPluginsService {
|
|
|
275
275
|
}
|
|
276
276
|
|
|
277
277
|
function findPnpmWorkspaceRoot(): string {
|
|
278
|
-
let dir = fileURLToPath(
|
|
278
|
+
let dir = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
|
279
279
|
for (let i = 0; i < 12; i++) {
|
|
280
280
|
if (existsSync(join(dir, "pnpm-workspace.yaml"))) return dir;
|
|
281
281
|
const parent = dirname(dir);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ArgsType, Field, Int } from "@nestjs/graphql";
|
|
2
|
+
import { GraphQLJSON } from "graphql-scalars";
|
|
3
|
+
|
|
4
|
+
@ArgsType()
|
|
5
|
+
export class AdminResourceListArgs {
|
|
6
|
+
@Field()
|
|
7
|
+
name!: string;
|
|
8
|
+
|
|
9
|
+
@Field(() => Int, { nullable: true, defaultValue: 1 })
|
|
10
|
+
page?: number;
|
|
11
|
+
|
|
12
|
+
@Field(() => Int, { nullable: true, defaultValue: 20 })
|
|
13
|
+
limit?: number;
|
|
14
|
+
|
|
15
|
+
@Field(() => String, { nullable: true })
|
|
16
|
+
search?: string;
|
|
17
|
+
|
|
18
|
+
@Field(() => String, { nullable: true })
|
|
19
|
+
sort?: string;
|
|
20
|
+
|
|
21
|
+
@Field(() => String, { nullable: true })
|
|
22
|
+
locale?: string;
|
|
23
|
+
|
|
24
|
+
@Field(() => GraphQLJSON, { nullable: true })
|
|
25
|
+
filter?: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@ArgsType()
|
|
29
|
+
export class AdminResourceRecordArgs {
|
|
30
|
+
@Field()
|
|
31
|
+
name!: string;
|
|
32
|
+
|
|
33
|
+
@Field()
|
|
34
|
+
id!: string;
|
|
35
|
+
}
|
|
@@ -10,16 +10,18 @@ describe("AdminResourcesResolver", () => {
|
|
|
10
10
|
const user = { id: "u1", permissions: ["*"] };
|
|
11
11
|
|
|
12
12
|
await resolver.adminResourceList(
|
|
13
|
-
"posts",
|
|
14
|
-
2,
|
|
15
|
-
15,
|
|
16
|
-
"hello",
|
|
17
|
-
"createdAt:desc",
|
|
18
|
-
"en",
|
|
19
13
|
{
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
name: "posts",
|
|
15
|
+
page: 2,
|
|
16
|
+
limit: 15,
|
|
17
|
+
search: "hello",
|
|
18
|
+
sort: "createdAt:desc",
|
|
19
|
+
locale: "en",
|
|
20
|
+
filter: {
|
|
21
|
+
status: { eq: "published" },
|
|
22
|
+
views: { gte: 10, lte: 100 },
|
|
23
|
+
authorId: "u1"
|
|
24
|
+
}
|
|
23
25
|
},
|
|
24
26
|
{ req: { user } } as never
|
|
25
27
|
);
|
|
@@ -41,4 +43,3 @@ describe("AdminResourcesResolver", () => {
|
|
|
41
43
|
);
|
|
42
44
|
});
|
|
43
45
|
});
|
|
44
|
-
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { UseGuards } from "@nestjs/common";
|
|
2
|
-
import { Args, Context,
|
|
2
|
+
import { Args, Context, Query, Resolver } from "@nestjs/graphql";
|
|
3
3
|
import { GraphQLJSON } from "graphql-scalars";
|
|
4
4
|
import { AdminResourceService } from "../admin/admin-resource.service";
|
|
5
5
|
import { AuthGuard } from "../common/auth.guard";
|
|
6
6
|
import type { RequestUser } from "../common/auth.guard";
|
|
7
7
|
import { RequireAuthRealm } from "../common/auth-realm.decorator";
|
|
8
8
|
import { resources } from "../resources/registry";
|
|
9
|
+
import { AdminResourceListArgs, AdminResourceRecordArgs } from "./admin-resources.args";
|
|
10
|
+
import { ensureGqlParamTypes } from "./ensure-gql-paramtypes";
|
|
9
11
|
|
|
10
12
|
type GqlAuthContext = { req: { user: RequestUser } };
|
|
11
13
|
|
|
@@ -43,16 +45,8 @@ export class AdminResourcesResolver {
|
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
@Query(() => GraphQLJSON, { name: "adminResourceList", description: "Paginated list (same rules as REST /admin/resources/:name)" })
|
|
46
|
-
adminResourceList(
|
|
47
|
-
|
|
48
|
-
@Args("page", { type: () => Int, nullable: true, defaultValue: 1 }) page: number,
|
|
49
|
-
@Args("limit", { type: () => Int, nullable: true, defaultValue: 20 }) limit: number,
|
|
50
|
-
@Args("search", { type: () => String, nullable: true }) search: string | undefined,
|
|
51
|
-
@Args("sort", { type: () => String, nullable: true }) sort: string | undefined,
|
|
52
|
-
@Args("locale", { type: () => String, nullable: true }) locale: string | undefined,
|
|
53
|
-
@Args("filter", { type: () => GraphQLJSON, nullable: true }) filter: unknown | undefined,
|
|
54
|
-
@Context() ctx: GqlAuthContext
|
|
55
|
-
) {
|
|
48
|
+
adminResourceList(@Args() args: AdminResourceListArgs, @Context() ctx: GqlAuthContext) {
|
|
49
|
+
const { name, page = 1, limit = 20, search, sort, locale, filter } = args;
|
|
56
50
|
const query: Record<string, unknown> = { page, limit, ...this.flattenFilters(filter) };
|
|
57
51
|
if (search) query.search = search;
|
|
58
52
|
if (sort) query.sort = sort;
|
|
@@ -61,7 +55,11 @@ export class AdminResourcesResolver {
|
|
|
61
55
|
}
|
|
62
56
|
|
|
63
57
|
@Query(() => GraphQLJSON, { name: "adminResourceRecord" })
|
|
64
|
-
adminResourceRecord(@Args(
|
|
65
|
-
return this.adminResources.get(name, id, ctx.req.user);
|
|
58
|
+
adminResourceRecord(@Args() args: AdminResourceRecordArgs, @Context() ctx: GqlAuthContext) {
|
|
59
|
+
return this.adminResources.get(args.name, args.id, ctx.req.user);
|
|
66
60
|
}
|
|
67
61
|
}
|
|
62
|
+
|
|
63
|
+
ensureGqlParamTypes(AdminResourcesResolver.prototype, "adminRegisteredResourceNames", [Object]);
|
|
64
|
+
ensureGqlParamTypes(AdminResourcesResolver.prototype, "adminResourceList", [AdminResourceListArgs, Object]);
|
|
65
|
+
ensureGqlParamTypes(AdminResourcesResolver.prototype, "adminResourceRecord", [AdminResourceRecordArgs, Object]);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { ensureGqlParamTypes } from "./ensure-gql-paramtypes";
|
|
4
|
+
|
|
5
|
+
describe("ensureGqlParamTypes", () => {
|
|
6
|
+
it("defines design:paramtypes when missing", () => {
|
|
7
|
+
class Sample {
|
|
8
|
+
handler(_a: string, _b: number) {}
|
|
9
|
+
}
|
|
10
|
+
ensureGqlParamTypes(Sample.prototype, "handler", [String, Number]);
|
|
11
|
+
expect(Reflect.getMetadata("design:paramtypes", Sample.prototype, "handler")).toEqual([
|
|
12
|
+
String,
|
|
13
|
+
Number
|
|
14
|
+
]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("does not overwrite existing metadata", () => {
|
|
18
|
+
class Sample {
|
|
19
|
+
handler() {}
|
|
20
|
+
}
|
|
21
|
+
Reflect.defineMetadata("design:paramtypes", [Date], Sample.prototype, "handler");
|
|
22
|
+
ensureGqlParamTypes(Sample.prototype, "handler", [String]);
|
|
23
|
+
expect(Reflect.getMetadata("design:paramtypes", Sample.prototype, "handler")).toEqual([Date]);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NestJS GraphQL reads `design:paramtypes` before applying explicit `@Args({ type })`.
|
|
3
|
+
* Toolchains that do not emit decorator metadata (e.g. `tsx watch`) leave it undefined and
|
|
4
|
+
* `@nestjs/graphql` crashes in `extractTypeIfArray`. Define metadata at runtime when missing.
|
|
5
|
+
*/
|
|
6
|
+
export function ensureGqlParamTypes(
|
|
7
|
+
prototype: object,
|
|
8
|
+
methodName: string,
|
|
9
|
+
paramTypes: unknown[]
|
|
10
|
+
): void {
|
|
11
|
+
const key = "design:paramtypes";
|
|
12
|
+
const existing = Reflect.getMetadata(key, prototype, methodName) as unknown[] | undefined;
|
|
13
|
+
if (Array.isArray(existing) && existing.length >= paramTypes.length) return;
|
|
14
|
+
Reflect.defineMetadata(key, paramTypes, prototype, methodName);
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { AdminResourceListArgs, AdminResourceRecordArgs } from "./admin-resources.args";
|
|
4
|
+
import { AdminResourcesResolver } from "./admin-resources.resolver";
|
|
5
|
+
|
|
6
|
+
describe("GraphQL schema (admin resources)", () => {
|
|
7
|
+
it("exposes design:paramtypes for Nest GraphQL when the compiler does not emit it", () => {
|
|
8
|
+
expect(
|
|
9
|
+
Reflect.getMetadata("design:paramtypes", AdminResourcesResolver.prototype, "adminResourceList")
|
|
10
|
+
).toEqual([AdminResourceListArgs, Object]);
|
|
11
|
+
expect(
|
|
12
|
+
Reflect.getMetadata("design:paramtypes", AdminResourcesResolver.prototype, "adminResourceRecord")
|
|
13
|
+
).toEqual([AdminResourceRecordArgs, Object]);
|
|
14
|
+
expect(
|
|
15
|
+
Reflect.getMetadata("design:paramtypes", AdminResourcesResolver.prototype, "adminRegisteredResourceNames")
|
|
16
|
+
).toEqual([Object]);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
-
import { isAbsolute, join, resolve } from "node:path";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
|
|
5
|
+
const pluginsDir = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const apiPkgDir = resolve(pluginsDir, "..", "..");
|
|
7
|
+
|
|
5
8
|
/** Same resolution order as plugin-loader (env → cwd → beside apps/api). */
|
|
6
9
|
export function getManifestCandidateStrings(): string[] {
|
|
7
10
|
const fromEnv = process.env.OPENADMIN_PLUGINS_MANIFEST;
|
|
8
11
|
const fromCwd = join(process.cwd(), "plugins.manifest.json");
|
|
9
|
-
const apiPkgDir = fileURLToPath(new URL("../..", import.meta.url));
|
|
10
12
|
const besideApi = join(apiPkgDir, "plugins.manifest.json");
|
|
11
13
|
return [...new Set([fromEnv, fromCwd, besideApi].filter(Boolean) as string[])];
|
|
12
14
|
}
|
|
@@ -26,7 +28,6 @@ export function resolveExistingManifestPath(): string | undefined {
|
|
|
26
28
|
|
|
27
29
|
/** Path to write when no file exists yet (beside apps/api). */
|
|
28
30
|
export function defaultWritableManifestPath(): string {
|
|
29
|
-
const apiPkgDir = fileURLToPath(new URL("../..", import.meta.url));
|
|
30
31
|
return join(apiPkgDir, "plugins.manifest.json");
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const nodeExternals = require("webpack-node-externals");
|
|
2
|
+
|
|
3
|
+
/** @param {import("webpack").Configuration} options */
|
|
4
|
+
module.exports = function configureWebpack(options) {
|
|
5
|
+
return {
|
|
6
|
+
...options,
|
|
7
|
+
externals: [
|
|
8
|
+
nodeExternals({
|
|
9
|
+
allowlist: [/^@openadminjs\//],
|
|
10
|
+
}),
|
|
11
|
+
],
|
|
12
|
+
};
|
|
13
|
+
};
|