prostgles-client 4.0.421 → 4.0.424

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 (35) hide show
  1. package/dist/SyncedTable/SyncedTable.d.ts +4 -2
  2. package/dist/SyncedTable/SyncedTable.d.ts.map +1 -1
  3. package/dist/SyncedTable/createSync.d.ts +3 -6
  4. package/dist/SyncedTable/createSync.d.ts.map +1 -1
  5. package/dist/SyncedTable/createSync.js +83 -37
  6. package/dist/SyncedTable/createSyncDataStore.d.ts.map +1 -1
  7. package/dist/SyncedTable/createSyncDataStore.js +4 -2
  8. package/dist/SyncedTable/createSyncStateUtils.d.ts +8 -2
  9. package/dist/SyncedTable/createSyncStateUtils.d.ts.map +1 -1
  10. package/dist/SyncedTable/createSyncStateUtils.js +11 -12
  11. package/dist/SyncedTable/createSyncSubscriptionManager.d.ts +7 -5
  12. package/dist/SyncedTable/createSyncSubscriptionManager.d.ts.map +1 -1
  13. package/dist/SyncedTable/createSyncSubscriptionManager.js +45 -15
  14. package/dist/auth/authRequest.d.ts +7 -0
  15. package/dist/auth/authRequest.d.ts.map +1 -0
  16. package/dist/auth/authRequest.js +51 -0
  17. package/dist/auth/getAuthCalls.d.ts +7 -0
  18. package/dist/auth/getAuthCalls.d.ts.map +1 -0
  19. package/dist/auth/getAuthCalls.js +19 -0
  20. package/dist/auth/useAuthState.d.ts +340 -52
  21. package/dist/auth/useAuthState.d.ts.map +1 -1
  22. package/dist/auth/useAuthState.js +4 -1
  23. package/dist/getAuthHandler.d.ts +11 -15
  24. package/dist/getAuthHandler.d.ts.map +1 -1
  25. package/dist/getAuthHandler.js +14 -52
  26. package/dist/getDbHandler.d.ts +2 -2
  27. package/dist/getDbHandler.d.ts.map +1 -1
  28. package/dist/getDbHandler.js +4 -4
  29. package/dist/{getSyncHandlerV2.d.ts → getSyncHandler.d.ts} +4 -4
  30. package/dist/getSyncHandler.d.ts.map +1 -0
  31. package/dist/{getSyncHandlerV2.js → getSyncHandler.js} +18 -10
  32. package/dist/index.umd.js +1 -1
  33. package/dist/prostgles.js +3 -9
  34. package/package.json +5 -5
  35. package/dist/getSyncHandlerV2.d.ts.map +0 -1
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.authRequest = exports.getAuthHandler = void 0;
3
+ exports.getAuthHandler = exports.authRequest = void 0;
4
4
  const prostgles_types_1 = require("prostgles-types");
5
5
  const prostgles_1 = require("./prostgles");
6
+ const getAuthCalls_1 = require("./auth/getAuthCalls");
7
+ var authRequest_1 = require("./auth/authRequest");
8
+ Object.defineProperty(exports, "authRequest", { enumerable: true, get: function () { return authRequest_1.authRequest; } });
6
9
  const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...authOpts }) => {
7
10
  const urlWithEndpointAndSearch = (route) => {
8
11
  const search = typeof window !== "undefined" ? window.location.search : "";
@@ -11,6 +14,12 @@ const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...a
11
14
  url = `${endpoint}${url}`;
12
15
  return url;
13
16
  };
17
+ const authCalls = (0, getAuthCalls_1.getAuthCalls)({
18
+ login: authConfig?.login?.loginRoute,
19
+ register: authConfig?.signupWithEmailAndPassword?.url,
20
+ magicLinkOrOTP: authConfig?.signupWithEmailAndPassword?.emailConfirmationRoute,
21
+ logout: authConfig?.login?.logoutRoute,
22
+ }, urlWithEndpointAndSearch, authOpts);
14
23
  if (authConfig?.pathGuard && prostgles_1.isClientSide) {
15
24
  const doReload = (res) => {
16
25
  if (res?.shouldReload) {
@@ -51,21 +60,10 @@ const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...a
51
60
  };
52
61
  return acc;
53
62
  }, {}));
54
- loginSignupOptions.login =
55
- login &&
56
- (async (params) => {
57
- return (0, exports.authRequest)(urlWithEndpointAndSearch(login.loginRoute), params, "POST", authOpts);
58
- });
63
+ loginSignupOptions.login = login && authCalls.login;
59
64
  loginSignupOptions.signupWithEmailAndPassword =
60
- signupWithEmailAndPassword &&
61
- ((params) => {
62
- return (0, exports.authRequest)(urlWithEndpointAndSearch(signupWithEmailAndPassword.url), params, "POST", authOpts);
63
- });
64
- loginSignupOptions.confirmEmail =
65
- signupWithEmailAndPassword &&
66
- ((data) => {
67
- return (0, exports.authRequest)(urlWithEndpointAndSearch(signupWithEmailAndPassword.emailConfirmationRoute), data, "POST", authOpts);
68
- });
65
+ signupWithEmailAndPassword && authCalls.register;
66
+ loginSignupOptions.confirmEmail = signupWithEmailAndPassword && authCalls.magicLinkOrOTP;
69
67
  }
70
68
  if (!authConfig?.user) {
71
69
  return {
@@ -77,44 +75,8 @@ const getAuthHandler = ({ authData: authConfig, socket, onReload, endpoint, ...a
77
75
  return {
78
76
  isLoggedin: true,
79
77
  user: authConfig.user,
80
- logout: async () => {
81
- const { logoutRoute } = authConfig.login ?? {};
82
- if (!logoutRoute)
83
- throw new Error("Unexpected");
84
- return (0, exports.authRequest)(urlWithEndpointAndSearch(logoutRoute), {}, "POST", authOpts);
85
- },
78
+ logout: () => authCalls.logout({}),
86
79
  ...loginSignupOptions,
87
80
  };
88
81
  };
89
82
  exports.getAuthHandler = getAuthHandler;
90
- const authRequest = async (path, data, method, { credentials, redirect }) => {
91
- const rawResponse = await fetch(path, {
92
- method: method,
93
- headers: {
94
- Accept: "application/json",
95
- "Content-Type": "application/json",
96
- },
97
- ...(method !== "GET" && { body: JSON.stringify(data) }),
98
- credentials,
99
- redirect,
100
- });
101
- if (!rawResponse.ok) {
102
- const error = await rawResponse
103
- .json()
104
- .catch(() => rawResponse.text())
105
- .catch(() => rawResponse.statusText);
106
- if (typeof error === "string") {
107
- return { success: false, code: "something-went-wrong", message: error };
108
- }
109
- return error;
110
- }
111
- const responseObject = await rawResponse
112
- .json()
113
- .catch(async () => ({ message: await rawResponse.text() }))
114
- .catch(() => ({ message: rawResponse.statusText }));
115
- if (rawResponse.redirected) {
116
- return { ...responseObject, success: true, redirect_url: rawResponse.url };
117
- }
118
- return responseObject;
119
- };
120
- exports.authRequest = authRequest;
@@ -1,12 +1,12 @@
1
1
  import { type DBSchemaTable } from "prostgles-types";
2
2
  import type { getSubscriptionHandler } from "./getSubscriptionHandler";
3
- import type { getSyncHandlerV2 } from "./getSyncHandlerV2";
3
+ import type { getSyncHandler } from "./getSyncHandler";
4
4
  import { type DBHandlerClient, type InitOptions } from "./prostgles";
5
5
  type Args = {
6
6
  tableSchema: DBSchemaTable[] | undefined;
7
7
  onDebug: InitOptions["onDebug"];
8
8
  socket: InitOptions["socket"];
9
- syncHandlerV2: ReturnType<typeof getSyncHandlerV2>;
9
+ syncHandlerV2: ReturnType<typeof getSyncHandler>;
10
10
  subscriptionHandler: ReturnType<typeof getSubscriptionHandler>;
11
11
  };
12
12
  export declare const getDB: <DBSchema = void>({ tableSchema, onDebug, syncHandlerV2, subscriptionHandler, socket, }: Args) => {
@@ -1 +1 @@
1
- {"version":3,"file":"getDbHandler.d.ts","sourceRoot":"","sources":["../lib/getDbHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,aAAa,EAKnB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAK3D,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,WAAW,EAEjB,MAAM,aAAa,CAAC;AAWrB,KAAK,IAAI,GAAG;IACV,WAAW,EAAE,aAAa,EAAE,GAAG,SAAS,CAAC;IACzC,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IAChC,MAAM,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC9B,aAAa,EAAE,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC;IACnD,mBAAmB,EAAE,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC;CAChE,CAAC;AAIF,eAAO,MAAM,KAAK,GAAI,QAAQ,GAAG,IAAI,EAAE,uEAMpC,IAAI;QAuLc,OAAO,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;CACtD,CAAC"}
1
+ {"version":3,"file":"getDbHandler.d.ts","sourceRoot":"","sources":["../lib/getDbHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,aAAa,EAKnB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAKvD,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,WAAW,EAEjB,MAAM,aAAa,CAAC;AAYrB,KAAK,IAAI,GAAG;IACV,WAAW,EAAE,aAAa,EAAE,GAAG,SAAS,CAAC;IACzC,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IAChC,MAAM,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC9B,aAAa,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC;IACjD,mBAAmB,EAAE,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC;CAChE,CAAC;AAIF,eAAO,MAAM,KAAK,GAAI,QAAQ,GAAG,IAAI,EAAE,uEAMpC,IAAI;QA0Lc,OAAO,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;CACtD,CAAC"}
@@ -34,7 +34,7 @@ const getDB = ({ tableSchema, onDebug, syncHandlerV2, subscriptionHandler, socke
34
34
  throw `Table ${tableName} does not have syncConfig in publishInfo.select`;
35
35
  }
36
36
  dboTable._syncInfo = { ...syncConfig };
37
- const syncOne = (async (basicFilter, options = { handlesOnData: true }, onChange) => {
37
+ const syncOne = (async (basicFilter, options = { handlesOnData: true }, onChange, onError) => {
38
38
  await onDebug?.({
39
39
  type: "table",
40
40
  command: "syncOne",
@@ -42,9 +42,9 @@ const getDB = ({ tableSchema, onDebug, syncHandlerV2, subscriptionHandler, socke
42
42
  data: { basicFilter, options },
43
43
  });
44
44
  checkSubscriptionArgs(basicFilter, options, onChange);
45
- return (await syncHandlerV2.getTableSyncFunctions({ db, tableName, columns })).addSyncOne(basicFilter, options, onChange);
45
+ return (await syncHandlerV2.getTableSyncFunctions({ db, tableName, columns })).addSyncOne(basicFilter, options, onChange, onError);
46
46
  });
47
- const sync = (async (basicFilter, options = { handlesOnData: true }, onChange) => {
47
+ const sync = (async (basicFilter, options = { handlesOnData: true }, onChange, onError) => {
48
48
  await onDebug?.({
49
49
  type: "table",
50
50
  command: "sync",
@@ -52,7 +52,7 @@ const getDB = ({ tableSchema, onDebug, syncHandlerV2, subscriptionHandler, socke
52
52
  data: { basicFilter, options },
53
53
  });
54
54
  checkSubscriptionArgs(basicFilter, options, onChange);
55
- return (await syncHandlerV2.getTableSyncFunctions({ db, tableName, columns })).addSync(basicFilter, options, onChange);
55
+ return (await syncHandlerV2.getTableSyncFunctions({ db, tableName, columns })).addSync(basicFilter, options, onChange, onError);
56
56
  });
57
57
  dboTable.sync = sync;
58
58
  dboTable.syncOne = syncOne;
@@ -1,7 +1,7 @@
1
- import { type AnyObject, type ValidatedColumnInfo } from "prostgles-types";
1
+ import { type AnyObject, type DBSchemaTable, type ValidatedColumnInfo } from "prostgles-types";
2
2
  import type { DBHandlerClient, InitOptions } from "./prostgles";
3
3
  import type { Sync, SyncOne } from "./SyncedTable/SyncedTable";
4
- export declare const getSyncHandlerV2: ({ socket, onDebug }: Pick<InitOptions, "socket" | "onDebug">) => {
4
+ export declare const getSyncHandler: ({ socket, onDebug }: Pick<InitOptions, "socket" | "onDebug">) => {
5
5
  getTableSyncFunctions: ({ db, tableName, columns, }: {
6
6
  db: Partial<DBHandlerClient>;
7
7
  tableName: string;
@@ -10,6 +10,6 @@ export declare const getSyncHandlerV2: ({ socket, onDebug }: Pick<InitOptions, "
10
10
  addSync: Sync<AnyObject>;
11
11
  addSyncOne: SyncOne<AnyObject>;
12
12
  }>;
13
- reAttachAll: () => Promise<void>;
13
+ reAttachAll: (db: Partial<DBHandlerClient>, tables?: DBSchemaTable[]) => Promise<void>;
14
14
  };
15
- //# sourceMappingURL=getSyncHandlerV2.d.ts.map
15
+ //# sourceMappingURL=getSyncHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSyncHandler.d.ts","sourceRoot":"","sources":["../lib/getSyncHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,aAAa,EAGlB,KAAK,mBAAmB,EACzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEhE,OAAO,KAAK,EAAwB,IAAI,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAErF,eAAO,MAAM,cAAc,GAAI,qBAAqB,IAAI,CAAC,WAAW,EAAE,QAAQ,GAAG,SAAS,CAAC;yDAmDtF;QACD,EAAE,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;QAC7B,SAAS,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,mBAAmB,EAAE,CAAC;KAChC;;;;sBAgB8B,OAAO,CAAC,eAAe,CAAC,WAAU,aAAa,EAAE;CAajF,CAAC"}
@@ -1,11 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSyncHandlerV2 = void 0;
3
+ exports.getSyncHandler = void 0;
4
4
  const prostgles_types_1 = require("prostgles-types");
5
5
  const createSync_1 = require("./SyncedTable/createSync");
6
- const getSyncHandlerV2 = ({ socket, onDebug }) => {
6
+ const getSyncHandler = ({ socket, onDebug }) => {
7
+ let currentSchema;
7
8
  const syncs = new Map();
8
9
  const upsertSync = async (db, columns, tableName, filter, select) => {
10
+ db = currentSchema?.db ?? db;
11
+ columns = currentSchema?.tables.find((table) => table.name === tableName)?.columns ?? columns;
12
+ if (!db[tableName]?._syncInfo) {
13
+ throw new Error(`Table ${tableName} does not support sync`);
14
+ }
9
15
  const channelName = (0, prostgles_types_1.getSyncChannelName)({
10
16
  filter,
11
17
  select,
@@ -26,20 +32,22 @@ const getSyncHandlerV2 = ({ socket, onDebug }) => {
26
32
  return syncConfig.sync;
27
33
  };
28
34
  const getTableSyncFunctions = async ({ db, tableName, columns, }) => {
29
- const addSync = (async (filter, opts, onChange) => {
35
+ const addSync = (async (filter, opts, onChange, onError) => {
30
36
  const { sync } = await upsertSync(db, columns, tableName, filter, opts.select);
31
- return sync(onChange, opts.handlesOnData);
37
+ return sync(onChange, opts.handlesOnData, onError);
32
38
  });
33
- const addSyncOne = (async (filter, opts, onChange) => {
39
+ const addSyncOne = (async (filter, opts, onChange, onError) => {
34
40
  const { syncOne } = await upsertSync(db, columns, tableName, filter, opts.select);
35
- return syncOne(filter, onChange, opts.handlesOnData);
41
+ return syncOne(filter, onChange, opts.handlesOnData, onError);
36
42
  });
37
43
  return { addSync, addSyncOne };
38
44
  };
39
- const reAttachAll = async () => {
40
- for (const { sync } of syncs.values()) {
45
+ const reAttachAll = async (db, tables = []) => {
46
+ currentSchema = { db, tables };
47
+ for (const { sync, options } of syncs.values()) {
41
48
  try {
42
- await (await sync).reAttach();
49
+ const columns = tables.find((table) => table.name === options.tableName)?.columns ?? [];
50
+ await (await sync).reAttach({ db, columns });
43
51
  }
44
52
  catch (error) {
45
53
  console.error("Failed to reattach sync", error);
@@ -48,4 +56,4 @@ const getSyncHandlerV2 = ({ socket, onDebug }) => {
48
56
  };
49
57
  return { getTableSyncFunctions, reAttachAll };
50
58
  };
51
- exports.getSyncHandlerV2 = getSyncHandlerV2;
59
+ exports.getSyncHandler = getSyncHandler;