@myco-dev/sdk 0.1.2 → 0.1.4
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/dist/index.d.ts +17 -21
- package/dist/index.js +97 -110
- package/dist/react.d.ts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/types-75pXTdeu.d.ts +86 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as better_auth from 'better-auth';
|
|
2
2
|
import * as _better_fetch_fetch from '@better-fetch/fetch';
|
|
3
|
-
import { M as MycoSDKConfig, W as Workspace, a as WorkspaceMember, G as GetRecordsOptions,
|
|
4
|
-
export {
|
|
5
|
-
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
3
|
+
import { M as MycoSDKConfig, W as Workspace, a as WorkspaceMember, G as GetRecordsOptions, P as PaginatedResponse, F as FlattenedRecord, E as Entity } from './types-75pXTdeu.js';
|
|
4
|
+
export { b as EntityField, c as EntityRecord, J as JoinResponse, V as VerifiedUser } from './types-75pXTdeu.js';
|
|
6
5
|
import { UseQueryResult } from '@tanstack/react-query';
|
|
7
6
|
|
|
8
7
|
/**
|
|
@@ -140,6 +139,11 @@ declare function createWorkspaces(getConfig: () => MycoSDKConfig, setWorkspaceId
|
|
|
140
139
|
removeMember(userId: string): Promise<void>;
|
|
141
140
|
};
|
|
142
141
|
|
|
142
|
+
type ApiRequest = <T>(path: string, options?: RequestInit) => Promise<T>;
|
|
143
|
+
/**
|
|
144
|
+
* Helper type - flattened record with data at top level.
|
|
145
|
+
*/
|
|
146
|
+
type TypedEntityRecord<T> = FlattenedRecord<T>;
|
|
143
147
|
interface UseRecordsOptions extends Omit<GetRecordsOptions, "page"> {
|
|
144
148
|
initialPage?: number;
|
|
145
149
|
onPageChange?: (page: number) => void;
|
|
@@ -152,19 +156,13 @@ type UseRecordsResult<T> = UseQueryResult<T[]> & {
|
|
|
152
156
|
previousPage: () => void;
|
|
153
157
|
setPage: (page: number) => void;
|
|
154
158
|
};
|
|
155
|
-
|
|
156
|
-
type ApiRequest = <T>(path: string, options?: RequestInit) => Promise<T>;
|
|
157
|
-
/**
|
|
158
|
-
* Helper type that wraps entity data with record metadata.
|
|
159
|
-
*/
|
|
160
|
-
type TypedEntityRecord<T> = Omit<EntityRecord, "data"> & {
|
|
161
|
-
data: T;
|
|
162
|
-
};
|
|
163
159
|
/**
|
|
164
160
|
* Create data namespace bound to the given API client.
|
|
165
161
|
* Accepts a generic type parameter for entity type mapping.
|
|
166
162
|
*/
|
|
167
163
|
declare function createData<TEntities extends Record<string, unknown>>(apiRequest: ApiRequest): {
|
|
164
|
+
getRecords: <K extends keyof TEntities & string>(entitySlug: K, options?: GetRecordsOptions) => Promise<PaginatedResponse<TypedEntityRecord<TEntities[K]>>>;
|
|
165
|
+
getRecord: <K extends keyof TEntities & string>(entitySlug: K, recordId: string) => Promise<TypedEntityRecord<TEntities[K]>>;
|
|
168
166
|
/**
|
|
169
167
|
* Get all entities for the current app.
|
|
170
168
|
*/
|
|
@@ -173,14 +171,6 @@ declare function createData<TEntities extends Record<string, unknown>>(apiReques
|
|
|
173
171
|
* Get a single entity by slug.
|
|
174
172
|
*/
|
|
175
173
|
getEntity<K extends keyof TEntities & string>(entitySlug: K): Promise<Entity>;
|
|
176
|
-
/**
|
|
177
|
-
* Get records for an entity.
|
|
178
|
-
*/
|
|
179
|
-
getRecords<K extends keyof TEntities & string>(entitySlug: K, options?: GetRecordsOptions): Promise<PaginatedResponse<TypedEntityRecord<TEntities[K]>>>;
|
|
180
|
-
/**
|
|
181
|
-
* Get a single record by ID.
|
|
182
|
-
*/
|
|
183
|
-
getRecord<K extends keyof TEntities & string>(entitySlug: K, recordId: string): Promise<TypedEntityRecord<TEntities[K]>>;
|
|
184
174
|
/**
|
|
185
175
|
* Create a new record.
|
|
186
176
|
*/
|
|
@@ -197,8 +187,14 @@ declare function createData<TEntities extends Record<string, unknown>>(apiReques
|
|
|
197
187
|
* Batch get multiple records by IDs.
|
|
198
188
|
*/
|
|
199
189
|
batchGetRecords<K extends keyof TEntities & string>(entitySlug: K, ids: string[]): Promise<TypedEntityRecord<TEntities[K]>[]>;
|
|
190
|
+
/**
|
|
191
|
+
* React hook to fetch records with pagination.
|
|
192
|
+
*/
|
|
200
193
|
useRecords<K extends keyof TEntities & string>(entitySlug: K | null | undefined, options?: UseRecordsOptions): UseRecordsResult<TypedEntityRecord<TEntities[K]>>;
|
|
201
|
-
|
|
194
|
+
/**
|
|
195
|
+
* React hook to fetch a single record.
|
|
196
|
+
*/
|
|
197
|
+
useRecord<K extends keyof TEntities & string>(entitySlug: K | null | undefined, recordId: string | null | undefined): UseQueryResult<TypedEntityRecord<TEntities[K]>>;
|
|
202
198
|
};
|
|
203
199
|
type Data<TEntities extends Record<string, unknown>> = ReturnType<typeof createData<TEntities>>;
|
|
204
200
|
|
|
@@ -250,4 +246,4 @@ interface MycoSDKInstance<TEntities extends Record<string, unknown>> {
|
|
|
250
246
|
*/
|
|
251
247
|
declare function createMycoSDK<TEntities extends Record<string, unknown>>(appKey: string, options?: MycoSDKOptions): MycoSDKInstance<TEntities>;
|
|
252
248
|
|
|
253
|
-
export { ApiError, Entity,
|
|
249
|
+
export { ApiError, Entity, FlattenedRecord, GetRecordsOptions, MycoSDKConfig, type MycoSDKInstance, type MycoSDKOptions, PaginatedResponse, type TypedEntityRecord, Workspace, WorkspaceMember, createMycoSDK };
|
package/dist/index.js
CHANGED
|
@@ -193,88 +193,49 @@ function createWorkspaces(getConfig, setWorkspaceId, apiRequest, ensureWorkspace
|
|
|
193
193
|
};
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
// src/
|
|
196
|
+
// src/data.ts
|
|
197
197
|
import { useState, useCallback } from "react";
|
|
198
198
|
import { useQuery } from "@tanstack/react-query";
|
|
199
|
-
function
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const setPage = useCallback(
|
|
206
|
-
(newPage) => {
|
|
207
|
-
if (newPage < 1) return;
|
|
208
|
-
setPageState(newPage);
|
|
209
|
-
onPageChange?.(newPage);
|
|
210
|
-
},
|
|
211
|
-
[onPageChange]
|
|
212
|
-
);
|
|
213
|
-
const nextPage = useCallback(() => {
|
|
214
|
-
if (hasNextPage) {
|
|
215
|
-
setPage(page + 1);
|
|
216
|
-
}
|
|
217
|
-
}, [hasNextPage, page, setPage]);
|
|
218
|
-
const previousPage = useCallback(() => {
|
|
219
|
-
if (page > 1) {
|
|
220
|
-
setPage(page - 1);
|
|
221
|
-
}
|
|
222
|
-
}, [page, setPage]);
|
|
223
|
-
const queryResult = useQuery({
|
|
224
|
-
queryKey: entitySlug ? ["records", entitySlug, page, queryOptions] : [],
|
|
225
|
-
queryFn: async () => {
|
|
226
|
-
const params = new URLSearchParams();
|
|
227
|
-
params.set("page", String(page));
|
|
228
|
-
if (queryOptions?.pageSize !== void 0) {
|
|
229
|
-
params.set("pageSize", String(queryOptions.pageSize));
|
|
230
|
-
}
|
|
231
|
-
if (queryOptions?.sort) {
|
|
232
|
-
params.set("sort", queryOptions.sort);
|
|
233
|
-
}
|
|
234
|
-
if (queryOptions?.order) {
|
|
235
|
-
params.set("order", queryOptions.order);
|
|
236
|
-
}
|
|
237
|
-
if (queryOptions?.filter) {
|
|
238
|
-
params.set("filter", JSON.stringify(queryOptions.filter));
|
|
239
|
-
}
|
|
240
|
-
const queryString = params.toString();
|
|
241
|
-
const path = `/api/entities/${entitySlug}/records?${queryString}`;
|
|
242
|
-
const response = await apiRequest(path);
|
|
243
|
-
setHasNextPage(response.pagination.hasMore);
|
|
244
|
-
return response.records;
|
|
245
|
-
},
|
|
246
|
-
enabled: !!entitySlug
|
|
247
|
-
});
|
|
248
|
-
return {
|
|
249
|
-
...queryResult,
|
|
250
|
-
page,
|
|
251
|
-
hasNextPage,
|
|
252
|
-
hasPreviousPage: page > 1,
|
|
253
|
-
nextPage,
|
|
254
|
-
previousPage,
|
|
255
|
-
setPage
|
|
256
|
-
};
|
|
257
|
-
},
|
|
258
|
-
useRecord(entitySlug, recordId) {
|
|
259
|
-
return useQuery({
|
|
260
|
-
queryKey: entitySlug && recordId ? ["record", entitySlug, recordId] : [],
|
|
261
|
-
queryFn: async () => {
|
|
262
|
-
const response = await apiRequest(
|
|
263
|
-
`/api/entities/${entitySlug}/records/${recordId}`
|
|
264
|
-
);
|
|
265
|
-
return response;
|
|
266
|
-
},
|
|
267
|
-
enabled: !!entitySlug && !!recordId
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
};
|
|
199
|
+
function transformRecord(raw) {
|
|
200
|
+
const record = raw.data;
|
|
201
|
+
record._id = raw.id;
|
|
202
|
+
record._createdAt = raw.createdAt;
|
|
203
|
+
record._updatedAt = raw.updatedAt;
|
|
204
|
+
return record;
|
|
271
205
|
}
|
|
272
|
-
|
|
273
|
-
// src/data.ts
|
|
274
206
|
function createData(apiRequest) {
|
|
275
|
-
const
|
|
207
|
+
const getRecords = async (entitySlug, options) => {
|
|
208
|
+
const params = new URLSearchParams();
|
|
209
|
+
if (options?.page !== void 0) {
|
|
210
|
+
params.set("page", String(options.page));
|
|
211
|
+
}
|
|
212
|
+
if (options?.pageSize !== void 0) {
|
|
213
|
+
params.set("pageSize", String(options.pageSize));
|
|
214
|
+
}
|
|
215
|
+
if (options?.sort) {
|
|
216
|
+
params.set("sort", options.sort);
|
|
217
|
+
}
|
|
218
|
+
if (options?.order) {
|
|
219
|
+
params.set("order", options.order);
|
|
220
|
+
}
|
|
221
|
+
if (options?.filter) {
|
|
222
|
+
params.set("filter", JSON.stringify(options.filter));
|
|
223
|
+
}
|
|
224
|
+
const queryString = params.toString();
|
|
225
|
+
const path = `/api/entities/${entitySlug}/records${queryString ? `?${queryString}` : ""}`;
|
|
226
|
+
const response = await apiRequest(path);
|
|
227
|
+
return {
|
|
228
|
+
...response,
|
|
229
|
+
records: response.records.map(transformRecord)
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
const getRecord = async (entitySlug, recordId) => {
|
|
233
|
+
const raw = await apiRequest(`/api/entities/${entitySlug}/records/${recordId}`);
|
|
234
|
+
return transformRecord(raw);
|
|
235
|
+
};
|
|
276
236
|
return {
|
|
277
|
-
|
|
237
|
+
getRecords,
|
|
238
|
+
getRecord,
|
|
278
239
|
/**
|
|
279
240
|
* Get all entities for the current app.
|
|
280
241
|
*/
|
|
@@ -287,53 +248,25 @@ function createData(apiRequest) {
|
|
|
287
248
|
async getEntity(entitySlug) {
|
|
288
249
|
return apiRequest(`/api/entities/${entitySlug}`);
|
|
289
250
|
},
|
|
290
|
-
/**
|
|
291
|
-
* Get records for an entity.
|
|
292
|
-
*/
|
|
293
|
-
async getRecords(entitySlug, options) {
|
|
294
|
-
const params = new URLSearchParams();
|
|
295
|
-
if (options?.page !== void 0) {
|
|
296
|
-
params.set("page", String(options.page));
|
|
297
|
-
}
|
|
298
|
-
if (options?.pageSize !== void 0) {
|
|
299
|
-
params.set("pageSize", String(options.pageSize));
|
|
300
|
-
}
|
|
301
|
-
if (options?.sort) {
|
|
302
|
-
params.set("sort", options.sort);
|
|
303
|
-
}
|
|
304
|
-
if (options?.order) {
|
|
305
|
-
params.set("order", options.order);
|
|
306
|
-
}
|
|
307
|
-
if (options?.filter) {
|
|
308
|
-
params.set("filter", JSON.stringify(options.filter));
|
|
309
|
-
}
|
|
310
|
-
const queryString = params.toString();
|
|
311
|
-
const path = `/api/entities/${entitySlug}/records${queryString ? `?${queryString}` : ""}`;
|
|
312
|
-
return apiRequest(path);
|
|
313
|
-
},
|
|
314
|
-
/**
|
|
315
|
-
* Get a single record by ID.
|
|
316
|
-
*/
|
|
317
|
-
async getRecord(entitySlug, recordId) {
|
|
318
|
-
return apiRequest(`/api/entities/${entitySlug}/records/${recordId}`);
|
|
319
|
-
},
|
|
320
251
|
/**
|
|
321
252
|
* Create a new record.
|
|
322
253
|
*/
|
|
323
254
|
async createRecord(entitySlug, data) {
|
|
324
|
-
|
|
255
|
+
const raw = await apiRequest(`/api/entities/${entitySlug}/records`, {
|
|
325
256
|
method: "POST",
|
|
326
257
|
body: JSON.stringify({ data })
|
|
327
258
|
});
|
|
259
|
+
return transformRecord(raw);
|
|
328
260
|
},
|
|
329
261
|
/**
|
|
330
262
|
* Update an existing record.
|
|
331
263
|
*/
|
|
332
264
|
async updateRecord(entitySlug, recordId, data) {
|
|
333
|
-
|
|
265
|
+
const raw = await apiRequest(`/api/entities/${entitySlug}/records/${recordId}`, {
|
|
334
266
|
method: "PATCH",
|
|
335
267
|
body: JSON.stringify({ data })
|
|
336
268
|
});
|
|
269
|
+
return transformRecord(raw);
|
|
337
270
|
},
|
|
338
271
|
/**
|
|
339
272
|
* Delete a record.
|
|
@@ -354,7 +287,61 @@ function createData(apiRequest) {
|
|
|
354
287
|
body: JSON.stringify({ ids })
|
|
355
288
|
}
|
|
356
289
|
);
|
|
357
|
-
return response.records;
|
|
290
|
+
return response.records.map(transformRecord);
|
|
291
|
+
},
|
|
292
|
+
/**
|
|
293
|
+
* React hook to fetch records with pagination.
|
|
294
|
+
*/
|
|
295
|
+
useRecords(entitySlug, options) {
|
|
296
|
+
const { initialPage = 1, onPageChange, ...queryOptions } = options ?? {};
|
|
297
|
+
const [page, setPageState] = useState(initialPage);
|
|
298
|
+
const [hasNextPage, setHasNextPage] = useState(false);
|
|
299
|
+
const setPage = useCallback(
|
|
300
|
+
(newPage) => {
|
|
301
|
+
if (newPage < 1) return;
|
|
302
|
+
setPageState(newPage);
|
|
303
|
+
onPageChange?.(newPage);
|
|
304
|
+
},
|
|
305
|
+
[onPageChange]
|
|
306
|
+
);
|
|
307
|
+
const nextPage = useCallback(() => {
|
|
308
|
+
if (hasNextPage) {
|
|
309
|
+
setPage(page + 1);
|
|
310
|
+
}
|
|
311
|
+
}, [hasNextPage, page, setPage]);
|
|
312
|
+
const previousPage = useCallback(() => {
|
|
313
|
+
if (page > 1) {
|
|
314
|
+
setPage(page - 1);
|
|
315
|
+
}
|
|
316
|
+
}, [page, setPage]);
|
|
317
|
+
const queryResult = useQuery({
|
|
318
|
+
queryKey: entitySlug ? ["records", entitySlug, page, queryOptions] : [],
|
|
319
|
+
queryFn: async () => {
|
|
320
|
+
const response = await getRecords(entitySlug, { ...queryOptions, page });
|
|
321
|
+
setHasNextPage(response.pagination.hasMore);
|
|
322
|
+
return response.records;
|
|
323
|
+
},
|
|
324
|
+
enabled: !!entitySlug
|
|
325
|
+
});
|
|
326
|
+
return {
|
|
327
|
+
...queryResult,
|
|
328
|
+
page,
|
|
329
|
+
hasNextPage,
|
|
330
|
+
hasPreviousPage: page > 1,
|
|
331
|
+
nextPage,
|
|
332
|
+
previousPage,
|
|
333
|
+
setPage
|
|
334
|
+
};
|
|
335
|
+
},
|
|
336
|
+
/**
|
|
337
|
+
* React hook to fetch a single record.
|
|
338
|
+
*/
|
|
339
|
+
useRecord(entitySlug, recordId) {
|
|
340
|
+
return useQuery({
|
|
341
|
+
queryKey: entitySlug && recordId ? ["record", entitySlug, recordId] : [],
|
|
342
|
+
queryFn: () => getRecord(entitySlug, recordId),
|
|
343
|
+
enabled: !!entitySlug && !!recordId
|
|
344
|
+
});
|
|
358
345
|
}
|
|
359
346
|
};
|
|
360
347
|
}
|
package/dist/react.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ReactNode } from 'react';
|
|
|
3
3
|
import { MycoSDKInstance } from './index.js';
|
|
4
4
|
import 'better-auth';
|
|
5
5
|
import '@better-fetch/fetch';
|
|
6
|
-
import './types-
|
|
6
|
+
import './types-75pXTdeu.js';
|
|
7
7
|
import '@tanstack/react-query';
|
|
8
8
|
|
|
9
9
|
interface AuthContextValue {
|
package/dist/server.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { V as VerifiedUser, W as Workspace, a as WorkspaceMember, E as Entity, G as GetRecordsOptions, P as PaginatedResponse,
|
|
1
|
+
import { V as VerifiedUser, W as Workspace, a as WorkspaceMember, E as Entity, G as GetRecordsOptions, P as PaginatedResponse, c as EntityRecord, S as ServerEnv } from './types-75pXTdeu.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Server-side SDK instance
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
interface JoinResponse {
|
|
2
|
+
success: boolean;
|
|
3
|
+
message?: string;
|
|
4
|
+
workspaceId?: string;
|
|
5
|
+
}
|
|
6
|
+
interface VerifiedUser {
|
|
7
|
+
userId: string;
|
|
8
|
+
email?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
}
|
|
11
|
+
interface Workspace {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
slug?: string;
|
|
15
|
+
createdAt: string;
|
|
16
|
+
updatedAt: string;
|
|
17
|
+
}
|
|
18
|
+
interface WorkspaceMember {
|
|
19
|
+
id: string;
|
|
20
|
+
userId: string;
|
|
21
|
+
workspaceId: string;
|
|
22
|
+
role: "owner" | "admin" | "member";
|
|
23
|
+
user?: {
|
|
24
|
+
id: string;
|
|
25
|
+
email: string;
|
|
26
|
+
name: string;
|
|
27
|
+
};
|
|
28
|
+
createdAt: string;
|
|
29
|
+
}
|
|
30
|
+
interface Entity {
|
|
31
|
+
id: string;
|
|
32
|
+
slug: string;
|
|
33
|
+
name: string;
|
|
34
|
+
description?: string;
|
|
35
|
+
fields: EntityField[];
|
|
36
|
+
}
|
|
37
|
+
interface EntityField {
|
|
38
|
+
id: string;
|
|
39
|
+
name: string;
|
|
40
|
+
slug: string;
|
|
41
|
+
type: string;
|
|
42
|
+
required?: boolean;
|
|
43
|
+
options?: Record<string, unknown>;
|
|
44
|
+
}
|
|
45
|
+
interface EntityRecord {
|
|
46
|
+
id: string;
|
|
47
|
+
entityId: string;
|
|
48
|
+
workspaceId: string;
|
|
49
|
+
data: Record<string, unknown>;
|
|
50
|
+
createdAt: string;
|
|
51
|
+
updatedAt: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Flattened record type with data spread at top level and underscore-prefixed metadata.
|
|
55
|
+
*/
|
|
56
|
+
type FlattenedRecord<T> = T & {
|
|
57
|
+
_id: string;
|
|
58
|
+
_createdAt: string;
|
|
59
|
+
_updatedAt: string;
|
|
60
|
+
};
|
|
61
|
+
interface PaginatedResponse<T> {
|
|
62
|
+
records: T[];
|
|
63
|
+
pagination: {
|
|
64
|
+
total: number;
|
|
65
|
+
limit: number;
|
|
66
|
+
offset: number;
|
|
67
|
+
hasMore: boolean;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
interface GetRecordsOptions {
|
|
71
|
+
page?: number;
|
|
72
|
+
pageSize?: number;
|
|
73
|
+
sort?: string;
|
|
74
|
+
order?: "asc" | "desc";
|
|
75
|
+
filter?: Record<string, unknown>;
|
|
76
|
+
}
|
|
77
|
+
interface MycoSDKConfig {
|
|
78
|
+
appKey: string;
|
|
79
|
+
workspaceId?: string;
|
|
80
|
+
baseUrl?: string;
|
|
81
|
+
}
|
|
82
|
+
interface ServerEnv {
|
|
83
|
+
MYCO_APP_KEY: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type { Entity as E, FlattenedRecord as F, GetRecordsOptions as G, JoinResponse as J, MycoSDKConfig as M, PaginatedResponse as P, ServerEnv as S, VerifiedUser as V, Workspace as W, WorkspaceMember as a, EntityField as b, EntityRecord as c };
|