-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use model classes instead of working directly on the responses
- Loading branch information
1 parent
ba39925
commit 067daaa
Showing
8 changed files
with
152 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./config"; | ||
export * from "./user"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { CourseDto, StreakDataDto, UserDto } from "../responses"; | ||
|
||
export class User { | ||
public readonly name: string; | ||
public readonly username: string; | ||
public readonly pictureUrl: string; | ||
public readonly streak: Streak; | ||
public readonly currentCourse?: CurrentCourse; | ||
|
||
public static fromResponse(response: UserDto): User { | ||
return new User(response); | ||
} | ||
|
||
private constructor(user: UserDto) { | ||
this.name = user.name; | ||
this.username = user.username; | ||
this.pictureUrl = user.picture.replace("//", "https://") + "/xxlarge"; | ||
this.streak = Streak.fromResponse(user.streakData); | ||
const currentCourse = user.courses.find( | ||
(c) => c.id === user.currentCourseId, | ||
); | ||
if (currentCourse) { | ||
this.currentCourse = CurrentCourse.fromResponse(currentCourse); | ||
} | ||
} | ||
} | ||
|
||
class Streak { | ||
public readonly days: number = 0; | ||
public readonly startDate?: string; | ||
public readonly endDate?: string; | ||
|
||
public static fromResponse(response: StreakDataDto): Streak { | ||
if (!response.currentStreak) { | ||
return new Streak(0); | ||
} | ||
return new Streak( | ||
response.currentStreak.length, | ||
response.currentStreak.startDate, | ||
response.currentStreak.endDate, | ||
); | ||
} | ||
|
||
private constructor(days: number, startDate?: string, endDate?: string) { | ||
this.days = days; | ||
this.startDate = startDate; | ||
this.endDate = endDate; | ||
} | ||
|
||
/** | ||
* Returns whether the user did a lesson today. | ||
*/ | ||
public didALessonToday(): boolean { | ||
if (this.endDate) { | ||
return ( | ||
new Date(this.endDate).getTime() >= new Date().setHours(0, 0, 0, 0) | ||
); | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
class CurrentCourse { | ||
public readonly title: string; | ||
public readonly languageCode: string; | ||
|
||
public static fromResponse(response: CourseDto): CurrentCourse { | ||
return new CurrentCourse(response); | ||
} | ||
|
||
private constructor(course: CourseDto) { | ||
this.title = course.title; | ||
this.languageCode = course.learningLanguage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export type DuolingoResponse = { | ||
users: UserDto[]; | ||
}; | ||
|
||
export type UserDto = { | ||
joinedClassroomIds: unknown[]; | ||
streak: number; | ||
motivation: string; | ||
acquisitionSurveyReason: string; | ||
shouldForceConnectPhoneNumber: boolean; | ||
picture: string; | ||
learningLanguage: string; | ||
hasFacebookId: boolean; | ||
shakeToReportEnabled: null | boolean; | ||
liveOpsFeatures: unknown[]; | ||
canUseModerationTools: boolean; | ||
id: number; | ||
betaStatus: string; | ||
hasGoogleId: boolean; | ||
privacySettings: string[]; | ||
fromLanguage: string; | ||
hasRecentActivity15: boolean; | ||
_achievements: unknown[]; | ||
observedClassroomIds: unknown[]; | ||
username: string; | ||
bio: string; | ||
profileCountry: null | string; | ||
chinaUserModerationRecords: unknown[]; | ||
globalAmbassadorStatus: unknown; | ||
currentCourseId: string; | ||
hasPhoneNumber: boolean; | ||
creationDate: number; | ||
achievements: unknown[]; | ||
hasPlus: boolean; | ||
name: string; | ||
roles: string[]; | ||
classroomLeaderboardsEnabled: boolean; | ||
emailVerified: boolean; | ||
courses: CourseDto[]; | ||
totalXp: number; | ||
streakData: StreakDataDto; | ||
}; | ||
|
||
export type CourseDto = { | ||
preload: boolean; | ||
placementTestAvailable: boolean; | ||
authorId: string; | ||
title: string; | ||
learningLanguage: string; | ||
xp: number; | ||
healthEnabled: boolean; | ||
fromLanguage: string; | ||
crowns: number; | ||
id: string; | ||
}; | ||
|
||
export type StreakDataDto = { | ||
currentStreak: StreakInfoDto | null; | ||
}; | ||
|
||
export type StreakInfoDto = { | ||
startDate: string; | ||
length: number; | ||
endDate: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./duolingo-api-response"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters