this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Exclude nullish values when formatting labels

futurGH e39ad9a3 6ec7fcb0

+16 -12
+2 -2
src/LabelerServer.ts
··· 28 28 SavedLabel, 29 29 SubscriptionHandler, 30 30 } from "./util/types.js"; 31 - import { excludeUndefined } from "./util/util.js"; 31 + import { excludeNullish } from "./util/util.js"; 32 32 33 33 /** 34 34 * Options for the {@link LabelerServer} class. ··· 163 163 */ 164 164 async createLabel(label: CreateLabelData): Promise<SavedLabel> { 165 165 return this.saveLabel( 166 - excludeUndefined({ 166 + excludeNullish({ 167 167 ...label, 168 168 src: label.src ?? this.did, 169 169 cts: label.cts ?? new Date().toISOString(),
+2 -2
src/util/labels.ts
··· 2 2 import type { ComAtprotoLabelDefs } from "@atproto/api"; 3 3 import type { Keypair } from "@atproto/crypto"; 4 4 import type { SignedLabel } from "./types.js"; 5 - import { excludeUndefined } from "./util.js"; 5 + import { excludeNullish } from "./util.js"; 6 6 7 7 const LABEL_VERSION = 1; 8 8 9 9 export function formatLabel(label: ComAtprotoLabelDefs.Label): ComAtprotoLabelDefs.Label { 10 - return excludeUndefined({ ...label, ver: LABEL_VERSION, neg: !!label.neg }); 10 + return excludeNullish({ ...label, ver: LABEL_VERSION, neg: !!label.neg }); 11 11 } 12 12 13 13 export async function signLabel(
+7 -5
src/util/types.ts
··· 9 9 RouteHandlerMethod, 10 10 } from "fastify"; 11 11 12 - type OptionalOrUndefinedKeys<T> = { [K in keyof T]: undefined extends T[K] ? K : never }[keyof T]; 13 - type RequiredDefinedKeys<T> = Exclude<keyof T, OptionalOrUndefinedKeys<T>>; 14 - export type StrictPartial<T> = 15 - & { [K in OptionalOrUndefinedKeys<T>]+?: Exclude<T[K], undefined> } 16 - & { [K in RequiredDefinedKeys<T>]-?: T[K] }; 12 + type NullishKeys<T> = { 13 + [K in keyof T]: null extends T[K] ? K : undefined extends T[K] ? K : never; 14 + }[keyof T]; 15 + type NonNullishKeys<T> = Exclude<keyof T, NullishKeys<T>>; 16 + export type NonNullishPartial<T> = 17 + & { [K in NullishKeys<T>]+?: Exclude<T[K], null | undefined> } 18 + & { [K in NonNullishKeys<T>]-?: T[K] }; 17 19 18 20 /** 19 21 * Data required to create a label.
+5 -3
src/util/util.ts
··· 1 - import { StrictPartial } from "./types.js"; 1 + import { NonNullishPartial } from "./types.js"; 2 2 3 - export function excludeUndefined<T extends Record<PropertyKey, unknown>>(obj: T): StrictPartial<T> { 3 + export function excludeNullish<T extends Record<PropertyKey, unknown>>( 4 + obj: T, 5 + ): NonNullishPartial<T> { 4 6 return Object.entries(obj).reduce<Record<string, unknown>>((acc, [key, value]) => { 5 - if (value !== undefined) { 7 + if (value != null) { 6 8 acc[key] = value; 7 9 } 8 10 return acc;