-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext-service.ts
More file actions
188 lines (174 loc) · 5.63 KB
/
next-service.ts
File metadata and controls
188 lines (174 loc) · 5.63 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Cause, Effect, Schema as S } from "effect";
import { cookies, headers } from "next/headers.js";
import { notFound, redirect, type RedirectType } from "next/navigation.js";
import { revalidatePath, revalidateTag } from "next/cache.js";
import { RequestContext } from "./request-context.ts";
export class NextPayloadError
extends S.TaggedError<NextPayloadError>("next-effect/NextPayloadError")(
"NextPayloadError",
{
schema: S.Any,
payload: S.Any,
error: S.Any,
},
) {}
export class NextUnexpectedError
extends S.TaggedError<NextUnexpectedError>("next-effect/NextUnexpectedError")(
"NextUnexpectedError",
{
cause: S.Cause({ defect: S.Unknown, error: S.Unknown }),
},
) {}
export class Next extends Effect.Service<Next>()("next-effect/Next", {
sync: () => {
const getCookieJar = Effect.tryPromise({
try: () => cookies(),
catch: (error) => error,
}).pipe(
Effect.tapErrorCause((cause) => Effect.logError(Cause.pretty(cause))),
Effect.mapError((error) =>
new NextUnexpectedError({ cause: Cause.fail(error) })
),
Effect.withSpan("Next.getCookieJar"),
);
const getHeaders = Effect.tryPromise({
try: () => headers(),
catch: (error) => error,
}).pipe(
Effect.tapErrorCause((cause) => Effect.logError(Cause.pretty(cause))),
Effect.mapError((error) =>
new NextUnexpectedError({ cause: Cause.fail(error) })
),
Effect.withSpan("Next.getHeaders"),
);
const redirectTo = (url: string, type?: RedirectType) =>
Effect.sync(() => redirect(url, type)).pipe(
Effect.withSpan("Next.redirectTo", { attributes: { url, type } }),
);
const failRedirectTo =
(url: string, type?: RedirectType) =>
<A, E, R>(input: Effect.Effect<A, E, R>) =>
Effect.tapErrorCause(input, () => redirectTo(url, type));
const nextRevalidatePath = (
originalPath: string,
type?: "layout" | "page",
) =>
Effect.sync(() => revalidatePath(originalPath, type)).pipe(
Effect.withSpan("Next.revalidatePath", {
attributes: { originalPath, type },
}),
);
const nextRevalidateTag = (tag: string) =>
Effect.sync(() => revalidateTag(tag)).pipe(
Effect.withSpan("Next.revalidateTag", { attributes: { tag } }),
);
const nextNotFound = Effect.sync(() => notFound()).pipe(
Effect.withSpan("Next.notFound"),
);
const failNotFound = <A, E, R>(input: Effect.Effect<A, E, R>) =>
Effect.tapErrorCause(input, () => nextNotFound);
const ensureSchema = <RequestSchema extends S.Schema.AnyNoContext>(
schema: RequestSchema,
requestValue: unknown,
): Effect.Effect<
S.Schema.Type<RequestSchema>,
NextPayloadError,
RequestContext
> =>
Effect.gen(function* () {
const decodeResult = yield* S.decodeUnknown(schema)(requestValue).pipe(
Effect.mapError((parseError) =>
new NextPayloadError({
schema,
payload: requestValue,
error: parseError,
})
),
);
return decodeResult as S.Schema.Type<RequestSchema>;
});
/**
* Only used by route handlers.
* @since 1.0.0
*/
const ensureRequestSchema = <RequestSchema extends S.Schema.AnyNoContext>(
schema: RequestSchema,
) =>
Effect.gen(function* () {
const { rawRequest } = yield* RequestContext;
if (!(rawRequest instanceof Request)) {
return yield* Effect.dieMessage(
`You must only use \`ensureRequestSchema\` in route handlers.`,
);
}
const jsonBody = yield* Effect.tryPromise({
try: () => rawRequest.json(),
catch: (error) => error,
}).pipe(
Effect.mapError((error) =>
new NextPayloadError({ schema, payload: rawRequest, error })
),
);
return yield* ensureSchema(schema, jsonBody);
}).pipe(
Effect.withSpan("Next.ensureRequestSchema"),
);
const text = Effect.gen(function* () {
const { rawRequest } = yield* RequestContext;
if (!(rawRequest instanceof Request)) {
return yield* Effect.dieMessage(
`You must only use \`text\` in route handlers.`,
);
}
return yield* Effect.tryPromise({
try: () => rawRequest.text(),
catch: (error) => error,
}).pipe(
Effect.mapError((error) =>
new NextUnexpectedError({ cause: Cause.fail(error) })
),
);
}).pipe(
Effect.withSpan("Next.text"),
);
const arrayBuffer = Effect.gen(function* () {
const { rawRequest } = yield* RequestContext;
if (!(rawRequest instanceof Request)) {
return yield* Effect.dieMessage(
`You must only use \`arrayBuffer\` in route handlers.`,
);
}
return yield* Effect.tryPromise({
try: () => rawRequest.arrayBuffer(),
catch: (error) => error,
}).pipe(
Effect.mapError((error) =>
new NextUnexpectedError({ cause: Cause.fail(error) })
),
);
}).pipe(
Effect.withSpan("Next.arrayBuffer"),
);
return {
getCookieJar,
ensureSchema,
ensureRequestSchema,
redirectTo,
failRedirectTo,
revalidatePath: nextRevalidatePath,
revalidateTag: nextRevalidateTag,
notFound: nextNotFound,
failNotFound,
text,
arrayBuffer,
getHeaders,
};
},
accessors: true,
}) {
// @ts-expect-error: can't use override modifier
static ensureRequestSchema = Effect.serviceFunctionEffect(
this,
(_) => _.ensureRequestSchema,
);
}