@xata.io/client 0.2.2 → 0.3.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/api/client.d.ts +95 -0
  3. package/dist/api/client.js +235 -0
  4. package/dist/api/components.d.ts +1440 -0
  5. package/dist/api/components.js +1001 -0
  6. package/dist/api/fetcher.d.ts +25 -0
  7. package/dist/api/fetcher.js +78 -0
  8. package/dist/api/index.d.ts +7 -0
  9. package/dist/api/index.js +21 -0
  10. package/dist/api/parameters.d.ts +16 -0
  11. package/dist/api/parameters.js +2 -0
  12. package/dist/api/providers.d.ts +8 -0
  13. package/dist/api/providers.js +29 -0
  14. package/dist/api/responses.d.ts +44 -0
  15. package/dist/api/responses.js +2 -0
  16. package/dist/api/schemas.d.ts +311 -0
  17. package/dist/api/schemas.js +2 -0
  18. package/dist/index.d.ts +2 -133
  19. package/dist/index.js +16 -368
  20. package/dist/schema/filters.d.ts +20 -0
  21. package/dist/schema/filters.js +24 -0
  22. package/dist/schema/index.d.ts +5 -0
  23. package/dist/schema/index.js +25 -0
  24. package/dist/schema/operators.d.ts +72 -0
  25. package/dist/schema/operators.js +91 -0
  26. package/dist/schema/pagination.d.ts +79 -0
  27. package/dist/schema/pagination.js +90 -0
  28. package/dist/schema/query.d.ts +114 -0
  29. package/dist/schema/query.js +227 -0
  30. package/dist/schema/record.d.ts +36 -0
  31. package/dist/schema/record.js +2 -0
  32. package/dist/schema/repository.d.ts +109 -0
  33. package/dist/schema/repository.js +273 -0
  34. package/dist/schema/selection.d.ts +13 -0
  35. package/dist/schema/selection.js +2 -0
  36. package/dist/util/lang.d.ts +2 -0
  37. package/dist/util/lang.js +10 -0
  38. package/dist/util/types.d.ts +3 -0
  39. package/dist/util/types.js +2 -0
  40. package/package.json +3 -3
  41. package/dist/index.test.d.ts +0 -1
  42. package/dist/index.test.js +0 -304
  43. package/src/index.test.ts +0 -392
  44. package/src/index.ts +0 -506
package/src/index.test.ts DELETED
@@ -1,392 +0,0 @@
1
- import { BaseClient, RestRepository, XataClientOptions, XataError, XataRecord } from './';
2
-
3
- interface User extends XataRecord {
4
- name: string;
5
- }
6
-
7
- const buildClient = (options: Partial<XataClientOptions> = {}) => {
8
- const {
9
- apiKey = '1234',
10
- databaseURL = 'https://my-workspace-5df34do.staging.xatabase.co/db/xata',
11
- branch = 'main'
12
- } = options;
13
-
14
- const fetch = jest.fn();
15
- const client = new BaseClient({ fetch, apiKey, databaseURL, branch }, {});
16
- const users = new RestRepository<User>(client, 'users');
17
-
18
- return { fetch, client, users };
19
- };
20
-
21
- describe('client options', () => {
22
- test('option parameters are set', () => {
23
- const { client } = buildClient({ apiKey: 'apiKey', databaseURL: 'url' });
24
- expect(client.options.apiKey).toBe('apiKey');
25
- expect(client.options.databaseURL).toBe('url');
26
- });
27
-
28
- test('throws if mandatory options are missing', () => {
29
- // @ts-expect-error Options are mandatory in TypeScript
30
- expect(() => buildClient({ apiKey: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
31
-
32
- // @ts-expect-error Options are mandatory in TypeScript
33
- expect(() => buildClient({ databaseURL: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
34
-
35
- // @ts-expect-error Options are mandatory in TypeScript
36
- expect(() => buildClient({ branch: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
37
- });
38
-
39
- test('throws if branch cannot be resolved', () => {
40
- const { users } = buildClient({ branch: () => null });
41
-
42
- expect(users.request('GET', '/foo')).rejects.toThrow('Unable to resolve branch value');
43
- });
44
-
45
- test('provide branch as a string', async () => {
46
- const { fetch, users } = buildClient({ branch: 'branch' });
47
-
48
- fetch.mockReset().mockImplementation(() => {
49
- return {
50
- ok: true,
51
- json: async () => ({})
52
- };
53
- });
54
-
55
- await users.request('GET', '/foo');
56
-
57
- expect(fetch).toHaveBeenCalledTimes(1);
58
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
59
- Array [
60
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
61
- Object {
62
- "body": undefined,
63
- "headers": Object {
64
- "Accept": "*/*",
65
- "Authorization": "Bearer 1234",
66
- "Content-Type": "application/json",
67
- },
68
- "method": "GET",
69
- },
70
- ]
71
- `);
72
- });
73
-
74
- test('provide branch as an array', async () => {
75
- const { fetch, users } = buildClient({
76
- branch: [process.env.NOT_DEFINED_VARIABLE, async () => null, 'branch', 'main']
77
- });
78
-
79
- fetch.mockReset().mockImplementation(() => {
80
- return {
81
- ok: true,
82
- json: async () => ({})
83
- };
84
- });
85
-
86
- await users.request('GET', '/foo');
87
-
88
- expect(fetch).toHaveBeenCalledTimes(1);
89
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
90
- Array [
91
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
92
- Object {
93
- "body": undefined,
94
- "headers": Object {
95
- "Accept": "*/*",
96
- "Authorization": "Bearer 1234",
97
- "Content-Type": "application/json",
98
- },
99
- "method": "GET",
100
- },
101
- ]
102
- `);
103
- });
104
-
105
- test('provide branch as a function', async () => {
106
- const { fetch, users } = buildClient({ branch: () => 'branch' });
107
-
108
- fetch.mockReset().mockImplementation(() => {
109
- return {
110
- ok: true,
111
- json: async () => ({})
112
- };
113
- });
114
-
115
- await users.request('GET', '/foo');
116
-
117
- expect(fetch).toHaveBeenCalledTimes(1);
118
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
119
- Array [
120
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
121
- Object {
122
- "body": undefined,
123
- "headers": Object {
124
- "Accept": "*/*",
125
- "Authorization": "Bearer 1234",
126
- "Content-Type": "application/json",
127
- },
128
- "method": "GET",
129
- },
130
- ]
131
- `);
132
- });
133
-
134
- test('ensure branch resolution is memoized', async () => {
135
- const branchGetter = jest.fn(() => 'branch');
136
-
137
- const { fetch, users } = buildClient({ branch: branchGetter });
138
-
139
- fetch.mockReset().mockImplementation(() => {
140
- return {
141
- ok: true,
142
- json: async () => ({})
143
- };
144
- });
145
-
146
- await users.request('GET', '/foo');
147
- await users.request('GET', '/foo');
148
-
149
- expect(branchGetter).toHaveBeenCalledTimes(1);
150
- });
151
- });
152
-
153
- describe('request', () => {
154
- test('builds the right arguments for a GET request', async () => {
155
- const { fetch, users } = buildClient();
156
-
157
- fetch.mockReset().mockImplementation(() => {
158
- return {
159
- ok: true,
160
- json: async () => ({})
161
- };
162
- });
163
-
164
- await users.request('GET', '/foo');
165
-
166
- expect(fetch).toHaveBeenCalledTimes(1);
167
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
168
- Array [
169
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/foo",
170
- Object {
171
- "body": undefined,
172
- "headers": Object {
173
- "Accept": "*/*",
174
- "Authorization": "Bearer 1234",
175
- "Content-Type": "application/json",
176
- },
177
- "method": "GET",
178
- },
179
- ]
180
- `);
181
- });
182
-
183
- test('builds the right arguments for a POST request', async () => {
184
- const { fetch, users } = buildClient();
185
-
186
- fetch.mockReset().mockImplementation(() => {
187
- return {
188
- ok: true,
189
- json: async () => ({})
190
- };
191
- });
192
-
193
- await users.request('POST', '/foo', { a: 1 });
194
-
195
- expect(fetch).toHaveBeenCalledTimes(1);
196
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
197
- Array [
198
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/foo",
199
- Object {
200
- "body": "{\\"a\\":1}",
201
- "headers": Object {
202
- "Accept": "*/*",
203
- "Authorization": "Bearer 1234",
204
- "Content-Type": "application/json",
205
- },
206
- "method": "POST",
207
- },
208
- ]
209
- `);
210
- });
211
-
212
- test('throws if the response is not ok', async () => {
213
- const { fetch, users } = buildClient();
214
-
215
- fetch.mockImplementation(() => {
216
- return {
217
- ok: false,
218
- status: 404,
219
- statusText: 'Not Found'
220
- };
221
- });
222
-
223
- expect(users.request('GET', '/foo')).rejects.toThrow(new XataError('Not Found', 404));
224
- });
225
-
226
- test('throws with the error from the server if the response is not ok', async () => {
227
- const { fetch, users } = buildClient();
228
-
229
- fetch.mockImplementation(() => {
230
- return {
231
- ok: false,
232
- status: 404,
233
- statusText: 'Not Found',
234
- json: async () => ({ message: 'Resource not found' })
235
- };
236
- });
237
-
238
- expect(users.request('GET', '/foo')).rejects.toThrow(new XataError('Resource not found', 404));
239
- });
240
-
241
- test('returns the json body if the response is ok', async () => {
242
- const { fetch, users } = buildClient();
243
-
244
- const json = { a: 1 };
245
- fetch.mockImplementation(() => {
246
- return {
247
- ok: true,
248
- json: async () => json
249
- };
250
- });
251
-
252
- const result = await users.request('GET', '/foo');
253
- expect(result).toEqual(json);
254
- });
255
- });
256
-
257
- type ExpectedRequest = {
258
- method: string;
259
- path: string;
260
- body: unknown;
261
- };
262
-
263
- async function expectRequest(
264
- users: RestRepository<User>,
265
- expectedRequest: ExpectedRequest,
266
- callback: () => void,
267
- response?: unknown
268
- ) {
269
- const request = jest.fn(async () => response);
270
- users.request = request;
271
-
272
- await callback();
273
-
274
- const { calls } = request.mock;
275
- expect(calls.length).toBe(1);
276
- const [method, path, body] = calls[0] as any;
277
- expect(method).toBe(expectedRequest.method);
278
- expect(path).toBe(expectedRequest.path);
279
- expect(JSON.stringify(body)).toBe(JSON.stringify(expectedRequest.body));
280
- }
281
-
282
- describe('query', () => {
283
- describe('getMany', () => {
284
- test('simple query', async () => {
285
- const { users } = buildClient();
286
-
287
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
288
- expectRequest(users, expected, () => users.getMany(), { records: [] });
289
- });
290
-
291
- test('query with one filter', async () => {
292
- const { users } = buildClient();
293
-
294
- const expected = { method: 'POST', path: '/tables/users/query', body: { filter: { $all: [{ name: 'foo' }] } } };
295
- expectRequest(users, expected, () => users.filter('name', 'foo').getMany(), { records: [] });
296
- });
297
- });
298
-
299
- describe('getOne', () => {
300
- test('returns a single object', async () => {
301
- const { users } = buildClient();
302
-
303
- const result = { records: [{ id: '1234' }] };
304
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
305
- expectRequest(
306
- users,
307
- expected,
308
- async () => {
309
- const first = await users.select().getOne();
310
- expect(first?.id).toBe(result.records[0].id);
311
- },
312
- result
313
- );
314
- });
315
-
316
- test('returns null if no objects are returned', async () => {
317
- const { users } = buildClient();
318
-
319
- const result = { records: [] };
320
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
321
- expectRequest(
322
- users,
323
- expected,
324
- async () => {
325
- const first = await users.getOne();
326
- expect(first).toBeNull();
327
- },
328
- result
329
- );
330
- });
331
- });
332
- });
333
-
334
- describe('read', () => {
335
- test('reads an object by id successfully', async () => {
336
- const { users } = buildClient();
337
-
338
- const id = 'rec_1234';
339
- const expected = { method: 'GET', path: `/tables/users/data/${id}`, body: undefined };
340
- expectRequest(users, expected, () => users.read(id));
341
- });
342
- });
343
-
344
- describe('Repository.update', () => {
345
- test('updates and object successfully', async () => {
346
- const { users } = buildClient();
347
-
348
- const object = { id: 'rec_1234', xata: { version: 1 }, name: 'Ada' } as User;
349
- const expected = { method: 'PUT', path: `/tables/users/data/${object.id}`, body: object };
350
- expectRequest(
351
- users,
352
- expected,
353
- async () => {
354
- const result = await users.update(object.id, object);
355
- expect(result.id).toBe(object.id);
356
- },
357
- { id: object.id }
358
- );
359
- });
360
- });
361
-
362
- describe('Repository.delete', () => {
363
- test('deletes a record by id successfully', async () => {
364
- const { users } = buildClient();
365
-
366
- const id = 'rec_1234';
367
- const expected = { method: 'DELETE', path: `/tables/users/data/${id}`, body: undefined };
368
- expectRequest(users, expected, async () => {
369
- const result = await users.delete(id);
370
- expect(result).toBe(undefined);
371
- });
372
- });
373
- });
374
-
375
- describe('create', () => {
376
- test('successful', async () => {
377
- const { users } = buildClient();
378
-
379
- const created = { id: 'rec_1234', _version: 0 };
380
- const object = { name: 'Ada' } as User;
381
- const expected = { method: 'POST', path: '/tables/users/data', body: object };
382
- expectRequest(
383
- users,
384
- expected,
385
- async () => {
386
- const result = await users.create(object);
387
- expect(result.id).toBe(created.id);
388
- },
389
- created
390
- );
391
- });
392
- });