-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-action.ts
More file actions
134 lines (124 loc) · 3.96 KB
/
server-action.ts
File metadata and controls
134 lines (124 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Cause, Effect, Either, Exit, Layer, Schema as S } from "effect";
import { RequestContext } from "./request-context.ts";
import { Next } from "./next-service.ts";
import { ParseError } from "effect/ParseResult";
type NextRuntimeProvidedServices = Next;
type TypeConstraint<
Schema extends S.Schema.AnyNoContext,
ProvidedRuntimeServices,
> = (payload: S.Schema.Type<Schema>) => Promise<
Effect.Effect<
unknown,
unknown,
NextRuntimeProvidedServices | ProvidedRuntimeServices
>
>;
type InferSuccess<
Schema extends S.Schema.AnyNoContext,
ProvidedRuntimeServices,
TEffectFn extends TypeConstraint<Schema, ProvidedRuntimeServices>,
> = TEffectFn extends (
payload: S.Schema.Type<Schema>,
) => Promise<Effect.Effect<infer A, unknown, unknown>> ? A
: never;
type InferError<
Schema extends S.Schema.AnyNoContext,
ProvidedRuntimeServices,
TEffectFn extends TypeConstraint<Schema, ProvidedRuntimeServices>,
> = TEffectFn extends (
payload: S.Schema.Type<Schema>,
) => Promise<Effect.Effect<unknown, infer E, unknown>> ? E
: never;
export type InvalidPayloadOptions = {
schema: S.Schema.AnyNoContext;
payload: S.Schema.Type<S.Schema.AnyNoContext>;
error: ParseError;
};
export type HandlerConfig<
InternalServerError,
InvalidPayloadError,
ProvidedServices,
> = {
errors: {
invalidPayload: (options: InvalidPayloadOptions) => InvalidPayloadError;
unexpected: (cause: Cause.Cause<unknown>) => InternalServerError;
};
layer?: Layer.Layer<ProvidedServices, never, RequestContext>;
generateRequestId?: () => string;
};
export const makeServerActionHandler = <
InternalServerError,
InvalidPayloadError,
ProvidedServices,
>(
config: HandlerConfig<
InternalServerError,
InvalidPayloadError,
ProvidedServices
>,
) => {
return <
Schema extends S.Schema.AnyNoContext,
TEffectFn extends TypeConstraint<Schema, ProvidedServices>,
>(schema: Schema, effectFn: TEffectFn) => {
const mergedContext = Layer.mergeAll(
config.layer ?? Layer.empty,
Next.Default,
);
return async function (
payload: S.Schema.Type<Schema>,
): Promise<
| InferSuccess<Schema, ProvidedServices, TEffectFn>
| InferError<Schema, ProvidedServices, TEffectFn>
| InternalServerError
| InvalidPayloadError
> {
const validatedPayload = S.decodeUnknownEither(schema)(payload).pipe(
Either.mapLeft((error) =>
config.errors.invalidPayload({ schema, payload, error })
),
);
if (Either.isLeft(validatedPayload)) return validatedPayload.left;
const effect = await effectFn(validatedPayload.right);
const responseExit = await Effect.runPromiseExit(
// @ts-expect-error: this is correct
effect.pipe(
Effect.catchTag(
// @ts-expect-error: this can be thrown by Next service, so we need to catch it
"NextUnexpectedError",
(error) => Effect.fail(config.errors.unexpected(Cause.fail(error))),
),
Effect.catchTag(
// @ts-expect-error: this can be thrown by Next service, so we need to catch it
"NextPayloadError",
(payload) => Effect.fail(config.errors.invalidPayload(payload)),
),
Effect.provide(mergedContext),
Effect.provideService(
RequestContext,
RequestContext.of({
type: "server-action",
requestId: config.generateRequestId?.() ?? crypto.randomUUID(),
rawRequest: payload,
}),
),
),
);
if (Exit.isSuccess(responseExit)) {
return responseExit.value as InferSuccess<
Schema,
ProvidedServices,
TEffectFn
>;
}
if (Cause.isFailType(responseExit.cause)) {
return responseExit.cause.error as InferError<
Schema,
ProvidedServices,
TEffectFn
>;
}
return config.errors.unexpected(responseExit.cause);
};
};
};