Skip to content

Commit 936d1f5

Browse files
refactor(core): make legacy success auth callback decoding 100% type-safe using Schema
1 parent 9432312 commit 936d1f5

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

packages/core/src/integration.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,25 @@ export const locationLayer = Layer.effect(
362362
return error instanceof Error ? error.message : String(error)
363363
}
364364

365+
const LegacySuccessOAuth = Schema.Struct({
366+
type: Schema.Literal("success"),
367+
access: Schema.String,
368+
refresh: Schema.optional(Schema.String),
369+
expires: Schema.optional(Schema.Number),
370+
accountId: Schema.optional(Schema.String),
371+
enterpriseUrl: Schema.optional(Schema.String),
372+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
373+
})
374+
375+
const LegacySuccessKey = Schema.Struct({
376+
type: Schema.Literal("success"),
377+
key: Schema.String,
378+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
379+
})
380+
381+
const LegacySuccessValue = Schema.Union([LegacySuccessOAuth, LegacySuccessKey])
382+
const decodeLegacySuccess = Schema.decodeUnknownOption(LegacySuccessValue)
383+
365384
const settle = Effect.fnUntraced(function* (attemptID: AttemptID, exit: Exit.Exit<Credential.Info, unknown>) {
366385
const now = yield* Clock.currentTimeMillis
367386
const result = yield* SynchronizedRef.modify(attempts, (current) => {
@@ -375,24 +394,26 @@ export const locationLayer = Layer.effect(
375394
if (!result) return
376395
if (Exit.isSuccess(exit)) {
377396
let value = exit.value
378-
if ((value as any).type === "success") {
379-
if ("access" in value) {
397+
const decodedOption = decodeLegacySuccess(value)
398+
if (decodedOption._tag === "Some") {
399+
const legacy = decodedOption.value
400+
if ("access" in legacy) {
380401
value = new Credential.OAuth({
381402
type: "oauth",
382-
access: (value as any).access,
383-
refresh: (value as any).refresh || "",
384-
expires: (value as any).expires || 0,
403+
access: legacy.access,
404+
refresh: legacy.refresh ?? "",
405+
expires: legacy.expires ?? 0,
385406
metadata: {
386-
...((value as any).accountId ? { clientId: (value as any).accountId } : {}),
387-
...((value as any).enterpriseUrl ? { clientSecret: (value as any).enterpriseUrl } : {}),
388-
...((value as any).metadata ?? {}),
407+
...(legacy.accountId ? { clientId: legacy.accountId } : {}),
408+
...(legacy.enterpriseUrl ? { clientSecret: legacy.enterpriseUrl } : {}),
409+
...(legacy.metadata ?? {}),
389410
},
390411
})
391412
} else {
392413
value = new Credential.Key({
393414
type: "key",
394-
key: (value as any).key,
395-
metadata: (value as any).metadata,
415+
key: legacy.key,
416+
metadata: legacy.metadata,
396417
})
397418
}
398419
}

0 commit comments

Comments
 (0)