mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 02:53:31 +01:00
feat(options): add support for custom JSON transformers (#7476)
This commit is contained in:
@@ -6,7 +6,6 @@ const { MessageFlags } = require('discord-api-types/v9');
|
|||||||
const { RangeError } = require('../errors');
|
const { RangeError } = require('../errors');
|
||||||
const DataResolver = require('../util/DataResolver');
|
const DataResolver = require('../util/DataResolver');
|
||||||
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
|
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
|
||||||
const Transformers = require('../util/Transformers');
|
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -133,7 +132,7 @@ class MessagePayload {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const components = this.options.components?.map(c =>
|
const components = this.options.components?.map(c =>
|
||||||
isJSONEncodable(c) ? c.toJSON() : Transformers.toSnakeCase(c),
|
isJSONEncodable(c) ? c.toJSON() : this.target.client.options.jsonTransformer(c),
|
||||||
);
|
);
|
||||||
|
|
||||||
let username;
|
let username;
|
||||||
@@ -194,7 +193,7 @@ class MessagePayload {
|
|||||||
tts,
|
tts,
|
||||||
nonce,
|
nonce,
|
||||||
embeds: this.options.embeds?.map(embed =>
|
embeds: this.options.embeds?.map(embed =>
|
||||||
embed instanceof Embed ? embed.toJSON() : Transformers.toSnakeCase(embed),
|
embed instanceof Embed ? embed.toJSON() : this.target.client.options.jsonTransformer(embed),
|
||||||
),
|
),
|
||||||
components,
|
components,
|
||||||
username,
|
username,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const process = require('node:process');
|
const process = require('node:process');
|
||||||
const { DefaultRestOptions } = require('@discordjs/rest');
|
const { DefaultRestOptions } = require('@discordjs/rest');
|
||||||
|
const Transformers = require('./Transformers');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Function} CacheFactory
|
* @typedef {Function} CacheFactory
|
||||||
@@ -35,6 +36,7 @@ const { DefaultRestOptions } = require('@discordjs/rest');
|
|||||||
* @property {SweeperOptions} [sweepers={}] Options for cache sweeping
|
* @property {SweeperOptions} [sweepers={}] Options for cache sweeping
|
||||||
* @property {WebsocketOptions} [ws] Options for the WebSocket
|
* @property {WebsocketOptions} [ws] Options for the WebSocket
|
||||||
* @property {RESTOptions} [rest] Options for the REST manager
|
* @property {RESTOptions} [rest] Options for the REST manager
|
||||||
|
* @property {Function} [jsonTransformer] A function used to transform outgoing json data
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,6 +90,7 @@ class Options extends null {
|
|||||||
version: 9,
|
version: 9,
|
||||||
},
|
},
|
||||||
rest: DefaultRestOptions,
|
rest: DefaultRestOptions,
|
||||||
|
jsonTransformer: Transformers.toSnakeCase,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1
packages/discord.js/typings/index.d.ts
vendored
1
packages/discord.js/typings/index.d.ts
vendored
@@ -3635,6 +3635,7 @@ export interface ClientOptions {
|
|||||||
sweepers?: SweeperOptions;
|
sweepers?: SweeperOptions;
|
||||||
ws?: WebSocketOptions;
|
ws?: WebSocketOptions;
|
||||||
rest?: Partial<RESTOptions>;
|
rest?: Partial<RESTOptions>;
|
||||||
|
jsonTransformer?: (obj: unknown) => unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ClientPresenceStatus = 'online' | 'idle' | 'dnd';
|
export type ClientPresenceStatus = 'online' | 'idle' | 'dnd';
|
||||||
|
|||||||
Reference in New Issue
Block a user