@object-ui/core 0.5.0 → 2.0.0
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +11 -0
- package/dist/actions/ActionRunner.d.ts +228 -4
- package/dist/actions/ActionRunner.js +397 -45
- package/dist/actions/TransactionManager.d.ts +193 -0
- package/dist/actions/TransactionManager.js +410 -0
- package/dist/actions/index.d.ts +1 -0
- package/dist/actions/index.js +1 -0
- package/dist/adapters/ApiDataSource.d.ts +69 -0
- package/dist/adapters/ApiDataSource.js +293 -0
- package/dist/adapters/ValueDataSource.d.ts +55 -0
- package/dist/adapters/ValueDataSource.js +287 -0
- package/dist/adapters/index.d.ts +3 -0
- package/dist/adapters/index.js +5 -2
- package/dist/adapters/resolveDataSource.d.ts +40 -0
- package/dist/adapters/resolveDataSource.js +59 -0
- package/dist/data-scope/DataScopeManager.d.ts +127 -0
- package/dist/data-scope/DataScopeManager.js +229 -0
- package/dist/data-scope/index.d.ts +10 -0
- package/dist/data-scope/index.js +10 -0
- package/dist/evaluator/ExpressionEvaluator.d.ts +11 -1
- package/dist/evaluator/ExpressionEvaluator.js +32 -8
- package/dist/evaluator/FormulaFunctions.d.ts +58 -0
- package/dist/evaluator/FormulaFunctions.js +350 -0
- package/dist/evaluator/index.d.ts +1 -0
- package/dist/evaluator/index.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -2
- package/dist/query/query-ast.d.ts +2 -2
- package/dist/query/query-ast.js +3 -3
- package/dist/registry/Registry.d.ts +10 -0
- package/dist/registry/Registry.js +2 -1
- package/dist/registry/WidgetRegistry.d.ts +120 -0
- package/dist/registry/WidgetRegistry.js +275 -0
- package/dist/theme/ThemeEngine.d.ts +82 -0
- package/dist/theme/ThemeEngine.js +400 -0
- package/dist/theme/index.d.ts +8 -0
- package/dist/theme/index.js +8 -0
- package/dist/validation/index.d.ts +1 -1
- package/dist/validation/index.js +1 -1
- package/dist/validation/validation-engine.d.ts +19 -1
- package/dist/validation/validation-engine.js +67 -2
- package/dist/validation/validators/index.d.ts +1 -1
- package/dist/validation/validators/index.js +1 -1
- package/dist/validation/validators/object-validation-engine.d.ts +2 -2
- package/dist/validation/validators/object-validation-engine.js +1 -1
- package/package.json +4 -3
- package/src/actions/ActionRunner.ts +577 -55
- package/src/actions/TransactionManager.ts +521 -0
- package/src/actions/__tests__/ActionRunner.params.test.ts +134 -0
- package/src/actions/__tests__/ActionRunner.test.ts +711 -0
- package/src/actions/__tests__/TransactionManager.test.ts +447 -0
- package/src/actions/index.ts +1 -0
- package/src/adapters/ApiDataSource.ts +349 -0
- package/src/adapters/ValueDataSource.ts +332 -0
- package/src/adapters/__tests__/ApiDataSource.test.ts +418 -0
- package/src/adapters/__tests__/ValueDataSource.test.ts +325 -0
- package/src/adapters/__tests__/resolveDataSource.test.ts +144 -0
- package/src/adapters/index.ts +6 -1
- package/src/adapters/resolveDataSource.ts +79 -0
- package/src/builder/__tests__/schema-builder.test.ts +235 -0
- package/src/data-scope/DataScopeManager.ts +269 -0
- package/src/data-scope/__tests__/DataScopeManager.test.ts +211 -0
- package/src/data-scope/index.ts +16 -0
- package/src/evaluator/ExpressionEvaluator.ts +34 -8
- package/src/evaluator/FormulaFunctions.ts +398 -0
- package/src/evaluator/__tests__/ExpressionContext.test.ts +110 -0
- package/src/evaluator/__tests__/FormulaFunctions.test.ts +447 -0
- package/src/evaluator/index.ts +1 -0
- package/src/index.ts +4 -3
- package/src/query/__tests__/window-functions.test.ts +1 -1
- package/src/query/query-ast.ts +3 -3
- package/src/registry/Registry.ts +12 -1
- package/src/registry/WidgetRegistry.ts +316 -0
- package/src/registry/__tests__/WidgetRegistry.test.ts +321 -0
- package/src/theme/ThemeEngine.ts +452 -0
- package/src/theme/__tests__/ThemeEngine.test.ts +606 -0
- package/src/theme/index.ts +22 -0
- package/src/validation/__tests__/object-validation-engine.test.ts +1 -1
- package/src/validation/__tests__/schema-validator.test.ts +118 -0
- package/src/validation/index.ts +1 -1
- package/src/validation/validation-engine.ts +61 -2
- package/src/validation/validators/index.ts +1 -1
- package/src/validation/validators/object-validation-engine.ts +2 -2
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — ApiDataSource Tests
|
|
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 { describe, it, expect, vi } from 'vitest';
|
|
10
|
+
import { ApiDataSource } from '../ApiDataSource';
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Mock fetch helper
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
function createMockFetch(response: any, options?: { status?: number; contentType?: string }) {
|
|
17
|
+
const status = options?.status ?? 200;
|
|
18
|
+
const ok = status >= 200 && status < 300;
|
|
19
|
+
const contentType = options?.contentType ?? 'application/json';
|
|
20
|
+
|
|
21
|
+
return vi.fn().mockResolvedValue({
|
|
22
|
+
ok,
|
|
23
|
+
status,
|
|
24
|
+
statusText: ok ? 'OK' : 'Error',
|
|
25
|
+
headers: {
|
|
26
|
+
get: (name: string) => (name.toLowerCase() === 'content-type' ? contentType : null),
|
|
27
|
+
},
|
|
28
|
+
json: () => Promise.resolve(response),
|
|
29
|
+
text: () => Promise.resolve(typeof response === 'string' ? response : JSON.stringify(response)),
|
|
30
|
+
} as unknown as Response);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// find
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
describe('ApiDataSource — find', () => {
|
|
38
|
+
it('should fetch from the read URL', async () => {
|
|
39
|
+
const mockFetch = createMockFetch([{ id: '1', name: 'Alice' }]);
|
|
40
|
+
const ds = new ApiDataSource({
|
|
41
|
+
read: { url: 'https://api.example.com/users' },
|
|
42
|
+
fetch: mockFetch,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const result = await ds.find('users');
|
|
46
|
+
expect(mockFetch).toHaveBeenCalledOnce();
|
|
47
|
+
expect(result.data).toHaveLength(1);
|
|
48
|
+
expect(result.data[0].name).toBe('Alice');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should pass query params to the URL', async () => {
|
|
52
|
+
const mockFetch = createMockFetch([]);
|
|
53
|
+
const ds = new ApiDataSource({
|
|
54
|
+
read: { url: 'https://api.example.com/users' },
|
|
55
|
+
fetch: mockFetch,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
await ds.find('users', { $top: 10, $skip: 20, $select: ['name', 'age'] });
|
|
59
|
+
|
|
60
|
+
const calledUrl = mockFetch.mock.calls[0][0] as string;
|
|
61
|
+
// URLSearchParams encodes $ as %24
|
|
62
|
+
expect(calledUrl).toContain('top=10');
|
|
63
|
+
expect(calledUrl).toContain('skip=20');
|
|
64
|
+
expect(calledUrl).toContain('select=');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should normalize array response into QueryResult', async () => {
|
|
68
|
+
const items = [{ id: '1' }, { id: '2' }, { id: '3' }];
|
|
69
|
+
const mockFetch = createMockFetch(items);
|
|
70
|
+
const ds = new ApiDataSource({
|
|
71
|
+
read: { url: '/api/users' },
|
|
72
|
+
fetch: mockFetch,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const result = await ds.find('users');
|
|
76
|
+
expect(result.data).toEqual(items);
|
|
77
|
+
expect(result.total).toBe(3);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should normalize { data, total } response', async () => {
|
|
81
|
+
const mockFetch = createMockFetch({
|
|
82
|
+
data: [{ id: '1' }],
|
|
83
|
+
total: 42,
|
|
84
|
+
});
|
|
85
|
+
const ds = new ApiDataSource({
|
|
86
|
+
read: { url: '/api/users' },
|
|
87
|
+
fetch: mockFetch,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const result = await ds.find('users');
|
|
91
|
+
expect(result.data).toHaveLength(1);
|
|
92
|
+
expect(result.total).toBe(42);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should normalize { items } response', async () => {
|
|
96
|
+
const mockFetch = createMockFetch({
|
|
97
|
+
items: [{ id: '1' }, { id: '2' }],
|
|
98
|
+
count: 10,
|
|
99
|
+
});
|
|
100
|
+
const ds = new ApiDataSource({
|
|
101
|
+
read: { url: '/api/users' },
|
|
102
|
+
fetch: mockFetch,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const result = await ds.find('users');
|
|
106
|
+
expect(result.data).toHaveLength(2);
|
|
107
|
+
expect(result.total).toBe(10);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should normalize { results } response', async () => {
|
|
111
|
+
const mockFetch = createMockFetch({
|
|
112
|
+
results: [{ id: '1' }],
|
|
113
|
+
});
|
|
114
|
+
const ds = new ApiDataSource({
|
|
115
|
+
read: { url: '/api/users' },
|
|
116
|
+
fetch: mockFetch,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const result = await ds.find('users');
|
|
120
|
+
expect(result.data).toHaveLength(1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('should normalize { records } response (Salesforce-style)', async () => {
|
|
124
|
+
const mockFetch = createMockFetch({
|
|
125
|
+
records: [{ id: '1' }, { id: '2' }],
|
|
126
|
+
totalCount: 50,
|
|
127
|
+
});
|
|
128
|
+
const ds = new ApiDataSource({
|
|
129
|
+
read: { url: '/api/users' },
|
|
130
|
+
fetch: mockFetch,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const result = await ds.find('users');
|
|
134
|
+
expect(result.data).toHaveLength(2);
|
|
135
|
+
expect(result.total).toBe(50);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should normalize { value } response (OData-style)', async () => {
|
|
139
|
+
const mockFetch = createMockFetch({
|
|
140
|
+
value: [{ id: '1' }],
|
|
141
|
+
});
|
|
142
|
+
const ds = new ApiDataSource({
|
|
143
|
+
read: { url: '/api/users' },
|
|
144
|
+
fetch: mockFetch,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const result = await ds.find('users');
|
|
148
|
+
expect(result.data).toHaveLength(1);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should wrap single-object response as array', async () => {
|
|
152
|
+
const mockFetch = createMockFetch({ id: '1', name: 'Single' });
|
|
153
|
+
const ds = new ApiDataSource({
|
|
154
|
+
read: { url: '/api/users' },
|
|
155
|
+
fetch: mockFetch,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const result = await ds.find('users');
|
|
159
|
+
expect(result.data).toHaveLength(1);
|
|
160
|
+
expect(result.data[0].name).toBe('Single');
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should merge base params with query params', async () => {
|
|
164
|
+
const mockFetch = createMockFetch([]);
|
|
165
|
+
const ds = new ApiDataSource({
|
|
166
|
+
read: { url: '/api/users', params: { tenant: 'acme' } },
|
|
167
|
+
fetch: mockFetch,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
await ds.find('users', { $top: 5 });
|
|
171
|
+
const calledUrl = mockFetch.mock.calls[0][0] as string;
|
|
172
|
+
expect(calledUrl).toContain('tenant=acme');
|
|
173
|
+
expect(calledUrl).toContain('top=5');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should merge default headers with request headers', async () => {
|
|
177
|
+
const mockFetch = createMockFetch([]);
|
|
178
|
+
const ds = new ApiDataSource({
|
|
179
|
+
read: { url: '/api/users', headers: { 'X-Api-Key': 'key123' } },
|
|
180
|
+
fetch: mockFetch,
|
|
181
|
+
defaultHeaders: { Authorization: 'Bearer token' },
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
await ds.find('users');
|
|
185
|
+
const init = mockFetch.mock.calls[0][1] as RequestInit;
|
|
186
|
+
const headers = init.headers as Record<string, string>;
|
|
187
|
+
expect(headers['Authorization']).toBe('Bearer token');
|
|
188
|
+
expect(headers['X-Api-Key']).toBe('key123');
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// ---------------------------------------------------------------------------
|
|
193
|
+
// findOne
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
describe('ApiDataSource — findOne', () => {
|
|
197
|
+
it('should append id to the URL path', async () => {
|
|
198
|
+
const mockFetch = createMockFetch({ id: '42', name: 'Alice' });
|
|
199
|
+
const ds = new ApiDataSource({
|
|
200
|
+
read: { url: 'https://api.example.com/users' },
|
|
201
|
+
fetch: mockFetch,
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const record = await ds.findOne('users', '42');
|
|
205
|
+
const calledUrl = mockFetch.mock.calls[0][0] as string;
|
|
206
|
+
expect(calledUrl).toBe('https://api.example.com/users/42');
|
|
207
|
+
expect(record?.name).toBe('Alice');
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('should return null on 404', async () => {
|
|
211
|
+
const mockFetch = createMockFetch('Not Found', { status: 404, contentType: 'text/plain' });
|
|
212
|
+
const ds = new ApiDataSource({
|
|
213
|
+
read: { url: '/api/users' },
|
|
214
|
+
fetch: mockFetch,
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const record = await ds.findOne('users', '999');
|
|
218
|
+
expect(record).toBeNull();
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// ---------------------------------------------------------------------------
|
|
223
|
+
// create
|
|
224
|
+
// ---------------------------------------------------------------------------
|
|
225
|
+
|
|
226
|
+
describe('ApiDataSource — create', () => {
|
|
227
|
+
it('should POST to the write URL', async () => {
|
|
228
|
+
const mockFetch = createMockFetch({ id: 'new', name: 'Bob' });
|
|
229
|
+
const ds = new ApiDataSource({
|
|
230
|
+
write: { url: '/api/users', method: 'POST' },
|
|
231
|
+
fetch: mockFetch,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const created = await ds.create('users', { name: 'Bob' });
|
|
235
|
+
const init = mockFetch.mock.calls[0][1] as RequestInit;
|
|
236
|
+
expect(init.method).toBe('POST');
|
|
237
|
+
expect(init.body).toBe(JSON.stringify({ name: 'Bob' }));
|
|
238
|
+
expect(created.name).toBe('Bob');
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('should fall back to read URL when write is not defined', async () => {
|
|
242
|
+
const mockFetch = createMockFetch({ id: 'new' });
|
|
243
|
+
const ds = new ApiDataSource({
|
|
244
|
+
read: { url: '/api/users' },
|
|
245
|
+
fetch: mockFetch,
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
await ds.create('users', { name: 'Test' });
|
|
249
|
+
const calledUrl = mockFetch.mock.calls[0][0] as string;
|
|
250
|
+
expect(calledUrl).toBe('/api/users');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('should set Content-Type to application/json', async () => {
|
|
254
|
+
const mockFetch = createMockFetch({});
|
|
255
|
+
const ds = new ApiDataSource({
|
|
256
|
+
write: { url: '/api/users' },
|
|
257
|
+
fetch: mockFetch,
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
await ds.create('users', { name: 'Test' });
|
|
261
|
+
const headers = (mockFetch.mock.calls[0][1] as RequestInit).headers as Record<string, string>;
|
|
262
|
+
expect(headers['Content-Type']).toBe('application/json');
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// ---------------------------------------------------------------------------
|
|
267
|
+
// update
|
|
268
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
|
|
270
|
+
describe('ApiDataSource — update', () => {
|
|
271
|
+
it('should PATCH to the write URL with id suffix', async () => {
|
|
272
|
+
const mockFetch = createMockFetch({ id: '1', name: 'Updated' });
|
|
273
|
+
const ds = new ApiDataSource({
|
|
274
|
+
write: { url: '/api/users' },
|
|
275
|
+
fetch: mockFetch,
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
const updated = await ds.update('users', '1', { name: 'Updated' });
|
|
279
|
+
const calledUrl = mockFetch.mock.calls[0][0] as string;
|
|
280
|
+
const init = mockFetch.mock.calls[0][1] as RequestInit;
|
|
281
|
+
expect(calledUrl).toContain('/1');
|
|
282
|
+
expect(init.method).toBe('PATCH');
|
|
283
|
+
expect(updated.name).toBe('Updated');
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
// ---------------------------------------------------------------------------
|
|
288
|
+
// delete
|
|
289
|
+
// ---------------------------------------------------------------------------
|
|
290
|
+
|
|
291
|
+
describe('ApiDataSource — delete', () => {
|
|
292
|
+
it('should DELETE at the write URL with id suffix', async () => {
|
|
293
|
+
const mockFetch = createMockFetch('', { contentType: 'text/plain' });
|
|
294
|
+
const ds = new ApiDataSource({
|
|
295
|
+
write: { url: '/api/users' },
|
|
296
|
+
fetch: mockFetch,
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
const result = await ds.delete('users', '1');
|
|
300
|
+
const init = mockFetch.mock.calls[0][1] as RequestInit;
|
|
301
|
+
expect(init.method).toBe('DELETE');
|
|
302
|
+
expect(result).toBe(true);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('should return false on failure', async () => {
|
|
306
|
+
const mockFetch = createMockFetch('Server Error', { status: 500, contentType: 'text/plain' });
|
|
307
|
+
const ds = new ApiDataSource({
|
|
308
|
+
write: { url: '/api/users' },
|
|
309
|
+
fetch: mockFetch,
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
const result = await ds.delete('users', '1');
|
|
313
|
+
expect(result).toBe(false);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// ---------------------------------------------------------------------------
|
|
318
|
+
// getObjectSchema
|
|
319
|
+
// ---------------------------------------------------------------------------
|
|
320
|
+
|
|
321
|
+
describe('ApiDataSource — getObjectSchema', () => {
|
|
322
|
+
it('should return a minimal stub', async () => {
|
|
323
|
+
const ds = new ApiDataSource({
|
|
324
|
+
read: { url: '/api/users' },
|
|
325
|
+
fetch: createMockFetch({}),
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
const schema = await ds.getObjectSchema('users');
|
|
329
|
+
expect(schema.name).toBe('users');
|
|
330
|
+
expect(schema.fields).toEqual({});
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// ---------------------------------------------------------------------------
|
|
335
|
+
// Error handling
|
|
336
|
+
// ---------------------------------------------------------------------------
|
|
337
|
+
|
|
338
|
+
describe('ApiDataSource — errors', () => {
|
|
339
|
+
it('should throw on HTTP error response for find', async () => {
|
|
340
|
+
const mockFetch = createMockFetch('Forbidden', { status: 403, contentType: 'text/plain' });
|
|
341
|
+
const ds = new ApiDataSource({
|
|
342
|
+
read: { url: '/api/users' },
|
|
343
|
+
fetch: mockFetch,
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
await expect(ds.find('users')).rejects.toThrow();
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('should throw when no read config provided for find', async () => {
|
|
350
|
+
const ds = new ApiDataSource({
|
|
351
|
+
write: { url: '/api/users' },
|
|
352
|
+
fetch: createMockFetch({}),
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
await expect(ds.find('users')).rejects.toThrow('No HTTP configuration');
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('should throw when no config provided at all for create', async () => {
|
|
359
|
+
const ds = new ApiDataSource({
|
|
360
|
+
fetch: createMockFetch({}),
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
await expect(ds.create('users', { name: 'X' })).rejects.toThrow('No HTTP configuration');
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
// ---------------------------------------------------------------------------
|
|
368
|
+
// URL building
|
|
369
|
+
// ---------------------------------------------------------------------------
|
|
370
|
+
|
|
371
|
+
describe('ApiDataSource — URL building', () => {
|
|
372
|
+
it('should handle trailing slashes in base URL', async () => {
|
|
373
|
+
const mockFetch = createMockFetch({ id: '1' });
|
|
374
|
+
const ds = new ApiDataSource({
|
|
375
|
+
read: { url: 'https://api.example.com/users/' },
|
|
376
|
+
fetch: mockFetch,
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
await ds.findOne('users', '42');
|
|
380
|
+
const calledUrl = mockFetch.mock.calls[0][0] as string;
|
|
381
|
+
expect(calledUrl).toBe('https://api.example.com/users/42');
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it('should serialize $filter as JSON string', async () => {
|
|
385
|
+
const mockFetch = createMockFetch([]);
|
|
386
|
+
const ds = new ApiDataSource({
|
|
387
|
+
read: { url: 'http://localhost/api/users' },
|
|
388
|
+
fetch: mockFetch,
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
await ds.find('users', { $filter: { status: 'active' } });
|
|
392
|
+
const calledUrl = mockFetch.mock.calls[0][0] as string;
|
|
393
|
+
// URLSearchParams encodes $ as %24
|
|
394
|
+
expect(calledUrl).toContain('filter=');
|
|
395
|
+
// The value should be URL-encoded JSON
|
|
396
|
+
const url = new URL(calledUrl);
|
|
397
|
+
const filterParam = url.searchParams.get('$filter');
|
|
398
|
+
expect(JSON.parse(filterParam!)).toEqual({ status: 'active' });
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it('should serialize $orderby from Record format', async () => {
|
|
402
|
+
const mockFetch = createMockFetch([]);
|
|
403
|
+
const ds = new ApiDataSource({
|
|
404
|
+
read: { url: 'http://localhost/api/users' },
|
|
405
|
+
fetch: mockFetch,
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
await ds.find('users', { $orderby: { name: 'asc', age: 'desc' } });
|
|
409
|
+
const calledUrl = mockFetch.mock.calls[0][0] as string;
|
|
410
|
+
// URLSearchParams encodes $ as %24
|
|
411
|
+
expect(calledUrl).toContain('orderby=');
|
|
412
|
+
// Should contain the formatted orderby string
|
|
413
|
+
const url = new URL(calledUrl);
|
|
414
|
+
const orderbyParam = url.searchParams.get('$orderby');
|
|
415
|
+
expect(orderbyParam).toContain('name asc');
|
|
416
|
+
expect(orderbyParam).toContain('age desc');
|
|
417
|
+
});
|
|
418
|
+
});
|