nyx Typedoc
    Preparing search index...

    Module @nyx-discord/http - v1.1.0

    @nyx-discord/http


    @nyx-discord/http lets your nyx bot receive Discord interactions and webhook events over HTTP instead of the gateway, with any server or runtime. See the guide's HTTP section for everything else.

    import { HttpBot } from '@nyx-discord/http';

    const bot = HttpBot.create(() => ({
    applicationId: env.APP_ID,
    publicKey: env.DISCORD_PUBLIC_KEY,
    token: env.TOKEN,
    logger: console,
    deployCommands: false,
    }));
    await bot.start();

    const client = bot.getClient();

    Point the Interactions Endpoint URL at client, and the Webhook Events URL (optional) at client.getWebhookEventHandler().

    import { createServer } from 'node:http';
    import { toNodeListener } from '@nyx-discord/http';

    const interactions = toNodeListener(client);
    const webhookEvents = toNodeListener(client.getWebhookEventHandler());

    createServer((req, res) => {
    const listener =
    req.url === '/interactions'
    ? interactions
    : req.url === '/webhook-events'
    ? webhookEvents
    : null;
    if (!listener) return void res.writeHead(404).end();

    listener(req, res).catch(console.error);
    }).listen(3000);
    import { toNodeListener } from '@nyx-discord/http';

    // no express.json() on these routes: the signature is checked against the raw body
    app.post('/interactions', toNodeListener(client));
    app.post('/webhook-events', toNodeListener(client.getWebhookEventHandler()));
    app.addContentTypeParser(
    'application/json',
    { parseAs: 'buffer' },
    (_req, body, done) => done(null, body),
    );

    for (const [path, handler] of [
    ['/interactions', client],
    ['/webhook-events', client.getWebhookEventHandler()],
    ] as const) {
    app.post(path, async (req, reply) => {
    const result = await handler.handle({
    body: req.body as Buffer,
    signature: req.headers['x-signature-ed25519'] as string | undefined,
    timestamp: req.headers['x-signature-timestamp'] as string | undefined,
    raw: req,
    });
    return reply.code(result.status).headers(result.headers).send(result.body);
    });
    }
    import { toFetchHandler } from '@nyx-discord/http';

    const interactions = toFetchHandler(client);
    const webhookEvents = toFetchHandler(client.getWebhookEventHandler());

    app.post('/interactions', (c) => interactions(c.req.raw));
    app.post('/webhook-events', (c) => webhookEvents(c.req.raw));
    import { toFetchHandler } from '@nyx-discord/http';

    const interactions = toFetchHandler(client);
    const webhookEvents = toFetchHandler(client.getWebhookEventHandler());

    export default {
    fetch(request, env, ctx) {
    const { pathname } = new URL(request.url);
    const handler =
    pathname === '/webhook-events' ? webhookEvents : interactions;

    // passing ctx keeps the worker alive until your handlers finish
    return handler(request, ctx);
    },
    };
    import { toFetchHandler } from '@nyx-discord/http';

    const interactions = toFetchHandler(client);
    const webhookEvents = toFetchHandler(client.getWebhookEventHandler());

    export default {
    fetch(request: Request) {
    const { pathname } = new URL(request.url);
    return pathname === '/webhook-events'
    ? webhookEvents(request)
    : interactions(request);
    },
    };
    // app/interactions/route.ts
    import { after } from 'next/server';
    import { toFetchHandler } from '@nyx-discord/http';

    const interactions = toFetchHandler(client);

    export const POST = (request: Request) =>
    interactions(request, { waitUntil: after });
    // app/webhook-events/route.ts
    import { after } from 'next/server';
    import { toFetchHandler } from '@nyx-discord/http';

    const webhookEvents = toFetchHandler(client.getWebhookEventHandler());

    export const POST = (request: Request) =>
    webhookEvents(request, { waitUntil: after });
    AbstractBusSubscriber
    AbstractChildableCommand
    AbstractCommand
    AbstractCommandFilter
    AbstractCommandMiddleware
    AbstractCommandSubscriber
    AbstractContextMenuCommand
    AbstractCoreClientSubscriber
    AbstractCustomIdCodec
    AbstractEventDispatcher
    AbstractEventSubscriber
    AbstractEventSubscriberFilter
    AbstractEventSubscriberMiddleware
    AbstractExecutableCommand
    AbstractFilter
    AbstractFilterAggregator
    AbstractMiddleware
    AbstractMiddlewareList
    AbstractParentCommand
    AbstractPluginSubscriber
    AbstractSchedule
    AbstractScheduleFilter
    AbstractScheduleMiddleware
    AbstractScheduleSubscriber
    AbstractServiceSubscriber
    AbstractStandaloneCommand
    AbstractSubCommand
    AbstractSubCommandGroup
    AbstractWebhookEventSubscriber
    AndFilter
    AssertionError
    BaseBot
    BaseChildableCommand
    BaseClientSubscriber
    BaseCommand
    BaseCommandFilter
    BaseCommandMiddleware
    BaseCommandSubscriber
    BaseContextMenuCommand
    BaseExecutableCommand
    BaseParentCommand
    BaseStandaloneCommand
    BaseSubCommand
    BaseSubCommandGroup
    BasicAsyncEventDispatcher
    BasicAwaitableEventEmitter
    BasicErrorHandler
    BasicEventBus
    BasicEventEmitterBus
    BasicFilterCheckMiddleware
    BasicSyncEventDispatcher
    CommandAutocompleteError
    CommandError
    CommandFilterCheckMiddleware
    CommandMiddlewareError
    CommandMiddlewareList
    CoreBot
    CoreNyxClient
    CronJobAdapter
    DefaultBotService
    DefaultCommandAutocompleteSubscriber
    DefaultCommandCustomIdCodec
    DefaultCommandDeployer
    DefaultCommandExecutor
    DefaultCommandInteractionSubscriber
    DefaultCommandManager
    DefaultCommandRepository
    DefaultCommandResolver
    DefaultCommandSubscriptionsContainer
    DefaultMetadataFactory
    DefaultPluginManager
    DefaultScheduleExecutionScheduler
    DefaultScheduleExecutor
    DefaultScheduleManager
    DefaultScheduleRepository
    DefaultSignatureVerifier
    EventSubscriberMiddlewareError
    FalseFilter
    FeatureError
    HandleCheckEventMiddleware
    HttpNyxClient
    IllegalDuplicateError
    IllegalStateError
    LifetimeCheckEventMiddleware
    NotFilter
    NotImplementedError
    ObjectNotFoundError
    OrFilter
    PluginAddError
    ProtectedObjectError
    ScheduleFilterCheckMiddleware
    ScheduleMiddlewareError
    ScheduleMiddlewareList
    SubscriberCallbackWrapper
    SubscriberFilterCheckMiddleware
    SubscriberMiddlewareList
    TimerJobAdapter
    TrueFilter
    UncaughtCommandMiddlewareError
    UncaughtEventSubscriberMiddlewareError
    UncaughtScheduleMiddlewareError
    AsyncEventDispatcher
    AwaitableEventEmitter
    BotLifecycleObserver
    BotOptions
    BotService
    BotServiceEventArgs
    ChatExecutableCommand
    ChildableCommand
    ChildCommand
    Command
    CommandCustomIdCodec
    CommandDeployer
    CommandErrorHandler
    CommandEventArgs
    CommandExecutor
    CommandFilter
    CommandManager
    CommandMiddleware
    CommandRepository
    CommandResolver
    CommandSubscriptionsContainer
    ContextMenuCommand
    CoreClient
    CoreInteractionTypes
    CustomIdCodec
    ErrorHandler
    ErrorHandlerContainer
    EventBus
    EventBusEventArgs
    EventDispatcher
    EventEmitterBus
    EventEmitterLike
    EventSubscriber
    EventSubscriberErrorHandler
    EventSubscriberFilter
    EventSubscriberMiddleware
    ExecutableCommand
    Filter
    Filterable
    HttpEndpointHandler
    HttpEndpointRequest
    HttpEndpointResult
    Identifiable
    InteractionTypes
    Metadatable
    MetadataFactory
    Middleware
    MiddlewareList
    MiddlewareListContainer
    Nameable
    NyxBot
    NyxClient
    NyxLogger
    NyxPlugin
    ParentCommand
    PluginEventArgs
    PluginManager
    PriorityAware
    Protectable
    ReadonlyCommandRepository
    ReadonlyScheduleExecutionScheduler
    ReadonlyScheduleRepository
    Schedule
    ScheduleErrorHandler
    ScheduleEventArgs
    ScheduleExecutionScheduler
    ScheduleExecutor
    ScheduleFilter
    ScheduleJobAdapter
    ScheduleManager
    ScheduleMiddleware
    ScheduleRepository
    SignatureVerifier
    StandaloneCommand
    SubCommand
    SubCommandGroup
    SyncEventDispatcher
    UndestroyableScheduleJobAdapter
    AnyClass
    AnyEventBus
    AnyEventSubscriber
    AnyEventSubscriberFrom
    AnyExecutableCommand
    ApplicationCommandCollection
    ApplicationCommandInteraction
    Awaitable
    BotServiceEvent
    BotStatus
    ClassImplements
    CommandCustomIdData
    CommandEvent
    CommandExecutableInteraction
    CommandExecutionArgs
    CommandFilterResolvable
    CommandMiddlewareResolvable
    CommandResolvableInteraction
    Comparator
    ComponentCommandInteraction
    Constructor
    CoreAnyExecutableCommand
    CoreBotDependencies
    CoreCommandErrorHandler
    CoreCommandEventArgs
    CoreCommandExecutableInteraction
    CoreCommandExecutionArgs
    CoreCommandFilterResolvable
    CoreCommandInteraction
    CoreCommandMiddlewareResolvable
    CoreCommandResolvableInteraction
    CoreComponentInteraction
    CoreNyxBot
    DefaultScheduleJob
    ErrorConsumer
    ErrorConsumerCollection
    EventBusEvent
    EventDispatchArgs
    EventSubscriberCollection
    EventSubscriberFilterResolvable
    EventSubscriberLifetime
    EventSubscriberMiddlewareResolvable
    FilterResolvable
    FilterResolvableFrom
    HttpBot
    HttpBotEvents
    HttpClientEvents
    Identifier
    ImplementsParentCommand
    ImplementsStandaloneCommand
    ImplementsSubCommand
    ImplementsSubCommandGroup
    InjectableBotDependencies
    Metadata
    MiddlewareResolvable
    MiddlewareResolvableFrom
    MiddlewareResponse
    NyxPluginData
    PluginEvent
    Priority
    ReadonlyCollectionFrom
    ReadonlyCommandDeployer
    ReadonlyMetadata
    ScheduleEvent
    ScheduleFilterResolvable
    ScheduleInterval
    ScheduleJobParams
    ScheduleMiddlewareResolvable
    ScheduleTickArgs
    SerializableFeature
    SubCommandData
    SubCommandGroupData
    Tail
    TimerJobHandle
    TopLevelCommand
    TypedField
    WebhookEventProps
    BotServiceEventEnum
    BotStatusEnum
    CommandEventEnum
    EventBusEventEnum
    EventSubscriberLifetimeEnum
    HttpBot
    PluginEventEnum
    PriorityEnum
    ScheduleEventEnum
    SerializableFeatureEnum
    TypedFields
    canBeIdentifier
    ensureKey
    getCommandNameTree
    toFetchHandler
    toNodeListener
    Bot → HttpBot