|
@@ -6,8 +6,29 @@ import { ProviderID } from "@/provider/schema"
|
|
|
import { mapValues } from "remeda"
|
|
import { mapValues } from "remeda"
|
|
|
import { Effect, Schema } from "effect"
|
|
import { Effect, Schema } from "effect"
|
|
|
import { HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
|
import { HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
|
|
-import { HttpApiBuilder, HttpApiError } from "effect/unstable/httpapi"
|
|
|
|
|
|
|
+import { HttpApiBuilder } from "effect/unstable/httpapi"
|
|
|
import { InstanceHttpApi } from "../api"
|
|
import { InstanceHttpApi } from "../api"
|
|
|
|
|
+import { ProviderAuthApiError } from "../groups/provider"
|
|
|
|
|
+
|
|
|
|
|
+function mapProviderAuthError<A, R>(self: Effect.Effect<A, ProviderAuth.Error, R>) {
|
|
|
|
|
+ return self.pipe(
|
|
|
|
|
+ Effect.mapError((error) => {
|
|
|
|
|
+ if (error instanceof ProviderAuth.OauthMissing) {
|
|
|
|
|
+ return new ProviderAuthApiError({ name: error._tag, data: { providerID: error.providerID } })
|
|
|
|
|
+ }
|
|
|
|
|
+ if (error instanceof ProviderAuth.OauthCodeMissing) {
|
|
|
|
|
+ return new ProviderAuthApiError({ name: error._tag, data: { providerID: error.providerID } })
|
|
|
|
|
+ }
|
|
|
|
|
+ if (error instanceof ProviderAuth.OauthCallbackFailed) {
|
|
|
|
|
+ return new ProviderAuthApiError({ name: error._tag, data: {} })
|
|
|
|
|
+ }
|
|
|
|
|
+ if (error instanceof ProviderAuth.ValidationFailed) {
|
|
|
|
|
+ return new ProviderAuthApiError({ name: error._tag, data: { field: error.field, message: error.message } })
|
|
|
|
|
+ }
|
|
|
|
|
+ return new ProviderAuthApiError({ name: "BadRequest", data: {} })
|
|
|
|
|
+ }),
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
export const providerHandlers = HttpApiBuilder.group(InstanceHttpApi, "provider", (handlers) =>
|
|
export const providerHandlers = HttpApiBuilder.group(InstanceHttpApi, "provider", (handlers) =>
|
|
|
Effect.gen(function* () {
|
|
Effect.gen(function* () {
|
|
@@ -44,13 +65,13 @@ export const providerHandlers = HttpApiBuilder.group(InstanceHttpApi, "provider"
|
|
|
params: { providerID: ProviderID }
|
|
params: { providerID: ProviderID }
|
|
|
payload: ProviderAuth.AuthorizeInput
|
|
payload: ProviderAuth.AuthorizeInput
|
|
|
}) {
|
|
}) {
|
|
|
- return yield* svc
|
|
|
|
|
- .authorize({
|
|
|
|
|
|
|
+ return yield* mapProviderAuthError(
|
|
|
|
|
+ svc.authorize({
|
|
|
providerID: ctx.params.providerID,
|
|
providerID: ctx.params.providerID,
|
|
|
method: ctx.payload.method,
|
|
method: ctx.payload.method,
|
|
|
inputs: ctx.payload.inputs,
|
|
inputs: ctx.payload.inputs,
|
|
|
- })
|
|
|
|
|
- .pipe(Effect.catch(() => Effect.fail(new HttpApiError.BadRequest({}))))
|
|
|
|
|
|
|
+ }),
|
|
|
|
|
+ )
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
const authorizeRaw = Effect.fn("ProviderHttpApi.authorizeRaw")(function* (ctx: {
|
|
const authorizeRaw = Effect.fn("ProviderHttpApi.authorizeRaw")(function* (ctx: {
|
|
@@ -59,7 +80,7 @@ export const providerHandlers = HttpApiBuilder.group(InstanceHttpApi, "provider"
|
|
|
}) {
|
|
}) {
|
|
|
const body = yield* Effect.orDie(ctx.request.text)
|
|
const body = yield* Effect.orDie(ctx.request.text)
|
|
|
const payload = yield* Schema.decodeUnknownEffect(Schema.fromJsonString(ProviderAuth.AuthorizeInput))(body).pipe(
|
|
const payload = yield* Schema.decodeUnknownEffect(Schema.fromJsonString(ProviderAuth.AuthorizeInput))(body).pipe(
|
|
|
- Effect.mapError(() => new HttpApiError.BadRequest({})),
|
|
|
|
|
|
|
+ Effect.mapError(() => new ProviderAuthApiError({ name: "BadRequest", data: {} })),
|
|
|
)
|
|
)
|
|
|
// Match legacy route behavior: when authorize() resolves without a
|
|
// Match legacy route behavior: when authorize() resolves without a
|
|
|
// result (e.g. no further redirect), serialize as JSON `null` instead
|
|
// result (e.g. no further redirect), serialize as JSON `null` instead
|
|
@@ -72,13 +93,13 @@ export const providerHandlers = HttpApiBuilder.group(InstanceHttpApi, "provider"
|
|
|
params: { providerID: ProviderID }
|
|
params: { providerID: ProviderID }
|
|
|
payload: ProviderAuth.CallbackInput
|
|
payload: ProviderAuth.CallbackInput
|
|
|
}) {
|
|
}) {
|
|
|
- yield* svc
|
|
|
|
|
- .callback({
|
|
|
|
|
|
|
+ yield* mapProviderAuthError(
|
|
|
|
|
+ svc.callback({
|
|
|
providerID: ctx.params.providerID,
|
|
providerID: ctx.params.providerID,
|
|
|
method: ctx.payload.method,
|
|
method: ctx.payload.method,
|
|
|
code: ctx.payload.code,
|
|
code: ctx.payload.code,
|
|
|
- })
|
|
|
|
|
- .pipe(Effect.catch(() => Effect.fail(new HttpApiError.BadRequest({}))))
|
|
|
|
|
|
|
+ }),
|
|
|
|
|
+ )
|
|
|
return true
|
|
return true
|
|
|
})
|
|
})
|
|
|
|
|
|