prostgles-server 4.2.587 → 4.2.588

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 (48) hide show
  1. package/dist/DBSchemaBuilder/getFunctionsTypescriptSchema.d.ts +1 -1
  2. package/dist/DBSchemaBuilder/getFunctionsTypescriptSchema.d.ts.map +1 -1
  3. package/dist/DBSchemaBuilder/getFunctionsTypescriptSchema.js +1 -1
  4. package/dist/DBSchemaBuilder/getFunctionsTypescriptSchema.js.map +1 -1
  5. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.d.ts.map +1 -1
  6. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.d.ts +2 -0
  7. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.d.ts.map +1 -0
  8. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.js +57 -0
  9. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.js.map +1 -0
  10. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.js +26 -14
  11. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.js.map +1 -1
  12. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.spec.d.ts +2 -0
  13. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.spec.d.ts.map +1 -0
  14. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.spec.js +18 -0
  15. package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.spec.js.map +1 -0
  16. package/dist/DBSchemaBuilder/resolveTypeToStructure.d.ts.map +1 -1
  17. package/dist/DBSchemaBuilder/resolveTypeToStructure.js +11 -4
  18. package/dist/DBSchemaBuilder/resolveTypeToStructure.js.map +1 -1
  19. package/dist/DboBuilder/DboBuilder.d.ts.map +1 -1
  20. package/dist/DboBuilder/DboBuilder.js +11 -7
  21. package/dist/DboBuilder/DboBuilder.js.map +1 -1
  22. package/dist/DboBuilder/schema/getTablesForSchemaPostgresSQL.d.ts +1 -1
  23. package/dist/ProstglesTypes.d.ts +2 -2
  24. package/dist/ProstglesTypes.d.ts.map +1 -1
  25. package/dist/PublishParser/PublishParser.d.ts.map +1 -1
  26. package/dist/PublishParser/PublishParser.js +44 -5
  27. package/dist/PublishParser/PublishParser.js.map +1 -1
  28. package/dist/PublishParser/defineServerFunction.d.ts +34 -54
  29. package/dist/PublishParser/defineServerFunction.d.ts.map +1 -1
  30. package/dist/PublishParser/defineServerFunction.js +4 -31
  31. package/dist/PublishParser/defineServerFunction.js.map +1 -1
  32. package/dist/PublishParser/defineServerFunction.spec.js +12 -3
  33. package/dist/PublishParser/defineServerFunction.spec.js.map +1 -1
  34. package/dist/runClientRequest.d.ts.map +1 -1
  35. package/dist/runClientRequest.js +2 -1
  36. package/dist/runClientRequest.js.map +1 -1
  37. package/lib/DBSchemaBuilder/getFunctionsTypescriptSchema.ts +3 -3
  38. package/lib/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.ts +33 -0
  39. package/lib/DBSchemaBuilder/getServerFunctionReturnTypes.spec.ts +18 -0
  40. package/lib/DBSchemaBuilder/getServerFunctionReturnTypes.ts +38 -28
  41. package/lib/DBSchemaBuilder/resolveTypeToStructure.ts +9 -4
  42. package/lib/DboBuilder/DboBuilder.ts +13 -6
  43. package/lib/ProstglesTypes.ts +2 -2
  44. package/lib/PublishParser/PublishParser.ts +46 -6
  45. package/lib/PublishParser/defineServerFunction.spec.ts +16 -4
  46. package/lib/PublishParser/defineServerFunction.ts +81 -93
  47. package/lib/runClientRequest.ts +2 -1
  48. package/package.json +1 -1
@@ -1,111 +1,99 @@
1
- import type { JSONB, JSONBObjectTypeIfDefined, MaybePromise } from "prostgles-types";
1
+ import type {
2
+ AnyObject,
3
+ DBSchema,
4
+ FullFilter,
5
+ JSONB,
6
+ JSONBObjectTypeIfDefined,
7
+ MaybePromise,
8
+ SQLHandler,
9
+ } from "prostgles-types";
2
10
  import type { SessionUser } from "../Auth/AuthTypes";
11
+ import type { DBOFullyTyped } from "../DBSchemaBuilder/DBSchemaBuilder";
12
+ import type { DB } from "../initProstgles";
3
13
  import type { PublishParams } from "./publishTypesAndUtils";
4
14
 
5
- export type ServerFunctionDefinition = {
6
- input?: Record<string, JSONB.FieldType> | undefined;
7
- description?: string;
8
- /**
9
- * undefined if not allowed
10
- */
11
- run: undefined | ((args: Record<string, unknown> | undefined) => MaybePromise<unknown>);
15
+ type FunctionContextBase<S, SUser extends SessionUser> = Pick<
16
+ PublishParams<S, SUser>,
17
+ "tables" | "clientReq" | "clientInfo"
18
+ > & {
19
+ dbo: DBOFullyTyped<S>;
20
+ user: S extends DBSchema ?
21
+ S["users"]["columns"] extends AnyObject ?
22
+ Required<S["users"]["columns"]>
23
+ : SUser["user"]
24
+ : SUser["user"];
12
25
  };
13
26
 
14
- export const defineServerFunction = <
15
- TInput extends Record<string, JSONB.FieldType> | undefined = undefined,
16
- >(args: {
17
- input?: TInput;
18
- description?: string;
19
- /**
20
- * undefined if not allowed
21
- */
22
- run: undefined | ((args: JSONBObjectTypeIfDefined<TInput>) => MaybePromise<unknown>);
23
- }) => args as unknown as ServerFunctionDefinition;
24
-
25
- export type ServerFunctionDefinitions<S = void, SUser extends SessionUser = SessionUser> = (
26
- /**
27
- * params will be undefined on first run to generate the definitions
28
- */
29
- params: undefined | PublishParams<S, SUser>,
30
- ) => MaybePromise<Record<string, ServerFunctionDefinition>>;
27
+ export type RestrictedFunctionContext<
28
+ S = void,
29
+ SUser extends SessionUser = SessionUser,
30
+ > = FunctionContextBase<S, SUser>;
31
31
 
32
- export const createServerFunctionWithContext = <Context>(
33
- /**
34
- * undefined if not allowed
35
- */
36
- context: Context | undefined,
37
- ) => {
38
- return <
39
- Input extends Record<string, JSONB.FieldType> | undefined,
40
- Run extends (args: JSONBObjectTypeIfDefined<Input>, context: Context) => MaybePromise<any>,
41
- >(args: {
42
- input?: Input;
43
- description?: string;
44
- run: Run;
45
- }) =>
46
- ({
47
- ...args,
48
- run:
49
- context === undefined ? undefined : (
50
- (validatedArgs: any) => {
51
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
52
- return args.run(validatedArgs, context) as Run;
53
- }
54
- ),
55
- }) satisfies ServerFunctionDefinition;
32
+ export type UnrestrictedFunctionContext<
33
+ S = void,
34
+ SUser extends SessionUser = SessionUser,
35
+ > = FunctionContextBase<S, SUser> & {
36
+ db: DB;
37
+ sql: SQLHandler;
56
38
  };
57
39
 
58
- type InputOf<T> = T extends { input?: infer I } ? I : undefined;
59
-
60
- // if inference fails or is invalid → fall back to undefined
61
- type SafeInput<I> = I extends Record<string, JSONB.FieldType> ? I : undefined;
40
+ export type ServerFunctionContext = RestrictedFunctionContext | UnrestrictedFunctionContext;
62
41
 
63
- type ServerFunctionArgsFrom<Context, Def> = {
64
- input?: InputOf<Def>;
42
+ export type ServerFunctionDefinition = {
43
+ input?: Record<string, JSONB.FieldType>;
65
44
  description?: string;
66
- run: (
67
- args: JSONBObjectTypeIfDefined<SafeInput<InputOf<Def>>>,
68
- context: Context,
69
- ) => MaybePromise<any>;
45
+ unrestrictedDbAccess?: true;
46
+ run: (...args: any[]) => MaybePromise<unknown>;
70
47
  };
71
48
 
72
- type ServerFunctionBlock<Context, Defs> = {
73
- [K in keyof Defs]: ServerFunctionArgsFrom<Context, Defs[K]>;
49
+ type DefineFunctionArgs<
50
+ TInput extends Record<string, JSONB.FieldType> | undefined,
51
+ Context,
52
+ Return,
53
+ > = {
54
+ input?: TInput;
55
+ description?: string;
56
+ run: (args: JSONBObjectTypeIfDefined<TInput>, context: Context) => MaybePromise<Return>;
74
57
  };
75
58
 
76
- type WrappedDef<Context, Def> =
77
- Def extends (
78
- {
79
- input?: infer I;
80
- description?: infer D;
81
- run: (a: any, c: Context) => infer R;
82
- }
83
- ) ?
84
- {
85
- input?: SafeInput<I>;
86
- description?: D extends string ? D : string | undefined;
87
- run: undefined | ((a: any) => R); // keep return type, widen args
88
- }
89
- : never;
59
+ export function defineFunction<
60
+ TInput extends Record<string, JSONB.FieldType> | undefined = undefined,
61
+ S = void,
62
+ SUser extends SessionUser = SessionUser,
63
+ Return = unknown,
64
+ >(
65
+ args: DefineFunctionArgs<TInput, UnrestrictedFunctionContext<S, SUser>, Return> & {
66
+ unrestrictedDbAccess: true;
67
+ },
68
+ ): typeof args;
90
69
 
91
- type WrappedBlock<Context, Defs> = {
92
- [K in keyof Defs]: WrappedDef<Context, Defs[K]>;
93
- };
70
+ export function defineFunction<
71
+ TInput extends Record<string, JSONB.FieldType> | undefined = undefined,
72
+ S = void,
73
+ SUser extends SessionUser = SessionUser,
74
+ Return = unknown,
75
+ >(
76
+ args: DefineFunctionArgs<TInput, RestrictedFunctionContext<S, SUser>, Return> & {
77
+ unrestrictedDbAccess?: undefined;
78
+ },
79
+ ): typeof args;
80
+ export function defineFunction(args: unknown) {
81
+ return args;
82
+ }
94
83
 
95
- export const createServerFunctionBlockWithContext = <Context>(context: Context | undefined) => {
96
- return <const Defs extends Record<string, any>>(
97
- defs: Defs & ServerFunctionBlock<Context, Defs>,
98
- ): WrappedBlock<Context, Defs> => {
99
- const wrapped = {} as WrappedBlock<Context, Defs>;
84
+ type UsersTableRow<S> =
85
+ S extends DBSchema ?
86
+ S extends { users: { columns: infer UserRow extends AnyObject } } ?
87
+ UserRow
88
+ : AnyObject
89
+ : AnyObject;
100
90
 
101
- for (const key in defs) {
102
- const def = defs[key];
103
- wrapped[key] = {
104
- ...def,
105
- run: context === undefined ? undefined : (args: any) => def.run(args, context),
106
- };
107
- }
91
+ type ValidDbSchema<S> = S extends DBSchema ? S : void;
108
92
 
109
- return wrapped satisfies Record<string, ServerFunctionDefinition>;
110
- };
111
- };
93
+ export type ServerFunctionDefinitions<S = void> = Record<
94
+ string,
95
+ {
96
+ userFilter: FullFilter<UsersTableRow<S>, ValidDbSchema<S>>;
97
+ functions: Record<string, ServerFunctionDefinition>;
98
+ }
99
+ >;
@@ -280,7 +280,8 @@ export const runClientMethod = async function (
280
280
  throw message;
281
281
  }
282
282
 
283
- const res = await functionDefinition.run(input as never);
283
+ /** Allowed functions are wrapped with their request context by PublishParser. */
284
+ const res = await functionDefinition.run(input as never, undefined);
284
285
 
285
286
  return res;
286
287
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.2.587",
3
+ "version": "4.2.588",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",