refactor(packages): flatten branches and keep helpers private

This commit is contained in:
Hampus committed 2026-10-24 20:55:15 +02:00
1 parent 9e64398ab7
commit 243dce38b8
7 files changed
+45 -31

No files matched your search

+2 -2
View File
@@ -39,7 +39,7 @@ function isStandardPort(scheme: string, port: number): boolean {
);
}
export function canonicalizeDomain(value: string): string {
function canonicalizeDomain(value: string): string {
const trimmed = value.trim().toLowerCase();
return trimmed.endsWith('.') ? trimmed.slice(0, -1) : trimmed;
}
@@ -115,7 +115,7 @@ export function normalizePublicEndpoint(url: string, baseDomain: string, publicP
return `${url.slice(0, authorityEnd)}:${publicPort}${url.slice(authorityEnd)}`;
}
export function deriveDomain(
function deriveDomain(
endpointType:
| 'api'
| 'api_client'
+1 -1
View File
@@ -27,7 +27,7 @@ export interface NumericTierPerk extends BasePerk {
unit?: 'count' | 'bytes' | 'characters';
}
export interface TextTierPerk extends BasePerk {
interface TextTierPerk extends BasePerk {
type: 'text';
restrictedValueI18nKey: string;
stockValueI18nKey: string;
@@ -31,7 +31,7 @@ export interface ThreadActor {
timedOut: boolean;
}
export interface ThreadFacts {
interface ThreadFacts {
type: number;
archived: boolean;
locked: boolean;
@@ -129,26 +129,33 @@ export function threadPatchRequirement(
if (unarchive !== null) return unarchive;
}
const staysArchived = ctx.thread.archived && patch.archived !== false;
const changesFlags = patch.flags !== undefined && patch.flags !== currentFlags;
const touchesOtherFields =
patch.name !== undefined ||
patch.auto_archive_duration !== undefined ||
patch.locked !== undefined ||
patch.invitable !== undefined ||
patch.rate_limit_per_user !== undefined ||
(patch.flags !== undefined && patch.flags !== currentFlags) ||
changesFlags ||
patch.applied_tags !== undefined;
if (staysArchived && touchesOtherFields) return APIErrorCodes.THREAD_ARCHIVED;
if (patch.locked !== undefined && patch.locked !== ctx.thread.locked && !moderator) {
if (!(patch.locked && ctx.isThreadOwner)) return APIErrorCodes.MISSING_PERMISSIONS;
const changesLock = patch.locked !== undefined && patch.locked !== ctx.thread.locked;
const ownerLocksThread = patch.locked && ctx.isThreadOwner;
if (changesLock && !moderator && !ownerLocksThread) {
return APIErrorCodes.MISSING_PERMISSIONS;
}
if (ctx.thread.locked && !moderator) return APIErrorCodes.THREAD_LOCKED;
if ((patch.name !== undefined || patch.auto_archive_duration !== undefined) && !ownerOrModerator) {
const requiresOwnership =
patch.name !== undefined ||
patch.auto_archive_duration !== undefined ||
(patch.archived === true && !ctx.thread.archived) ||
patch.invitable !== undefined;
if (requiresOwnership && !ownerOrModerator) {
return APIErrorCodes.MISSING_PERMISSIONS;
}
if ((patch.rate_limit_per_user !== undefined || changesFlags) && !moderator) {
return APIErrorCodes.MISSING_PERMISSIONS;
}
if (patch.archived === true && !ctx.thread.archived && !ownerOrModerator) return APIErrorCodes.MISSING_PERMISSIONS;
if (patch.invitable !== undefined && !ownerOrModerator) return APIErrorCodes.MISSING_PERMISSIONS;
if (patch.rate_limit_per_user !== undefined && !moderator) return APIErrorCodes.MISSING_PERMISSIONS;
if (patch.flags !== undefined && patch.flags !== currentFlags && !moderator) return APIErrorCodes.MISSING_PERMISSIONS;
return null;
}
@@ -88,11 +88,14 @@ function toPascalCase(value: string): string {
function branchNamePrefix(discriminator: core.JSONSchema._JSONSchema | undefined): string | undefined {
if (typeof discriminator !== 'object') return undefined;
const enumNames = discriminator['x-enumNames'];
const value = Array.isArray(enumNames)
? enumNames[0]
: discriminator.enum?.length === 1
? discriminator.enum[0]
: discriminator.const;
let value: unknown;
if (Array.isArray(enumNames)) {
value = enumNames[0];
} else if (discriminator.enum?.length === 1) {
value = discriminator.enum[0];
} else {
value = discriminator.const;
}
return typeof value === 'string' ? toPascalCase(value) : undefined;
}
@@ -156,7 +156,12 @@ export class OpenAPIOperationBuilder {
}
const required = new Set(schema.required ?? []);
for (const [name, property] of Object.entries(schema.properties)) {
const propertySchema: OpenAPISchema = typeof property === 'boolean' ? (property ? {} : {not: {}}) : property;
let propertySchema: OpenAPISchema;
if (typeof property === 'boolean') {
propertySchema = property ? {} : {not: {}};
} else {
propertySchema = property;
}
const key = `${location}:${name}`;
if (parameters.has(key)) throw new Error(`Duplicate parameter ${key} for ${route.method} ${route.path}`);
parameters.set(key, {
@@ -195,12 +195,10 @@ export function computeGuildChannelReorderPlan<Id extends string | bigint, Chann
return {ok: false, code: 'TARGET_CHANNEL_NOT_FOUND'};
}
const requestedParentId = operation.parentId;
const desiredParentId =
targetChannel.type === ChannelTypes.GUILD_CATEGORY
? null
: requestedParentId !== undefined
? requestedParentId
: (targetChannel.parentId ?? null);
let desiredParentId: Id | null = null;
if (targetChannel.type !== ChannelTypes.GUILD_CATEGORY) {
desiredParentId = requestedParentId !== undefined ? requestedParentId : (targetChannel.parentId ?? null);
}
if (targetChannel.type === ChannelTypes.GUILD_CATEGORY && operation.parentId) {
return {ok: false, code: 'CATEGORIES_CANNOT_HAVE_PARENTS'};
}
@@ -214,7 +212,8 @@ export function computeGuildChannelReorderPlan<Id extends string | bigint, Chann
}
}
const precedingId = operation.precedingSiblingId ?? null;
if (precedingId && !channelById.has(precedingId)) {
const precedingChannel = precedingId ? channelById.get(precedingId) : undefined;
if (precedingId && !precedingChannel) {
return {ok: false, code: 'PRECEDING_CHANNEL_NOT_FOUND'};
}
const blockIds = computeChannelMoveBlockIds({channels: orderedChannels, targetId: targetChannel.id});
@@ -224,20 +223,18 @@ export function computeGuildChannelReorderPlan<Id extends string | bigint, Chann
const remainingChannels = orderedChannels.filter((ch) => !blockIds.has(ch.id));
const blockChannels = orderedChannels.filter((ch) => blockIds.has(ch.id));
const expectedParent = desiredParentId ?? null;
if (precedingId) {
const precedingChannel = channelById.get(precedingId)!;
if (precedingChannel) {
const precedingParent = precedingChannel.parentId ?? null;
if (precedingParent !== expectedParent) {
return {ok: false, code: 'PRECEDING_PARENT_MISMATCH'};
}
}
let insertIndex = 0;
if (precedingId) {
if (precedingChannel) {
const precedingIndex = remainingChannels.findIndex((ch) => ch.id === precedingId);
if (precedingIndex === -1) {
return {ok: false, code: 'PRECEDING_NOT_IN_GUILD_LIST'};
}
const precedingChannel = channelById.get(precedingId)!;
if (precedingChannel.type === ChannelTypes.GUILD_CATEGORY) {
const span = findCategorySpanInOrderedList(remainingChannels, precedingChannel.id);
insertIndex = span.end;
@@ -33,8 +33,10 @@ export function encodeSyncedPreferences(preferences: SyncedPreferences): string
export function encodedSyncedPreferencesByteLength(encoded: string): number {
if (encoded === '') return 0;
const padding = encoded.endsWith('==') ? 2 : encoded.endsWith('=') ? 1 : 0;
return Math.floor((encoded.length * 3) / 4) - padding;
const paddedByteLength = Math.floor((encoded.length * 3) / 4);
if (encoded.endsWith('==')) return paddedByteLength - 2;
if (encoded.endsWith('=')) return paddedByteLength - 1;
return paddedByteLength;
}
export function decodeSyncedPreferences(encoded: string | null | undefined): SyncedPreferences {