@myco-dev/sdk 0.1.1 → 0.1.3

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 CHANGED
@@ -1,9 +1,8 @@
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, E as Entity, P as PaginatedResponse, b as EntityRecord } from './types-D2J6kXGR.js';
4
- export { c as EntityField, J as JoinResponse, V as VerifiedUser } from './types-D2J6kXGR.js';
5
- import * as swr from 'swr';
6
- import { SWRResponse } from 'swr';
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';
5
+ import { UseQueryResult } from '@tanstack/react-query';
7
6
 
8
7
  /**
9
8
  * Create auth namespace bound to the given config.
@@ -140,31 +139,30 @@ 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;
146
150
  }
147
- interface UseRecordsResult<T> extends SWRResponse<T[]> {
151
+ type UseRecordsResult<T> = UseQueryResult<T[]> & {
148
152
  page: number;
149
153
  hasNextPage: boolean;
150
154
  hasPreviousPage: boolean;
151
155
  nextPage: () => void;
152
156
  previousPage: () => void;
153
157
  setPage: (page: number) => void;
154
- }
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
158
  };
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
- useRecord<K extends keyof TEntities & string>(entitySlug: K | null | undefined, recordId: string | null | undefined): swr.SWRResponse<TypedEntityRecord<TEntities[K]>, any, any>;
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, EntityRecord, GetRecordsOptions, MycoSDKConfig, type MycoSDKInstance, type MycoSDKOptions, PaginatedResponse, type TypedEntityRecord, Workspace, WorkspaceMember, createMycoSDK };
249
+ export { ApiError, Entity, GetRecordsOptions, MycoSDKConfig, type MycoSDKInstance, type MycoSDKOptions, PaginatedResponse, type TypedEntityRecord, Workspace, WorkspaceMember, createMycoSDK };
package/dist/index.js CHANGED
@@ -193,86 +193,49 @@ function createWorkspaces(getConfig, setWorkspaceId, apiRequest, ensureWorkspace
193
193
  };
194
194
  }
195
195
 
196
- // src/hooks/records.ts
196
+ // src/data.ts
197
197
  import { useState, useCallback } from "react";
198
- import useSWR from "swr";
199
- function createRecordsHooks(apiRequest) {
200
- return {
201
- useRecords(entitySlug, options) {
202
- const { initialPage = 1, onPageChange, ...queryOptions } = options ?? {};
203
- const [page, setPageState] = useState(initialPage);
204
- const [hasNextPage, setHasNextPage] = useState(false);
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 swrResult = useSWR(
224
- entitySlug ? ["records", entitySlug, page, queryOptions] : null,
225
- 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
- );
247
- return {
248
- ...swrResult,
249
- page,
250
- hasNextPage,
251
- hasPreviousPage: page > 1,
252
- nextPage,
253
- previousPage,
254
- setPage
255
- };
256
- },
257
- useRecord(entitySlug, recordId) {
258
- return useSWR(
259
- entitySlug && recordId ? ["record", entitySlug, recordId] : null,
260
- async () => {
261
- const response = await apiRequest(
262
- `/api/entities/${entitySlug}/records/${recordId}`
263
- );
264
- return response;
265
- }
266
- );
267
- }
268
- };
198
+ import { useQuery } from "@tanstack/react-query";
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;
269
205
  }
270
-
271
- // src/data.ts
272
206
  function createData(apiRequest) {
273
- const hooks = createRecordsHooks(apiRequest);
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
+ };
274
236
  return {
275
- ...hooks,
237
+ getRecords,
238
+ getRecord,
276
239
  /**
277
240
  * Get all entities for the current app.
278
241
  */
@@ -285,53 +248,25 @@ function createData(apiRequest) {
285
248
  async getEntity(entitySlug) {
286
249
  return apiRequest(`/api/entities/${entitySlug}`);
287
250
  },
288
- /**
289
- * Get records for an entity.
290
- */
291
- async getRecords(entitySlug, options) {
292
- const params = new URLSearchParams();
293
- if (options?.page !== void 0) {
294
- params.set("page", String(options.page));
295
- }
296
- if (options?.pageSize !== void 0) {
297
- params.set("pageSize", String(options.pageSize));
298
- }
299
- if (options?.sort) {
300
- params.set("sort", options.sort);
301
- }
302
- if (options?.order) {
303
- params.set("order", options.order);
304
- }
305
- if (options?.filter) {
306
- params.set("filter", JSON.stringify(options.filter));
307
- }
308
- const queryString = params.toString();
309
- const path = `/api/entities/${entitySlug}/records${queryString ? `?${queryString}` : ""}`;
310
- return apiRequest(path);
311
- },
312
- /**
313
- * Get a single record by ID.
314
- */
315
- async getRecord(entitySlug, recordId) {
316
- return apiRequest(`/api/entities/${entitySlug}/records/${recordId}`);
317
- },
318
251
  /**
319
252
  * Create a new record.
320
253
  */
321
254
  async createRecord(entitySlug, data) {
322
- return apiRequest(`/api/entities/${entitySlug}/records`, {
255
+ const raw = await apiRequest(`/api/entities/${entitySlug}/records`, {
323
256
  method: "POST",
324
257
  body: JSON.stringify({ data })
325
258
  });
259
+ return transformRecord(raw);
326
260
  },
327
261
  /**
328
262
  * Update an existing record.
329
263
  */
330
264
  async updateRecord(entitySlug, recordId, data) {
331
- return apiRequest(`/api/entities/${entitySlug}/records/${recordId}`, {
265
+ const raw = await apiRequest(`/api/entities/${entitySlug}/records/${recordId}`, {
332
266
  method: "PATCH",
333
267
  body: JSON.stringify({ data })
334
268
  });
269
+ return transformRecord(raw);
335
270
  },
336
271
  /**
337
272
  * Delete a record.
@@ -352,7 +287,61 @@ function createData(apiRequest) {
352
287
  body: JSON.stringify({ ids })
353
288
  }
354
289
  );
355
- 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
+ });
356
345
  }
357
346
  };
358
347
  }
package/dist/react.d.ts CHANGED
@@ -3,8 +3,8 @@ 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-D2J6kXGR.js';
7
- import 'swr';
6
+ import './types-75pXTdeu.js';
7
+ import '@tanstack/react-query';
8
8
 
9
9
  interface AuthContextValue {
10
10
  isLoading: boolean;
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, b as EntityRecord, S as ServerEnv } from './types-D2J6kXGR.js';
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myco-dev/sdk",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -29,12 +29,13 @@
29
29
  "peerDependencies": {
30
30
  "react": "^18 || ^19",
31
31
  "better-auth": "^1.4",
32
- "swr": "^2.4.0"
32
+ "@tanstack/react-query": "^5.0.0"
33
33
  },
34
34
  "dependencies": {
35
35
  "jose": "^5.0.0"
36
36
  },
37
37
  "devDependencies": {
38
+ "@tanstack/react-query": "^5.0.0",
38
39
  "@types/react": "^19.0.0",
39
40
  "tsup": "^8.0.0",
40
41
  "typescript": "^5.0.0"