Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = {
},

get gift_links() {
return require('./gift-links');
return require('../../../../../services/gift-links/serializers').giftLinksSerializers;
},

get redirects() {
Expand Down
46 changes: 46 additions & 0 deletions ghost/core/core/server/services/gift-links/serializers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {z} from 'zod';
import {GiftLink} from './models';
import type {Post} from './models';

interface Frame {
response?: unknown;
}

// Response schemas — the shapes the admin endpoints emit.
const GiftLinkResource = z.object({
token: z.string(),
redeemed_count: z.number(),
last_redeemed_at: z.date().nullable(),
created_at: z.date()
});
const GiftLinksResponse = z.object({gift_links: z.array(GiftLinkResource)});
const RevokeAllResponse = z.object({meta: z.object({count: z.number()})});

const toGiftLinksResponse = z.array(GiftLink)
.transform((links): z.input<typeof GiftLinksResponse> => ({
gift_links: links.map(link => ({
token: link.token,
redeemed_count: link.redeemedCount,
last_redeemed_at: link.lastRedeemedAt,
created_at: link.createdAt
}))
}))
.pipe(GiftLinksResponse);

const toRevokeAllResponse = z.object({count: z.number()})
.transform((data): z.input<typeof RevokeAllResponse> => ({meta: {count: data.count}}))
.pipe(RevokeAllResponse);

const serializeLiveLinks = (post: Post, _apiConfig: unknown, frame: Frame): void => {
frame.response = toGiftLinksResponse.parse(post.giftLinks);
};

// Endpoint -> response shape.
export const giftLinksSerializers = {
read: serializeLiveLinks,
issue: serializeLiveLinks,
reissue: serializeLiveLinks,
revokeAll(data: {count: number}, _apiConfig: unknown, frame: Frame): void {
frame.response = toRevokeAllResponse.parse(data);
}
};
Loading