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.
- package/dist/DBSchemaBuilder/getFunctionsTypescriptSchema.d.ts +1 -1
- package/dist/DBSchemaBuilder/getFunctionsTypescriptSchema.d.ts.map +1 -1
- package/dist/DBSchemaBuilder/getFunctionsTypescriptSchema.js +1 -1
- package/dist/DBSchemaBuilder/getFunctionsTypescriptSchema.js.map +1 -1
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.d.ts.map +1 -1
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.d.ts +2 -0
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.d.ts.map +1 -0
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.js +57 -0
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.js.map +1 -0
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.js +26 -14
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.js.map +1 -1
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.spec.d.ts +2 -0
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.spec.d.ts.map +1 -0
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.spec.js +18 -0
- package/dist/DBSchemaBuilder/getServerFunctionReturnTypes.spec.js.map +1 -0
- package/dist/DBSchemaBuilder/resolveTypeToStructure.d.ts.map +1 -1
- package/dist/DBSchemaBuilder/resolveTypeToStructure.js +11 -4
- package/dist/DBSchemaBuilder/resolveTypeToStructure.js.map +1 -1
- package/dist/DboBuilder/DboBuilder.d.ts.map +1 -1
- package/dist/DboBuilder/DboBuilder.js +11 -7
- package/dist/DboBuilder/DboBuilder.js.map +1 -1
- package/dist/DboBuilder/schema/getTablesForSchemaPostgresSQL.d.ts +1 -1
- package/dist/ProstglesTypes.d.ts +2 -2
- package/dist/ProstglesTypes.d.ts.map +1 -1
- package/dist/PublishParser/PublishParser.d.ts.map +1 -1
- package/dist/PublishParser/PublishParser.js +44 -5
- package/dist/PublishParser/PublishParser.js.map +1 -1
- package/dist/PublishParser/defineServerFunction.d.ts +34 -54
- package/dist/PublishParser/defineServerFunction.d.ts.map +1 -1
- package/dist/PublishParser/defineServerFunction.js +4 -31
- package/dist/PublishParser/defineServerFunction.js.map +1 -1
- package/dist/PublishParser/defineServerFunction.spec.js +12 -3
- package/dist/PublishParser/defineServerFunction.spec.js.map +1 -1
- package/dist/runClientRequest.d.ts.map +1 -1
- package/dist/runClientRequest.js +2 -1
- package/dist/runClientRequest.js.map +1 -1
- package/lib/DBSchemaBuilder/getFunctionsTypescriptSchema.ts +3 -3
- package/lib/DBSchemaBuilder/getServerFunctionReturnTypes.fixture.ts +33 -0
- package/lib/DBSchemaBuilder/getServerFunctionReturnTypes.spec.ts +18 -0
- package/lib/DBSchemaBuilder/getServerFunctionReturnTypes.ts +38 -28
- package/lib/DBSchemaBuilder/resolveTypeToStructure.ts +9 -4
- package/lib/DboBuilder/DboBuilder.ts +13 -6
- package/lib/ProstglesTypes.ts +2 -2
- package/lib/PublishParser/PublishParser.ts +46 -6
- package/lib/PublishParser/defineServerFunction.spec.ts +16 -4
- package/lib/PublishParser/defineServerFunction.ts +81 -93
- package/lib/runClientRequest.ts +2 -1
- package/package.json +1 -1
|
@@ -1,111 +1,99 @@
|
|
|
1
|
-
import type {
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
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
|
|
64
|
-
input?:
|
|
42
|
+
export type ServerFunctionDefinition = {
|
|
43
|
+
input?: Record<string, JSONB.FieldType>;
|
|
65
44
|
description?: string;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
context: Context,
|
|
69
|
-
) => MaybePromise<any>;
|
|
45
|
+
unrestrictedDbAccess?: true;
|
|
46
|
+
run: (...args: any[]) => MaybePromise<unknown>;
|
|
70
47
|
};
|
|
71
48
|
|
|
72
|
-
type
|
|
73
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
92
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
>;
|
package/lib/runClientRequest.ts
CHANGED
|
@@ -280,7 +280,8 @@ export const runClientMethod = async function (
|
|
|
280
280
|
throw message;
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
-
|
|
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
|
};
|