prostgles-server 4.2.589 → 4.2.591

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 (64) hide show
  1. package/dist/Auth/AuthHandler.d.ts +3 -5
  2. package/dist/Auth/AuthHandler.d.ts.map +1 -1
  3. package/dist/Auth/AuthHandler.js +16 -15
  4. package/dist/Auth/AuthHandler.js.map +1 -1
  5. package/dist/Auth/AuthTypes.d.ts +5 -1
  6. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  7. package/dist/Auth/AuthTypes.js.map +1 -1
  8. package/dist/Auth/endpoints/setCatchAllRequestHandler.d.ts +2 -1
  9. package/dist/Auth/endpoints/setCatchAllRequestHandler.d.ts.map +1 -1
  10. package/dist/Auth/endpoints/setCatchAllRequestHandler.js +5 -4
  11. package/dist/Auth/endpoints/setCatchAllRequestHandler.js.map +1 -1
  12. package/dist/Auth/endpoints/setLogoutRequestHandler.d.ts +2 -1
  13. package/dist/Auth/endpoints/setLogoutRequestHandler.d.ts.map +1 -1
  14. package/dist/Auth/endpoints/setLogoutRequestHandler.js +2 -2
  15. package/dist/Auth/endpoints/setLogoutRequestHandler.js.map +1 -1
  16. package/dist/Auth/endpoints/setMagicLinkOrOTPRequestHandler.js +1 -1
  17. package/dist/Auth/endpoints/setRegisterRequestHandler.js +1 -1
  18. package/dist/Auth/endpoints/setRegisterRequestHandler.js.map +1 -1
  19. package/dist/Auth/getClientAuth.js +1 -1
  20. package/dist/Auth/getClientAuth.js.map +1 -1
  21. package/dist/Auth/login.d.ts.map +1 -1
  22. package/dist/Auth/login.js +8 -4
  23. package/dist/Auth/login.js.map +1 -1
  24. package/dist/Auth/setupAuthRoutes.d.ts +2 -1
  25. package/dist/Auth/setupAuthRoutes.d.ts.map +1 -1
  26. package/dist/Auth/setupAuthRoutes.js +5 -5
  27. package/dist/Auth/setupAuthRoutes.js.map +1 -1
  28. package/dist/Auth/utils/handleGetUser.d.ts.map +1 -1
  29. package/dist/Auth/utils/handleGetUser.js +9 -10
  30. package/dist/Auth/utils/handleGetUser.js.map +1 -1
  31. package/dist/Auth/utils/{setCookieAndGoToReturnURLIFSet.d.ts → setCookieAndGoToReturnURLIfSet.d.ts} +2 -2
  32. package/dist/Auth/utils/{setCookieAndGoToReturnURLIFSet.d.ts.map → setCookieAndGoToReturnURLIfSet.d.ts.map} +1 -1
  33. package/dist/Auth/utils/{setCookieAndGoToReturnURLIFSet.js → setCookieAndGoToReturnURLIfSet.js} +8 -5
  34. package/dist/Auth/utils/setCookieAndGoToReturnURLIfSet.js.map +1 -0
  35. package/dist/DBSchemaBuilder/DBSchemaBuilder.spec.js +3 -0
  36. package/dist/DBSchemaBuilder/DBSchemaBuilder.spec.js.map +1 -1
  37. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.js +2 -0
  38. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.js.map +1 -1
  39. package/dist/ProstglesTypes.d.ts +1 -1
  40. package/dist/ProstglesTypes.d.ts.map +1 -1
  41. package/dist/PublishParser/PublishParser.d.ts.map +1 -1
  42. package/dist/PublishParser/PublishParser.js +6 -5
  43. package/dist/PublishParser/PublishParser.js.map +1 -1
  44. package/dist/PublishParser/defineServerFunction.d.ts +8 -4
  45. package/dist/PublishParser/defineServerFunction.d.ts.map +1 -1
  46. package/dist/PublishParser/defineServerFunction.js.map +1 -1
  47. package/lib/Auth/AuthHandler.ts +16 -17
  48. package/lib/Auth/AuthTypes.ts +9 -0
  49. package/lib/Auth/endpoints/setCatchAllRequestHandler.ts +5 -5
  50. package/lib/Auth/endpoints/setLogoutRequestHandler.ts +3 -2
  51. package/lib/Auth/endpoints/setMagicLinkOrOTPRequestHandler.ts +1 -1
  52. package/lib/Auth/endpoints/setRegisterRequestHandler.ts +2 -2
  53. package/lib/Auth/getClientAuth.ts +1 -1
  54. package/lib/Auth/login.ts +10 -4
  55. package/lib/Auth/setupAuthRoutes.ts +6 -5
  56. package/lib/Auth/utils/handleGetUser.ts +11 -10
  57. package/lib/Auth/utils/{setCookieAndGoToReturnURLIFSet.ts → setCookieAndGoToReturnURLIfSet.ts} +6 -3
  58. package/lib/DBSchemaBuilder/DBSchemaBuilder.spec.ts +3 -0
  59. package/lib/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.ts +3 -1
  60. package/lib/ProstglesTypes.ts +1 -1
  61. package/lib/PublishParser/PublishParser.ts +12 -5
  62. package/lib/PublishParser/defineServerFunction.ts +15 -4
  63. package/package.json +1 -1
  64. package/dist/Auth/utils/setCookieAndGoToReturnURLIFSet.js.map +0 -1
@@ -181,7 +181,7 @@ export type ProstglesInitOptions<S = void, SUser extends SessionUser = SessionUs
181
181
  /**
182
182
  * Server-side functions that can be invoked by the client
183
183
  */
184
- functions?: ServerFunctionDefinitions<S>;
184
+ functions?: ServerFunctionDefinitions<S, SUser>;
185
185
 
186
186
  /**
187
187
  * If true then will test all table methods on each socket connect.
@@ -71,9 +71,13 @@ export class PublishParser {
71
71
  if (!functionGroups || !publishParams.user) {
72
72
  return;
73
73
  }
74
+ const { findUser } = this.prostgles.opts.auth ?? {};
75
+ if (!findUser) {
76
+ throw new Error(
77
+ "findUser function is missing in auth config. It is required for functions to work.",
78
+ );
79
+ }
74
80
  const user = publishParams.user;
75
- const users = publishParams.dbo.users;
76
- if (!users?.findOne) return;
77
81
  const userId = user.id;
78
82
  if (!userId || typeof userId !== "string") {
79
83
  throw "User ID is missing or invalid";
@@ -81,9 +85,12 @@ export class PublishParser {
81
85
  const allowedFunctionsMap = new Map<string, ServerFunctionDefinition>();
82
86
 
83
87
  for (const group of Object.values(functionGroups)) {
84
- const matchingUser = await users.findOne({
85
- $and: [group.userFilter, { id: userId }],
86
- });
88
+ const matchingUser = await findUser(
89
+ {
90
+ $and: [group.userFilter, { id: userId }],
91
+ },
92
+ publishParams.dbo,
93
+ );
87
94
  if (!matchingUser) continue;
88
95
 
89
96
  for (const [name, method] of Object.entries(group.functions)) {
@@ -47,6 +47,11 @@ export type ServerFunctionDefinition = {
47
47
  run: (...args: any[]) => MaybePromise<unknown>;
48
48
  };
49
49
 
50
+ declare const serverFunctionContext: unique symbol;
51
+ type ServerFunctionContextMarker<S, SUser extends SessionUser> = {
52
+ [serverFunctionContext]?: [S, SUser];
53
+ };
54
+
50
55
  type DefineFunctionArgs<
51
56
  TInput extends Record<string, JSONB.FieldType> | undefined,
52
57
  Context,
@@ -66,7 +71,7 @@ export function defineFunction<
66
71
  args: DefineFunctionArgs<TInput, UnrestrictedFunctionContext<S, SUser>, Return> & {
67
72
  unrestrictedDbAccess: true;
68
73
  },
69
- ): typeof args;
74
+ ): typeof args & ServerFunctionContextMarker<S, SUser>;
70
75
 
71
76
  export function defineFunction<
72
77
  TInput extends Record<string, JSONB.FieldType> | undefined = undefined,
@@ -77,7 +82,7 @@ export function defineFunction<
77
82
  args: DefineFunctionArgs<TInput, RestrictedFunctionContext<S, SUser>, Return> & {
78
83
  unrestrictedDbAccess?: undefined;
79
84
  },
80
- ): typeof args;
85
+ ): typeof args & ServerFunctionContextMarker<S, SUser>;
81
86
  export function defineFunction(args: unknown) {
82
87
  return args;
83
88
  }
@@ -91,10 +96,16 @@ type UsersTableRow<S> =
91
96
 
92
97
  type ValidDbSchema<S> = S extends DBSchema ? S : void;
93
98
 
94
- export type ServerFunctionDefinitions<S = void> = Record<
99
+ export type ServerFunctionDefinitions<
100
+ S = void,
101
+ SUser extends SessionUser = SessionUser,
102
+ > = Record<
95
103
  string,
96
104
  {
97
105
  userFilter: FullFilter<UsersTableRow<S>, ValidDbSchema<S>>;
98
- functions: Record<string, ServerFunctionDefinition>;
106
+ functions: Record<
107
+ string,
108
+ ServerFunctionDefinition & ServerFunctionContextMarker<S, SUser>
109
+ >;
99
110
  }
100
111
  >;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.2.589",
3
+ "version": "4.2.591",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1 +0,0 @@
1
- {"version":3,"file":"setCookieAndGoToReturnURLIFSet.js","sourceRoot":"","sources":["../../../lib/Auth/utils/setCookieAndGoToReturnURLIFSet.ts"],"names":[],"mappings":";;AAQA,kEAcC;AAED,wEA8CC;AAtED,gDAA+F;AAG/F,oCAAoD;AACpD,2EAAwE;AACxE,yDAAsD;AACtD,iDAA8C;AAE9C,SAAgB,2BAA2B,CAEzC,MAAwC,EACxC,cAA8D;IAE9D,MAAM,gBAAgB,GAAG,IAAA,gCAAwB,EAAC,MAAM,CAAC,CAAC;IAC1D,IAAI,gBAAgB,EAAE,CAAC;QACrB,MAAM,EAAE,GAAG,EAAE,GAAG,cAAc,CAAC;QAC/B,OAAO,GAAG,CAAC,MAAM,CAAC,6BAAe,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC;YACnD,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,gBAAgB;SACvB,CAAC,CAAC;IACL,CAAC;IACD,OAAO,IAAI,CAAC,8BAA8B,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;AACrE,CAAC;AAED,SAAgB,8BAA8B,CAE5C,MAAwC,EACxC,cAA8D;IAE9D,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IAChC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,cAAc,CAAC;IACpC,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;IACjC,CAAC;IAED,MAAM,YAAY,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,YAAY;IAE/C,IAAI,cAAc,GAA4B;QAC5C,MAAM,EAAE,YAAY;KACrB,CAAC;IAEF,IAAI,OAAO,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;QACtE,cAAc,GAA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