@nest-my-admin/core 0.8.1 → 0.9.0

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 (39) hide show
  1. package/README.md +23 -2
  2. package/dist/api/action.service.js +24 -6
  3. package/dist/api/admin-api.service.js +38 -16
  4. package/dist/audit/audit-reader.js +3 -2
  5. package/dist/auth/auth.service.js +9 -1
  6. package/dist/crud/computed-columns.js +9 -0
  7. package/dist/dashboard/dashboard.service.js +2 -1
  8. package/dist/dashboard/widget-data.d.ts +8 -0
  9. package/dist/dashboard/widget-data.js +55 -0
  10. package/dist/decorators/admin-action.d.ts +29 -5
  11. package/dist/decorators/admin-action.js +50 -14
  12. package/dist/decorators/admin-print.d.ts +4 -1
  13. package/dist/decorators/admin-print.js +0 -6
  14. package/dist/decorators/admin-resource.d.ts +6 -2
  15. package/dist/decorators/admin-scope.d.ts +5 -2
  16. package/dist/decorators/admin-scope.js +0 -6
  17. package/dist/decorators/admin-settings-page.d.ts +2 -1
  18. package/dist/decorators/admin-widget.d.ts +2 -1
  19. package/dist/decorators/hooks.d.ts +11 -6
  20. package/dist/decorators/hooks.js +3 -3
  21. package/dist/decorators/typed.d.ts +20 -0
  22. package/dist/decorators/typed.js +2 -0
  23. package/dist/files/file-owner.js +2 -1
  24. package/dist/host-contract.d.ts +30 -0
  25. package/dist/host-contract.js +91 -0
  26. package/dist/http/error-response.js +4 -0
  27. package/dist/i18n/texts.js +9 -3
  28. package/dist/index.d.ts +2 -1
  29. package/dist/inlines/inline-reader.js +3 -2
  30. package/dist/inlines/inline-writer.js +3 -2
  31. package/dist/options.js +23 -1
  32. package/dist/pages/page-registry.js +3 -2
  33. package/dist/policy/admin-policy.service.js +30 -11
  34. package/dist/policy/explain.js +3 -2
  35. package/dist/rbac/rbac-routes.js +2 -1
  36. package/dist/registry/resource-registry.js +1 -1
  37. package/dist/resource/admin-resource-base.js +16 -5
  38. package/dist/resource/admin-source-base.d.ts +2 -0
  39. package/package.json +2 -2
@@ -4,6 +4,7 @@ import { runInAdminContext } from '../resource/admin-context.js';
4
4
  import { EffectivePermissions } from './effective.js';
5
5
  import { RESOURCE_OPERATIONS, actionCode, codeMatches, pageCode } from './roles.js';
6
6
  import { bootText } from '../i18n/texts.js';
7
+ import { checkFoundRecord } from '../host-contract.js';
7
8
  const SCOPED = ['view', 'update', 'delete'];
8
9
  /**
9
10
  * Why a user may or may not do things with a resource (spec §6.7). Every answer comes from the code the API
@@ -105,7 +106,7 @@ async function explainRecord(policy, entry, raw, ctx) {
105
106
  }
106
107
  const keys = entry.schema.primaryKeys;
107
108
  // A source's row exists when its own findOne finds it.
108
- const stored = entry.dataSource ? await entry.dataSource.manager.getRepository(entry.metadata.target).createQueryBuilder('nma_explain').whereInIds([id]).getOne() : await entry.resource.findOne(id, ctx);
109
+ const stored = entry.dataSource ? await entry.dataSource.manager.getRepository(entry.metadata.target).createQueryBuilder('nma_explain').whereInIds([id]).getOne() : checkFoundRecord(await entry.resource.findOne(id, ctx), `${entry.resource.constructor.name}.findOne()`);
109
110
  const exists = stored !== null;
110
111
  const key = recordIdOf(typeof id === 'object' ? id : { [keys[0]]: id }, keys);
111
112
  const inScope = async (operation) => (await policy.allowedIds(entry, [id], operation, ctx)).has(key);
@@ -116,7 +117,7 @@ async function explainRecord(policy, entry, raw, ctx) {
116
117
  else if (!perms.canOn(r, 'view'))
117
118
  reasons.view = 'permission';
118
119
  else {
119
- found = await entry.resource.findOne(id, ctx);
120
+ found = checkFoundRecord(await entry.resource.findOne(id, ctx), `${entry.resource.constructor.name}.findOne()`);
120
121
  view = found !== null && (await inScope('view'));
121
122
  if (!view)
122
123
  reasons.view = 'scope';
@@ -3,6 +3,7 @@ import { AdminForbiddenError, AdminNotFoundError, AdminValidationError } from '.
3
3
  import { readJsonBody, sendJson } from '../http/http-io.js';
4
4
  import { RESOURCE_OPERATIONS } from '../policy/roles.js';
5
5
  import { diffPlain } from '../audit/diff.js';
6
+ import { checkFoundRecord } from '../host-contract.js';
6
7
  const noContent = (res) => {
7
8
  res.statusCode = 204;
8
9
  res.end();
@@ -55,7 +56,7 @@ export function addRbacRoutes(router, services) {
55
56
  });
56
57
  const findUser = async (id) => {
57
58
  const adapter = auth.adapter;
58
- const user = adapter?.getUser ? await adapter.getUser(id) : (await rbac.knownUserIds()).includes(id) ? { id, displayName: id, isSuperuser: false } : null;
59
+ const user = adapter?.getUser ? checkFoundRecord(await adapter.getUser(id), "the auth adapter's getUser()") : (await rbac.knownUserIds()).includes(id) ? { id, displayName: id, isSuperuser: false } : null;
59
60
  if (!user)
60
61
  throw new AdminNotFoundError(`User "${id}" not found`);
61
62
  return user;
@@ -319,7 +319,7 @@ let ResourceRegistry = class ResourceRegistry {
319
319
  });
320
320
  const actions = resolveActions(resource.constructor, BASE_MEMBERS, (message) => {
321
321
  throw new Error(`${className}: ${message}`);
322
- });
322
+ }, definition.entity);
323
323
  // Action inputs picked like one of the resource's relation fields (`@AdminField({ pickFrom })`).
324
324
  for (const action of actions.values()) {
325
325
  if (!action.form)
@@ -5,6 +5,8 @@ import { toRelationReferences } from '../crud/relation-writes.js';
5
5
  import { toFileKeys } from '../files/admin-file.js';
6
6
  import { AdminNotFoundError } from '../errors.js';
7
7
  import { getHooks } from '../decorators/hooks.js';
8
+ import { checkQueryBuilder, describeValue, HostContractError, isThenable } from '../host-contract.js';
9
+ const HOOK_NAMES = { beforeSave: 'BeforeSave', afterSave: 'AfterSave', beforeDelete: 'BeforeDelete' };
8
10
  import { AdminContext } from './admin-context.js';
9
11
  /**
10
12
  * Base class for admin resources. Override `create`/`update` (and the finders) to route
@@ -52,7 +54,7 @@ export class AdminResourceBase {
52
54
  }
53
55
  /** `query()` and then the signed-in user's row scopes: what every read of this resource starts from. */
54
56
  restrict(qb, ctx) {
55
- const restricted = this.query(qb, ctx);
57
+ const restricted = checkQueryBuilder(this.query(qb, ctx), `${this.constructor.name}.query()`);
56
58
  return this.#scoper ? this.#scoper(restricted, ctx) : restricted;
57
59
  }
58
60
  #computedSort;
@@ -93,8 +95,13 @@ export class AdminResourceBase {
93
95
  const qb = this.restrict(repository.createQueryBuilder(alias), context);
94
96
  const sorter = this.#computedSort;
95
97
  // The tab's own restriction (a tab with `where`); its filters arrive as normal filter params.
96
- if (params.tab)
97
- this.list?.tabs?.find((tab) => tab.key === params.tab)?.where?.(qb, context);
98
+ if (params.tab) {
99
+ const tab = this.list?.tabs?.find((candidate) => candidate.key === params.tab);
100
+ const added = tab?.where?.(qb, context);
101
+ if (added !== undefined && (isThenable(added) || added !== qb)) {
102
+ throw new HostContractError(`${this.constructor.name} list tab "${tab.key}": where(qb, ctx) adds its conditions to qb at once and returns nothing, but it returned ${describeValue(added)}`);
103
+ }
104
+ }
98
105
  return applyListParams(qb, params, repository.metadata, sorter && ((query, field) => sorter(query, field, context)));
99
106
  }
100
107
  /**
@@ -225,8 +232,12 @@ export class AdminResourceBase {
225
232
  /** Runs @BeforeSave/@AfterSave/@BeforeDelete methods in declaration order (parent class first). */
226
233
  async runHooks(kind, ...args) {
227
234
  const methods = this;
228
- for (const key of getHooks(this.constructor, kind))
229
- await methods[key].apply(this, args);
235
+ for (const key of getHooks(this.constructor, kind)) {
236
+ const returned = await methods[key].apply(this, args);
237
+ if (returned !== undefined) {
238
+ throw new HostContractError(`${this.constructor.name}.${String(key)}: a @${HOOK_NAMES[kind]} hook returns nothing, but it returned ${describeValue(returned)}`);
239
+ }
240
+ }
230
241
  }
231
242
  }
232
243
  //# sourceMappingURL=admin-resource-base.js.map
@@ -11,6 +11,8 @@ import { AdminResourceBase, type FindManyResult, type ListParams, type RecordId
11
11
  * of AdminResourceBase (`query`, `buildListQuery`, `repository`, `restore`, `purge`) are not used.
12
12
  */
13
13
  export declare abstract class AdminSourceBase<T extends ObjectLiteral = ObjectLiteral, Id extends RecordId = RecordId> extends AdminResourceBase<T, Id> {
14
+ /** Types only: tells a source class from a table resource, so `@AdminSource` refuses a class that is not one. */
15
+ private readonly sourceResource;
14
16
  abstract findMany(params: ListParams, ctx: AdminContext): Promise<FindManyResult<T>>;
15
17
  abstract findOne(id: Id, ctx: AdminContext, options?: {
16
18
  withDeleted?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nest-my-admin/core",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "Django-admin-class admin panel for NestJS + TypeORM",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -35,7 +35,7 @@
35
35
  "typecheck": "tsc -p tsconfig.json --noEmit"
36
36
  },
37
37
  "dependencies": {
38
- "@nest-my-admin/ui": "0.8.1"
38
+ "@nest-my-admin/ui": "0.9.0"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "@nestjs/common": "^11.0.0 || ^12.0.0",