Skip to content

Commit

Permalink
feat: support neis api v4
Browse files Browse the repository at this point in the history
  • Loading branch information
star0202 committed Jun 22, 2023
1 parent b3ab813 commit 016d768
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
22 changes: 14 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
SchulAflcoinfoParam,
SpsTimetableParam,
TiClrminfoParam,
TimetableParams,
} from './types'

export class Neis extends NeisRequest {
Expand Down Expand Up @@ -62,42 +63,42 @@ export class Neis extends NeisRequest {

/** elsTimetable */
async getElsTimetable(params: ElsTimetableParam) {
return this.elsTimetableRaw(params)
return this.elsTimetableRaw(params, isLegacyTimetable(params))
}

/** elsTimetable */
async getElsTimetableOne(params: ElsTimetableParam) {
return firstOf(this.elsTimetableRaw(params))
return firstOf(this.elsTimetableRaw(params, isLegacyTimetable(params)))
}

/** misTimetable */
async getMisTimetable(params: MisTimetableParam) {
return this.misTimetableRaw(params)
return this.misTimetableRaw(params, isLegacyTimetable(params))
}

/** misTimetable */
async getMisTimetableOne(params: MisTimetableParam) {
return firstOf(this.misTimetableRaw(params))
return firstOf(this.misTimetableRaw(params, isLegacyTimetable(params)))
}

/** hisTimetable */
async getHisTimetable(params: HisTimetableParam) {
return this.hisTimetableRaw(params)
return this.hisTimetableRaw(params, isLegacyTimetable(params))
}

/** hisTimetable */
async getHisTimetableOne(params: HisTimetableParam) {
return firstOf(this.hisTimetableRaw(params))
return firstOf(this.hisTimetableRaw(params, isLegacyTimetable(params)))
}

/** spsTimetable */
async getSpsTimetable(params: SpsTimetableParam) {
return this.spsTimetableRaw(params)
return this.spsTimetableRaw(params, isLegacyTimetable(params))
}

/** spsTimetable */
async getSpsTimetableOne(params: SpsTimetableParam) {
return firstOf(this.spsTimetableRaw(params))
return firstOf(this.spsTimetableRaw(params, isLegacyTimetable(params)))
}

/** classInfo */
Expand Down Expand Up @@ -142,3 +143,8 @@ export class Neis extends NeisRequest {
}

const firstOf = <T>(promise: Promise<T[]>) => promise.then((res) => res[0])

const isLegacyTimetable = (params: TimetableParams) =>
Number(params.AY) < 2023 ||
Number(params.TI_FROM_YMD?.slice(0, 4)) < 2023 ||
Number(params.TI_TO_YMD?.slice(0, 4)) < 2023
28 changes: 20 additions & 8 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,32 @@ export class NeisRequest {
return this.get<AcaInsTiInfoResponse>('acaInsTiInfo', params)
}

protected async elsTimetableRaw(params: ElsTimetableParam) {
return this.get<ElsTimetableResponse>('elsTimetable', params)
protected async elsTimetableRaw(params: ElsTimetableParam, legacy: boolean) {
return this.get<ElsTimetableResponse>(
`elsTimetable${legacy ? 'bgs' : ''}`,
params
)
}

protected async misTimetableRaw(params: MisTimetableParam) {
return this.get<MisTimetableResponse>('misTimetable', params)
protected async misTimetableRaw(params: MisTimetableParam, legacy: boolean) {
return this.get<MisTimetableResponse>(
`misTimetable${legacy ? 'bgs' : ''}`,
params
)
}

protected async hisTimetableRaw(params: HisTimetableParam) {
return this.get<HisTimetableResponse>('hisTimetable', params)
protected async hisTimetableRaw(params: HisTimetableParam, legacy: boolean) {
return this.get<HisTimetableResponse>(
`hisTimetable${legacy ? 'bgs' : ''}`,
params
)
}

protected async spsTimetableRaw(params: SpsTimetableParam) {
return this.get<SpsTimetableResponse>('spsTimetable', params)
protected async spsTimetableRaw(params: SpsTimetableParam, legacy: boolean) {
return this.get<SpsTimetableResponse>(
`spsTimetable${legacy ? 'bgs' : ''}`,
params
)
}

protected async classInfoRaw(params: ClassInfoParam) {
Expand Down
6 changes: 6 additions & 0 deletions src/types/params/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export type Params =
| SchulAflcoinfoParam
| TiClrminfoParam

export type TimetableParams =
| ElsTimetableParam
| MisTimetableParam
| HisTimetableParam
| SpsTimetableParam

export interface DefaultParam {
/** 인증키 */
readonly key?: string
Expand Down

0 comments on commit 016d768

Please sign in to comment.