@nocobase/client-v2 2.2.0-beta.12 → 2.2.0-beta.14

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": "@nocobase/client-v2",
3
- "version": "2.2.0-beta.12",
3
+ "version": "2.2.0-beta.14",
4
4
  "license": "Apache-2.0",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.mjs",
@@ -27,11 +27,11 @@
27
27
  "@formily/antd-v5": "1.2.3",
28
28
  "@formily/react": "^2.2.27",
29
29
  "@formily/shared": "^2.2.27",
30
- "@nocobase/evaluators": "2.2.0-beta.12",
31
- "@nocobase/flow-engine": "2.2.0-beta.12",
32
- "@nocobase/sdk": "2.2.0-beta.12",
33
- "@nocobase/shared": "2.2.0-beta.12",
34
- "@nocobase/utils": "2.2.0-beta.12",
30
+ "@nocobase/evaluators": "2.2.0-beta.14",
31
+ "@nocobase/flow-engine": "2.2.0-beta.14",
32
+ "@nocobase/sdk": "2.2.0-beta.14",
33
+ "@nocobase/shared": "2.2.0-beta.14",
34
+ "@nocobase/utils": "2.2.0-beta.14",
35
35
  "ahooks": "^3.7.2",
36
36
  "antd": "5.24.2",
37
37
  "antd-style": "3.7.1",
@@ -47,5 +47,5 @@
47
47
  "react-i18next": "^11.15.1",
48
48
  "react-router-dom": "^6.30.1"
49
49
  },
50
- "gitHead": "ad8ed32d47eaec2dc56f15999934f720787d5070"
50
+ "gitHead": "1ef021f3ce0a84469e40f181a19551a50b6f850c"
51
51
  }
package/src/APIClient.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
- import { APIClient as APIClientSDK, type APIClientOptions, hasHeaderValue } from '@nocobase/sdk';
10
+ import { APIClient as APIClientSDK, hasHeaderValue } from '@nocobase/sdk';
11
11
 
12
12
  function offsetToTimeZone(offset: number) {
13
13
  const hours = Math.floor(Math.abs(offset));
@@ -23,15 +23,6 @@ function getCurrentTimezone() {
23
23
  }
24
24
 
25
25
  export class APIClient extends APIClientSDK {
26
- appName?: string;
27
-
28
- constructor(options?: APIClientOptions) {
29
- super(options);
30
- if (options && typeof options !== 'function') {
31
- this.appName = options.appName;
32
- }
33
- }
34
-
35
26
  getHostname() {
36
27
  if (process.env.API_BASE_URL) {
37
28
  try {
@@ -46,6 +46,10 @@ export class Application extends BaseApplication<
46
46
 
47
47
  protected createApiClient(options: ApplicationOptions) {
48
48
  return new APIClient({
49
+ // Cross-origin API deployments rely on cookies for auth (e.g. permanent file
50
+ // URLs); trust is enforced server-side via the CORS origin whitelist and the
51
+ // CSRF middleware, not by omitting credentials.
52
+ withCredentials: true,
49
53
  ...options.apiClient,
50
54
  appName: options.name || getSubAppName(options.publicPath),
51
55
  });
@@ -403,7 +403,7 @@ export abstract class BaseApplication<
403
403
  this.favicon = favicon || '';
404
404
  }
405
405
 
406
- const iconHref = this.favicon || '/favicon/favicon.ico';
406
+ const iconHref = this.favicon || this.getCdnUrl() + 'favicon/favicon.ico';
407
407
 
408
408
  if (faviconLinkElement) {
409
409
  faviconLinkElement.href = iconHref;
@@ -52,6 +52,24 @@ describe('app', () => {
52
52
  expect(app.getHref('/test')).toBe('/test');
53
53
  });
54
54
 
55
+ it('should enable credentials for api clients by default', () => {
56
+ const sameOriginApp = new Application({
57
+ router,
58
+ apiClient: {
59
+ baseURL: '/api/',
60
+ },
61
+ });
62
+ const crossOriginApp = new Application({
63
+ router,
64
+ apiClient: {
65
+ baseURL: 'https://api.example.com/api/',
66
+ },
67
+ });
68
+
69
+ expect(sameOriginApp.apiClient.axios.defaults.withCredentials).toBe(true);
70
+ expect(crossOriginApp.apiClient.axios.defaults.withCredentials).toBe(true);
71
+ });
72
+
55
73
  it('should initialize shared jsonLogic operators', () => {
56
74
  const app = new Application({ router });
57
75
 
@@ -55,6 +55,7 @@ describe('nocobase buildin plugin auth redirect', () => {
55
55
  const originalLocation = globalThis.window.location;
56
56
 
57
57
  beforeEach(() => {
58
+ globalThis.window.localStorage.clear();
58
59
  // These fixtures mount the modern client under the `v2` segment; tell the
59
60
  // runtime-prefix helper so v2-runtime detection matches (server injects it
60
61
  // in production).
@@ -75,6 +76,7 @@ describe('nocobase buildin plugin auth redirect', () => {
75
76
  });
76
77
 
77
78
  afterEach(() => {
79
+ globalThis.window.localStorage.clear();
78
80
  delete (globalThis.window as any).__nocobase_modern_client_prefix__;
79
81
  Object.defineProperty(globalThis.window, 'location', {
80
82
  configurable: true,
@@ -116,6 +118,7 @@ describe('nocobase buildin plugin auth redirect', () => {
116
118
  plugins: [NocoBaseBuildInPlugin as any],
117
119
  router: { type: 'memory', initialEntries: ['/v2/admin'] },
118
120
  });
121
+ app.apiClient.auth.setToken('test-token');
119
122
  app.apiMock.onGet('app:getLang').reply(200, {
120
123
  data: { lang: 'en-US', resources: { client: {} }, cron: {} },
121
124
  });
@@ -130,6 +133,7 @@ describe('nocobase buildin plugin auth redirect', () => {
130
133
  await new Promise((resolve) => setTimeout(resolve, 50));
131
134
  expect(app.router.router.state.location.pathname).toBe('/v2/admin');
132
135
  expect(app.router.router.state.location.search).toBe('');
136
+ expect(app.apiMock.history.post.filter((request) => request.url === 'auth:syncCookies')).toHaveLength(0);
133
137
  });
134
138
 
135
139
  it('should redirect unauthenticated v2 root access to v2 signin via <Navigate />', async () => {
@@ -326,6 +330,29 @@ describe('nocobase buildin plugin auth redirect', () => {
326
330
  data: { lang: 'en-US', resources: { client: {} }, cron: {} },
327
331
  });
328
332
  app.apiMock.onGet('/auth:check').reply(200, { data: { id: 1 } });
333
+ app.apiMock.onPost('auth:syncCookies').reply(200, { data: { synced: true } });
334
+ const ensureLoaded = vi.spyOn(app.dataSourceManager, 'ensureLoaded').mockResolvedValue(undefined);
335
+
336
+ const Root = app.getRootComponent();
337
+ render(<Root />);
338
+
339
+ expect(await screen.findByText('secure page')).toBeInTheDocument();
340
+ expect(ensureLoaded).toHaveBeenCalledTimes(1);
341
+ expect(app.apiMock.history.post.filter((request) => request.url === 'auth:syncCookies')).toHaveLength(1);
342
+ });
343
+
344
+ it('should keep authenticated v2 startup working when cookie bootstrap fails', async () => {
345
+ const app = createMockClient({
346
+ publicPath: '/v2/',
347
+ plugins: [NocoBaseBuildInPlugin as any, AuthBootstrapRoutePlugin as any],
348
+ router: { type: 'memory', initialEntries: ['/v2/secure'] },
349
+ });
350
+ app.apiClient.auth.setToken('test-token');
351
+ app.apiMock.onGet('app:getLang').reply(200, {
352
+ data: { lang: 'en-US', resources: { client: {} }, cron: {} },
353
+ });
354
+ app.apiMock.onGet('/auth:check').reply(200, { data: { id: 1 } });
355
+ app.apiMock.onPost('auth:syncCookies').reply(500, { errors: [{ message: 'cookie sync failed' }] });
329
356
  const ensureLoaded = vi.spyOn(app.dataSourceManager, 'ensureLoaded').mockResolvedValue(undefined);
330
357
 
331
358
  const Root = app.getRootComponent();
@@ -333,6 +360,7 @@ describe('nocobase buildin plugin auth redirect', () => {
333
360
 
334
361
  expect(await screen.findByText('secure page')).toBeInTheDocument();
335
362
  expect(ensureLoaded).toHaveBeenCalledTimes(1);
363
+ expect(app.apiMock.history.post.filter((request) => request.url === 'auth:syncCookies')).toHaveLength(1);
336
364
  });
337
365
 
338
366
  it('should not auth-check or bootstrap authCheck false routes', async () => {
@@ -132,7 +132,7 @@ fieldValidationConfigureRegistry.registerMany([
132
132
  { key: 'uri', label: 'URI', hasValue: false, params: [], paramsType: 'object' },
133
133
  {
134
134
  key: 'multiple',
135
- label: 'Multiple',
135
+ label: 'Multiple of',
136
136
  hasValue: true,
137
137
  params: [{ key: 'base', label: 'Base', componentType: 'inputNumber', required: true }],
138
138
  },
@@ -49,6 +49,21 @@ function CodeScannerContent({ visible, formatsToSupport, onClose, onScanSuccess
49
49
  message.error(t('Code recognition failed, please scan again'));
50
50
  }, [t]);
51
51
 
52
+ const showCameraStartFailure = useCallback(
53
+ (error: unknown) => {
54
+ const errorName = error instanceof Error ? error.name : '';
55
+ const errorMessage = error instanceof Error ? error.message : '';
56
+ const errorMap: Record<string, string> = {
57
+ NotFoundError: t('No camera device detected'),
58
+ NotAllowedError: t('You have not granted permission to use the camera'),
59
+ };
60
+
61
+ message.error(errorMap[errorName] || errorMessage || t('You have not granted permission to use the camera'));
62
+ onClose();
63
+ },
64
+ [onClose, t],
65
+ );
66
+
52
67
  const handleScanSuccess = useCallback(
53
68
  (text: string) => {
54
69
  onScanSuccess(text);
@@ -64,6 +79,7 @@ function CodeScannerContent({ visible, formatsToSupport, onClose, onScanSuccess
64
79
  scanBoxSize,
65
80
  onScanSuccess: handleScanSuccess,
66
81
  onScanFailure: showScanFailure,
82
+ onCameraStartFailure: showCameraStartFailure,
67
83
  });
68
84
 
69
85
  useEffect(() => {
@@ -129,7 +145,7 @@ function CodeScannerContent({ visible, formatsToSupport, onClose, onScanSuccess
129
145
  inset: 0;
130
146
  z-index: ${token.zIndexPopupBase + 1000};
131
147
  overflow: hidden;
132
- background: ${token.colorBgMask};
148
+ background: #000;
133
149
  `;
134
150
  const scannerWrapperClass = css`
135
151
  position: absolute;
@@ -137,16 +153,17 @@ function CodeScannerContent({ visible, formatsToSupport, onClose, onScanSuccess
137
153
  overflow: hidden;
138
154
  `;
139
155
  const scannerClass = css`
156
+ position: relative;
140
157
  width: 100%;
141
158
  height: 100%;
142
- display: flex;
143
- justify-content: center;
144
- align-items: center;
159
+ overflow: hidden;
145
160
 
146
161
  video {
147
- width: auto !important;
162
+ position: absolute !important;
163
+ inset: 0 !important;
164
+ width: 100% !important;
148
165
  height: 100% !important;
149
- max-width: none !important;
166
+ object-fit: cover !important;
150
167
  }
151
168
 
152
169
  #qr-shaded-region {
@@ -74,13 +74,23 @@ vi.mock('html5-qrcode', () => ({
74
74
 
75
75
  vi.mock('jsqr', () => jsQrMocks);
76
76
 
77
- function ScannerHost({ scanBoxSize }: { scanBoxSize?: { width: number; height: number } } = {}) {
77
+ function ScannerHost({
78
+ onCameraStartFailure,
79
+ onScanFailure,
80
+ scanBoxSize,
81
+ }: {
82
+ onCameraStartFailure?: (error: unknown) => void;
83
+ onScanFailure?: () => void;
84
+ scanBoxSize?: { width: number; height: number };
85
+ } = {}) {
78
86
  const handleScanSuccess = useCallback(() => undefined, []);
79
87
 
80
88
  useCodeScanner({
81
89
  elementId: 'scanner',
82
90
  enabled: true,
83
91
  scanBoxSize,
92
+ onCameraStartFailure,
93
+ onScanFailure,
84
94
  onScanSuccess: handleScanSuccess,
85
95
  });
86
96
 
@@ -194,6 +204,30 @@ describe('useCodeScanner', () => {
194
204
  expect(config?.qrbox?.(1200, 844)).toEqual({ width: 319, height: 240 });
195
205
  });
196
206
 
207
+ it('clamps the scan box to the camera viewfinder size', async () => {
208
+ render(<ScannerHost scanBoxSize={{ width: 319, height: 240 }} />);
209
+
210
+ await waitFor(() => expect(mocks.start).toHaveBeenCalled());
211
+
212
+ const config = mocks.start.mock.calls[0]?.[1] as
213
+ | { qrbox?: (width: number, height: number) => { width: number; height: number } }
214
+ | undefined;
215
+
216
+ expect(config?.qrbox?.(200, 160)).toEqual({ width: 200, height: 160 });
217
+ });
218
+
219
+ it('reports camera start failures through the dedicated handler', async () => {
220
+ const cameraStartError = Object.assign(new Error('Camera failed'), { name: 'NotAllowedError' });
221
+ const handleCameraStartFailure = vi.fn();
222
+ const handleScanFailure = vi.fn();
223
+ mocks.start.mockRejectedValueOnce(cameraStartError);
224
+
225
+ render(<ScannerHost onCameraStartFailure={handleCameraStartFailure} onScanFailure={handleScanFailure} />);
226
+
227
+ await waitFor(() => expect(handleCameraStartFailure).toHaveBeenCalledWith(cameraStartError));
228
+ expect(handleScanFailure).not.toHaveBeenCalled();
229
+ });
230
+
197
231
  it('uses jsQR first for Safari uploaded QR images', async () => {
198
232
  const handleScanSuccess = vi.fn();
199
233
  jsQrMocks.default.mockReturnValueOnce({ data: 'JSQR-CODE' });
@@ -25,6 +25,7 @@ type UseCodeScannerOptions = {
25
25
  onScannerSizeChanged?: (size: ScannerSize) => void;
26
26
  onScanSuccess: (text: string) => void;
27
27
  onScanFailure?: () => void;
28
+ onCameraStartFailure?: (error: unknown) => void;
28
29
  };
29
30
 
30
31
  type ImageDataVariant = {
@@ -70,6 +71,13 @@ export function getCodeScanBoxSize(width: number, height: number) {
70
71
  };
71
72
  }
72
73
 
74
+ function clampCodeScanBoxSize(scanBoxSize: ScannerSize, width: number, height: number) {
75
+ return {
76
+ width: Math.min(scanBoxSize.width, width),
77
+ height: Math.min(scanBoxSize.height, height),
78
+ };
79
+ }
80
+
73
81
  async function stopScanner(scanner?: Html5Qrcode, options: { clear?: boolean } = {}) {
74
82
  if (!scanner) {
75
83
  return;
@@ -212,6 +220,7 @@ export function useCodeScanner({
212
220
  onScannerSizeChanged,
213
221
  onScanSuccess,
214
222
  onScanFailure,
223
+ onCameraStartFailure,
215
224
  }: UseCodeScannerOptions) {
216
225
  const [scanner, setScanner] = useState<Html5Qrcode>();
217
226
 
@@ -223,7 +232,7 @@ export function useCodeScanner({
223
232
  fps: 10,
224
233
  qrbox(width, height) {
225
234
  onScannerSizeChanged?.({ width, height });
226
- return scanBoxSize ?? getCodeScanBoxSize(width, height);
235
+ return clampCodeScanBoxSize(scanBoxSize ?? getCodeScanBoxSize(width, height), width, height);
227
236
  },
228
237
  },
229
238
  (decodedText) => {
@@ -273,14 +282,18 @@ export function useCodeScanner({
273
282
  verbose: false,
274
283
  });
275
284
  setScanner(scannerInstance);
276
- startScanCamera(scannerInstance).catch(() => {
285
+ startScanCamera(scannerInstance).catch((error: unknown) => {
286
+ if (onCameraStartFailure) {
287
+ onCameraStartFailure(error);
288
+ return;
289
+ }
277
290
  onScanFailure?.();
278
291
  });
279
292
 
280
293
  return () => {
281
294
  stopScanner(scannerInstance, { clear: true }).catch(() => undefined);
282
295
  };
283
- }, [elementId, enabled, formatsToSupport, onScanFailure, startScanCamera]);
296
+ }, [elementId, enabled, formatsToSupport, onCameraStartFailure, onScanFailure, startScanCamera]);
284
297
 
285
298
  return {
286
299
  startScanFile,
@@ -310,7 +310,7 @@ export class TableBlockModel extends CollectionBlockModel<TableBlockModelStructu
310
310
  transform: translateY(-50%);
311
311
  }
312
312
  &:hover {
313
- background: rgba(24, 144, 255, 0.1) !important;
313
+ box-shadow: inset 0 0 0 9999px rgba(24, 144, 255, 0.1);
314
314
  }
315
315
  &:hover .edit-icon {
316
316
  display: inline-flex;
@@ -102,7 +102,7 @@ const RenderCell = observer<any>((props) => {
102
102
  transform: translateY(-50%);
103
103
  }
104
104
  &:hover {
105
- background: rgba(24, 144, 255, 0.1) !important;
105
+ box-shadow: inset 0 0 0 9999px rgba(24, 144, 255, 0.1);
106
106
  }
107
107
  &:hover .edit-icon {
108
108
  display: inline-flex;
@@ -231,7 +231,7 @@ export const FIELDS_VALIDATION_OPTIONS = {
231
231
  },
232
232
  {
233
233
  key: 'multiple',
234
- label: 'Multiple',
234
+ label: 'Multiple of',
235
235
  hasValue: true,
236
236
  params: [{ key: 'base', label: 'Base', componentType: 'inputNumber', required: true }],
237
237
  },
@@ -240,6 +240,16 @@ const CurrentUserProvider: FC = ({ children }) => {
240
240
  return;
241
241
  }
242
242
 
243
+ try {
244
+ await app.apiClient.auth.syncCookies();
245
+ } catch {
246
+ // Cookie bootstrap is best-effort; auth:check remains the source of truth for the current page load.
247
+ }
248
+
249
+ if (!mounted) {
250
+ return;
251
+ }
252
+
243
253
  const userMeta = createCollectionContextMeta(
244
254
  () => app.flowEngine.context.dataSourceManager?.getDataSource('main')?.getCollection('users') || null,
245
255
  app.flowEngine.translate('Current user'),