Skip to content

Commit 3d5c4b3

Browse files
committed
refactor(react): type AdapterCtx by DataSource; home isApiAccessDeniedError in core (WIP)
Claude-Session: https://claude.ai/code/session_017sGTFTS9edirCTVBgQysK7 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4f7e041 commit 3d5c4b3

5 files changed

Lines changed: 88 additions & 54 deletions

File tree

‎ui/packages/core/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export * from './utils/non-grid-row-ceiling.js';
4040
// the floor itself never grows past the four.
4141
export * from './utils/emptiness.js';
4242
export * from './utils/expand-fields.js';
43+
// The `enable`-block API denial predicate, read by `@object-ui/react` and
44+
// re-exported by `@object-ui/data-objectstack` (objectstack#25917).
45+
export * from './utils/api-access-denied.js';
4346
// The RETIREMENT gate (objectui#4914, maintainer ruling B). Homed here rather
4447
// than in `@object-ui/fields` because `@object-ui/components` is one of its six
4548
// consumers and `fields` depends on `components` — see the module's docblock
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* ObjectUI — the `enable`-block API denial predicate
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
import { errorCodeIsAnyOf } from '@object-ui/types';
10+
11+
// Homed in core rather than in `@object-ui/data-objectstack`: the predicate is
12+
// a pure read of the ADR-0112 `code`, and `@object-ui/react` asks it
13+
// (`classifyLoadError`) without depending on any one adapter. The adapter
14+
// re-exports both names unchanged.
15+
16+
/**
17+
* The two denials the server derives from an object's `enable` block —
18+
* `apiAccessDenialFromEnable` (objectstack `packages/rest/src/rest-server.ts`).
19+
*
20+
* - `OBJECT_API_DISABLED` (404) — `enable.apiEnabled: false`; the object is
21+
* not exposed over the data API at all.
22+
* - `OBJECT_API_METHOD_NOT_ALLOWED` (405) — the operation is absent from the
23+
* `enable.apiMethods` whitelist.
24+
*
25+
* Both are **pure functions of the object's metadata**: no user, no permission,
26+
* no context, no request body. So neither is transient and neither is
27+
* per-user — when one happens it is a permanent property of that object, and
28+
* every retry of every persona gets the identical answer.
29+
*
30+
* That is exactly why they must not be degraded into "no data". A 404 from a
31+
* missing collection means *this backend doesn't have that table*, which the
32+
* optional-collection probes below legitimately read as "feature unavailable";
33+
* a 404 from `OBJECT_API_DISABLED` means *this page can never work*. Answering
34+
* the second with an empty result set renders "you have no records" over a
35+
* surface that is not allowed to have any (objectui#4408 — it also hid the
36+
* upstream defect objectstack#7544 for its entire life, because a merely
37+
* unpopulated page invites nobody to click through).
38+
*/
39+
export const API_ACCESS_DENIED_CODES = [
40+
'OBJECT_API_DISABLED',
41+
'OBJECT_API_METHOD_NOT_ALLOWED',
42+
] as const;
43+
44+
/**
45+
* True when `error` is an `enable`-block API denial (see
46+
* {@link API_ACCESS_DENIED_CODES}).
47+
*
48+
* Discriminates on the ADR-0112 `code`, never on the status: 404 alone cannot
49+
* separate a disabled object from a missing collection, and 405 alone cannot
50+
* separate a withheld method from any other method rejection. The code survives
51+
* the transport — `@objectstack/client`'s fetch wrapper stamps `error.code`
52+
* from the response envelope, and both spellings are declared members of the
53+
* spec's `StandardErrorCode` — so no heuristic on status is needed or wanted.
54+
*/
55+
export function isApiAccessDeniedError(error: unknown): boolean {
56+
return errorCodeIsAnyOf(error, API_ACCESS_DENIED_CODES);
57+
}

‎ui/packages/data-objectstack/src/index.ts‎

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
convertFiltersToAST,
8080
emulateBatchTransaction,
8181
normalizeSchemaReferenceKeys,
82+
isApiAccessDeniedError,
8283
type DatasetDrillRange,
8384
} from '@object-ui/core';
8485
import { MetadataCache } from './cache/MetadataCache';
@@ -1017,48 +1018,11 @@ export function is404Error(error: unknown): boolean {
10171018
return errorCodeIsAnyOf({ code }, ['OBJECT_NOT_FOUND', 'RECORD_NOT_FOUND']);
10181019
}
10191020

1020-
/**
1021-
* The two denials the server derives from an object's `enable` block —
1022-
* `apiAccessDenialFromEnable` (objectstack `packages/rest/src/rest-server.ts`).
1023-
*
1024-
* - `OBJECT_API_DISABLED` (404) — `enable.apiEnabled: false`; the object is
1025-
* not exposed over the data API at all.
1026-
* - `OBJECT_API_METHOD_NOT_ALLOWED` (405) — the operation is absent from the
1027-
* `enable.apiMethods` whitelist.
1028-
*
1029-
* Both are **pure functions of the object's metadata**: no user, no permission,
1030-
* no context, no request body. So neither is transient and neither is
1031-
* per-user — when one happens it is a permanent property of that object, and
1032-
* every retry of every persona gets the identical answer.
1033-
*
1034-
* That is exactly why they must not be degraded into "no data". A 404 from a
1035-
* missing collection means *this backend doesn't have that table*, which the
1036-
* optional-collection probes below legitimately read as "feature unavailable";
1037-
* a 404 from `OBJECT_API_DISABLED` means *this page can never work*. Answering
1038-
* the second with an empty result set renders "you have no records" over a
1039-
* surface that is not allowed to have any (objectui#4408 — it also hid the
1040-
* upstream defect objectstack#7544 for its entire life, because a merely
1041-
* unpopulated page invites nobody to click through).
1042-
*/
1043-
export const API_ACCESS_DENIED_CODES = [
1044-
'OBJECT_API_DISABLED',
1045-
'OBJECT_API_METHOD_NOT_ALLOWED',
1046-
] as const;
1047-
1048-
/**
1049-
* True when `error` is an `enable`-block API denial (see
1050-
* {@link API_ACCESS_DENIED_CODES}).
1051-
*
1052-
* Discriminates on the ADR-0112 `code`, never on the status: 404 alone cannot
1053-
* separate a disabled object from a missing collection, and 405 alone cannot
1054-
* separate a withheld method from any other method rejection. The code survives
1055-
* the transport — `@objectstack/client`'s fetch wrapper stamps `error.code`
1056-
* from the response envelope, and both spellings are declared members of the
1057-
* spec's `StandardErrorCode` — so no heuristic on status is needed or wanted.
1058-
*/
1059-
export function isApiAccessDeniedError(error: unknown): boolean {
1060-
return errorCodeIsAnyOf(error, API_ACCESS_DENIED_CODES);
1061-
}
1021+
// `API_ACCESS_DENIED_CODES` / `isApiAccessDeniedError` live in
1022+
// `@object-ui/core` (`utils/api-access-denied.ts`) so that `@object-ui/react`
1023+
// can classify the denial without depending on this adapter. Re-exported here
1024+
// unchanged: this package's published face keeps both names.
1025+
export { API_ACCESS_DENIED_CODES, isApiAccessDeniedError } from '@object-ui/core';
10621026

10631027
/**
10641028
* What the by-name meta app route said about THIS session's access to an app

‎ui/packages/react/src/context/AppShellContext.tsx‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { createContext, useContext, useEffect, useState } from 'react';
2-
import type { ObjectStackAdapter } from '@object-ui/data-objectstack';
2+
import type { DataSource } from '@object-ui/types';
33

44
// ---------------------------------------------------------------------------
55
// AdapterContext
66
// ---------------------------------------------------------------------------
77

8-
export const AdapterCtx = createContext<ObjectStackAdapter | null>(null);
8+
/**
9+
* The host's data adapter, typed by the protocol-level `DataSource` interface
10+
* from `@object-ui/types` — never by one concrete adapter (AGENTS #1: inject a
11+
* `DataSource`, don't hard-code a backend). The runtime therefore installs
12+
* without any adapter package, and a bring-your-own backend learns at the
13+
* type level which members it is missing, not at runtime.
14+
*
15+
* A host that provides a concrete adapter and reads members beyond
16+
* `DataSource` narrows in its own package, where it depends on that adapter.
17+
*/
18+
export const AdapterCtx = createContext<DataSource | null>(null);
919

10-
export function useAdapter(): ObjectStackAdapter | null {
20+
export function useAdapter(): DataSource | null {
1121
return useContext(AdapterCtx);
1222
}
1323

‎ui/packages/react/src/utils/error-message.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* real reason (e.g. a row-level-security denial) instead of swallowing it.
2222
*/
2323

24-
import { isApiAccessDeniedError } from '@object-ui/data-objectstack';
24+
import { isApiAccessDeniedError } from '@object-ui/core';
2525
import type { ApiError } from '@objectstack/spec/api';
2626

2727
/**
@@ -259,14 +259,14 @@ export function classifyLoadError(err: unknown): LoadErrorKind {
259259
}
260260
const code = typeof e?.code === 'string' ? e.code.toUpperCase() : '';
261261
// The object's `enable` block withholds this operation — checked FIRST, on
262-
// the CODE alone (delegated to the shared predicate so this file and
263-
// `@object-ui/data-objectstack` cannot drift on which codes count). Status
264-
// cannot carry this verdict: the denial is a 404, the same status a missing
265-
// collection and a missing record answer with, and its 405 sibling is the
266-
// same status any other method rejection uses. Unlike every kind below it
267-
// this one is not about the request, the session or the network — it is a
268-
// permanent property of the object, identical for every persona and every
269-
// retry (objectui#4408).
262+
// the CODE alone (delegated to the shared `@object-ui/core` predicate, which
263+
// `@object-ui/data-objectstack` re-exports, so the two cannot drift on which
264+
// codes count). Status cannot carry this verdict: the denial is a 404, the
265+
// same status a missing collection and a missing record answer with, and its
266+
// 405 sibling is the same status any other method rejection uses. Unlike
267+
// every kind below it this one is not about the request, the session or the
268+
// network — it is a permanent property of the object, identical for every
269+
// persona and every retry (objectui#4408).
270270
if (isApiAccessDeniedError(err)) return 'api-disabled';
271271
if (status === 403 || code === 'PERMISSION_DENIED' || code === 'FORBIDDEN') return 'forbidden';
272272
if (status === 401 || code === 'UNAUTHORIZED' || code === 'UNAUTHENTICATED') return 'unauthorized';

0 commit comments

Comments
 (0)