Skip to content

Commit

Permalink
add TypeScript types for V2 api (#777)
Browse files Browse the repository at this point in the history
- add a script to validate them from the test snapshots

[#188793802]
  • Loading branch information
uraniumanchor authored Jan 22, 2025
1 parent ad9c972 commit cba013c
Show file tree
Hide file tree
Showing 37 changed files with 730 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import classNames from 'classnames';
import { useDrop } from 'react-dnd';

import type { Donation } from '@public/apiv2/APITypes';
import type { APIDonation as Donation } from '@public/apiv2/APITypes';

import styles from './DonationRow.mod.css';

Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/donations/DonationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { CSSTransition, TransitionGroup } from 'react-transition-group';
import { Text } from '@spyrothon/sparx';

import type { Donation } from '@public/apiv2/APITypes';
import type { APIDonation as Donation } from '@public/apiv2/APITypes';
import Spinner from '@public/spinner';

import styles from './DonationList.mod.css';
Expand Down
3 changes: 2 additions & 1 deletion bundles/processing/modules/donations/DonationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import classNames from 'classnames';
import { useDrag, useDrop } from 'react-dnd';
import { Clickable, Stack, Text } from '@spyrothon/sparx';

import type { Donation, DonationBid } from '@public/apiv2/APITypes';
import type { APIDonation as Donation } from '@public/apiv2/APITypes';
import { DonationBid } from '@public/apiv2/Models';
import * as CurrencyUtils from '@public/util/currency';
import DragHandle from '@uikit/icons/DragHandle';

Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/donations/DonationsStore.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import create from 'zustand';

import { Donation } from '@public/apiv2/APITypes';
import { APIDonation as Donation } from '@public/apiv2/APITypes';

type DonationId = Donation['id'];

Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/donations/ModCommentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useMutation } from 'react-query';
import { Button, Card, FormControl, Header, Stack, Text, TextArea } from '@spyrothon/sparx';

import APIClient from '@public/apiv2/APIClient';
import { Donation } from '@public/apiv2/APITypes';
import { APIDonation as Donation } from '@public/apiv2/APITypes';
import * as CurrencyUtils from '@public/util/currency';
import TimeUtils from '@public/util/TimeUtils';

Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/layout/SidebarLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import classNames from 'classnames';
import { Header, Stack, Text } from '@spyrothon/sparx';

import { Event } from '@public/apiv2/APITypes';
import { APIEvent as Event } from '@public/apiv2/APITypes';

import EventTotalDisplay from '../event/EventTotalDisplay';
import { PrimaryNavPopoutButton } from '../settings/PrimaryNavPopout';
Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/processing/ActionLog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CSSTransition, TransitionGroup } from 'react-transition-group';
import { Anchor, Button, Header, Text } from '@spyrothon/sparx';

import APIClient from '@public/apiv2/APIClient';
import { Donation } from '@public/apiv2/APITypes';
import { APIDonation as Donation } from '@public/apiv2/APITypes';
import * as CurrencyUtils from '@public/util/currency';
import Undo from '@uikit/icons/Undo';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Anchor, Button, openModal, Stack, Text } from '@spyrothon/sparx';

import { usePermission } from '@public/api/helpers/auth';
import APIClient from '@public/apiv2/APIClient';
import type { Donation } from '@public/apiv2/APITypes';
import type { APIDonation as Donation } from '@public/apiv2/APITypes';
import TimeUtils from '@public/util/TimeUtils';
import Approve from '@uikit/icons/Approve';
import Comment from '@uikit/icons/Comment';
Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/processing/ProcessingStore.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import create from 'zustand';
import { persist } from 'zustand/middleware';

import { Donation } from '@public/apiv2/APITypes';
import { APIDonation as Donation } from '@public/apiv2/APITypes';

let nextId = 0;

Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/processing/ProcessingTypes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Donation } from '@public/apiv2/APITypes';
import { APIDonation as Donation } from '@public/apiv2/APITypes';

import { DonationState } from '../donations/DonationsStore';

Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/reading/ReadingDonationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useMutation } from 'react-query';
import { Button, openPopout, PressEvent, Stack, Tag, Text, useTooltip } from '@spyrothon/sparx';

import APIClient from '@public/apiv2/APIClient';
import { Donation } from '@public/apiv2/APITypes';
import { APIDonation as Donation } from '@public/apiv2/APITypes';
import TimeUtils from '@public/util/TimeUtils';
import Approve from '@uikit/icons/Approve';
import Deny from '@uikit/icons/Deny';
Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/modules/reading/ReadingTypes.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TabColor } from '@spyrothon/sparx';

import type { Donation } from '@public/apiv2/APITypes';
import type { APIDonation as Donation } from '@public/apiv2/APITypes';

export interface FilterGroupTabItemBase {
type: 'filter' | 'group';
Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/pages/ProcessDonations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FormControl, Stack } from '@spyrothon/sparx';

import { usePermission } from '@public/api/helpers/auth';
import APIClient from '@public/apiv2/APIClient';
import type { Donation, Event } from '@public/apiv2/APITypes';
import type { APIDonation as Donation, APIEvent as Event } from '@public/apiv2/APITypes';

import DonationList from '../modules/donations/DonationList';
import { loadDonations, useDonationsInState } from '../modules/donations/DonationsStore';
Expand Down
2 changes: 1 addition & 1 deletion bundles/processing/pages/ReadDonations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useParams } from 'react-router';
import { Button, openModal, Stack, Tabs } from '@spyrothon/sparx';

import APIClient from '@public/apiv2/APIClient';
import type { Donation } from '@public/apiv2/APITypes';
import { APIDonation as Donation } from '@public/apiv2/APITypes';
import Plus from '@uikit/icons/Plus';

import DonationDropTarget from '@processing/modules/donations/DonationDropTarget';
Expand Down
File renamed without changes.
229 changes: 229 additions & 0 deletions bundles/public/apiv2/APITypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import {
Ad,
BidBase,
Country,
CountryRegion,
Donation,
DonationBid,
Donor,
Event,
Interview,
Milestone,
Prize,
PrizeState,
Run,
Talent,
} from '@public/apiv2/Models';

export type Me = {
username: string;
superuser: boolean;
staff: boolean;
permissions: string[];
};

type SingleKey = number | string | [string];

export type EventAPIId = SingleKey;
export type TalentAPIId = SingleKey;
export type RunAPId = number | [string, string, [string]];

interface BidChain extends Omit<BidBase, 'event' | 'speedrun' | 'parent' | 'chain' | 'repeat' | 'allowuseroptions'> {
readonly bid_type: 'challenge';
chain_goal: number;
chain_remaining: number;
}

interface BidChild
extends Omit<
BidBase,
'event' | 'speedrun' | 'parent' | 'chain' | 'allowuseroptions' | 'close_at' | 'post_run' | 'repeat' | 'goal'
> {
readonly bid_type: 'option';
}

interface TreeBid extends Omit<BidBase, 'event' | 'repeat' | 'allowuseroptions'> {
event?: number;
level?: number;
repeat?: null | number;
allowuseroptions?: boolean;
options?: BidChild[];
chain_steps?: BidChain[];
}

export interface BidPost {
event?: EventAPIId;
name: string;
speedrun?: RunAPId;
parent?: number;
goal?: number;
}

export type BidPatch = Omit<Partial<BidPost>, 'event'>;

export interface APIEvent extends Omit<Event, 'datetime'> {
datetime: string;
amount?: number;
donation_count?: number;
}

export interface APIDonation extends Omit<Donation, 'event'> {
event?: number;
}

export interface APIRun
extends Omit<Run, 'event' | 'starttime' | 'endtime' | 'run_time' | 'setup_time' | 'anchor_time'> {
event?: APIEvent;
starttime: null | string;
endtime: null | string;
run_time: string;
setup_time: string;
anchor_time: null | string;
}

export interface RunPost {
event?: EventAPIId;
name: string;
category: string;
display_name?: string;
twitch_name?: string;
description?: string;
coop?: boolean;
onsite?: 'ONSITE' | 'ONLINE' | 'HYBRID';
release_year?: number;
console?: string;
order?: number | 'last';
runners: TalentAPIId[];
hosts?: TalentAPIId[];
commentators?: TalentAPIId[];
run_time: string;
setup_time: string;
anchor_time?: string;
tech_notes?: string;
video_links?: {
link_type: string;
url: string;
}[];
priority_tag?: null | string;
tags?: string[];
}

export type RunPatch = Partial<RunPost>;

export interface APIMilestone extends Omit<Milestone, 'event'> {
event?: APIEvent;
}

export interface MilestonePost {
event?: EventAPIId;
name: string;
amount: number;
run?: RunAPId;
}

export type MilestonePatch = Partial<MilestonePost>;

export interface APIInterview extends Omit<Interview, 'event' | 'length' | 'interviewers' | 'subjects'> {
event?: number | APIEvent;
length: string;
interviewers: Talent[];
subjects: Talent[];
}

interface InterstitialPost {
event?: EventAPIId;
anchor?: RunAPId;
order?: number;
suborder: number | 'last';
length: string;
tags?: string[];
}

export interface InterviewPost extends InterstitialPost {
interviewers: TalentAPIId[];
subjects?: TalentAPIId[];
topic: string;
}

export type InterviewPatch = Partial<InterviewPost>;

export interface APIAd extends Omit<Ad, 'event' | 'length'> {
event?: number | APIEvent;
length: string;
}

export interface AdPost extends InterstitialPost {
filename: string;
sponsor_name: string;
ad_name: string;
ad_type: 'VIDEO' | 'IMAGE';
blurb?: string;
}

export type AdPatch = Partial<AdPost>;

export interface APIPrize extends Omit<Prize, 'event' | 'starttime' | 'endtime' | 'start_draw_time' | 'end_draw_time'> {
event?: APIEvent;
starttime: null | string;
endtime: null | string;
start_draw_time: null | string;
end_draw_time: null | string;
}

export interface PrizePost {
event?: EventAPIId;
name: string;
startrun?: number;
endrun?: number;
starttime?: string;
endtime?: string;
description?: string;
shortdescription?: string;
image?: string;
altimage?: string;
estimatedvalue?: number;
minimumbid?: number;
sumdonations?: boolean;
provider?: string;
creator?: string;
creatorwebsite?: string;
state?: PrizeState;
}

export type PrizePatch = Partial<PrizePost>;

export interface TalentPost {
name: string;
stream?: string;
twitter?: string;
youtube?: string;
pronouns?: string;
}

export type TalentPatch = Partial<TalentPost>;

export interface DonationCommentPatch {
comment: string;
}

export type APIModel =
| APIAd
| APIDonation
| APIEvent
| APIInterview
| APIMilestone
| APIPrize
| APIRun
| TreeBid
| Country
| CountryRegion
| DonationBid
| Donor
| Talent;

export interface PaginationInfo<T extends APIModel = APIModel> {
count: number;
next: null | string;
previous: null | string;
results: T[];
}
Loading

0 comments on commit cba013c

Please sign in to comment.