Skip to content

Commit f9cc0a4

Browse files
Add metadata discovery methods to client (getTypes, getItems, getItem)
Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com>
1 parent 11e01c1 commit f9cc0a4

2 files changed

Lines changed: 98 additions & 2 deletions

File tree

packages/client/src/client.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,61 @@ describe('ObjectStackClient', () => {
3131
await client.connect();
3232
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/api/v1', expect.any(Object));
3333
});
34+
35+
it('should get metadata types', async () => {
36+
const fetchMock = vi.fn().mockResolvedValue({
37+
ok: true,
38+
json: async () => ({
39+
types: ['object', 'plugin', 'view']
40+
})
41+
});
42+
43+
const client = new ObjectStackClient({
44+
baseUrl: 'http://localhost:3000',
45+
fetch: fetchMock
46+
});
47+
48+
const result = await client.meta.getTypes();
49+
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/api/v1/meta', expect.any(Object));
50+
expect(result.types).toEqual(['object', 'plugin', 'view']);
51+
});
52+
53+
it('should get metadata items by type', async () => {
54+
const fetchMock = vi.fn().mockResolvedValue({
55+
ok: true,
56+
json: async () => ({
57+
type: 'object',
58+
items: [{ name: 'customer' }, { name: 'order' }]
59+
})
60+
});
61+
62+
const client = new ObjectStackClient({
63+
baseUrl: 'http://localhost:3000',
64+
fetch: fetchMock
65+
});
66+
67+
const result = await client.meta.getItems('object');
68+
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/api/v1/meta/object', expect.any(Object));
69+
expect(result.type).toBe('object');
70+
expect(result.items).toHaveLength(2);
71+
});
72+
73+
it('should get metadata item by type and name', async () => {
74+
const fetchMock = vi.fn().mockResolvedValue({
75+
ok: true,
76+
json: async () => ({
77+
name: 'customer',
78+
fields: []
79+
})
80+
});
81+
82+
const client = new ObjectStackClient({
83+
baseUrl: 'http://localhost:3000',
84+
fetch: fetchMock
85+
});
86+
87+
const result = await client.meta.getItem('object', 'customer');
88+
expect(fetchMock).toHaveBeenCalledWith('http://localhost:3000/api/v1/meta/object/customer', expect.any(Object));
89+
expect(result.name).toBe('customer');
90+
});
3491
});

packages/client/src/index.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
MetadataCacheResponse,
1010
StandardErrorCode,
1111
ErrorCategory,
12-
GetDiscoveryResponse
12+
GetDiscoveryResponse,
13+
GetMetaTypesResponse,
14+
GetMetaItemsResponse
1315
} from '@objectstack/spec/api';
1416
import { Logger, createLogger } from '@objectstack/core';
1517

@@ -112,11 +114,46 @@ export class ObjectStackClient {
112114
* Metadata Operations
113115
*/
114116
meta = {
117+
/**
118+
* Get all available metadata types
119+
* Returns types like 'object', 'plugin', 'view', etc.
120+
*/
121+
getTypes: async (): Promise<GetMetaTypesResponse> => {
122+
const route = this.getRoute('metadata');
123+
const res = await this.fetch(`${this.baseUrl}${route}`);
124+
return res.json();
125+
},
126+
127+
/**
128+
* Get all items of a specific metadata type
129+
* @param type - Metadata type name (e.g., 'object', 'plugin')
130+
*/
131+
getItems: async (type: string): Promise<GetMetaItemsResponse> => {
132+
const route = this.getRoute('metadata');
133+
const res = await this.fetch(`${this.baseUrl}${route}/${type}`);
134+
return res.json();
135+
},
136+
137+
/**
138+
* Get a specific object definition by name
139+
* Legacy method - prefer getItem for consistency
140+
*/
115141
getObject: async (name: string) => {
116142
const route = this.getRoute('metadata');
117143
const res = await this.fetch(`${this.baseUrl}${route}/object/${name}`);
118144
return res.json();
119145
},
146+
147+
/**
148+
* Get a specific metadata item by type and name
149+
* @param type - Metadata type (e.g., 'object', 'plugin')
150+
* @param name - Item name (snake_case identifier)
151+
*/
152+
getItem: async (type: string, name: string) => {
153+
const route = this.getRoute('metadata');
154+
const res = await this.fetch(`${this.baseUrl}${route}/${type}/${name}`);
155+
return res.json();
156+
},
120157

121158
/**
122159
* Get object metadata with cache support
@@ -436,5 +473,7 @@ export type {
436473
MetadataCacheResponse,
437474
StandardErrorCode,
438475
ErrorCategory,
439-
GetDiscoveryResponse
476+
GetDiscoveryResponse,
477+
GetMetaTypesResponse,
478+
GetMetaItemsResponse
440479
} from '@objectstack/spec/api';

0 commit comments

Comments
 (0)