File size: 3,657 Bytes
57aa51e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | // Key mappings for minifying post list/calendar responses to reduce payload size.
// Both backend (minify) and frontend (expand) import from here.
const POST_LIST_KEYS: Record<string, string> = {
posts: 'p',
total: 't',
page: 'pg',
limit: 'l',
hasMore: 'hm',
};
const POST_CALENDAR_KEYS: Record<string, string> = {
posts: 'p',
};
const POST_ITEM_KEYS: Record<string, string> = {
id: 'i',
content: 'c',
publishDate: 'd',
releaseURL: 'u',
releaseId: 'ri',
state: 's',
group: 'g',
tags: 'tg',
integration: 'n',
intervalInDays: 'iv',
actualDate: 'ad',
creationMethod: 'cm',
};
const INTEGRATION_KEYS: Record<string, string> = {
id: 'i',
providerIdentifier: 'pi',
name: 'n',
picture: 'p',
};
const TAG_KEYS: Record<string, string> = {
tag: 't',
};
const TAG_INNER_KEYS: Record<string, string> = {
id: 'i',
name: 'n',
color: 'c',
orgId: 'o',
createdAt: 'ca',
updatedAt: 'ua',
deletedAt: 'da',
};
function mapKeys(obj: Record<string, any>, keyMap: Record<string, string>) {
const result: Record<string, any> = {};
for (const [key, value] of Object.entries(obj)) {
result[keyMap[key] || key] = value;
}
return result;
}
function reverseMap(keyMap: Record<string, string>) {
const reversed: Record<string, string> = {};
for (const [key, value] of Object.entries(keyMap)) {
reversed[value] = key;
}
return reversed;
}
function minifyPostItem(post: any) {
return mapKeys(
{
...post,
integration: post.integration
? mapKeys(post.integration, INTEGRATION_KEYS)
: post.integration,
tags: post.tags?.map((tagWrapper: any) =>
mapKeys(
{
...tagWrapper,
tag: tagWrapper.tag
? mapKeys(tagWrapper.tag, TAG_INNER_KEYS)
: tagWrapper.tag,
},
TAG_KEYS
)
),
},
POST_ITEM_KEYS
);
}
function expandPostItem(post: any) {
const postReversed = reverseMap(POST_ITEM_KEYS);
const integrationReversed = reverseMap(INTEGRATION_KEYS);
const tagReversed = reverseMap(TAG_KEYS);
const tagInnerReversed = reverseMap(TAG_INNER_KEYS);
const expandedPost = mapKeys(post, postReversed);
if (expandedPost.integration) {
expandedPost.integration = mapKeys(
expandedPost.integration,
integrationReversed
);
}
if (expandedPost.tags) {
expandedPost.tags = expandedPost.tags.map((tagWrapper: any) => {
const expandedWrapper = mapKeys(tagWrapper, tagReversed);
if (expandedWrapper.tag) {
expandedWrapper.tag = mapKeys(expandedWrapper.tag, tagInnerReversed);
}
return expandedWrapper;
});
}
return expandedPost;
}
// --- getPostsList (paginated list view) ---
export function minifyPostsList(data: {
posts: any[];
total: number;
page: number;
limit: number;
hasMore: boolean;
}) {
return mapKeys(
{
...data,
posts: data.posts.map(minifyPostItem),
},
POST_LIST_KEYS
);
}
export function expandPostsList(data: any) {
const topReversed = reverseMap(POST_LIST_KEYS);
const expanded = mapKeys(data, topReversed);
expanded.posts = (expanded.posts || []).map(expandPostItem);
return expanded;
}
// --- getPosts (calendar view) ---
export function minifyPosts(data: { posts: any[] }) {
return mapKeys(
{
...data,
posts: data.posts.map(minifyPostItem),
},
POST_CALENDAR_KEYS
);
}
export function expandPosts(data: any) {
const topReversed = reverseMap(POST_CALENDAR_KEYS);
const expanded = mapKeys(data, topReversed);
expanded.posts = (expanded.posts || []).map(expandPostItem);
return expanded;
}
|