Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestDiffHttp.ts

101 lines3.5 KB

1import {
2 EnvironmentAuthInvalidError,
3 type PullRequestDiffInput,
4 type PullRequestDiffResult,
5} from "@t3tools/contracts";
6import * as Context from "effect/Context";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10import * as Schema from "effect/Schema";
11import { HttpClient } from "effect/unstable/http";
12​
13import { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
14import type { PreparedConnection } from "../connection/model.ts";
15import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
16import {
17 makeEnvironmentHttpApiUrlBuilder,
18 type RemoteEnvironmentRequestError,
19} from "../rpc/http.ts";
20import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
21​
22const DEFAULT_PULL_REQUEST_DIFF_TIMEOUT_MS = 60_000;
23​
24export class PullRequestDiffCredentialRejectedError extends Schema.TaggedError<PullRequestDiffCredentialRejectedError>()(
25 "PullRequestDiffCredentialRejectedError",
26 {
27 repository: Schema.String,
28 number: Schema.Number,
29 traceId: Schema.String,
30 cause: EnvironmentAuthInvalidError,
31 },
32) {
33 override get message(): string {
34 return "This environment session is no longer valid (invalid_credential). Refresh the page or quit and reopen T3 Code.";
35 }
37​
38export type PullRequestDiffLoadError =
39 | RemoteEnvironmentRequestError
40 | PullRequestDiffCredentialRejectedError;
41​
42export const fetchEnvironmentPullRequestDiff = Effect.fn(
43 "clientRuntime.state.fetchEnvironmentPullRequestDiff",
44)(function* (input: {
45 readonly prepared: PreparedConnection;
46 readonly diff: PullRequestDiffInput;
47 readonly signer: Option.Option<ManagedRelayDpopSigner["Service"]>;
48 readonly remoteAuthorization?: Option.Option<RemoteEnvironmentAuthorization["Service"]>;
49 readonly timeoutMs?: number;
50}) {
51 return yield* executeAuthenticatedEnvironmentHttpRequest({
52 ...input,
53 method: "POST",
54 url: (httpBaseUrl) => makeEnvironmentHttpApiUrlBuilder(httpBaseUrl).pullRequests.diff(),
55 timeoutMs: input.timeoutMs ?? DEFAULT_PULL_REQUEST_DIFF_TIMEOUT_MS,
56 request: ({ client, headers }) => client.pullRequests.diff({ payload: input.diff, headers }),
57 }).pipe(
58 Effect.mapError((error) =>
59 error._tag === "EnvironmentAuthInvalidError" && error.reason === "invalid_credential"
60 ? new PullRequestDiffCredentialRejectedError({
61 repository: input.diff.repository,
62 number: input.diff.number,
63 traceId: error.traceId,
64 cause: error,
65 })
66 : error,
67 ),
68 );
69});
70​
71export class PullRequestDiffLoader extends Context.Service<
72 PullRequestDiffLoader,
73 {
74 readonly load: (
75 prepared: PreparedConnection,
76 input: PullRequestDiffInput,
77 ) => Effect.Effect<PullRequestDiffResult, PullRequestDiffLoadError>;
78 }
79>()("@t3tools/client-runtime/state/pullRequestDiffHttp/PullRequestDiffLoader") {}
80​
81export const pullRequestDiffLoaderLayer: Layer.Layer<
82 PullRequestDiffLoader,
83 never,
84 HttpClient.HttpClient
85> = Layer.effect(
86 PullRequestDiffLoader,
87 Effect.gen(function* () {
88 const httpClient = yield* HttpClient.HttpClient;
89 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
90 const remoteAuthorization = yield* Effect.serviceOption(RemoteEnvironmentAuthorization);
91 return PullRequestDiffLoader.of({
92 load: (prepared, input) =>
93 fetchEnvironmentPullRequestDiff({
94 prepared,
95 diff: input,
96 signer,
97 remoteAuthorization,
98 }).pipe(Effect.provideService(HttpClient.HttpClient, httpClient)),
99 });
100 }),
101);