Skip to content

Commit 9e3894e

Browse files
committed
fix some things
1 parent e69fd94 commit 9e3894e

File tree

10 files changed

+28
-53
lines changed

10 files changed

+28
-53
lines changed

exercises/04.user/01.problem.token/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { withCors } from './utils.ts'
1818
// 🐨 create a type for the State object. It should be empty.
1919
// 🐨 create a type for the Props object. It should have a authToken property set to the AuthToken type
2020

21+
// 🐨 add the State and Props types to the generic after Env here:
2122
export class EpicMeMCP extends McpAgent<Env> {
2223
db!: DBClient
2324
server = new McpServer(
@@ -46,7 +47,8 @@ You can also help users add tags to their entries and get all tags for an entry.
4647

4748
async init() {
4849
// 🐨 pass this.props?.authToken.token to getClient
49-
// 💯 note throw an error if there's no token (we shouldn't get to this point without one, you can use invariant)
50+
// 💯 throw an error if there's no token (we shouldn't get to this point without one, you can use invariant)
51+
// 💯 as an extra bonus, make a `requireAuthInfo` utility method and use that instead
5052
this.db = getClient()
5153
await initializeTools(this)
5254
await initializeResources(this)
@@ -86,8 +88,8 @@ export default {
8688
const mcp = EpicMeMCP.serve('/mcp', {
8789
binding: 'EPIC_ME_MCP_OBJECT',
8890
})
89-
// 🐨 set ctx.props.authInfo to the authInfo object
9091

92+
// 🐨 set ctx.props.authInfo to the authInfo object
9193
return mcp.fetch(request, env, ctx)
9294
}
9395

exercises/04.user/01.solution.token/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can also help users add tags to their entries and get all tags for an entry.
4545
)
4646

4747
async init() {
48-
this.db = getClient(this.requireToken())
48+
this.db = getClient(this.requireAuthInfo().token)
4949

5050
await initializeTools(this)
5151
await initializeResources(this)
@@ -57,10 +57,6 @@ You can also help users add tags to their entries and get all tags for an entry.
5757
invariant(authInfo, 'Auth info not found')
5858
return authInfo
5959
}
60-
61-
requireToken() {
62-
return this.requireAuthInfo().token
63-
}
6460
}
6561

6662
export default {
@@ -95,6 +91,7 @@ export default {
9591
const mcp = EpicMeMCP.serve('/mcp', {
9692
binding: 'EPIC_ME_MCP_OBJECT',
9793
})
94+
9895
ctx.props.authInfo = authInfo
9996
return mcp.fetch(request, env, ctx)
10097
}

exercises/04.user/02.problem.user/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can also help users add tags to their entries and get all tags for an entry.
4545
)
4646

4747
async init() {
48-
this.db = getClient(this.requireToken())
48+
this.db = getClient(this.requireAuthInfo().token)
4949

5050
await initializeTools(this)
5151
await initializeResources(this)
@@ -58,10 +58,6 @@ You can also help users add tags to their entries and get all tags for an entry.
5858
return authInfo
5959
}
6060

61-
requireToken() {
62-
return this.requireAuthInfo().token
63-
}
64-
6561
// 🐨 create an async requireUser function
6662
// 🐨 get the user from await this.db.getUserById(Number(this.requireAuthInfo().extra.userId))
6763
// 🐨 the user should absolutely exist by this point,
@@ -102,6 +98,7 @@ export default {
10298
const mcp = EpicMeMCP.serve('/mcp', {
10399
binding: 'EPIC_ME_MCP_OBJECT',
104100
})
101+
105102
ctx.props.authInfo = authInfo
106103
return mcp.fetch(request, env, ctx)
107104
}

exercises/04.user/02.solution.user/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can also help users add tags to their entries and get all tags for an entry.
4545
)
4646

4747
async init() {
48-
this.db = getClient(this.requireToken())
48+
this.db = getClient(this.requireAuthInfo().token)
4949

5050
await initializeTools(this)
5151
await initializeResources(this)
@@ -58,10 +58,6 @@ You can also help users add tags to their entries and get all tags for an entry.
5858
return authInfo
5959
}
6060

61-
requireToken() {
62-
return this.requireAuthInfo().token
63-
}
64-
6561
async requireUser() {
6662
const user = await this.db.getUserById(
6763
Number(this.requireAuthInfo().extra.userId),
@@ -103,6 +99,7 @@ export default {
10399
const mcp = EpicMeMCP.serve('/mcp', {
104100
binding: 'EPIC_ME_MCP_OBJECT',
105101
})
102+
106103
ctx.props.authInfo = authInfo
107104
return mcp.fetch(request, env, ctx)
108105
}

exercises/05.scopes/01.problem.check-scopes/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ You can also help users add tags to their entries and get all tags for an entry.
4949
)
5050

5151
async init() {
52-
this.db = getClient(this.requireToken())
52+
this.db = getClient(this.requireAuthInfo().token)
5353

5454
await initializeTools(this)
5555
await initializeResources(this)
@@ -62,10 +62,6 @@ You can also help users add tags to their entries and get all tags for an entry.
6262
return authInfo
6363
}
6464

65-
requireToken() {
66-
return this.requireAuthInfo().token
67-
}
68-
6965
async requireUser() {
7066
const user = await this.db.getUserById(
7167
Number(this.requireAuthInfo().extra.userId),
@@ -111,6 +107,7 @@ export default {
111107
const mcp = EpicMeMCP.serve('/mcp', {
112108
binding: 'EPIC_ME_MCP_OBJECT',
113109
})
110+
114111
ctx.props.authInfo = authInfo
115112
return mcp.fetch(request, env, ctx)
116113
}

exercises/05.scopes/01.solution.check-scopes/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ You can also help users add tags to their entries and get all tags for an entry.
4747
)
4848

4949
async init() {
50-
this.db = getClient(this.requireToken())
50+
this.db = getClient(this.requireAuthInfo().token)
5151

5252
await initializeTools(this)
5353
await initializeResources(this)
@@ -60,10 +60,6 @@ You can also help users add tags to their entries and get all tags for an entry.
6060
return authInfo
6161
}
6262

63-
requireToken() {
64-
return this.requireAuthInfo().token
65-
}
66-
6763
async requireUser() {
6864
const user = await this.db.getUserById(
6965
Number(this.requireAuthInfo().extra.userId),
@@ -109,6 +105,7 @@ export default {
109105
const mcp = EpicMeMCP.serve('/mcp', {
110106
binding: 'EPIC_ME_MCP_OBJECT',
111107
})
108+
112109
ctx.props.authInfo = authInfo
113110
return mcp.fetch(request, env, ctx)
114111
}

exercises/05.scopes/02.problem.validate-sufficient-scope/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ You can also help users add tags to their entries and get all tags for an entry.
5151
)
5252

5353
async init() {
54-
this.db = getClient(this.requireToken())
54+
this.db = getClient(this.requireAuthInfo().token)
5555

5656
await initializeTools(this)
5757
await initializeResources(this)
@@ -64,10 +64,6 @@ You can also help users add tags to their entries and get all tags for an entry.
6464
return authInfo
6565
}
6666

67-
requireToken() {
68-
return this.requireAuthInfo().token
69-
}
70-
7167
async requireUser() {
7268
const user = await this.db.getUserById(
7369
Number(this.requireAuthInfo().extra.userId),
@@ -116,6 +112,7 @@ export default {
116112
const mcp = EpicMeMCP.serve('/mcp', {
117113
binding: 'EPIC_ME_MCP_OBJECT',
118114
})
115+
119116
ctx.props.authInfo = authInfo
120117
return mcp.fetch(request, env, ctx)
121118
}

exercises/05.scopes/02.solution.validate-sufficient-scope/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ You can also help users add tags to their entries and get all tags for an entry.
4949
)
5050

5151
async init() {
52-
this.db = getClient(this.requireToken())
52+
this.db = getClient(this.requireAuthInfo().token)
5353

5454
await initializeTools(this)
5555
await initializeResources(this)
@@ -62,10 +62,6 @@ You can also help users add tags to their entries and get all tags for an entry.
6262
return authInfo
6363
}
6464

65-
requireToken() {
66-
return this.requireAuthInfo().token
67-
}
68-
6965
async requireUser() {
7066
const user = await this.db.getUserById(
7167
Number(this.requireAuthInfo().extra.userId),
@@ -115,6 +111,7 @@ export default {
115111
const mcp = EpicMeMCP.serve('/mcp', {
116112
binding: 'EPIC_ME_MCP_OBJECT',
117113
})
114+
118115
ctx.props.authInfo = authInfo
119116
return mcp.fetch(request, env, ctx)
120117
}

exercises/05.scopes/03.problem.scope-hints/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ You can also help users add tags to their entries and get all tags for an entry.
4949
)
5050

5151
async init() {
52-
this.db = getClient(this.requireToken())
52+
this.db = getClient(this.requireAuthInfo().token)
5353

5454
await initializeTools(this)
5555
await initializeResources(this)
@@ -70,10 +70,6 @@ You can also help users add tags to their entries and get all tags for an entry.
7070
return authInfo
7171
}
7272

73-
requireToken() {
74-
return this.requireAuthInfo().token
75-
}
76-
7773
hasScope(...scopes: Array<SupportedScopes>) {
7874
return validateScopes(this.requireAuthInfo(), scopes)
7975
}
@@ -115,6 +111,7 @@ export default {
115111
const mcp = EpicMeMCP.serve('/mcp', {
116112
binding: 'EPIC_ME_MCP_OBJECT',
117113
})
114+
118115
ctx.props.authInfo = authInfo
119116
return mcp.fetch(request, env, ctx)
120117
}

exercises/05.scopes/03.solution.scope-hints/src/index.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,13 @@ You can also help users add tags to their entries and get all tags for an entry.
4949
)
5050

5151
async init() {
52-
this.db = getClient(this.requireToken())
52+
this.db = getClient(this.requireAuthInfo().token)
5353

5454
await initializeTools(this)
5555
await initializeResources(this)
5656
await initializePrompts(this)
5757
}
5858

59-
requireAuthInfo() {
60-
const { authInfo } = this.props ?? {}
61-
invariant(authInfo, 'Auth info not found')
62-
return authInfo
63-
}
64-
65-
requireToken() {
66-
return this.requireAuthInfo().token
67-
}
68-
6959
async requireUser() {
7060
const user = await this.db.getUserById(
7161
Number(this.requireAuthInfo().extra.userId),
@@ -74,6 +64,12 @@ You can also help users add tags to their entries and get all tags for an entry.
7464
return user
7565
}
7666

67+
requireAuthInfo() {
68+
const { authInfo } = this.props ?? {}
69+
invariant(authInfo, 'Auth info not found')
70+
return authInfo
71+
}
72+
7773
hasScope(...scopes: Array<SupportedScopes>) {
7874
return validateScopes(this.requireAuthInfo(), scopes)
7975
}
@@ -115,6 +111,7 @@ export default {
115111
const mcp = EpicMeMCP.serve('/mcp', {
116112
binding: 'EPIC_ME_MCP_OBJECT',
117113
})
114+
118115
ctx.props.authInfo = authInfo
119116
return mcp.fetch(request, env, ctx)
120117
}

0 commit comments

Comments
 (0)