Skip to content

feat(plugin-gateway): add discord.js serializers (Transformers, DataResolver, Util) - #158

Merged
kodiakhq[bot] merged 1 commit into
mainfrom
feat/gateway-serializers
Sep 28, 2026
Merged

kodiakhq[bot] merged 1 commit into
mainfrom
feat/gateway-serializers

Conversation

@RedStar071

Copy link
Copy Markdown
Member

🔗 Linked issue

N/A: continues the discord.js parity work in plugin-gateway (#151, #152, #154, #156, #157).

🧭 Context

discord.js serializes API payloads in both directions. Transformers.js, the transformers in Channels.js, and each structure's _patch turn snake_case payloads into camelCase objects. DataResolver.js resolves files, images, and invite/template codes, and Util.js holds the public helpers. plugin-gateway had almost none of this. Its getters returned raw API objects, image options only took data URIs, and invite/template codes were pulled out of URLs with split("/").

📚 Description

This adds the missing serializers and uses them in the structures and managers.

  • util/Transformers.ts: toSnakeCase plus a transformAPI*/transform* pair for:
    • auto moderation triggers and actions
    • forum tags and default reactions
    • scheduled event recurrence rules and entity metadata
    • incidents, role tags, audit log changes
    • avatar decorations, collectibles, primary guilds
    • message references, activities, calls, role subscriptions, crossposted channels, interaction metadata
    • embed assets, authors, and footers
  • util/DataResolver.ts: resolveFile (moved out of MessagePayload), resolveBase64, resolveImage, resolveInviteCode, resolveGuildTemplateCode, and InvitesPattern/GuildTemplatesPattern, which are also static on BaseInvite/GuildTemplate like in discord.js.
  • util/Util.ts: flatten, cleanContent, cleanCodeBlockContent, parseWebhookURL, verifyString, discordSort, moveElementInArray, getSortableGroupTypes, makeError/makePlainError, basename, findName, resolveSKUId. This builds on the parseEmoji/resolvePartialEmoji from feat(plugin-gateway): add parseEmoji and resolvePartialEmoji utils #157.

Deliberately not ported:

  • transformResolved: interactions are out of scope.
  • resolveGuildEmoji: needs a sync cache. For the same reason, cleanContent takes the names as a parameter instead of reading the cache.
  • fetchRecommendedShardCount: belongs to sharding.

Key changes

Before After
Getters listed in the breaking note below raw API objects camelCase objects, like discord.js (toJSON() is still raw)
Message#messageSnapshots raw snapshots Message structures carrying the forwarded IDs
Options (automod trigger and actions, forum tags, default reaction, recurrence rule) raw only camelCase or raw
Images (icons, avatars, banners, splashes, emojis, event covers) data URI only data URI, bytes, path, URL, stream, or blob
Soundboard sounds data URI only same inputs as images, plus a contentType option
Invite/template codes split("/") resolveInviteCode/resolveGuildTemplateCode
New getters — GuildScheduledEvent#entityMetadata, GuildMember#collectibles

Breaking: these getters now return camelCase objects:

  • AutoModerationRule#triggerMetadata and #actions, AutoModerationActionExecution#action
  • availableTags and defaultReactionEmoji on forum and media channels
  • GuildScheduledEvent#recurrenceRule
  • Guild#incidentsData, and the result of setIncidentActions
  • User#avatarDecorationData, #collectibles, #primaryGuild, and GuildMember#avatarDecorationData
  • Role#tags (boolean tags become true)
  • Message#reference, #activity, #interactionMetadata, #call, #roleSubscriptionData
  • MessageMentions#crosspostedChannels
  • GuildAuditLogsEntry#changes (old/new)
  • Embed#thumbnail, #image, #video, #author, #footer

The changeset is minor because the package is still 0.x.

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Workflow / CI change

Pre-flight Checklist

  • I have read the Contributing Guidelines (if applicable)
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly
  • I added a changeset with pnpm changeset (or pnpm changeset add --empty if no release is needed). See .changeset/README.md.

@wolfstarbot wolfstarbot added the packages:plugin-gateway Changes in plugin-gateway label Sep 28, 2026

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

This PR ports discord.js's Transformers, DataResolver, and Util serializers into plugin-gateway, continuing the discord.js-parity series (#151, #152, #154, #156, #157).

  • util/Transformers.ts (new, 934 lines): toSnakeCase/snakeCase plus transformAPI*/transform* pairs converting between raw snake_case API payloads and camelCased objects for auto-moderation triggers/actions, forum tags/default reactions, scheduled-event recurrence rules/entity metadata, incidents data, role tags, audit-log changes, avatar decorations/collectibles/primary guilds, and several message-related sub-objects (references, activities, calls, role subscriptions, crossposted channels, interaction metadata) and embed assets/author/footer.
  • util/DataResolver.ts (new): resolveFile (moved out of MessagePayload to be shared), resolveBase64, resolveImage/resolveImageOption, and resolveInviteCode/resolveGuildTemplateCode with their matching URL patterns, also exposed as statics on BaseInvite/GuildTemplate.
  • util/Util.ts additions: flatten, cleanContent, cleanCodeBlockContent, parseWebhookURL, verifyString, discordSort, moveElementInArray, getSortableGroupTypes, makeError/makePlainError, basename, findName, resolveSKUId.
  • Consumer updates across managers/structures: getters that used to return raw snake_case objects (auto-mod triggers/actions, forum tags, scheduled-event recurrence rule, incidents data, role tags, user/member avatar decoration & collectibles & primary guild, message reference/activity/call/interaction-metadata/role-subscription, crossposted channels, audit-log changes, embed assets) now return camelCased, transformed objects — documented as breaking in the changeset. Option-taking methods (guild/role/emoji/webhook/soundboard/template edits, auto-mod triggers/actions, scheduled-event recurrence rule) now accept either camelCased or raw shapes, and image-accepting options take paths/URLs/streams/blobs/bytes in addition to data URIs via the new resolveImage/resolveFile.
  • Message#messageSnapshots now returns real Message structures reconstructed from message_snapshots, filling in id/channelId/guildId from the message's own message_reference since the raw snapshot payload doesn't carry them.

I read the full diff (38 files) and traced the null/undefined round-tripping in several option builders (e.g. GuildScheduledEventManager.toEventBody's recurrence_rule, AutoModerationRule#editTrigger's merge) — the value && transform(value) pattern correctly preserves "explicit null clears the field, undefined leaves it untouched" semantics, since && short-circuits and returns null unchanged. The extensive camelCase↔snake_case mapping is consistently applied and well covered by the new tests/serializers.test.ts suite (round-trip tests for most transformer pairs, plus DataResolver/Util coverage including path/stream/blob resolution). resolveFile's JSDoc already documents the SSRF-adjacent caveat about fetching user-supplied URLs, so that's not a gap.

No correctness issues found in this pass.

Pullfrog  | View workflow run | Using Claude Sonnet | 𝕏

@kodiakhq
kodiakhq Bot merged commit 53aee8f into main Sep 28, 2026
17 checks passed
@kodiakhq
kodiakhq Bot deleted the feat/gateway-serializers branch September 28, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

packages:plugin-gateway Changes in plugin-gateway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants