Skip to content

Commit ec8c32a

Browse files
committed
docs: use descriptive string status names for auth errors
Convert auth-related numeric status codes to descriptive strings: - status(401) → status("Unauthorized") - status(403) → status("Forbidden") Files updated: - tutorial/getting-started/guard - tutorial/getting-started/encapsulation - essential/plugin - essential/best-practice
1 parent 8e0d1ce commit ec8c32a

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

docs/essential/best-practice.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ const AuthService = new Elysia({ name: 'Auth.Service' })
316316
.macro({
317317
isSignIn: {
318318
resolve({ cookie, status }) {
319-
if (!cookie.session.value) return status(401)
319+
if (!cookie.session.value) return status("Unauthorized")
320320

321321
return {
322322
session: cookie.session.value,
@@ -367,7 +367,7 @@ class AuthService {
367367
// ❌ Don't do this
368368
isSignIn({ status, cookie: { session } }: Context) {
369369
if (session.value)
370-
return status(401)
370+
return status("Unauthorized")
371371
}
372372
}
373373
```
@@ -390,7 +390,7 @@ class AuthService {
390390
// ✅ Do
391391
isSignIn({ status, cookie: { session } }: InferContext<typeof setup>) {
392392
if (session.value)
393-
return status(401)
393+
return status("Unauthorized")
394394
}
395395
}
396396
```

docs/essential/plugin.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,22 @@ const _mock3 = {
115115
}
116116

117117
const profile1 = new Elysia()
118-
.onBeforeHandle(({ status }) => status(401))
119-
.get('/profile', ({ status }) => status(401))
118+
.onBeforeHandle(({ status }) => status("Unauthorized"))
119+
.get('/profile', ({ status }) => status("Unauthorized"))
120120

121121
const scope1 = new Elysia()
122122
.use(profile1)
123123
// This will NOT have sign in check
124124
.patch('/rename', () => 'Updated!')
125125

126126
const profile2 = new Elysia()
127-
.onBeforeHandle({ as: 'global' }, ({ status }) => status(401))
128-
.get('/profile', ({ status }) => status(401))
127+
.onBeforeHandle({ as: 'global' }, ({ status }) => status("Unauthorized"))
128+
.get('/profile', ({ status }) => status("Unauthorized"))
129129

130130
const scope2 = new Elysia()
131131
.use(profile2)
132132
// This will NOT have sign in check
133-
.patch('/rename', ({ status }) => status(401))
133+
.patch('/rename', ({ status }) => status("Unauthorized"))
134134
</script>
135135

136136
# Plugin <TutorialBadge href="/tutorial/getting-started/plugin" />

docs/tutorial/getting-started/encapsulation/data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const code = `import { Elysia, t } from 'elysia'
55
const nameCheck = new Elysia()
66
.onBeforeHandle(
77
({ query: { name }, status }) => {
8-
if(!name) return status(401)
8+
if(!name) return status("Unauthorized")
99
}
1010
)
1111
@@ -16,7 +16,7 @@ const ageCheck = new Elysia()
1616
name: t.Optional(t.String())
1717
}),
1818
beforeHandle({ query: { age }, status }) {
19-
if(age < 18) return status(403)
19+
if(age < 18) return status("Forbidden")
2020
}
2121
})
2222

docs/tutorial/getting-started/encapsulation/index.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const profile1 = new Elysia()
3030
.onBeforeHandle(
3131
({ query: { name }, status }) => {
3232
if(!name)
33-
return status(401)
33+
return status("Unauthorized")
3434
}
3535
)
3636
.get('/profile', () => 'Hi!')
@@ -44,7 +44,7 @@ const profile2 = new Elysia()
4444
{ as: 'global' },
4545
({ query: { name }, status }) => {
4646
if(!name)
47-
return status(401)
47+
return status("Unauthorized")
4848
}
4949
)
5050
.get('/profile', ({ status }) => status(401))
@@ -70,7 +70,7 @@ const profile = new Elysia()
7070
.onBeforeHandle(
7171
({ query: { name }, status }) => {
7272
if(!name)
73-
return status(401)
73+
return status("Unauthorized")
7474
}
7575
)
7676
.get('/profile', () => 'Hi!')
@@ -143,7 +143,7 @@ const user = new Elysia()
143143
name: t.Optional(t.String())
144144
}),
145145
beforeHandle({ query: { age }, status }) {
146-
if(age < 18) return status(403)
146+
if(age < 18) return status("Forbidden")
147147
}
148148
})
149149
.get('/profile', () => 'Hi!')
@@ -171,7 +171,7 @@ const nameCheck = new Elysia()
171171
.onBeforeHandle(
172172
{ as: 'scoped' }, // [!code ++]
173173
({ query: { name }, status }) => {
174-
if(!name) return status(401)
174+
if(!name) return status("Unauthorized")
175175
}
176176
)
177177

@@ -183,7 +183,7 @@ const ageCheck = new Elysia()
183183
name: t.Optional(t.String())
184184
}),
185185
beforeHandle({ query: { age }, status }) {
186-
if(age < 18) return status(403)
186+
if(age < 18) return status("Forbidden")
187187
}
188188
})
189189

docs/tutorial/getting-started/guard/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { Elysia, t } from 'elysia'
3737

3838
new Elysia()
3939
.onBeforeHandle(({ query: { name }, status }) => { // [!code --]
40-
if(!name) return status(401) // [!code --]
40+
if(!name) return status("Unauthorized") // [!code --]
4141
}) // [!code --]
4242
.onBeforeHandle(({ query: { name } }) => { // [!code --]
4343
console.log(name) // [!code --]
@@ -48,7 +48,7 @@ new Elysia()
4848
.guard({ // [!code ++]
4949
beforeHandle: [ // [!code ++]
5050
({ query: { name }, status }) => { // [!code ++]
51-
if(!name) return status(401) // [!code ++]
51+
if(!name) return status("Unauthorized") // [!code ++]
5252
}, // [!code ++]
5353
({ query: { name } }) => { // [!code ++]
5454
console.log(name) // [!code ++]
@@ -88,7 +88,7 @@ new Elysia()
8888
.guard({
8989
beforeHandle: [
9090
({ query: { name }, status }) => {
91-
if(!name) return status(401)
91+
if(!name) return status("Unauthorized")
9292
},
9393
({ query: { name } }) => {
9494
console.log(name)
@@ -139,7 +139,7 @@ import { Elysia } from 'elysia'
139139

140140
new Elysia()
141141
.onBeforeHandle(({ query: { name }, status }) => {
142-
if(!name) return status(401)
142+
if(!name) return status("Unauthorized")
143143
})
144144
.get('/auth', ({ query: { name = 'anon' } }) => {
145145
return `Hello ${name}!`

0 commit comments

Comments
 (0)