338 lines
9.8 KiB
TypeScript
338 lines
9.8 KiB
TypeScript
import { Fields, Fn, OperationParser } from "../types/Anilist.ts"
|
|
import { AtLeastOne } from "../types/AtLeastOne.ts"
|
|
import {
|
|
PageActivitiesArgs,
|
|
PageActivityRepliesArgs,
|
|
PageAiringSchedulesArgs,
|
|
PageCharactersArgs,
|
|
PageFollowersArgs,
|
|
PageFollowingArgs,
|
|
// PageInfo,
|
|
PageLikesArgs,
|
|
PageMediaArgs,
|
|
PageMediaListArgs,
|
|
PageMediaTrendsArgs,
|
|
PageNotificationsArgs,
|
|
PageRecommendationsArgs,
|
|
PageReviewsArgs,
|
|
PageStaffArgs,
|
|
PageStudiosArgs,
|
|
PageThreadCommentsArgs,
|
|
PageThreadsArgs,
|
|
PageUsersArgs,
|
|
} from "../../graphql-codegen/generated/graphql.ts"
|
|
import { ActivityReply } from "./ActivityReply.ts"
|
|
import { ActivityUnion } from "./ActivityUnion.ts"
|
|
import { AiringSchedule } from "./AiringSchedule.ts"
|
|
import { Character } from "./Character.ts"
|
|
import { Media } from "./Media.ts"
|
|
import { MediaList } from "./MediaList.ts"
|
|
import { MediaTrend } from "./MediaTrend.ts"
|
|
import { NotificationUnion } from "./NotificationUnion.ts"
|
|
import { Recommendation } from "./Recommendation.ts"
|
|
import { Review } from "./Review.ts"
|
|
import { Staff } from "./Staff.ts"
|
|
import { Studio } from "./Studio.ts"
|
|
import { Thread } from "./Thread.ts"
|
|
import { ThreadComment } from "./ThreadComment.ts"
|
|
import { User } from "./User.ts"
|
|
|
|
export const PageInfo = ({ operation, level }: Fields<OperationParser>) => ({
|
|
/** The total number of items. Note: This value is not guaranteed to be accurate, do not rely on this for logic */
|
|
withTotal(op?: { alias?: string }) {
|
|
operation.set({ alias: op?.alias, subField: "total", level })
|
|
return this
|
|
},
|
|
/** The count on a page */
|
|
withPerPage(op?: { alias?: string }) {
|
|
operation.set({ alias: op?.alias, subField: "perPage", level })
|
|
return this
|
|
},
|
|
/** The current page */
|
|
withCurrentPage(op?: { alias?: string }) {
|
|
operation.set({ alias: op?.alias, subField: "currentPage", level })
|
|
return this
|
|
},
|
|
/** The last page */
|
|
withLastPage(op?: { alias?: string }) {
|
|
operation.set({ alias: op?.alias, subField: "lastPage", level })
|
|
return this
|
|
},
|
|
/** If there is another page */
|
|
withHasNextPage(op?: { alias?: string }) {
|
|
operation.set({ alias: op?.alias, subField: "hasNextPage", level })
|
|
return this
|
|
},
|
|
})
|
|
|
|
export const Page = ({ operation, level }: Fields<OperationParser>) => ({
|
|
/** The pagination information */
|
|
withPageInfo(op: { alias?: string; fn: Fn<typeof PageInfo> }) {
|
|
operation.set({ alias: op.alias, subField: "pageInfo", level, hasSubField: true })
|
|
op.fn(PageInfo({ operation, level: level + 1 }))
|
|
return this
|
|
},
|
|
withUsers(op?: { alias?: string; args?: AtLeastOne<PageUsersArgs>; fn?: Fn<typeof User> }) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "users",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(User({ operation, level: level + 1 }))
|
|
else User({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withMedia(op?: { alias?: string; args?: AtLeastOne<PageMediaArgs>; fn?: Fn<typeof Media> }) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "media",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(Media({ operation, level: level + 1 }))
|
|
else Media({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withCharacters(
|
|
op?: { alias?: string; args?: AtLeastOne<PageCharactersArgs>; fn?: Fn<typeof Character> },
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "characters",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(Character({ operation, level: level + 1 }))
|
|
else Character({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withStaff(op?: { alias?: string; args?: AtLeastOne<PageStaffArgs>; fn?: Fn<typeof Staff> }) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "staff",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(Staff({ operation, level: level + 1 }))
|
|
else Staff({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withStudios(op?: { alias?: string; args?: AtLeastOne<PageStudiosArgs>; fn?: Fn<typeof Studio> }) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "studios",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(Studio({ operation, level: level + 1 }))
|
|
else Studio({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withMediaList(
|
|
op?: { alias?: string; args?: AtLeastOne<PageMediaListArgs>; fn?: Fn<typeof MediaList> },
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "mediaList",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(MediaList({ operation, level: level + 1 }))
|
|
else MediaList({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withAiringSchedules(
|
|
op?: {
|
|
alias?: string
|
|
args?: AtLeastOne<PageAiringSchedulesArgs>
|
|
fn?: Fn<typeof AiringSchedule>
|
|
},
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "airingSchedules",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(AiringSchedule({ operation, level: level + 1 }))
|
|
else AiringSchedule({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withMediaTrends(
|
|
op?: { alias?: string; args?: AtLeastOne<PageMediaTrendsArgs>; fn?: Fn<typeof MediaTrend> },
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "mediaTrends",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(MediaTrend({ operation, level: level + 1 }))
|
|
else MediaTrend({ operation, level: level + 1 }).withMediaId()
|
|
return this
|
|
},
|
|
withNotifications(
|
|
op: {
|
|
alias?: string
|
|
args?: AtLeastOne<PageNotificationsArgs>
|
|
fn: Fn<typeof NotificationUnion>
|
|
},
|
|
) {
|
|
operation.set({
|
|
alias: op.alias,
|
|
subField: "notifications",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
op.fn(NotificationUnion({ operation, level: level + 1 }))
|
|
return this
|
|
},
|
|
withFollowers(
|
|
op?: { alias?: string; args?: AtLeastOne<PageFollowersArgs>; fn?: Fn<typeof User> },
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "followers",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(User({ operation, level: level + 1 }))
|
|
else User({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withFollowing(
|
|
op?: { alias?: string; args?: AtLeastOne<PageFollowingArgs>; fn?: Fn<typeof User> },
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "following",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(User({ operation, level: level + 1 }))
|
|
else User({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withActivities(
|
|
op?: { alias?: string; args?: AtLeastOne<PageActivitiesArgs>; fn?: Fn<typeof ActivityUnion> },
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "activities",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(ActivityUnion({ operation, level: level + 1 }))
|
|
else {
|
|
ActivityUnion({ operation, level: level + 1 })
|
|
.withListActivity()
|
|
.withMessageActivity()
|
|
.withTextActivity()
|
|
}
|
|
return this
|
|
},
|
|
withActivityReplies(
|
|
op?: {
|
|
alias?: string
|
|
args?: AtLeastOne<PageActivityRepliesArgs>
|
|
fn?: Fn<typeof ActivityReply>
|
|
},
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "activityReplies",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(ActivityReply({ operation, level: level + 1 }))
|
|
else ActivityReply({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withThreads(op?: { alias?: string; args?: AtLeastOne<PageThreadsArgs>; fn?: Fn<typeof Thread> }) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "threads",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(Thread({ operation, level: level + 1 }))
|
|
else Thread({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withThreadComments(
|
|
op?: {
|
|
alias?: string
|
|
args?: AtLeastOne<PageThreadCommentsArgs>
|
|
fn?: Fn<typeof ThreadComment>
|
|
},
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "threadComments",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(ThreadComment({ operation, level: level + 1 }))
|
|
else ThreadComment({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withReviews(op?: { alias?: string; args?: AtLeastOne<PageReviewsArgs>; fn?: Fn<typeof Review> }) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "reviews",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(Review({ operation, level: level + 1 }))
|
|
else Review({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withRecommendations(
|
|
op?: {
|
|
alias?: string
|
|
args?: AtLeastOne<PageRecommendationsArgs>
|
|
fn?: Fn<typeof Recommendation>
|
|
},
|
|
) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "recommendations",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(Recommendation({ operation, level: level + 1 }))
|
|
else Recommendation({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
withLikes(op?: { alias?: string; args?: AtLeastOne<PageLikesArgs>; fn?: Fn<typeof User> }) {
|
|
operation.set({
|
|
alias: op?.alias,
|
|
subField: "likes",
|
|
level,
|
|
hasSubField: true,
|
|
variables: op?.args,
|
|
})
|
|
if (op?.fn) op.fn(User({ operation, level: level + 1 }))
|
|
else User({ operation, level: level + 1 }).withId()
|
|
return this
|
|
},
|
|
})
|