Skip to content

Commit

Permalink
feat: update stop schedules return type
Browse files Browse the repository at this point in the history
  • Loading branch information
_ committed Jun 1, 2023
1 parent a58666c commit 37f4329
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"author": "marklai1998 <marklai1998@gmail.com>",
"license": "MIT",
"scripts": {
"dev": "tsx watch src/index.ts"
"dev": "tsx watch src/index.ts",
"tsc": "tsc"
},
"dependencies": {
"@koa/cors": "^3.4.1",
Expand All @@ -21,6 +22,7 @@
"@types/koa": "^2.13.5",
"@types/koa__cors": "^3.3.0",
"@types/koa__router": "^12.0.0",
"@types/memoizee": "^0.4.8"
"@types/memoizee": "^0.4.8",
"typescript": "^5.1.3"
}
}
31 changes: 18 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ koa.use(cors({ origin: 'http://localhost:5173' }))

const router = new Router()

router.get('/api/v1/lines', async ctx => {
ctx.body = lines
})

router.get('/api/v1/lines/:line', async ctx => {
const { line } = ctx.params
ctx.body = lines.find(item => item.code === line)
})

// Stop API
router.get('/api/v1/stops', async ctx => {
ctx.body = stops
})
Expand All @@ -29,13 +21,20 @@ router.get('/api/v1/stops/:stop', async ctx => {
ctx.body = stops.find(item => item.code === stop)
})

router.get('/api/v1/lines/:line/stops', async ctx => {
// Line API

router.get('/api/v1/lines', async ctx => {
ctx.body = lines
})

router.get('/api/v1/lines/:line', async ctx => {
const { line } = ctx.params
ctx.body = lines.find(item => item.code === line)?.stops
ctx.body = lines.find(item => item.code === line)
})

router.get('/api/v1/schedules', async ctx => {
ctx.body = await scheduleService.getSchedule()
router.get('/api/v1/lines/:line/stops', async ctx => {
const { line } = ctx.params
ctx.body = lines.find(item => item.code === line)?.stops
})

router.get('/api/v1/lines/:line/schedules', async ctx => {
Expand All @@ -51,5 +50,11 @@ router.get('/api/v1/lines/:line/stops/:stop/schedules', async ctx => {
)
})

// Schedule API

router.get('/api/v1/schedules', async ctx => {
ctx.body = await scheduleService.getSchedule()
})

koa.use(router.routes()).use(router.allowedMethods())
koa.listen(3000)
7 changes: 3 additions & 4 deletions src/services/scheduleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ const getStopSchedules = memoize(
const getLineSchedule = async (code: LineCode) => {
const stops = lines.find(item => item.code === code)?.stops || []

let result: {
let result: (Awaited<ReturnType<typeof getStopSchedules>> & {
code: StopCode
schedule: Awaited<ReturnType<typeof getStopSchedules>>
}[] = []
})[] = []

for (const stop of stops) {
const schedule = await getStopSchedules(code, stop.code)

result = [...result, { code: stop.code, schedule }]
if (schedule) result = [...result, { code: stop.code, ...schedule }]
}

return result
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,11 @@ type@^2.7.2:
resolved "https://registry.yarnpkg.com/type/-/type-2.7.2.tgz#2376a15a3a28b1efa0f5350dcf72d24df6ef98d0"
integrity sha512-dzlvlNlt6AXU7EBSfpAscydQ7gXB+pPGsPnfJnZpiNJBDj7IaJzQlBZYGdEi4R9HmPdBv2XmWJ6YUtoTa7lmCw==

typescript@^5.1.3:
version "5.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.3.tgz#8d84219244a6b40b6fb2b33cc1c062f715b9e826"
integrity sha512-XH627E9vkeqhlZFQuL+UsyAXEnibT0kWR2FWONlr4sTjvxyJYnyefgrkyECLzM5NenmKzRAy2rR/OlYLA1HkZw==

vary@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
Expand Down

0 comments on commit 37f4329

Please sign in to comment.