refactor: clean up rewrite.ts in Piefed adapter and fix type errors

This commit is contained in:
Xylight committed 2026-02-08 11:01:15 -08:00
1 parent 11f56b7010
commit 9d1d172924
4 files changed
+58 -50

No files matched your search

+4 -1
View File
@@ -289,10 +289,13 @@ const methods: MethodDefinitions = {
}), }),
}, },
// @ts-expect-error for some reason this method doesn't exist on MethodDefinitions
voteOnPoll: { voteOnPoll: {
method: 'POST', method: 'POST',
path: '/api/alpha/post/poll_vote', path: '/api/alpha/post/poll_vote',
transform: (r: piefed['PollVoteResponse']) => toPostView(r.post_view), transform: (r: piefed['PollVoteResponse']) => ({
post_view: toPostView(r.post_view),
}),
}, },
createComment: { createComment: {
+46 -43
View File
@@ -1,9 +1,23 @@
import * as base from '../types' import * as base from '../types'
import type { components } from './schema' import type { components, paths } from './schema'
type Schema<T extends keyof components['schemas']> = components['schemas'][T]
type PathQuery<P extends keyof paths> = paths[P] extends {
get: { parameters: { query?: infer Q } }
}
? Q
: never
type PathBody<P extends keyof paths> = paths[P] extends {
post: { requestBody: { content: { 'application/json': infer B } } }
}
? B
: never
export function toPersonView( export function toPersonView(
personView: components['schemas']['PersonView'], personView: Schema<'PersonView'>,
): PersonView { ): base.PersonView {
return { return {
counts: personView.counts, counts: personView.counts,
is_admin: personView.is_admin, is_admin: personView.is_admin,
@@ -11,7 +25,7 @@ export function toPersonView(
} }
} }
export function toPerson(person: components['schemas']['Person']): base.Person { export function toPerson(person: Schema<'Person'>): base.Person {
return { return {
...person, ...person,
id: person.id, id: person.id,
@@ -20,15 +34,13 @@ export function toPerson(person: components['schemas']['Person']): base.Person {
} }
} }
export function toLocalSite( export function toLocalSite(site: Schema<'Site'>): base.LocalSite {
site: components['schemas']['Site'],
): base.LocalSite {
if (site) return {} if (site) return {}
return {} return {}
} }
export function toCommunityView( export function toCommunityView(
communityView: components['schemas']['CommunityView'], communityView: Schema<'CommunityView'>,
): base.CommunityView { ): base.CommunityView {
return { return {
...communityView, ...communityView,
@@ -49,9 +61,7 @@ export function toCommunityView(
} }
} }
export function toCommunity( export function toCommunity(community: Schema<'Community'>): base.Community {
community: components['schemas']['Community'],
): base.Community {
return { return {
...community, ...community,
posting_restricted_to_mods: false, posting_restricted_to_mods: false,
@@ -59,9 +69,7 @@ export function toCommunity(
} }
} }
export function toMyUser( export function toMyUser(myUser: Schema<'MyUserInfo'>): base.MyUserInfo {
myUser: components['schemas']['MyUserInfo'],
): base.MyUserInfo {
return { return {
...myUser, ...myUser,
community_blocks: myUser.community_blocks.map((i) => ({ community_blocks: myUser.community_blocks.map((i) => ({
@@ -100,9 +108,7 @@ export function toMyUser(
} }
} }
export function toPostView( export function toPostView(postView: Schema<'PostView'>): base.PostView {
postView: components['schemas']['PostView'],
): base.PostView {
return { return {
...postView, ...postView,
creator: toPerson(postView.creator), creator: toPerson(postView.creator),
@@ -112,7 +118,7 @@ export function toPostView(
} }
} }
export function toPost(post: components['schemas']['Post']): Post { export function toPost(post: Schema<'Post'>): base.Post {
return { return {
...post, ...post,
creator_id: -1, creator_id: -1,
@@ -123,7 +129,7 @@ export function toPost(post: components['schemas']['Post']): Post {
} }
export function toCommentView( export function toCommentView(
commentView: components['schemas']['CommentView'], commentView: Schema<'CommentView'>,
): base.CommentView { ): base.CommentView {
return { return {
...commentView, ...commentView,
@@ -131,11 +137,11 @@ export function toCommentView(
creator: toPerson(commentView.creator), creator: toPerson(commentView.creator),
post: toPost(commentView.post), post: toPost(commentView.post),
community: toCommunity(commentView.community), community: toCommunity(commentView.community),
subscribed: commentView.subscribed as SubscribedType, subscribed: commentView.subscribed as base.SubscribedType,
} }
} }
export function toComment(comment: components['schemas']['Comment']): Comment { export function toComment(comment: Schema<'Comment'>): base.Comment {
return { return {
...comment, ...comment,
content: comment.body, content: comment.body,
@@ -145,7 +151,7 @@ export function toComment(comment: components['schemas']['Comment']): Comment {
} }
export function toCommentReplyView( export function toCommentReplyView(
commentReplyView: components['schemas']['CommentReplyView'], commentReplyView: Schema<'CommentReplyView'>,
): base.CommentReplyView { ): base.CommentReplyView {
return { return {
...commentReplyView, ...commentReplyView,
@@ -160,7 +166,7 @@ export function toCommentReplyView(
} }
export function toPrivateMessageView( export function toPrivateMessageView(
privateMessageView: components['schemas']['PrivateMessageView'], privateMessageView: Schema<'PrivateMessageView'>,
): base.PrivateMessageView { ): base.PrivateMessageView {
return { return {
...privateMessageView, ...privateMessageView,
@@ -170,7 +176,7 @@ export function toPrivateMessageView(
} }
export function toPersonMentionView( export function toPersonMentionView(
personMentionView: components['schemas']['CommentReplyView'], personMentionView: Schema<'CommentReplyView'>,
): base.PersonMentionView { ): base.PersonMentionView {
return { return {
...personMentionView, ...personMentionView,
@@ -185,9 +191,7 @@ export function toPersonMentionView(
} }
} }
export function toTopicView( export function toTopicView(topicView: Schema<'TopicView'>): base.TopicView {
topicView: components['schemas']['TopicView'],
): base.TopicView {
return { return {
...topicView, ...topicView,
communities: topicView.communities?.map(toCommunity), communities: topicView.communities?.map(toCommunity),
@@ -195,9 +199,7 @@ export function toTopicView(
} }
} }
export function toFeedView( export function toFeedView(feedView: Schema<'FeedView'>): base.FeedView {
feedView: components['schemas']['FeedView'],
): base.FeedView {
return { return {
...feedView, ...feedView,
communities: feedView.communities?.map(toCommunity), communities: feedView.communities?.map(toCommunity),
@@ -205,10 +207,9 @@ export function toFeedView(
} }
} }
// Query calls
export function fromGetPosts( export function fromGetPosts(
getPosts: base.GetPosts, getPosts: base.GetPosts,
): components['parameters']['GetPosts'] { ): PathQuery<'/api/alpha/post/list'> {
return { return {
...getPosts, ...getPosts,
sort: toSortType(getPosts.sort), sort: toSortType(getPosts.sort),
@@ -218,7 +219,7 @@ export function fromGetPosts(
export function fromGetComments( export function fromGetComments(
getComments: base.GetComments, getComments: base.GetComments,
): components['schemas']['GetComments'] { ): PathQuery<'/api/alpha/comment/list'> {
return { return {
...getComments, ...getComments,
sort: toCommentSortType(getComments.sort), sort: toCommentSortType(getComments.sort),
@@ -227,13 +228,13 @@ export function fromGetComments(
export function fromGetPost( export function fromGetPost(
getPost: base.GetPost, getPost: base.GetPost,
): components['schemas']['GetPost'] { ): PathQuery<'/api/alpha/post'> {
return getPost return getPost
} }
export function fromGetReplies( export function fromGetReplies(
getReplies: base.GetReplies, getReplies: base.GetReplies,
): components['schemas']['GetReplies'] { ): PathQuery<'/api/alpha/user/replies'> {
return { return {
...getReplies, ...getReplies,
sort: toCommentSortType(getReplies.sort), sort: toCommentSortType(getReplies.sort),
@@ -242,7 +243,7 @@ export function fromGetReplies(
export function fromSearch( export function fromSearch(
search: base.Search, search: base.Search,
): components['schemas']['Search'] { ): PathQuery<'/api/alpha/search'> {
return { return {
...(search as Omit<base.Search, 'type_'> & { ...(search as Omit<base.Search, 'type_'> & {
type_: 'Communities' | 'Users' | 'Url' | 'Posts' type_: 'Communities' | 'Users' | 'Url' | 'Posts'
@@ -253,7 +254,7 @@ export function fromSearch(
export function fromCreatePost( export function fromCreatePost(
createPost: base.CreatePost, createPost: base.CreatePost,
): components['schemas']['CreatePost'] { ): PathBody<'/api/alpha/post'> {
return { return {
...createPost, ...createPost,
title: createPost.name, title: createPost.name,
@@ -262,26 +263,27 @@ export function fromCreatePost(
export function fromListCommunities( export function fromListCommunities(
listCommunities: base.ListCommunities, listCommunities: base.ListCommunities,
): components['schemas']['CommunityRequest'] { ): PathQuery<'/api/alpha/community/list'> {
return { return {
...listCommunities, ...listCommunities,
type_: listCommunities.type_ as 'All',
sort: toCommunitiesSortType(listCommunities.sort), sort: toCommunitiesSortType(listCommunities.sort),
} }
} }
export function fromCreateComment( export function fromCreateComment(
createComment: base.CreateComment, createComment: base.CreateComment,
): components['schemas']['CreateComment'] { ): PathBody<'/api/alpha/comment'> {
return { return {
...createComment, ...createComment,
body: createComment.content, body: createComment.content,
} }
} }
// Misc
export function toSortType( export function toSortType(
sortType?: base.SortType, sortType?: base.SortType,
): components['schemas']['SortType'] | undefined { ): Schema<'SortType'> | undefined {
if (!sortType) return
if ( if (
sortType == 'Old' || sortType == 'Old' ||
sortType == 'MostComments' || sortType == 'MostComments' ||
@@ -289,12 +291,13 @@ export function toSortType(
sortType == 'NewComments' sortType == 'NewComments'
) )
sortType = 'New' sortType = 'New'
return sortType if (sortType?.startsWith('Top')) sortType = 'TopAll'
return sortType as Schema<'SortType'>
} }
export function toCommentSortType( export function toCommentSortType(
commentSortType?: base.CommentSortType, commentSortType?: base.CommentSortType,
): components['schemas']['CommentSortType'] | undefined { ): Schema<'CommentSortType'> | undefined {
if (commentSortType == 'Controversial') commentSortType = undefined if (commentSortType == 'Controversial') commentSortType = undefined
return commentSortType return commentSortType
} }
+4 -2
View File
@@ -4706,7 +4706,7 @@ export interface components {
* Format: datetime * Format: datetime
* @example 2025-06-07T02:29:07.980084Z * @example 2025-06-07T02:29:07.980084Z
*/ */
published?: string published: string
title?: string title?: string
} }
PersonView: { PersonView: {
@@ -6057,7 +6057,7 @@ export interface components {
* Format: datetime * Format: datetime
* @example 2025-06-07T02:29:07.980084Z * @example 2025-06-07T02:29:07.980084Z
*/ */
published?: string published: string
} }
PostReportView: { PostReportView: {
post_report: components['schemas']['PostReport'] post_report: components['schemas']['PostReport']
@@ -6215,6 +6215,8 @@ export interface components {
private_message_id: number private_message_id: number
reason: string reason: string
} }
SortType: 'Active' | 'Hot' | 'TopAll' | 'New' | 'Old'
CommentSortType: 'Hot' | 'Top' | 'TopAll' | 'New' | 'Old'
} }
responses: { responses: {
/** @description Unprocessable Content */ /** @description Unprocessable Content */
+4 -4
View File
@@ -165,7 +165,7 @@ export interface GetPersonMentionsResponse {
} }
export interface GetPost { export interface GetPost {
id?: PostId id: PostId
comment_id?: CommentId comment_id?: CommentId
} }
@@ -190,8 +190,8 @@ export interface GetPosts {
show_read?: boolean show_read?: boolean
show_nsfw?: boolean show_nsfw?: boolean
page_cursor?: PaginationCursor page_cursor?: PaginationCursor
feed_id?: string feed_id?: number
topic_id?: string topic_id?: number
} }
export interface GetPostsResponse { export interface GetPostsResponse {
@@ -2333,7 +2333,7 @@ export interface UpdateTotpResponse {
} }
export interface UploadImage { export interface UploadImage {
image: File | Buffer image: File
} }
export interface UploadImageResponse { export interface UploadImageResponse {
msg: string msg: string