Skip to content

Commit 11e01c1

Browse files
Align client discovery interface with spec API protocol
Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com>
1 parent cd7c736 commit 11e01c1

2 files changed

Lines changed: 35 additions & 29 deletions

File tree

packages/client/src/client.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ describe('ObjectStackClient', () => {
1515
it('should make discovery request on connect', async () => {
1616
const fetchMock = vi.fn().mockResolvedValue({
1717
ok: true,
18-
json: async () => ({ routes: { data: '/api/v1/data' } })
18+
json: async () => ({
19+
version: 'v1',
20+
apiName: 'ObjectStack',
21+
capabilities: ['metadata', 'data', 'ui'],
22+
endpoints: {}
23+
})
1924
});
2025

2126
const client = new ObjectStackClient({

packages/client/src/index.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
MetadataCacheRequest,
99
MetadataCacheResponse,
1010
StandardErrorCode,
11-
ErrorCategory
11+
ErrorCategory,
12+
GetDiscoveryResponse
1213
} from '@objectstack/spec/api';
1314
import { Logger, createLogger } from '@objectstack/core';
1415

@@ -29,16 +30,11 @@ export interface ClientConfig {
2930
debug?: boolean;
3031
}
3132

32-
export interface DiscoveryResult {
33-
routes: {
34-
discovery: string;
35-
metadata: string;
36-
data: string;
37-
auth: string;
38-
ui: string;
39-
};
40-
capabilities?: Record<string, boolean>;
41-
}
33+
/**
34+
* Discovery Result
35+
* Re-export from @objectstack/spec/api for convenience
36+
*/
37+
export type DiscoveryResult = GetDiscoveryResponse;
4238

4339
export interface QueryOptions {
4440
select?: string[]; // Simplified Selection
@@ -69,7 +65,7 @@ export class ObjectStackClient {
6965
private baseUrl: string;
7066
private token?: string;
7167
private fetchImpl: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
72-
private routes?: DiscoveryResult['routes'];
68+
private discoveryInfo?: DiscoveryResult;
7369
private logger: Logger;
7470

7571
constructor(config: ClientConfig) {
@@ -87,21 +83,21 @@ export class ObjectStackClient {
8783
}
8884

8985
/**
90-
* Initialize the client by discovering server capabilities and routes.
86+
* Initialize the client by discovering server capabilities.
9187
*/
9288
async connect() {
9389
this.logger.debug('Connecting to ObjectStack server', { baseUrl: this.baseUrl });
9490

9591
try {
96-
// Connect to the discovery endpoint
97-
// During boot, we might not know routes, so we check convention /api/v1 first
92+
// Connect to the discovery endpoint at /api/v1
9893
const res = await this.fetch(`${this.baseUrl}/api/v1`);
9994

10095
const data = await res.json();
101-
this.routes = data.routes;
96+
this.discoveryInfo = data;
10297

10398
this.logger.info('Connected to ObjectStack server', {
104-
routes: Object.keys(data.routes || {}),
99+
version: data.version,
100+
apiName: data.apiName,
105101
capabilities: data.capabilities
106102
});
107103

@@ -407,16 +403,20 @@ export class ObjectStackClient {
407403
return res;
408404
}
409405

410-
private getRoute(key: keyof DiscoveryResult['routes']): string {
411-
if (!this.routes) {
412-
// Fallback for strictness, but we allow bootstrapping
413-
this.logger.warn('Accessing route before connect()', {
414-
route: key,
415-
fallback: `/api/v1/${key}`
416-
});
417-
return `/api/v1/${key}`;
418-
}
419-
return this.routes[key] || `/api/v1/${key}`;
406+
/**
407+
* Get the conventional route path for a given API endpoint type
408+
* ObjectStack uses standard conventions: /api/v1/data, /api/v1/meta, /api/v1/ui
409+
*/
410+
private getRoute(type: 'data' | 'metadata' | 'ui' | 'auth'): string {
411+
// Use conventional ObjectStack API paths
412+
const routeMap: Record<string, string> = {
413+
data: '/api/v1/data',
414+
metadata: '/api/v1/meta',
415+
ui: '/api/v1/ui',
416+
auth: '/api/v1/auth'
417+
};
418+
419+
return routeMap[type] || `/api/v1/${type}`;
420420
}
421421
}
422422

@@ -435,5 +435,6 @@ export type {
435435
MetadataCacheRequest,
436436
MetadataCacheResponse,
437437
StandardErrorCode,
438-
ErrorCategory
438+
ErrorCategory,
439+
GetDiscoveryResponse
439440
} from '@objectstack/spec/api';

0 commit comments

Comments
 (0)