@shivaedev/contract 0.22.0 → 0.24.0
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/CHANGELOG.md +22 -0
- package/README.md +3 -3
- package/dist/automaticLive.d.ts +12 -5
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +20 -65
- package/dist/automaticLive.js.map +1 -1
- package/dist/contract.d.ts +2 -1
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +3 -10
- package/dist/contract.js.map +1 -1
- package/dist/internal/automaticQuery.d.ts +21 -0
- package/dist/internal/automaticQuery.d.ts.map +1 -0
- package/dist/internal/automaticQuery.js +78 -0
- package/dist/internal/automaticQuery.js.map +1 -0
- package/dist/internal/changesSuccess.d.ts +13 -0
- package/dist/internal/changesSuccess.d.ts.map +1 -0
- package/dist/internal/changesSuccess.js +16 -0
- package/dist/internal/changesSuccess.js.map +1 -0
- package/dist/internal/clientOperations.d.ts.map +1 -1
- package/dist/internal/clientOperations.js +1 -1
- package/dist/internal/clientOperations.js.map +1 -1
- package/dist/internal/compareQueries.d.ts +23 -0
- package/dist/internal/compareQueries.d.ts.map +1 -0
- package/dist/internal/compareQueries.js +56 -0
- package/dist/internal/compareQueries.js.map +1 -0
- package/dist/internal/queryDelivery.d.ts +28 -0
- package/dist/internal/queryDelivery.d.ts.map +1 -0
- package/dist/internal/queryDelivery.js +22 -0
- package/dist/internal/queryDelivery.js.map +1 -0
- package/dist/makeLiveServer.d.ts +8 -39
- package/dist/makeLiveServer.d.ts.map +1 -1
- package/dist/makeLiveServer.js +14 -8
- package/dist/makeLiveServer.js.map +1 -1
- package/dist/operation.d.ts +11 -8
- package/dist/operation.d.ts.map +1 -1
- package/dist/operation.js +1 -0
- package/dist/operation.js.map +1 -1
- package/dist/publicConfig.d.ts +3 -3
- package/dist/publicConfig.d.ts.map +1 -1
- package/dist/querySubscription.d.ts +9 -9
- package/dist/querySubscription.d.ts.map +1 -1
- package/dist/querySubscription.js +10 -7
- package/dist/querySubscription.js.map +1 -1
- package/dist/queryUpdate.d.ts +9 -0
- package/dist/queryUpdate.d.ts.map +1 -0
- package/dist/queryUpdate.js +2 -0
- package/dist/queryUpdate.js.map +1 -0
- package/dist/readPolicy.d.ts +22 -6
- package/dist/readPolicy.d.ts.map +1 -1
- package/dist/readPolicy.js +10 -9
- package/dist/readPolicy.js.map +1 -1
- package/dist/serverMetadata.d.ts +12 -25
- package/dist/serverMetadata.d.ts.map +1 -1
- package/dist/serverMetadata.js +10 -3
- package/dist/serverMetadata.js.map +1 -1
- package/package.json +5 -5
- package/src/automaticLive.ts +35 -73
- package/src/contract.ts +13 -12
- package/src/internal/automaticQuery.ts +113 -0
- package/src/internal/changesSuccess.ts +32 -0
- package/src/internal/clientOperations.ts +1 -0
- package/src/internal/compareQueries.ts +65 -0
- package/src/internal/queryDelivery.ts +28 -0
- package/src/makeLiveServer.ts +24 -17
- package/src/operation.ts +22 -9
- package/src/querySubscription.ts +30 -14
- package/src/queryUpdate.ts +9 -0
- package/src/readPolicy.ts +29 -13
- package/src/serverMetadata.ts +32 -8
package/src/querySubscription.ts
CHANGED
|
@@ -1,21 +1,37 @@
|
|
|
1
1
|
import { Context, Effect, type Option } from "effect";
|
|
2
|
+
import type { Rpc } from "effect/rpc";
|
|
2
3
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
3
|
-
import { checkQuery, type Viewer } from "#readPolicy.ts";
|
|
4
|
-
import type {
|
|
4
|
+
import { checkQuery, type PolicyChannels, type Viewer } from "#readPolicy.ts";
|
|
5
|
+
import type { MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
6
|
+
import { ChangesQueries, type QueryReference, type QueryRequest } from "#serverMetadata.ts";
|
|
5
7
|
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
+
export type QueryRerun<E = unknown, R = never> = (request: QueryRequest) => Effect.Effect<unknown, E, R>;
|
|
9
|
+
|
|
10
|
+
export interface QuerySubscription<E = never, R = never> {
|
|
11
|
+
readonly authorized: Effect.Effect<boolean, E, R>;
|
|
8
12
|
readonly keys: readonly Key[];
|
|
13
|
+
readonly queries?: readonly QueryRequest[];
|
|
9
14
|
readonly scopes: readonly (readonly Key[])[];
|
|
10
15
|
}
|
|
11
16
|
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
export function querySubscription<TRpc extends Rpc.Any>(
|
|
18
|
+
rpc: TRpc,
|
|
19
|
+
references: readonly QueryReference[],
|
|
20
|
+
viewer: Option.Option<Viewer>,
|
|
21
|
+
): Effect.Effect<
|
|
22
|
+
QuerySubscription<PolicyChannels<TRpc>["error"], PolicyChannels<TRpc>["services"]>,
|
|
23
|
+
PolicyChannels<TRpc>["error"] | MissingReadPolicy | QueryReadDenied,
|
|
24
|
+
PolicyChannels<TRpc>["services"]
|
|
25
|
+
>;
|
|
26
|
+
export function querySubscription(rpc: Rpc.Any, references: readonly QueryReference[], viewer: Option.Option<Viewer>) {
|
|
27
|
+
return Effect.fnUntraced(function* () {
|
|
28
|
+
const queries = yield* Context.getUnsafe(rpc.annotations, ChangesQueries).resolve(references);
|
|
29
|
+
yield* Effect.forEach(queries, ({ query, payload }) => checkQuery(query, payload, viewer));
|
|
30
|
+
const scopes = queries.map(({ query, payload }) => query.reads(payload, viewer));
|
|
31
|
+
const authorized = Effect.map(
|
|
32
|
+
Effect.forEach(queries, ({ query, payload }) => query.readPolicy?.(payload, viewer) ?? Effect.succeed(false)),
|
|
33
|
+
(permissions) => permissions.every(Boolean),
|
|
34
|
+
);
|
|
35
|
+
return { authorized, keys: scopes.flat(), queries, scopes } satisfies QuerySubscription<unknown, unknown>;
|
|
36
|
+
})();
|
|
37
|
+
}
|
package/src/readPolicy.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Context, Effect, Option } from "effect";
|
|
1
|
+
import { Context, Effect, Option, type Stream, type Types } from "effect";
|
|
2
2
|
import type { Rpc } from "effect/rpc";
|
|
3
3
|
import type { QueryShape } from "#operation.ts";
|
|
4
4
|
import { MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
@@ -7,27 +7,43 @@ import { QueryDefinition } from "#serverMetadata.ts";
|
|
|
7
7
|
export interface Viewer {
|
|
8
8
|
readonly id: string;
|
|
9
9
|
}
|
|
10
|
-
export type QueryReadPolicy<TPayload> = (payload: TPayload, viewer: Option.Option<Viewer>) => Effect.Effect<boolean,
|
|
10
|
+
export type QueryReadPolicy<TPayload, E = never, R = never> = (payload: TPayload, viewer: Option.Option<Viewer>) => Effect.Effect<boolean, E, R>;
|
|
11
11
|
|
|
12
12
|
export const CurrentViewer = Context.Reference<Option.Option<Viewer>>("@shivaedev/contract/CurrentViewer", { defaultValue: Option.none });
|
|
13
|
-
export const checkQuery = Effect.fnUntraced(function* (query: QueryShape, payload: unknown, viewer: Option.Option<Viewer>) {
|
|
13
|
+
export const checkQuery = Effect.fnUntraced(function* <E, R>(query: QueryShape<E, R>, payload: unknown, viewer: Option.Option<Viewer>) {
|
|
14
14
|
if (query.readPolicy === undefined) {
|
|
15
15
|
return yield* new MissingReadPolicy({ message: `Declare a readPolicy on query ${query.name} before running it live or subscribing` });
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
if (!(yield* Effect.provideContext(query.readPolicy(payload, viewer), context))) {
|
|
17
|
+
if (!(yield* query.readPolicy(payload, viewer))) {
|
|
19
18
|
return yield* new QueryReadDenied({ message: `Read not allowed for query ${query.name}` });
|
|
20
19
|
}
|
|
21
20
|
});
|
|
22
21
|
|
|
23
|
-
|
|
22
|
+
// Middleware receives erased RPC metadata; its continuation represents operation failures as unhandled.
|
|
23
|
+
export type PolicyChannels<TRpc> = TRpc extends {
|
|
24
|
+
readonly setPayload: unknown;
|
|
25
|
+
readonly errorSchema: { readonly Type: infer E };
|
|
26
|
+
readonly successSchema: { readonly Type: infer TSuccess };
|
|
27
|
+
readonly "~requires": infer R;
|
|
28
|
+
}
|
|
29
|
+
? { readonly error: E | (TSuccess extends Stream.Stream<unknown, infer TError, unknown> ? TError : never); readonly services: R }
|
|
30
|
+
: { readonly error: Types.unhandled; readonly services: never };
|
|
31
|
+
|
|
32
|
+
export function authorizeReads<A, E, R, TRpc extends Rpc.Any>(
|
|
24
33
|
effect: Effect.Effect<A, E, R>,
|
|
25
|
-
request: { readonly rpc:
|
|
34
|
+
request: { readonly rpc: TRpc; readonly payload: unknown },
|
|
35
|
+
viewer: Option.Option<Viewer>,
|
|
36
|
+
): Effect.Effect<A, E | PolicyChannels<TRpc>["error"] | MissingReadPolicy | QueryReadDenied, R | PolicyChannels<TRpc>["services"]>;
|
|
37
|
+
export function authorizeReads(
|
|
38
|
+
effect: Effect.Effect<unknown, unknown, unknown>,
|
|
39
|
+
request: { readonly rpc: Rpc.Any; readonly payload: unknown },
|
|
26
40
|
viewer: Option.Option<Viewer>,
|
|
27
41
|
) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
42
|
+
return Effect.fnUntraced(function* () {
|
|
43
|
+
const definition = Context.getOption(request.rpc.annotations, QueryDefinition);
|
|
44
|
+
if (Option.isSome(definition) && definition.value.definition.readPolicy !== undefined) {
|
|
45
|
+
yield* checkQuery(definition.value.definition, request.payload, viewer);
|
|
46
|
+
}
|
|
47
|
+
return yield* Effect.provideService(effect, CurrentViewer, viewer);
|
|
48
|
+
})();
|
|
49
|
+
}
|
package/src/serverMetadata.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { Context, Effect, Option, Schema } from "effect";
|
|
2
|
-
import { Rpc } from "effect/rpc";
|
|
2
|
+
import { Rpc, type RpcSchema } from "effect/rpc";
|
|
3
3
|
import type { Key } from "@shivaedev/changes/keys.ts";
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import type { QueryServices } from "#contract.ts";
|
|
5
|
+
import { changesSuccess } from "#internal/changesSuccess.ts";
|
|
6
|
+
import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
|
|
6
7
|
import type { Viewer } from "#readPolicy.ts";
|
|
7
8
|
import { MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
8
9
|
|
|
9
10
|
export class QueryDefinition extends Context.Service<QueryDefinition, { readonly definition: QueryShape }>()("@shivaedev/contract/QueryDefinition") {}
|
|
10
|
-
export interface QueryRequest {
|
|
11
|
+
export interface QueryRequest<E = unknown, R = unknown> {
|
|
11
12
|
readonly payload: unknown;
|
|
12
|
-
readonly query: QueryShape
|
|
13
|
+
readonly query: QueryShape<E, R>;
|
|
13
14
|
}
|
|
14
15
|
export class ChangesQueries extends Context.Service<
|
|
15
16
|
ChangesQueries,
|
|
@@ -56,11 +57,34 @@ function referenceSchemas(queries: readonly QueryShape[]) {
|
|
|
56
57
|
return Schema.Array(Schema.Union(queries.map((query) => Schema.Struct({ payload: query.payload, query: Schema.Literal(query.name) }))));
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
type ChangesError<TQueries extends readonly QueryShape[]> = Schema.Union<
|
|
61
|
+
readonly [typeof MissingReadPolicy, typeof QueryReadDenied, ...TQueries[number]["error"][]]
|
|
62
|
+
>;
|
|
63
|
+
|
|
64
|
+
export function changesRpc<const TName extends string, const TQueries extends readonly QueryShape[]>(
|
|
65
|
+
name: TName,
|
|
66
|
+
queries: TQueries,
|
|
67
|
+
): Rpc.Rpc<
|
|
68
|
+
`${TName}.$changes`,
|
|
69
|
+
Schema.Struct<{ queries: ReturnType<typeof referenceSchemas<TQueries>> }>,
|
|
70
|
+
RpcSchema.Stream<ReturnType<typeof changesSuccess<TQueries>>, ChangesError<TQueries>>,
|
|
71
|
+
Schema.Never,
|
|
72
|
+
never,
|
|
73
|
+
QueryServices<TQueries[number]>
|
|
74
|
+
>;
|
|
75
|
+
export function changesRpc(name: string, queries: readonly QueryShape[]) {
|
|
60
76
|
return Rpc.make(`${name}.$changes`, {
|
|
61
|
-
error: Schema.Union([MissingReadPolicy, QueryReadDenied]),
|
|
77
|
+
error: Schema.Union([MissingReadPolicy, QueryReadDenied, ...queries.map((query) => query.error)]),
|
|
62
78
|
payload: { queries: referenceSchemas(queries) },
|
|
63
79
|
stream: true,
|
|
64
|
-
success:
|
|
80
|
+
success: changesSuccess(queries),
|
|
65
81
|
}).annotate(ChangesQueries, { resolve: (references) => resolveQueries(queries, references) });
|
|
66
82
|
}
|
|
83
|
+
|
|
84
|
+
export function operationAnnotations(operation: QueryShape | CommandShape | StreamShape, keyScope: "session" | undefined) {
|
|
85
|
+
const scope = keyScope === undefined ? Context.empty() : Context.make(KeyScope, keyScope);
|
|
86
|
+
if (operation.kind === "command") {
|
|
87
|
+
return Context.add(scope, CommandInvalidation, { invalidates: operation.invalidates });
|
|
88
|
+
}
|
|
89
|
+
return operation.kind === "query" ? Context.add(scope, QueryDefinition, { definition: operation }) : scope;
|
|
90
|
+
}
|