Skip to content

Commit

Permalink
feat: move components to models/ folder, add gameweek model, remove f…
Browse files Browse the repository at this point in the history
…ixtures model
  • Loading branch information
roboflank committed Jan 7, 2021
1 parent 27b2dae commit 08f0921
Show file tree
Hide file tree
Showing 12 changed files with 557 additions and 76 deletions.
27 changes: 0 additions & 27 deletions src/fixture.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/fixtures.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './user'
export * from './fpl'
export * from './player'
export * from './fixture'
export * from './fixtures'
export * from './models/user'
export * from './models/fpl'
export * from './models/player'
export * from './models/fixture'
export * from './models/gameweek'
3 changes: 3 additions & 0 deletions src/models/classic-league.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import FPL from './fpl'

export class ClassicLeague extends FPL {}
267 changes: 267 additions & 0 deletions src/models/fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
import FPL from './fpl'
import { API_URLS } from '../constants'
import {
FixtureDelegate,
FixturesRespDelegate,
FixStatsItemDelegate,
} from '../types'

export class Fixture extends FPL {
id: number
constructor(id: number) {
super()
this.id = id
// TODO: Fetch Fixture on default call
}
public async getDetails(): Promise<FixtureDelegate | null> {
let fixture = null
try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((elem) => {
if (elem.id == this.id) {
fixture = elem
}
})
return fixture
} catch (error) {
return error
}
}

// TODO: Replace forEach with above function
public async getAssisters(): Promise<FixStatsItemDelegate> {
let assisters: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'assists') {
delete elem.identifier
assisters = elem
}
})
})
return assisters
} catch (error) {
return error
}
}

public async getBonus(): Promise<FixStatsItemDelegate> {
let bonus: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'bonus') {
delete elem.identifier
bonus = elem
}
})
})
return bonus
} catch (error) {
return error
}
}

public async getBPS(): Promise<FixStatsItemDelegate> {
let bps: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'bps') {
delete elem.identifier
bps = elem
}
})
})
return bps
} catch (error) {
return error
}
}

public async getGoalscorers(): Promise<FixStatsItemDelegate> {
let goalscorers: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'goals_scored') {
delete elem.identifier
goalscorers = elem
}
})
})
return goalscorers
} catch (error) {
return error
}
}
public async getOwnGoalscorers(): Promise<FixStatsItemDelegate> {
let owngoals: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'own_goals') {
delete elem.identifier
owngoals = elem
}
})
})
return owngoals
} catch (error) {
return error
}
}
public async getPenaltyMisses(): Promise<FixStatsItemDelegate> {
let missedpenalties: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'penalties_missed') {
delete elem.identifier
missedpenalties = elem
}
})
})
return missedpenalties
} catch (error) {
return error
}
}
public async getPenaltySaves(): Promise<FixStatsItemDelegate> {
let savedpenalties: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'penalties_saved') {
delete elem.identifier
savedpenalties = elem
}
})
})
return savedpenalties
} catch (error) {
return error
}
}
public async getRedCards(): Promise<FixStatsItemDelegate> {
let redcards: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'red_cards') {
delete elem.identifier
redcards = elem
}
})
})
return redcards
} catch (error) {
return error
}
}
public async getYellowCards(): Promise<FixStatsItemDelegate> {
let yellowcards: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'yellow_cards') {
delete elem.identifier
yellowcards = elem
}
})
})
return yellowcards
} catch (error) {
return error
}
}
public async getSaves(): Promise<FixStatsItemDelegate> {
let saves: FixStatsItemDelegate = {
a: [],
h: [],
}

try {
const { data }: FixturesRespDelegate = await this.fetchAPI(
API_URLS.FIXTURES,
)
data.forEach((fixture) => {
fixture.stats.forEach((elem) => {
if (elem.identifier === 'saves') {
delete elem.identifier
saves = elem
}
})
})
return saves
} catch (error) {
return error
}
}
}
2 changes: 1 addition & 1 deletion src/fpl.ts → src/models/fpl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { AxiosResponse } from 'axios'
import { randomUserAgent } from 'user-agent-array-ts'

import { API_BASE_URL } from './constants'
import { API_BASE_URL } from '../constants'
axios.defaults.baseURL = API_BASE_URL
export default class FPL {
headers = {
Expand Down
Loading

0 comments on commit 08f0921

Please sign in to comment.