|
| 1 | +export interface ClientConfig { |
| 2 | + baseUrl: string; |
| 3 | + token?: string; |
| 4 | +} |
| 5 | + |
| 6 | +export interface DiscoveryResult { |
| 7 | + routes: { |
| 8 | + discovery: string; |
| 9 | + metadata: string; |
| 10 | + data: string; |
| 11 | + auth: string; |
| 12 | + }; |
| 13 | + capabilities?: Record<string, boolean>; |
| 14 | +} |
| 15 | + |
| 16 | +export class ObjectStackClient { |
| 17 | + private baseUrl: string; |
| 18 | + private token?: string; |
| 19 | + private routes?: DiscoveryResult['routes']; |
| 20 | + |
| 21 | + constructor(config: ClientConfig) { |
| 22 | + this.baseUrl = config.baseUrl.replace(/\/$/, ''); // Remove trailing slash |
| 23 | + this.token = config.token; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Initialize the client by discovering server capabilities and routes. |
| 28 | + */ |
| 29 | + async connect() { |
| 30 | + try { |
| 31 | + const res = await this.fetch(`${this.baseUrl}/api/v1`); |
| 32 | + const data = await res.json(); |
| 33 | + this.routes = data.routes; |
| 34 | + return data; |
| 35 | + } catch (e) { |
| 36 | + console.error('Failed to connect to ObjectStack Server', e); |
| 37 | + throw e; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Metadata Operations |
| 43 | + */ |
| 44 | + meta = { |
| 45 | + getObject: async (name: string) => { |
| 46 | + const route = this.getRoute('metadata'); |
| 47 | + const res = await this.fetch(`${this.baseUrl}${route}/object/${name}`); |
| 48 | + return res.json(); |
| 49 | + }, |
| 50 | + |
| 51 | + getView: async (object: string, type: 'list' | 'form' = 'list') => { |
| 52 | + // UI routes might not be in discovery map yet, assume convention or add to server |
| 53 | + // Convention from server/src/index.ts: /api/v1/ui/view/:object |
| 54 | + const res = await this.fetch(`${this.baseUrl}/api/v1/ui/view/${object}?type=${type}`); |
| 55 | + return res.json(); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + /** |
| 60 | + * Data Operations |
| 61 | + */ |
| 62 | + data = { |
| 63 | + find: async (object: string, query: any = {}) => { |
| 64 | + const route = this.getRoute('data'); |
| 65 | + const queryString = new URLSearchParams(query).toString(); |
| 66 | + const res = await this.fetch(`${this.baseUrl}${route}/${object}?${queryString}`); |
| 67 | + return res.json(); |
| 68 | + }, |
| 69 | + |
| 70 | + get: async (object: string, id: string) => { |
| 71 | + const route = this.getRoute('data'); |
| 72 | + const res = await this.fetch(`${this.baseUrl}${route}/${object}/${id}`); |
| 73 | + return res.json(); |
| 74 | + }, |
| 75 | + |
| 76 | + create: async (object: string, data: any) => { |
| 77 | + const route = this.getRoute('data'); |
| 78 | + const res = await this.fetch(`${this.baseUrl}${route}/${object}`, { |
| 79 | + method: 'POST', |
| 80 | + body: JSON.stringify(data) |
| 81 | + }); |
| 82 | + return res.json(); |
| 83 | + }, |
| 84 | + |
| 85 | + update: async (object: string, id: string, data: any) => { |
| 86 | + const route = this.getRoute('data'); |
| 87 | + const res = await this.fetch(`${this.baseUrl}${route}/${object}/${id}`, { |
| 88 | + method: 'PATCH', |
| 89 | + body: JSON.stringify(data) |
| 90 | + }); |
| 91 | + return res.json(); |
| 92 | + }, |
| 93 | + |
| 94 | + delete: async (object: string, id: string) => { |
| 95 | + const route = this.getRoute('data'); |
| 96 | + const res = await this.fetch(`${this.baseUrl}${route}/${object}/${id}`, { |
| 97 | + method: 'DELETE' |
| 98 | + }); |
| 99 | + return res.json(); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + private getRoute(key: keyof DiscoveryResult['routes']): string { |
| 104 | + if (!this.routes) { |
| 105 | + throw new Error('Client not connected. Call client.connect() first.'); |
| 106 | + } |
| 107 | + return this.routes[key]; |
| 108 | + } |
| 109 | + |
| 110 | + private async fetch(url: string, options: RequestInit = {}) { |
| 111 | + const headers: Record<string, string> = { |
| 112 | + 'Content-Type': 'application/json', |
| 113 | + ...(options.headers as any || {}) |
| 114 | + }; |
| 115 | + |
| 116 | + if (this.token) { |
| 117 | + headers['Authorization'] = `Bearer ${this.token}`; |
| 118 | + } |
| 119 | + |
| 120 | + const res = await fetch(url, { |
| 121 | + ...options, |
| 122 | + headers |
| 123 | + }); |
| 124 | + |
| 125 | + if (res.status >= 400) { |
| 126 | + throw new Error(`API Error ${res.status}: ${res.statusText}`); |
| 127 | + } |
| 128 | + |
| 129 | + return res; |
| 130 | + } |
| 131 | +} |
0 commit comments