Skip to content

Commit

Permalink
fix: omit null for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhooks committed Dec 18, 2024
1 parent df9dc0b commit d368956
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/convertkit-sdk/src/convertkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,29 @@ export async function updateSubscriber(subscriber: {
.then(({subscriber}) => subscriber)
}

function deepOmitNull(obj: any): any {
if (Array.isArray(obj)) {
return obj.map(deepOmitNull).filter((x) => x !== null)
}

if (obj && typeof obj === 'object') {
return Object.entries(obj).reduce((acc, [key, value]) => {
const cleaned = deepOmitNull(value)
if (cleaned !== null) {
acc[key] = cleaned
}
return acc
}, {} as Record<string, any>)
}

return obj === null ? undefined : obj
}

export function getConvertkitSubscriberCookie(subscriber: any): Cookie[] {
return [
{
name: 'ck_subscriber',
value: JSON.stringify(omitBy(subscriber, isNull)),
value: JSON.stringify(deepOmitNull(subscriber)),
options: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
Expand Down

0 comments on commit d368956

Please sign in to comment.