Skip to content

Commit

Permalink
feat: global-types
Browse files Browse the repository at this point in the history
  • Loading branch information
lmly9193 committed Jul 15, 2024
1 parent 27d0fb2 commit 6a809c9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 82 deletions.
10 changes: 7 additions & 3 deletions src/controllers/feed.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { User, Feeder } from '../iwara';
import { response } from '../utils';
import { FeedRequest } from '../types';

export async function feed({ username, format }: FeedRequest) {
const user = await User.from(username);
type Request = {
username: string;
format?: 'atom' | 'rss' | 'json';
};

export async function feed({ username, format }: Request) {
const user = await User.find(username);
const feeder = await Feeder.create(user);

if (format === undefined || response[format] === undefined) {
Expand Down
32 changes: 32 additions & 0 deletions src/global-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
declare namespace API {
type Profile = {
[key: string]: unknown;
user: User;
};

type User = {
[key: string]: unknown;
id: string;
name: string;
username: string;
};

type Search = {
[key: string]: unknown;
count: number;
results: Video[];
};

type Video = {
[key: string]: unknown;
id: string;
title: string;
thumbnail: number;
file: {
[key: string]: unknown;
id: string;
};
user: User;
createdAt: string;
};
}
14 changes: 6 additions & 8 deletions src/iwara/feeder.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Feed } from 'feed';
import { User } from './user';
import { IFeeder, IFeederFactory, IwaraVideo } from '../types';

// Todo: 解藕類型
export class Feeder implements IFeeder {
export class Feeder {
feed: Feed;

constructor(user: User) {
Expand All @@ -16,19 +14,19 @@ export class Feeder implements IFeeder {
});
}

static async create(this: IFeederFactory ,user: User): Promise<Feeder> {
static async create(user: User): Promise<Feeder> {
const feeder = new this(user);
await feeder.import(await user.video());
feeder.import(await user.video());
return feeder;
}

async import(videos: IwaraVideo[]): Promise<void> {
for (const video of videos) {
import(list: API.Video[]): void {
for (const video of list) {
this.add(video);
}
}

add(video: IwaraVideo): void {
add(video: API.Video): void {
let src = `https://fakeimg.pl/440x230/282828/eae0d0/?retina=1&text=Not+Found`;
if (!!video.file) {
let thumb = String(video.thumbnail.toString()).padStart(2, '0');
Expand Down
15 changes: 7 additions & 8 deletions src/iwara/user.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import { get } from '../utils';
import { IwaraUser, IwaraProfile, IwaraVideoSearch, IwaraVideo, IUser, IUserFactory } from '../types';

export class User implements IUser {
export class User {
id: string;
name: string;
username: string;

constructor({ id, name, username }: IwaraUser) {
constructor({ id, name, username }: API.User) {
this.id = id;
this.name = name;
this.username = username;
}

static async from(this: IUserFactory, username: string): Promise<User> {
static async find(username: string): Promise<User> {
try {
const { user }: IwaraProfile = await get(`https://api.iwara.tv/profile/${username}`);
const { user }: API.Profile = await get(`https://api.iwara.tv/profile/${username}`);
return new this(user);
} catch (error) {
throw new Error(`User not found: ${username}`);
}
}

async video(): Promise<IwaraVideo[]> {
let data: IwaraVideo[] = [];
async video(): Promise<API.Video[]> {
let data: API.Video[] = [];
let qty: number = 0;
let page: number = 0;

do {
const search: IwaraVideoSearch = await get(`https://api.iwara.tv/videos`, {
const search: API.Search = await get(`https://api.iwara.tv/videos`, {
user: this.id,
page: page++,
sort: 'date',
Expand Down
63 changes: 0 additions & 63 deletions src/types.ts

This file was deleted.

0 comments on commit 6a809c9

Please sign in to comment.