@shivaedev/contract 0.48.0 → 0.50.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 +33 -0
- package/README.md +167 -143
- package/dist/adapters/erasedClient.d.ts +18 -0
- package/dist/adapters/erasedClient.d.ts.map +1 -0
- package/dist/adapters/erasedClient.js +10 -0
- package/dist/adapters/erasedClient.js.map +1 -0
- package/dist/automaticLive.d.ts +2 -10
- package/dist/automaticLive.d.ts.map +1 -1
- package/dist/automaticLive.js +3 -1
- package/dist/automaticLive.js.map +1 -1
- package/dist/bind.d.ts +12 -5
- package/dist/bind.d.ts.map +1 -1
- package/dist/bind.js +2 -1
- package/dist/bind.js.map +1 -1
- package/dist/catchUp.d.ts +2 -1
- package/dist/catchUp.d.ts.map +1 -1
- package/dist/catchUp.js.map +1 -1
- package/dist/clientContract.d.ts +3 -3
- package/dist/clientContract.d.ts.map +1 -1
- package/dist/clientContract.js +1 -1
- package/dist/clientContract.js.map +1 -1
- package/dist/clientOperations.d.ts +5 -1
- package/dist/clientOperations.d.ts.map +1 -1
- package/dist/clientOperations.js +7 -2
- package/dist/clientOperations.js.map +1 -1
- package/dist/contract.d.ts +1 -0
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +1 -0
- package/dist/contract.js.map +1 -1
- package/dist/defineModule.d.ts +6 -8
- package/dist/defineModule.d.ts.map +1 -1
- package/dist/defineModule.js +1 -1
- package/dist/defineModule.js.map +1 -1
- package/dist/failureKind.d.ts +10 -0
- package/dist/failureKind.d.ts.map +1 -0
- package/dist/failureKind.js +20 -0
- package/dist/failureKind.js.map +1 -0
- package/dist/liveSyncRequest.d.ts +2 -2
- package/dist/liveSyncRequest.d.ts.map +1 -1
- package/dist/liveSyncRequest.js +1 -2
- package/dist/liveSyncRequest.js.map +1 -1
- package/dist/liveSyncRpc.d.ts +1 -0
- package/dist/liveSyncRpc.d.ts.map +1 -1
- package/dist/moduleHandlers.d.ts +3 -3
- package/dist/moduleHandlers.d.ts.map +1 -1
- package/dist/moduleHandlers.js.map +1 -1
- package/dist/moduleProof.d.ts +16 -33
- package/dist/moduleProof.d.ts.map +1 -1
- package/dist/operation.d.ts +4 -5
- package/dist/operation.d.ts.map +1 -1
- package/dist/operation.js +2 -2
- package/dist/operation.js.map +1 -1
- package/dist/operationKind.d.ts +3 -0
- package/dist/operationKind.d.ts.map +1 -0
- package/dist/operationKind.js +14 -0
- package/dist/operationKind.js.map +1 -0
- package/dist/rpcClient.d.ts +2 -1
- package/dist/rpcClient.d.ts.map +1 -1
- package/dist/rpcClient.js.map +1 -1
- package/dist/runProof.d.ts +3 -41
- package/dist/runProof.d.ts.map +1 -1
- package/dist/servedOperation.d.ts +28 -25
- package/dist/servedOperation.d.ts.map +1 -1
- package/dist/servedOperation.js +9 -3
- package/dist/servedOperation.js.map +1 -1
- package/dist/subject.d.ts +1 -1
- package/dist/subject.d.ts.map +1 -1
- package/dist/subjectLayers.d.ts +1 -1
- package/dist/subjectLayers.d.ts.map +1 -1
- package/dist/subjectLayers.js.map +1 -1
- package/dist/wireProof.d.ts +48 -0
- package/dist/wireProof.d.ts.map +1 -0
- package/dist/wireProof.js +2 -0
- package/dist/wireProof.js.map +1 -0
- package/package.json +15 -15
- package/src/adapters/erasedClient.ts +43 -0
- package/src/automaticLive.ts +5 -15
- package/src/bind.ts +15 -14
- package/src/catchUp.ts +2 -1
- package/src/clientContract.ts +5 -5
- package/src/clientOperations.ts +8 -10
- package/src/contract.ts +2 -0
- package/src/defineModule.ts +17 -14
- package/src/failureKind.ts +33 -0
- package/src/liveSyncRequest.ts +3 -11
- package/src/liveSyncRpc.ts +2 -0
- package/src/moduleHandlers.ts +3 -3
- package/src/moduleProof.ts +29 -46
- package/src/operation.ts +8 -16
- package/src/operationKind.ts +15 -0
- package/src/rpcClient.ts +3 -3
- package/src/runProof.ts +3 -61
- package/src/servedOperation.ts +47 -47
- package/src/subject.ts +1 -1
- package/src/subjectLayers.ts +6 -2
- package/src/wireProof.ts +104 -0
- package/dist/successProof.d.ts +0 -51
- package/dist/successProof.d.ts.map +0 -1
- package/dist/successProof.js +0 -2
- package/dist/successProof.js.map +0 -1
- package/src/successProof.ts +0 -120
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Predicate } from "effect";
|
|
2
|
+
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
3
|
+
import { isRequestRejection, isServerFailure, type RequestRejection, type ServerFailure } from "@shivaedev/errors/taxonomy.ts";
|
|
4
|
+
import { MissingReadPolicies, MissingReadPolicy, QueryReadDenied } from "#readPolicyErrors.ts";
|
|
5
|
+
import { WebSocketRequired } from "#WebSocketRequired.ts";
|
|
6
|
+
|
|
7
|
+
// The transport, the server's own failures and the contract's middleware fail every operation alike; no screen chose them.
|
|
8
|
+
export type FrameworkFailure = ServerFailure | RpcClientError | MissingReadPolicy | MissingReadPolicies | QueryReadDenied | WebSocketRequired;
|
|
9
|
+
|
|
10
|
+
// What remains of an operation's failure once the shared request rejections and the framework's failures are taken out: the rejections its
|
|
11
|
+
// feature declared for itself.
|
|
12
|
+
export type AppRejection<E> = Exclude<E, RequestRejection | FrameworkFailure>;
|
|
13
|
+
|
|
14
|
+
export type FailureKind = "app" | "framework" | "request";
|
|
15
|
+
|
|
16
|
+
export function isFrameworkFailure(value: unknown): value is FrameworkFailure {
|
|
17
|
+
return (
|
|
18
|
+
isServerFailure(value)
|
|
19
|
+
|| Predicate.isTagged(value, "RpcClientError")
|
|
20
|
+
|| value instanceof MissingReadPolicy
|
|
21
|
+
|| value instanceof MissingReadPolicies
|
|
22
|
+
|| value instanceof QueryReadDenied
|
|
23
|
+
|| value instanceof WebSocketRequired
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function failureKind(error: unknown): FailureKind {
|
|
28
|
+
if (isRequestRejection(error)) {
|
|
29
|
+
return "request";
|
|
30
|
+
}
|
|
31
|
+
const tagged = Predicate.hasProperty(error, "_tag") && Predicate.isString(error._tag);
|
|
32
|
+
return tagged && !isFrameworkFailure(error) ? "app" : "framework";
|
|
33
|
+
}
|
package/src/liveSyncRequest.ts
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import type { Effect, Schema } from "effect";
|
|
2
1
|
import type { Rpc, RpcClient } from "effect/rpc";
|
|
3
2
|
import type { RpcClientError } from "effect/rpc/RpcClientError";
|
|
4
3
|
import type { SyncToken } from "@shivaedev/changes/syncToken.ts";
|
|
5
|
-
import { LIVE_SYNC } from "#liveSyncRpc.ts";
|
|
4
|
+
import { LIVE_SYNC, type LiveSyncRpc } from "#liveSyncRpc.ts";
|
|
6
5
|
import { commandSyncHeaders } from "#syncHeader.ts";
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export function liveSyncRequest(client: RpcClient.RpcClient.Flat<Rpc.Any, RpcClientError>, token: SyncToken): Effect.Effect<void, unknown>;
|
|
11
|
-
export function liveSyncRequest(
|
|
12
|
-
client: RpcClient.RpcClient.Flat<Rpc.Any, RpcClientError>,
|
|
13
|
-
token: SyncToken,
|
|
14
|
-
): Effect.Effect<unknown, unknown, unknown> {
|
|
15
|
-
const erased: RpcClient.RpcClient.Flat<Erased, RpcClientError> = client;
|
|
16
|
-
return erased(LIVE_SYNC, undefined, { headers: commandSyncHeaders(token) });
|
|
7
|
+
export function liveSyncRequest(client: RpcClient.RpcClient.Flat<Rpc.Any | LiveSyncRpc, RpcClientError>, token: SyncToken) {
|
|
8
|
+
return client(LIVE_SYNC, undefined, { headers: commandSyncHeaders(token) });
|
|
17
9
|
}
|
package/src/liveSyncRpc.ts
CHANGED
|
@@ -9,3 +9,5 @@ export const LIVE_SYNC: string = LIVE_SYNC_TAG;
|
|
|
9
9
|
|
|
10
10
|
// The browser's copy of the sync command: the host's declaration in LiveSync.ts is an operation file, which a browser bundle refuses.
|
|
11
11
|
export const liveSyncRpc = RpcGroup.make(Rpc.make(LIVE_SYNC_TAG, { success: Schema.Void }).annotate(CommandInvalidation, { invalidates: () => [] }));
|
|
12
|
+
|
|
13
|
+
export type LiveSyncRpc = RpcGroup.Rpcs<typeof liveSyncRpc>;
|
package/src/moduleHandlers.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Layer } from "effect";
|
|
2
2
|
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
3
|
-
import type { RequirementRecord, RequirementsOf } from "@shivaedev/service/ServiceRequirements.ts";
|
|
4
3
|
import { servedHandlers } from "#adapters/servedHandlers.ts";
|
|
5
4
|
import type { Declared } from "#contract.ts";
|
|
5
|
+
import type { ModuleServices } from "#moduleProof.ts";
|
|
6
6
|
import type { CommandShape, QueryShape, StreamShape } from "#operation.ts";
|
|
7
7
|
|
|
8
8
|
export interface AnyModule {
|
|
9
|
-
readonly "~requires":
|
|
9
|
+
readonly "~requires": ModuleServices;
|
|
10
10
|
readonly application: { readonly requests: ReadonlyMap<string, Rpc.Any> };
|
|
11
11
|
readonly declaration: Declared<string, readonly QueryShape[], readonly CommandShape[], readonly StreamShape[], "session" | undefined>;
|
|
12
12
|
}
|
|
@@ -14,7 +14,7 @@ export interface AnyModule {
|
|
|
14
14
|
export type ModuleHandlers<TModule extends AnyModule> = Layer.Layer<
|
|
15
15
|
Rpc.ToHandler<RpcGroup.Rpcs<TModule["application"]>>,
|
|
16
16
|
never,
|
|
17
|
-
|
|
17
|
+
TModule["~requires"][keyof TModule["~requires"]]
|
|
18
18
|
>;
|
|
19
19
|
|
|
20
20
|
export function moduleHandlers<TModule extends AnyModule>(module: TModule): ModuleHandlers<TModule>;
|
package/src/moduleProof.ts
CHANGED
|
@@ -1,27 +1,11 @@
|
|
|
1
|
-
import type { Effect, Scope } from "effect";
|
|
1
|
+
import type { Effect, Schema, Scope } from "effect";
|
|
2
2
|
import type { RpcMiddleware } from "effect/rpc";
|
|
3
|
-
import type { InfrastructureFailure } from "@shivaedev/errors/infrastructure.ts";
|
|
4
|
-
import type { RequirementRecord, RequirementsOf } from "@shivaedev/service/ServiceRequirements.ts";
|
|
5
3
|
import type { Duplicated } from "#declarationError.ts";
|
|
6
4
|
import type { OperationShape } from "#operation.ts";
|
|
7
|
-
import type {
|
|
8
|
-
import type { RejectedBy, RejectionSpecs } from "#rejection.ts";
|
|
9
|
-
import type { Raised, RejectionProblem } from "#runProof.ts";
|
|
5
|
+
import type { Raised } from "#runProof.ts";
|
|
10
6
|
import type { SubjectDeclaration, SubjectProvides } from "#subject.ts";
|
|
11
|
-
import type { SuccessProblem } from "#successProof.ts";
|
|
12
7
|
import type { WireEntry, WireEntryOf } from "#wire.ts";
|
|
13
|
-
|
|
14
|
-
export interface OperationNeedsUndeclaredServices<TOperation extends string, TMissing> {
|
|
15
|
-
readonly missingServices: TMissing;
|
|
16
|
-
readonly operation: TOperation;
|
|
17
|
-
readonly problem: "run or readPolicy uses services that the module's requires and middleware do not provide";
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface OperationFailsOutsideItsRejections<TOperation extends string, TUndeclared> {
|
|
21
|
-
readonly operation: TOperation;
|
|
22
|
-
readonly problem: "run or readPolicy fails with errors outside the operation's rejections";
|
|
23
|
-
readonly undeclaredFailures: TUndeclared;
|
|
24
|
-
}
|
|
8
|
+
import type { RejectionsDiffer, SuccessDiffers } from "#wireProof.ts";
|
|
25
9
|
|
|
26
10
|
type Regenerate = "the generated file is stale; run pnpm generate";
|
|
27
11
|
|
|
@@ -42,7 +26,7 @@ export interface WireRejectionsStale<TOperation extends string, TFix extends Reg
|
|
|
42
26
|
export interface OperationMissingFromWire<TOperation extends string, TFix extends Regenerate> {
|
|
43
27
|
readonly fix: TFix;
|
|
44
28
|
readonly operation: TOperation;
|
|
45
|
-
readonly problem: "
|
|
29
|
+
readonly problem: "run returns a value or fails with a rejection, and the module's wire has no entry for the operation";
|
|
46
30
|
}
|
|
47
31
|
|
|
48
32
|
export interface OperationNameRepeated<TOperation extends string> {
|
|
@@ -57,45 +41,44 @@ type Parts<TOperation> =
|
|
|
57
41
|
| (TOperation extends { readonly kind: "query"; readonly readPolicy: infer TPolicy extends Effectful } ? ReturnType<TPolicy> : never);
|
|
58
42
|
|
|
59
43
|
type Returns<TOperation> = TOperation extends { readonly run: infer TRun extends Effectful } ? Effect.Success<ReturnType<TRun>> : never;
|
|
60
|
-
type Declared<TOperation> = TOperation extends { readonly rejections: infer TSpecs extends RejectionSpecs } ? RejectedBy<TSpecs> : never;
|
|
61
44
|
type Tags<E> = E extends { readonly _tag: infer TTag } ? TTag : E;
|
|
62
|
-
type Success<TOperation> = TOperation extends { readonly success: infer TSuccess } ? TSuccess : never;
|
|
63
45
|
type NameOf<TOperation> = TOperation extends { readonly name: infer TName extends string } ? TName : string;
|
|
64
46
|
|
|
65
|
-
type
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// An entry stands in for the operation's own success and rejections, so only the entry is compared with run.
|
|
69
|
-
type WireProblem<TOperation, TEntry extends WireEntry> = [SuccessProblem<NameOf<TOperation>, TEntry["success"], Returns<TOperation>>] extends [never]
|
|
70
|
-
? [RejectionProblem<NameOf<TOperation>, TEntry["rejections"], Effect.Error<Parts<TOperation>>>] extends [never]
|
|
47
|
+
type WireProblem<TOperation, TEntry extends WireEntry> = [SuccessDiffers<TEntry["success"], Returns<TOperation>>] extends [never]
|
|
48
|
+
? [RejectionsDiffer<TEntry["rejections"], Effect.Error<Parts<TOperation>>>] extends [never]
|
|
71
49
|
? never
|
|
72
50
|
: WireRejectionsStale<NameOf<TOperation>, Regenerate, Tags<Raised<Effect.Error<Parts<TOperation>>>>, keyof TEntry["rejections"]>
|
|
73
51
|
: WireSuccessStale<NameOf<TOperation>, Regenerate, Returns<TOperation>, TEntry["success"]["Type"]>;
|
|
74
52
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
:
|
|
53
|
+
// Without an entry the operation is served with Void and the automatic rejections, which fit only a run that returns nothing
|
|
54
|
+
// and raises nothing else.
|
|
55
|
+
type EntryProblem<TOperation> = [SuccessDiffers<Schema.Void, Returns<TOperation>> | Raised<Effect.Error<Parts<TOperation>>>] extends [never]
|
|
56
|
+
? never
|
|
57
|
+
: OperationMissingFromWire<NameOf<TOperation>, Regenerate>;
|
|
80
58
|
|
|
81
|
-
type OperationProblem<TOperation, TWire
|
|
82
|
-
|
|
83
|
-
? WireProblem<TOperation, TEntry>
|
|
84
|
-
: OwnProblem<TOperation>
|
|
85
|
-
: OperationNeedsUndeclaredServices<NameOf<TOperation>, Missing<TOperation, TAllowed>>;
|
|
59
|
+
type OperationProblem<TOperation, TWire> =
|
|
60
|
+
WireEntryOf<TWire, NameOf<TOperation>> extends infer TEntry extends WireEntry ? WireProblem<TOperation, TEntry> : EntryProblem<TOperation>;
|
|
86
61
|
|
|
87
62
|
type SubjectServices<TSubject> = TSubject extends SubjectDeclaration ? SubjectProvides<TSubject> : never;
|
|
88
63
|
|
|
89
|
-
export type
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
64
|
+
export type ModuleServices = { readonly [TOperation in string]: unknown };
|
|
65
|
+
|
|
66
|
+
export type ModuleRequirements<TOperations extends readonly OperationShape[], TMiddleware extends RpcMiddleware.AnyService, TSubject> = {
|
|
67
|
+
readonly [TOperation in TOperations[number] as NameOf<TOperation>]: Exclude<
|
|
68
|
+
Effect.Services<Parts<TOperation>>,
|
|
69
|
+
RpcMiddleware.Provides<TMiddleware["Identifier"]> | SubjectServices<TSubject> | Scope.Scope
|
|
70
|
+
>;
|
|
71
|
+
};
|
|
93
72
|
|
|
94
|
-
type Problems<TOperations extends readonly OperationShape[], TWire
|
|
95
|
-
| { readonly [TIndex in keyof TOperations]: OperationProblem<TOperations[TIndex], TWire
|
|
73
|
+
type Problems<TOperations extends readonly OperationShape[], TWire> =
|
|
74
|
+
| { readonly [TIndex in keyof TOperations]: OperationProblem<TOperations[TIndex], TWire> }[number]
|
|
96
75
|
| (Duplicated<TOperations> extends infer TName extends string ? (TName extends string ? OperationNameRepeated<TName> : never) : never);
|
|
97
76
|
|
|
98
77
|
// The proof sits on the module's name, so the first line of the compiler's message names the operation and its problem.
|
|
99
|
-
|
|
78
|
+
// A definition without a wire leaves TWire at its constraint, whose index signature has every name; proving nothing then
|
|
79
|
+
// lets the compiler's own error say that wire is missing.
|
|
80
|
+
export type NameProof<TOperations extends readonly OperationShape[], TWire> = string extends keyof TWire
|
|
100
81
|
? unknown
|
|
101
|
-
: Problems<TOperations, TWire
|
|
82
|
+
: [Problems<TOperations, TWire>] extends [never]
|
|
83
|
+
? unknown
|
|
84
|
+
: Problems<TOperations, TWire>;
|
package/src/operation.ts
CHANGED
|
@@ -5,9 +5,8 @@ import { type CommandDeclaration, type Loose, operation, type QueryDeclaration,
|
|
|
5
5
|
import type { QueryServerReads } from "#QueryServerReads.ts";
|
|
6
6
|
import { type QueryReadPolicy, signedIn, type Viewer } from "#readPolicy.ts";
|
|
7
7
|
import type { Reject, RejectedBy, RejectionSpecs, Rejections, RejectionUnion } from "#rejection.ts";
|
|
8
|
-
import type {
|
|
9
|
-
import
|
|
10
|
-
import { runOf, withAutomatic } from "#servedOperation.ts";
|
|
8
|
+
import type { LooseCommand, LooseQuery, RunCommandDeclaration, RunQueryDeclaration, ServedCommandOf, ServedQueryOf } from "#servedOperation.ts";
|
|
9
|
+
import { invalidatesOf, runOf, withAutomatic } from "#servedOperation.ts";
|
|
11
10
|
|
|
12
11
|
export type PayloadSchema<TPayload extends Schema.Top | Schema.Struct.Fields> = TPayload extends Schema.Struct.Fields
|
|
13
12
|
? Schema.Struct<TPayload>
|
|
@@ -90,18 +89,16 @@ export function query<
|
|
|
90
89
|
export function query<
|
|
91
90
|
const TName extends string,
|
|
92
91
|
TYielded extends Effect.Effect<unknown, unknown, unknown> = never,
|
|
93
|
-
TReturn =
|
|
92
|
+
TReturn = unknown,
|
|
94
93
|
TPayload extends Schema.Top | Schema.Struct.Fields = Schema.Void,
|
|
95
|
-
TSuccess extends Schema.Top | undefined = undefined,
|
|
96
|
-
const TSpecs extends ClassRejections | undefined = undefined,
|
|
97
94
|
TPolicyError = never,
|
|
98
95
|
TPolicyServices = never,
|
|
99
96
|
TReads extends Effect.Effect<readonly Key[], unknown, unknown> = never,
|
|
100
97
|
const TCollections extends readonly { readonly name: string }[] | undefined = undefined,
|
|
101
98
|
>(
|
|
102
99
|
name: TName,
|
|
103
|
-
declaration: RunQueryDeclaration<TName, TPayload, TYielded, TReturn,
|
|
104
|
-
): NoInfer<ServedQueryOf<TName, TPayload, TYielded, TReturn,
|
|
100
|
+
declaration: RunQueryDeclaration<TName, TPayload, TYielded, TReturn, TPolicyError, TPolicyServices, TReads, TCollections>,
|
|
101
|
+
): NoInfer<ServedQueryOf<TName, TPayload, TYielded, TReturn, TPolicyError, TPolicyServices, TReads>>;
|
|
105
102
|
export function query(name: string, declaration: LooseQuery): QueryShape {
|
|
106
103
|
const collections: readonly Key[] = declaration.readsCollections?.map((collection) => ({ _tag: "List", collection: collection.name })) ?? [];
|
|
107
104
|
return {
|
|
@@ -126,16 +123,11 @@ export function command<
|
|
|
126
123
|
export function command<
|
|
127
124
|
const TName extends string,
|
|
128
125
|
TYielded extends Effect.Effect<unknown, unknown, unknown> = never,
|
|
129
|
-
TReturn =
|
|
126
|
+
TReturn = unknown,
|
|
130
127
|
TPayload extends Schema.Top | Schema.Struct.Fields = Schema.Void,
|
|
131
|
-
|
|
132
|
-
const TSpecs extends ClassRejections | undefined = undefined,
|
|
133
|
-
>(
|
|
134
|
-
name: TName,
|
|
135
|
-
declaration: RunCommandDeclaration<TName, TPayload, TYielded, TReturn, TSuccess, TSpecs>,
|
|
136
|
-
): NoInfer<ServedCommand<TName, PayloadSchema<TPayload>, SuccessSchema<TSuccess>, SpecsOf<TSpecs>, RunEffect<TYielded, TReturn>>>;
|
|
128
|
+
>(name: TName, declaration: RunCommandDeclaration<TName, TPayload, TYielded, TReturn>): NoInfer<ServedCommandOf<TName, TPayload, TYielded, TReturn>>;
|
|
137
129
|
export function command(name: string, declaration: LooseCommand): CommandShape {
|
|
138
|
-
return { ...operation("command", name, withAutomatic(declaration)), invalidates: declaration
|
|
130
|
+
return { ...operation("command", name, withAutomatic(declaration)), invalidates: invalidatesOf(declaration), ...runOf(declaration) };
|
|
139
131
|
}
|
|
140
132
|
|
|
141
133
|
export function stream<
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Context, Option } from "effect";
|
|
2
|
+
import { type Rpc, RpcSchema } from "effect/rpc";
|
|
3
|
+
import { ChangesQueries, CommandInvalidation, QueryDefinition } from "#serverMetadata.ts";
|
|
4
|
+
|
|
5
|
+
// A query, a live query's changes stream and any other stream only read; a command, or an RPC no contract marked, may write.
|
|
6
|
+
export function operationKind(rpc: Pick<Rpc.AnyWithProps, "annotations" | "successSchema">): "read" | "write" {
|
|
7
|
+
if (Option.isSome(Context.getOption(rpc.annotations, CommandInvalidation))) {
|
|
8
|
+
return "write";
|
|
9
|
+
}
|
|
10
|
+
const reads =
|
|
11
|
+
Option.isSome(Context.getOption(rpc.annotations, QueryDefinition))
|
|
12
|
+
|| Option.isSome(Context.getOption(rpc.annotations, ChangesQueries))
|
|
13
|
+
|| RpcSchema.isStreamSchema(rpc.successSchema);
|
|
14
|
+
return reads ? "read" : "write";
|
|
15
|
+
}
|
package/src/rpcClient.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { Activity } from "#clientActivity.ts";
|
|
|
10
10
|
import type { ClientOptions } from "#clientOptions.ts";
|
|
11
11
|
import { ConnectionScope, connectionProtocol } from "#connectionProtocol.ts";
|
|
12
12
|
import { commandConnections } from "#connectionReadiness.ts";
|
|
13
|
-
import { liveSyncRpc } from "#liveSyncRpc.ts";
|
|
13
|
+
import { type LiveSyncRpc, liveSyncRpc } from "#liveSyncRpc.ts";
|
|
14
14
|
import { reflections } from "#reflection.ts";
|
|
15
15
|
import type { ResumeWindow } from "#resume.ts";
|
|
16
16
|
import { CommandInvalidation } from "#serverMetadata.ts";
|
|
@@ -53,7 +53,7 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
53
53
|
}
|
|
54
54
|
const protocolFor = configuredProtocol();
|
|
55
55
|
const protocol = protocolFor(undefined);
|
|
56
|
-
function observeClient(client: RpcClient.RpcClient.Flat<Rpc.Any, RpcClientError>, wait: Effect.Effect<void, RpcClientError>) {
|
|
56
|
+
function observeClient(client: RpcClient.RpcClient.Flat<Rpc.Any | LiveSyncRpc, RpcClientError>, wait: Effect.Effect<void, RpcClientError>) {
|
|
57
57
|
const observed = new Proxy(client, {
|
|
58
58
|
apply(target, receiver, args) {
|
|
59
59
|
const result = Reflect.apply(target, receiver, args);
|
|
@@ -72,7 +72,7 @@ export function rpcClient(name: string, source: RpcSource, options: ClientOption
|
|
|
72
72
|
const make = Effect.gen(function* () {
|
|
73
73
|
const { connection, reflection } = yield* ConnectionScope;
|
|
74
74
|
const wait = Effect.suspend(() => (connection.isReady() ? Effect.void : activity.hold(connection.wait)));
|
|
75
|
-
const native = yield* RpcClient.make
|
|
75
|
+
const native = yield* RpcClient.make(group, { flatten: true });
|
|
76
76
|
const observed = observeClient(native, wait);
|
|
77
77
|
// Only the shared socket proves a command reaches the host that holds its streams; any other protocol syncs after the reply.
|
|
78
78
|
reflections.set(observed, { piggyback: options.protocol === undefined, reflection });
|
package/src/runProof.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import type { Effect
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
2
|
import type { InfrastructureFailure } from "@shivaedev/errors/infrastructure.ts";
|
|
3
3
|
import type { InternalServerError, ServiceUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
4
4
|
import type { QueryReadDenied } from "#readPolicyErrors.ts";
|
|
5
5
|
import type { TaggedRejection } from "#rejection.ts";
|
|
6
|
-
import type { Identical, SuccessProblem } from "#successProof.ts";
|
|
7
6
|
|
|
8
7
|
export type RunBody<TPayload, TYielded, A> = (payload: TPayload) => Generator<TYielded, A, never>;
|
|
9
8
|
|
|
@@ -22,75 +21,18 @@ export interface Automatic {
|
|
|
22
21
|
|
|
23
22
|
export type Raised<E> = Exclude<E, InfrastructureFailure | QueryReadDenied | InternalServerError | ServiceUnavailable>;
|
|
24
23
|
|
|
25
|
-
export interface RunFailsOutsideRejections<TOperation extends string, TTags> {
|
|
26
|
-
readonly missingRejections: TTags;
|
|
27
|
-
readonly operation: TOperation;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface RejectionsRunNeverRaises<TOperation extends string, TTags> {
|
|
31
|
-
readonly neverRaised: TTags;
|
|
32
|
-
readonly operation: TOperation;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface RejectionAutomatic<TOperation extends string, TTags> {
|
|
36
|
-
readonly automatic: TTags;
|
|
37
|
-
readonly operation: TOperation;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface RejectionClassDiffers<TOperation extends string, TTags> {
|
|
41
|
-
readonly operation: TOperation;
|
|
42
|
-
readonly raisedClassDiffers: TTags;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
24
|
export interface RunReturnsAny<TOperation extends string, TFix extends string> {
|
|
46
25
|
readonly fix: TFix;
|
|
47
26
|
readonly operation: TOperation;
|
|
48
27
|
}
|
|
49
28
|
|
|
50
|
-
type Tags<E> = E extends { readonly _tag: infer TTag extends string } ? TTag : never;
|
|
51
29
|
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
52
|
-
type Declared<TSpecs> = TSpecs extends ClassRejections ? keyof TSpecs & string : never;
|
|
53
|
-
type AutomaticTags = keyof Automatic;
|
|
54
|
-
|
|
55
|
-
type DifferentClasses<TSpecs, E> = {
|
|
56
|
-
readonly [TTag in Declared<TSpecs>]: TSpecs extends { readonly [TKey in TTag]: new (...args: never) => infer TDeclared }
|
|
57
|
-
? Identical<TDeclared, Extract<Raised<E>, { readonly _tag: TTag }>> extends true
|
|
58
|
-
? never
|
|
59
|
-
: TTag
|
|
60
|
-
: never;
|
|
61
|
-
}[Declared<TSpecs>];
|
|
62
|
-
|
|
63
|
-
export type RejectionProblem<TName extends string, TSpecs, E> = TSpecs extends undefined
|
|
64
|
-
? never
|
|
65
|
-
: [Extract<Declared<TSpecs>, AutomaticTags>] extends [never]
|
|
66
|
-
? [Exclude<Tags<Raised<E>>, Declared<TSpecs>>] extends [never]
|
|
67
|
-
? [Exclude<Declared<TSpecs>, Tags<Raised<E>>>] extends [never]
|
|
68
|
-
? [DifferentClasses<TSpecs, E>] extends [never]
|
|
69
|
-
? never
|
|
70
|
-
: RejectionClassDiffers<TName, DifferentClasses<TSpecs, E>>
|
|
71
|
-
: RejectionsRunNeverRaises<TName, Exclude<Declared<TSpecs>, Tags<Raised<E>>>>
|
|
72
|
-
: RunFailsOutsideRejections<TName, Exclude<Tags<Raised<E>>, Declared<TSpecs>>>
|
|
73
|
-
: RejectionAutomatic<TName, Extract<Declared<TSpecs>, AutomaticTags>>;
|
|
74
30
|
|
|
75
|
-
type Problems<TName extends string, TSuccess, TSpecs, TReturn, E> = SuccessProblem<TName, TSuccess, TReturn> | RejectionProblem<TName, TSpecs, E>;
|
|
76
|
-
|
|
77
|
-
export type SuccessSchema<TSuccess> = TSuccess extends Schema.Top ? TSuccess : Schema.Void;
|
|
78
31
|
// A mapped type, unlike the Automatic interface, satisfies the index signature of RejectionSpecs.
|
|
79
32
|
type Flat<T> = { readonly [TKey in keyof T]: T[TKey] };
|
|
80
33
|
export type SpecsOf<TSpecs> = TSpecs extends ClassRejections ? Flat<TSpecs & Automatic> : Flat<Automatic>;
|
|
81
34
|
|
|
82
|
-
|
|
83
|
-
export interface Pending {
|
|
84
|
-
readonly pendingInference: unique symbol;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
type IsPending<TReturn> = [TReturn] extends [never] ? false : [TReturn] extends [Pending] ? true : false;
|
|
88
|
-
|
|
89
|
-
export type RunCheck<TName extends string, TSuccess, TSpecs, TReturn, E> =
|
|
35
|
+
export type RunCheck<TName extends string, TReturn> =
|
|
90
36
|
IsAny<TReturn> extends true
|
|
91
37
|
? { readonly operationProblem: RunReturnsAny<TName, "decode the value with a schema, or give run's return a type"> }
|
|
92
|
-
:
|
|
93
|
-
? unknown
|
|
94
|
-
: [Problems<TName, TSuccess, TSpecs, TReturn, E>] extends [never]
|
|
95
|
-
? unknown
|
|
96
|
-
: { readonly operationProblem: Problems<TName, TSuccess, TSpecs, TReturn, E> };
|
|
38
|
+
: unknown;
|
package/src/servedOperation.ts
CHANGED
|
@@ -6,8 +6,8 @@ import type { Loose } from "#operationDeclaration.ts";
|
|
|
6
6
|
import type { PermissionReadPolicy } from "#permissionReadPolicy.ts";
|
|
7
7
|
import type { QueryServerReads } from "#QueryServerReads.ts";
|
|
8
8
|
import type { QueryReadPolicy, Viewer } from "#readPolicy.ts";
|
|
9
|
-
import type {
|
|
10
|
-
import type { ErrorOf, RunBody, RunCheck, RunEffect, ServicesOf, SpecsOf
|
|
9
|
+
import type { RejectionSpecs } from "#rejection.ts";
|
|
10
|
+
import type { ErrorOf, RunBody, RunCheck, RunEffect, ServicesOf, SpecsOf } from "#runProof.ts";
|
|
11
11
|
|
|
12
12
|
export interface ServedQuery<
|
|
13
13
|
TName extends string,
|
|
@@ -38,22 +38,14 @@ export interface ServedCommand<
|
|
|
38
38
|
readonly run: (payload: TPayload["Type"]) => TRun;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
interface RunFields<TPayload extends Schema.Top | Schema.Struct.Fields, TYielded, TReturn
|
|
41
|
+
interface RunFields<TPayload extends Schema.Top | Schema.Struct.Fields, TYielded, TReturn> {
|
|
42
42
|
readonly payload?: TPayload;
|
|
43
|
-
readonly rejections?: TSpecs & MatchingTags<NonNullable<TSpecs>>;
|
|
44
43
|
readonly run?: RunBody<PayloadSchema<TPayload>["Type"], TYielded, TReturn>;
|
|
45
|
-
readonly success?: TSuccess;
|
|
46
44
|
}
|
|
47
45
|
|
|
48
46
|
// A run required where its types are inferred makes TypeScript report a declaration without run against default type
|
|
49
47
|
// arguments, hiding the declaration's own mistakes; requiring it apart keeps those errors.
|
|
50
|
-
export type RunDeclaration<TPayload extends Schema.Top | Schema.Struct.Fields, TYielded, TReturn,
|
|
51
|
-
TPayload,
|
|
52
|
-
TYielded,
|
|
53
|
-
TReturn,
|
|
54
|
-
TSuccess,
|
|
55
|
-
TSpecs
|
|
56
|
-
> &
|
|
48
|
+
export type RunDeclaration<TPayload extends Schema.Top | Schema.Struct.Fields, TYielded, TReturn> = RunFields<TPayload, TYielded, TReturn> &
|
|
57
49
|
NoInfer<{ readonly run: RunBody<never, unknown, unknown> }>;
|
|
58
50
|
|
|
59
51
|
export type CollectionReadPolicy<TCollections, TPayload, E, R> = TCollections extends undefined
|
|
@@ -65,27 +57,23 @@ export type RunQueryDeclaration<
|
|
|
65
57
|
TPayload extends Schema.Top | Schema.Struct.Fields,
|
|
66
58
|
TYielded,
|
|
67
59
|
TReturn,
|
|
68
|
-
TSuccess,
|
|
69
|
-
TSpecs,
|
|
70
60
|
TPolicyError,
|
|
71
61
|
TPolicyServices,
|
|
72
62
|
TReads,
|
|
73
63
|
TCollections,
|
|
74
|
-
> = RunDeclaration<TPayload, TYielded, TReturn
|
|
64
|
+
> = RunDeclaration<TPayload, TYielded, TReturn> &
|
|
75
65
|
CollectionReadPolicy<TCollections, PayloadSchema<TPayload>["Type"], TPolicyError, TPolicyServices> & {
|
|
76
66
|
readonly readPolicy?: QueryReadPolicy<PayloadSchema<TPayload>["Type"], TPolicyError, TPolicyServices>;
|
|
77
67
|
readonly reads?: (payload: PayloadSchema<TPayload>["Type"], viewer: Option.Option<Viewer>) => readonly Key[];
|
|
78
68
|
readonly readsCollections?: TCollections;
|
|
79
69
|
readonly serverReads?: (payload: PayloadSchema<TPayload>["Type"], viewer: Option.Option<Viewer>) => TReads;
|
|
80
|
-
} & NoInfer<RunCheck<TName,
|
|
70
|
+
} & NoInfer<RunCheck<TName, TReturn>>;
|
|
81
71
|
|
|
82
72
|
export type ServedQueryOf<
|
|
83
73
|
TName extends string,
|
|
84
74
|
TPayload extends Schema.Top | Schema.Struct.Fields,
|
|
85
75
|
TYielded,
|
|
86
76
|
TReturn,
|
|
87
|
-
TSuccess,
|
|
88
|
-
TSpecs,
|
|
89
77
|
TPolicyError,
|
|
90
78
|
TPolicyServices,
|
|
91
79
|
TReads,
|
|
@@ -93,53 +81,65 @@ export type ServedQueryOf<
|
|
|
93
81
|
? ServedQuery<
|
|
94
82
|
TName,
|
|
95
83
|
PayloadSchema<TPayload>,
|
|
96
|
-
|
|
97
|
-
SpecsOf<
|
|
84
|
+
Schema.Void,
|
|
85
|
+
SpecsOf<undefined>,
|
|
98
86
|
TPolicyError | ErrorOf<TReads>,
|
|
99
87
|
TPolicyServices | ServicesOf<TReads>,
|
|
100
88
|
RunEffect<TYielded, TReturn>
|
|
101
89
|
>
|
|
102
90
|
: never;
|
|
103
91
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
92
|
+
export type ServedCommandOf<TName extends string, TPayload extends Schema.Top | Schema.Struct.Fields, TYielded, TReturn> = ServedCommand<
|
|
93
|
+
TName,
|
|
94
|
+
PayloadSchema<TPayload>,
|
|
95
|
+
Schema.Void,
|
|
96
|
+
SpecsOf<undefined>,
|
|
97
|
+
RunEffect<TYielded, TReturn>
|
|
98
|
+
>;
|
|
108
99
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
export type RunCommandDeclaration<
|
|
112
|
-
|
|
113
|
-
TPayload extends Schema.Top | Schema.Struct.Fields,
|
|
100
|
+
// Object keys are sorted, so invalidates is checked before run and cannot see its result: it is called without one, and
|
|
101
|
+
// keys computed from the result are contributed inside run with commandResult.
|
|
102
|
+
export type RunCommandDeclaration<TName extends string, TPayload extends Schema.Top | Schema.Struct.Fields, TYielded, TReturn> = RunDeclaration<
|
|
103
|
+
TPayload,
|
|
114
104
|
TYielded,
|
|
115
|
-
TReturn
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
readonly invalidates?: (payload: PayloadSchema<TPayload>["Type"], result: ResultOf<TSuccess>, viewer?: Option.Option<Viewer>) => readonly Key[];
|
|
120
|
-
} & NoInfer<RunCheck<TName, TSuccess, TSpecs, TReturn, ErrorOf<TYielded>>>;
|
|
121
|
-
|
|
122
|
-
interface LooseRun extends Loose {
|
|
123
|
-
readonly run?: (payload: never) => Generator<Effect.Effect<unknown, unknown, unknown>, unknown, never>;
|
|
124
|
-
}
|
|
105
|
+
TReturn
|
|
106
|
+
> & {
|
|
107
|
+
readonly invalidates?: (payload: PayloadSchema<TPayload>["Type"], viewer?: Option.Option<Viewer>) => readonly Key[];
|
|
108
|
+
} & NoInfer<RunCheck<TName, TReturn>>;
|
|
125
109
|
|
|
126
|
-
|
|
110
|
+
type RunGenerator = (payload: never) => Generator<Effect.Effect<unknown, unknown, unknown>, unknown, never>;
|
|
111
|
+
|
|
112
|
+
export interface LooseQuery extends Loose {
|
|
127
113
|
readonly readPolicy?: QueryReadPolicy<unknown, unknown, unknown>;
|
|
128
114
|
readonly reads?: (payload: unknown, viewer: Option.Option<Viewer>) => readonly Key[];
|
|
129
115
|
readonly readsCollections?: readonly { readonly name: string }[];
|
|
116
|
+
readonly run?: RunGenerator;
|
|
130
117
|
readonly serverReads?: QueryServerReads<unknown, unknown, unknown>;
|
|
131
118
|
}
|
|
132
119
|
|
|
133
|
-
|
|
134
|
-
|
|
120
|
+
type Invalidates = (payload: unknown, result: unknown, viewer?: Option.Option<Viewer>) => readonly Key[];
|
|
121
|
+
|
|
122
|
+
export type LooseCommand =
|
|
123
|
+
| (Loose & { readonly invalidates?: Invalidates; readonly run?: undefined })
|
|
124
|
+
| {
|
|
125
|
+
readonly invalidates?: (payload: unknown, viewer?: Option.Option<Viewer>) => readonly Key[];
|
|
126
|
+
readonly payload?: Schema.Top | Schema.Struct.Fields;
|
|
127
|
+
readonly run: RunGenerator;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export function withAutomatic(declaration: LooseQuery | LooseCommand): Loose {
|
|
131
|
+
const { payload, run } = declaration;
|
|
132
|
+
return run === undefined ? declaration : { ...(payload === undefined ? {} : { payload }), rejections: { InternalServerError, ServiceUnavailable } };
|
|
135
133
|
}
|
|
136
134
|
|
|
137
|
-
export function
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
135
|
+
export function invalidatesOf(declaration: LooseCommand): Invalidates {
|
|
136
|
+
if (declaration.run === undefined) {
|
|
137
|
+
return declaration.invalidates ?? (() => []);
|
|
138
|
+
}
|
|
139
|
+
const { invalidates } = declaration;
|
|
140
|
+
return invalidates === undefined ? () => [] : (payload, _result, viewer) => invalidates(payload, viewer);
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
export function runOf({ run }:
|
|
143
|
+
export function runOf({ run }: { readonly run?: RunGenerator | undefined }) {
|
|
144
144
|
return run === undefined ? {} : { run: Effect.fnUntraced(run) };
|
|
145
145
|
}
|
package/src/subject.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { subjectMiddleware } from "#subjectMiddleware.ts";
|
|
|
4
4
|
|
|
5
5
|
export interface SubjectDeclaration {
|
|
6
6
|
readonly error: Schema.Top;
|
|
7
|
-
readonly middleware: Effect.Effect<RpcMiddleware.Any,
|
|
7
|
+
readonly middleware: Effect.Effect<RpcMiddleware.Any, unknown, unknown>;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export type SubjectProvides<TSubject extends SubjectDeclaration> =
|
package/src/subjectLayers.ts
CHANGED
|
@@ -15,9 +15,13 @@ function isSubject(middleware: RpcMiddleware.AnyServiceWithProps): middleware is
|
|
|
15
15
|
|
|
16
16
|
export function subjectLayers<R extends Rpc.Any>(
|
|
17
17
|
group: RpcGroup.RpcGroup<R>,
|
|
18
|
-
): Layer.Layer<
|
|
18
|
+
): Layer.Layer<
|
|
19
|
+
Context.Service.Identifier<Middleware<R>>,
|
|
20
|
+
Effect.Error<Middleware<R>[typeof SUBJECT_TYPE_ID]["middleware"]>,
|
|
21
|
+
Effect.Services<Middleware<R>[typeof SUBJECT_TYPE_ID]["middleware"]>
|
|
22
|
+
>;
|
|
19
23
|
export function subjectLayers(group: RpcGroup.RpcGroup<Rpc.Any>) {
|
|
20
|
-
const layers: Layer.Layer<never,
|
|
24
|
+
const layers: Layer.Layer<never, unknown, unknown>[] = [];
|
|
21
25
|
for (const rpc of group.requests.values()) {
|
|
22
26
|
if (!Rpc.isRpc(rpc)) {
|
|
23
27
|
continue;
|