openadminjs 0.1.19 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openadminjs",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "openadminjs": "dist/index.js"
@@ -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/apps/api/src/main.js",
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(new URL("../..", import.meta.url));
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);
@@ -28,6 +28,6 @@ import { SsoOidcService } from "./sso-oidc.service";
28
28
  ],
29
29
  controllers: [AuthController, SsoOidcController],
30
30
  providers: [AuthService, SsoOidcService, PrismaService, AuthGuard],
31
- exports: [AuthService, AuthGuard, JwtModule]
31
+ exports: [AuthService, AuthGuard, JwtModule, PrismaService]
32
32
  })
33
33
  export class AuthModule {}
@@ -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,18 +10,20 @@ describe("AdminResourcesResolver", () => {
10
10
  const user = { id: "u1", permissions: ["*"] };
11
11
 
12
12
  await resolver.adminResourceList(
13
- "posts",
14
- user as never,
15
- 2,
16
- 15,
17
- "hello",
18
- "createdAt:desc",
19
- "en",
20
13
  {
21
- status: { eq: "published" },
22
- views: { gte: 10, lte: 100 },
23
- authorId: "u1"
24
- }
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
+ }
25
+ },
26
+ { req: { user } } as never
25
27
  );
26
28
 
27
29
  expect(adminResources.list).toHaveBeenCalledWith(
@@ -41,4 +43,3 @@ describe("AdminResourcesResolver", () => {
41
43
  );
42
44
  });
43
45
  });
44
-
@@ -1,12 +1,15 @@
1
1
  import { UseGuards } from "@nestjs/common";
2
- import { Args, Int, Query, Resolver } from "@nestjs/graphql";
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
- import { CurrentUser } from "../common/current-user.decorator";
9
8
  import { resources } from "../resources/registry";
9
+ import { AdminResourceListArgs, AdminResourceRecordArgs } from "./admin-resources.args";
10
+ import { ensureGqlParamTypes } from "./ensure-gql-paramtypes";
11
+
12
+ type GqlAuthContext = { req: { user: RequestUser } };
10
13
 
11
14
  @Resolver()
12
15
  @UseGuards(AuthGuard)
@@ -30,7 +33,8 @@ export class AdminResourcesResolver {
30
33
  }
31
34
 
32
35
  @Query(() => [String], { name: "adminRegisteredResourceNames" })
33
- adminRegisteredResourceNames(@CurrentUser() user: RequestUser): string[] {
36
+ adminRegisteredResourceNames(@Context() ctx: GqlAuthContext): string[] {
37
+ const user = ctx.req.user;
34
38
  return resources
35
39
  .filter(
36
40
  (r) =>
@@ -41,25 +45,21 @@ export class AdminResourcesResolver {
41
45
  }
42
46
 
43
47
  @Query(() => GraphQLJSON, { name: "adminResourceList", description: "Paginated list (same rules as REST /admin/resources/:name)" })
44
- adminResourceList(
45
- @Args("name") name: string,
46
- @CurrentUser() user: RequestUser,
47
- @Args("page", { type: () => Int, nullable: true, defaultValue: 1 }) page: number,
48
- @Args("limit", { type: () => Int, nullable: true, defaultValue: 20 }) limit: number,
49
- @Args("search", { nullable: true }) search?: string,
50
- @Args("sort", { nullable: true }) sort?: string,
51
- @Args("locale", { nullable: true }) locale?: string,
52
- @Args("filter", { type: () => GraphQLJSON, nullable: true }) filter?: Record<string, unknown>
53
- ) {
48
+ adminResourceList(@Args() args: AdminResourceListArgs, @Context() ctx: GqlAuthContext) {
49
+ const { name, page = 1, limit = 20, search, sort, locale, filter } = args;
54
50
  const query: Record<string, unknown> = { page, limit, ...this.flattenFilters(filter) };
55
51
  if (search) query.search = search;
56
52
  if (sort) query.sort = sort;
57
53
  if (locale) query.locale = locale;
58
- return this.adminResources.list(name, query, user);
54
+ return this.adminResources.list(name, query, ctx.req.user);
59
55
  }
60
56
 
61
57
  @Query(() => GraphQLJSON, { name: "adminResourceRecord" })
62
- adminResourceRecord(@Args("name") name: string, @Args("id") id: string, @CurrentUser() user: RequestUser) {
63
- return this.adminResources.get(name, id, user);
58
+ adminResourceRecord(@Args() args: AdminResourceRecordArgs, @Context() ctx: GqlAuthContext) {
59
+ return this.adminResources.get(args.name, args.id, ctx.req.user);
64
60
  }
65
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
+ });
@@ -4,7 +4,6 @@ import { ApolloDriver, ApolloDriverConfig } from "@nestjs/apollo";
4
4
  import { GraphQLJSON } from "graphql-scalars";
5
5
  import { AdminModule } from "../admin/admin.module";
6
6
  import { AuthModule } from "../auth/auth.module";
7
- import { AuthGuard } from "../common/auth.guard";
8
7
  import { AdminResourcesResolver } from "./admin-resources.resolver";
9
8
 
10
9
  @Module({
@@ -21,6 +20,6 @@ import { AdminResourcesResolver } from "./admin-resources.resolver";
21
20
  AdminModule,
22
21
  AuthModule
23
22
  ],
24
- providers: [AdminResourcesResolver, AuthGuard]
23
+ providers: [AdminResourcesResolver]
25
24
  })
26
25
  export class GraphqlApiModule {}
@@ -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,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
4
+ }
@@ -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
+ };