projects/sdk/src/internal/application.ts

/**
 * Bot application requests: Validates current-application and public-application reads and projects their responses.
 * Invariant: Both operations are fresh bot-token reads without cache retention, and their projections keep only the
 * documented public fields without private credentials, bot tokens or caller session data.
 * Implements [SDK contracts: Delivery, requests and caches](/docs/SDK-CONTRACTS.md#delivery-requests-and-caches)
 */
import type { BotApplication, PublicApplication, PublicApplicationBot } from "#sdk/application"
import { inputValidationFailure, type InputValidationFailure } from "#sdk/input-validation"
import { count, identifier, inputId, nullableText, optionalNullableText, record } from "./decode/primitives.js"
import { rejectCheck } from "./decode/trace.js"

/** Validated application read request. Shared REST owns admission, retries and cleanup */
export interface BotApplicationRequest<A> {
    readonly path: string
    readonly method: "GET"
    readonly status: 200
    readonly decode: (value: unknown) => A | undefined
}

const strings = (value: unknown): value is readonly string[] =>
    Array.isArray(value) && value.every((item) => typeof item === "string")

function decodeApplication(value: unknown): BotApplication | undefined {
    if (
        !record(value) ||
        !identifier(value.id) ||
        typeof value.name !== "string" ||
        !nullableText(value.icon) ||
        !nullableText(value.description) ||
        typeof value.bot_public !== "boolean" ||
        typeof value.bot_require_code_grant !== "boolean" ||
        !record(value.owner) ||
        !identifier(value.owner.id)
    )
        return undefined
    return Object.freeze({
        id: value.id,
        name: value.name,
        icon: value.icon,
        description: value.description,
        botPublic: value.bot_public,
        botRequireCodeGrant: value.bot_require_code_grant,
        ownerId: value.owner.id,
    })
}

function decodePublicBot(value: unknown): PublicApplicationBot | null | undefined {
    if (value === null) return null
    if (
        !record(value) ||
        !identifier(value.id) ||
        typeof value.username !== "string" ||
        typeof value.discriminator !== "string" ||
        !optionalNullableText(value.avatar) ||
        !optionalNullableText(value.banner) ||
        !nullableText(value.bio) ||
        !count(value.flags)
    )
        return undefined
    return Object.freeze({
        id: value.id,
        username: value.username,
        discriminator: value.discriminator,
        avatar: value.avatar ?? null,
        banner: value.banner ?? null,
        bio: value.bio,
        flags: value.flags,
    })
}

function decodePublicApplication(value: unknown): PublicApplication | undefined {
    if (
        !record(value) ||
        !identifier(value.id) ||
        typeof value.name !== "string" ||
        !nullableText(value.icon) ||
        !nullableText(value.description) ||
        !strings(value.redirect_uris) ||
        !strings(value.scopes) ||
        typeof value.bot_public !== "boolean"
    )
        return undefined
    const bot = decodePublicBot(value.bot)
    if (bot === undefined) return undefined
    return Object.freeze({
        id: value.id,
        name: value.name,
        icon: value.icon,
        description: value.description,
        redirectUris: Object.freeze([...value.redirect_uris]),
        scopes: Object.freeze([...value.scopes]),
        botPublic: value.bot_public,
        bot,
    })
}

/** Fetch only the bot-token response's public application fields and owner ID, without cache retention or application management */
export function applicationCurrent(): BotApplicationRequest<BotApplication> {
    return {
        path: "/oauth2/applications/@me",
        method: "GET",
        status: 200,
        decode: decodeApplication,
    }
}

/** Fetch public metadata for one OAuth2 application by ID without caching or exposing bot tokens or current_user */
export function applicationPublic(
    rawApplicationId: string,
): BotApplicationRequest<PublicApplication> | InputValidationFailure {
    const id = inputId(rawApplicationId)
    if (id === undefined)
        return inputValidationFailure(
            "applicationId",
            "format",
            "Application IDs must be decimal strings, because JavaScript numbers cannot hold Fluxer IDs exactly",
        )
    return {
        path: `/oauth2/applications/${id}/public`,
        method: "GET",
        status: 200,
        decode: (value) => {
            const application = decodePublicApplication(value)
            return application === undefined || application.id === id ? application : rejectCheck("targetMismatch")
        },
    }
}