Skip to content

Commit 259de30

Browse files
authored
refactor: clean up typescript smells (#65)
1 parent 687aea1 commit 259de30

File tree

6 files changed

+42
-59
lines changed

6 files changed

+42
-59
lines changed

src/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isString = (x: any): x is string => typeof x === 'string'

src/routes/auth.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
RegistrationRequest,
99
} from '@oryd/kratos-client'
1010
import {IncomingMessage} from 'http'
11+
import {isString} from "../helpers";
1112

1213
const kratos = new AdminApi(config.kratos.admin)
1314

@@ -19,11 +20,11 @@ export const authHandler = (type: 'login' | 'registration') => (
1920
res: Response,
2021
next: NextFunction
2122
) => {
22-
const request = String(req.query.request)
23+
const request = req.query.request
2324

2425
// The request is used to identify the login and registration request and
2526
// return data like the csrf_token and so on.
26-
if (!request) {
27+
if (!request || !isString(request)) {
2728
console.log('No request found in URL, initializing auth flow.')
2829
res.redirect(`${config.kratos.browser}/self-service/browser/flows/${type}`)
2930
return
@@ -78,8 +79,5 @@ export const authHandler = (type: 'login' | 'registration') => (
7879
password: methodConfig("password"),
7980
})
8081
})
81-
.catch(err => {
82-
console.error(err)
83-
next(err)
84-
})
82+
.catch(next)
8583
}

src/routes/error.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import {NextFunction, Request, Response} from 'express'
22
import config from '../config'
33
import {CommonApi, ErrorContainer} from '@oryd/kratos-client'
44
import {IncomingMessage} from 'http'
5+
import {isString} from "../helpers";
56

67
const kratos = new CommonApi(config.kratos.admin)
78

89
export default (req: Request, res: Response, next: NextFunction) => {
9-
const error = String(req.query.error)
10+
const error = req.query.error
1011

11-
if (!error) {
12+
if (!error || !isString(error)) {
1213
// No error was send, redirecting back to home.
1314
res.redirect(config.baseUrl)
1415
return
@@ -47,5 +48,5 @@ export default (req: Request, res: Response, next: NextFunction) => {
4748
)}`
4849
)
4950
})
50-
.catch(err => next(err))
51+
.catch(next)
5152
}

src/routes/recovery.ts

+16-18
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import {NextFunction, Request, Response} from 'express'
22
import config from '../config'
33
import {CommonApi} from '@oryd/kratos-client'
44
import {IncomingMessage} from 'http'
5+
import {isString} from "../helpers";
56

67
const kratos = new CommonApi(config.kratos.admin)
78

89
export default (req: Request, res: Response, next: NextFunction) => {
9-
const request = String(req.query.request)
10+
const request = req.query.request
1011

1112
// The request is used to identify the account recovery request and
1213
// return data like the csrf_token and so on.
13-
if (!request) {
14+
if (!request || !isString(request)) {
1415
console.log('No request found in URL, initializing recovery flow.')
1516
res.redirect(`${config.kratos.browser}/self-service/browser/flows/recovery`)
1617
return
@@ -19,32 +20,29 @@ export default (req: Request, res: Response, next: NextFunction) => {
1920
kratos
2021
.getSelfServiceBrowserRecoveryRequest(request)
2122
.then(({body, response}: { response: IncomingMessage, body?: any }) => {
22-
if (response.statusCode == 404) {
23-
res.redirect(
24-
`${config.kratos.browser}/self-service/browser/flows/recovery`
25-
)
26-
return
27-
} else if (response.statusCode != 200) {
28-
return Promise.reject(body)
29-
}
30-
31-
return body
23+
if (response.statusCode == 404) {
24+
res.redirect(
25+
`${config.kratos.browser}/self-service/browser/flows/recovery`
26+
)
27+
return
28+
} else if (response.statusCode != 200) {
29+
return Promise.reject(body)
3230
}
33-
).then((request: any) => {
31+
3432
// This helper returns a request method config (e.g. for the password flow).
3533
// If active is set and not the given request method key, it wil be omitted.
3634
// This prevents the user from e.g. signing up with email but still seeing
3735
// other sign up form elements when an input is incorrect.
3836
const methodConfig = (key: string) => {
39-
if (request?.active === key || !request?.active) {
40-
return request?.methods[key]?.config
37+
if (body?.active === key || !body?.active) {
38+
return body?.methods[key]?.config
4139
}
4240
}
4341

4442
res.render('recovery', {
45-
...request,
43+
...body,
4644
token: methodConfig('link')
4745
})
48-
}
49-
).catch((err: any) => next(err))
46+
})
47+
.catch(next)
5048
}

src/routes/settings.ts

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {NextFunction, Request, Response} from 'express'
22
import config from '../config'
33
import {CommonApi} from '@oryd/kratos-client'
4+
import {isString} from "../helpers";
45

56
const kratos = new CommonApi(config.kratos.admin)
67

78
const settingsHandler = (req: Request, res: Response, next: NextFunction) => {
8-
const request = String(req.query.request)
9+
const request = req.query.request
910
// The request is used to identify the account settings request and
1011
// return data like the csrf_token and so on.
11-
if (!request) {
12+
if (!request || !isString(request)) {
1213
console.log('No request found in URL, initializing flow.')
1314
res.redirect(`${config.kratos.browser}/self-service/browser/flows/settings`)
1415
return
@@ -26,29 +27,16 @@ const settingsHandler = (req: Request, res: Response, next: NextFunction) => {
2627
return Promise.reject(body)
2728
}
2829

29-
return Promise.resolve(body)
30-
})
31-
.then(request => {
32-
const methodConfig = (key: string) => request?.methods[key]?.config
30+
const methodConfig = (key: string) => body?.methods[key]?.config
3331

34-
if (request) {
35-
res.render('settings', {
36-
...request,
37-
password: methodConfig("password"),
38-
profile: methodConfig("profile"),
39-
oidc: methodConfig("oidc"),
40-
})
41-
return
42-
}
43-
44-
return Promise.reject(
45-
'Expected self service settings request to be defined.'
46-
)
47-
})
48-
.catch(err => {
49-
console.error(err)
50-
next(err)
32+
res.render('settings', {
33+
...body,
34+
password: methodConfig("password"),
35+
profile: methodConfig("profile"),
36+
oidc: methodConfig("oidc"),
37+
})
5138
})
39+
.catch(next)
5240
}
5341

5442
export default settingsHandler

src/routes/verification.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import {NextFunction, Request, Response} from 'express'
22
import config from '../config'
33
import {CommonApi} from '@oryd/kratos-client'
44
import {IncomingMessage} from 'http'
5+
import {isString} from "../helpers";
56

67
const kratos = new CommonApi(config.kratos.admin)
78

89
export default (req: Request, res: Response, next: NextFunction) => {
9-
const request = String(req.query.request)
10+
const request = req.query.request
1011

1112
// The request is used to identify the login and registration request and
1213
// return data like the csrf_token and so on.
13-
if (!request) {
14+
if (!request || !isString(request)) {
1415
console.log('No request found in URL, initializing verify flow.')
1516
res.redirect(`${config.kratos.browser}/self-service/browser/flows/verification/email`)
1617
return
@@ -28,12 +29,8 @@ export default (req: Request, res: Response, next: NextFunction) => {
2829
return Promise.reject(body)
2930
}
3031

31-
return body
32+
res.render('verification', body)
3233
}
33-
).then((request: any) => {
34-
res.render('verification', {
35-
...request
36-
})
37-
}
38-
).catch((err: any) => next(err))
34+
)
35+
.catch(next)
3936
}

0 commit comments

Comments
 (0)