Skip to content

Commit c591ac2

Browse files
authored
Merge pull request #1 from OultimoCoder/type-fixes
Fixed all the type errors and did code review
2 parents 2f4e8bb + 9452539 commit c591ac2

46 files changed

Lines changed: 396 additions & 511 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ node_modules/
1010
.mf
1111
wrangler.toml
1212
coverage
13+
.vscode
14+
pnpm-lock.yaml

build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ try {
1313
target: 'esnext'
1414
})
1515
} catch(err) {
16-
process.exitCode = 1;
16+
process.exitCode = 1
1717
}
1818

package-lock.json

Lines changed: 68 additions & 131 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
"scripts": {
4040
"build": "node ./build.js",
4141
"dev": "miniflare --live-reload --debug --env .env --modules dist/index.mjs",
42-
"tests": "npm run build && NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles --runInBand",
43-
"tests:coverage": "npm run build && NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles --runInBand --coverage",
44-
"tests:summary": "npm run build && NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles --runInBand --coverage --coverageReporters=\"text-summary\"",
45-
"tests:unit": "npm run build && NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles ./tests/unit",
46-
"tests:integration": "npm run build && NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles ./tests/integration",
42+
"tests": "npm run build && cross-env NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles --runInBand",
43+
"tests:coverage": "npm run build && cross-env NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles --runInBand --coverage",
44+
"tests:summary": "npm run build && cross-env NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles --runInBand --coverage --coverageReporters=\"text-summary\"",
45+
"tests:unit": "npm run build && cross-env NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles ./tests/unit",
46+
"tests:integration": "npm run build && cross-env NODE_OPTIONS=--experimental-vm-modules jest -i --colors --verbose --detectOpenHandles ./tests/integration",
4747
"migrate:dev:latest": "node --experimental-specifier-resolution=node --loader ts-node/esm ./scripts/migrate.ts dev latest",
4848
"migrate:dev:down": "node --experimental-specifier-resolution=node --loader ts-node/esm ./scripts/migrate.ts dev down",
4949
"migrate:dev:none": "node --experimental-specifier-resolution=node --loader ts-node/esm ./scripts/migrate.ts dev none",
@@ -64,7 +64,8 @@
6464
"@cloudflare/workers-types": "^3.11.0",
6565
"@faker-js/faker": "^7.6.0",
6666
"@types/bcryptjs": "^2.4.2",
67-
"@types/jest": "^29.2.2",
67+
"@types/jest": "^29.2.5",
68+
"@types/node": "^18.11.18",
6869
"@typescript-eslint/eslint-plugin": "^5.45.0",
6970
"@typescript-eslint/parser": "^5.45.0",
7071
"aws-sdk-client-mock": "^2.0.0",
@@ -95,12 +96,14 @@
9596
"@planetscale/database": "^1.4.0",
9697
"@tsndr/cloudflare-worker-jwt": "2.1.2",
9798
"bcryptjs": "^2.4.3",
99+
"cross-env": "^7.0.3",
98100
"dayjs": "^1.11.6",
99101
"hono": "^2.2.5",
100102
"http-status": "^1.5.3",
101103
"kysely": "^0.22.0",
102104
"kysely-planetscale": "^1.1.0",
103-
"worker-auth-providers": "^0.0.10-beta7",
105+
"toucan-js": "2.7.0",
106+
"worker-auth-providers-typed": "^0.0.10-beta7-7",
104107
"zod": "^3.19.1",
105108
"zod-error": "^1.0.5"
106109
}

src/config/authProviders.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
const authProviders = {
1+
export const authProviders = {
22
GITHUB: 'github',
33
SPOTIFY: 'spotify',
44
DISCORD: 'discord',
55
GOOGLE: 'google',
66
FACEBOOK: 'facebook'
77
} as const
88

9-
type AuthProviderType = typeof authProviders[keyof typeof authProviders]
10-
export { authProviders, AuthProviderType }
9+
export type AuthProviderType = typeof authProviders[keyof typeof authProviders]

src/config/config.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const envVarsSchema = z.object({
5252
OAUTH_FACEBOOK_REDIRECT_URL: z.string()
5353
})
5454

55-
interface Config {
55+
export interface Config {
5656
env: 'production' | 'development' | 'test'
5757
database: {
5858
name: string
@@ -74,27 +74,27 @@ interface Config {
7474
}
7575
email: {
7676
sender: string
77-
},
77+
}
7878
oauth: {
7979
github: {
8080
clientId: string
8181
clientSecret: string
82-
},
82+
}
8383
google: {
8484
clientId: string
8585
clientSecret: string
8686
redirectUrl: string
87-
},
87+
}
8888
spotify: {
8989
clientId: string
9090
clientSecret: string
9191
redirectUrl: string
92-
},
92+
}
9393
discord: {
9494
clientId: string
9595
clientSecret: string
9696
redirectUrl: string
97-
},
97+
}
9898
facebook: {
9999
clientId: string
100100
clientSecret: string
@@ -105,7 +105,7 @@ interface Config {
105105

106106
let config: Config
107107

108-
const getConfig = (env: Bindings) => {
108+
export const getConfig = (env: Bindings) => {
109109
if (config) {
110110
return config
111111
}
@@ -162,5 +162,3 @@ const getConfig = (env: Bindings) => {
162162
}
163163
return config
164164
}
165-
166-
export { getConfig, Config }

src/config/database.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { Config } from './config'
66

77
let dbClient: Kysely<Database>
88

9-
interface Database {
10-
user: UserTable,
9+
export interface Database {
10+
user: UserTable
1111
authorisations: AuthProviderTable
1212
}
1313

14-
const getDBClient = (databaseConfig: Config['database']) => {
14+
export const getDBClient = (databaseConfig: Config['database']): Kysely<Database> => {
1515
dbClient =
1616
dbClient ||
1717
new Kysely<Database>({
@@ -23,5 +23,3 @@ const getDBClient = (databaseConfig: Config['database']) => {
2323
})
2424
return dbClient
2525
}
26-
27-
export { getDBClient, Database }

src/config/roles.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
const roleRights = {
1+
export const roleRights = {
22
user: [],
33
admin: ['getUsers', 'manageUsers']
44
} as const
55

6-
const roles = Object.keys(roleRights) as Role[]
6+
export const roles = Object.keys(roleRights) as Role[]
77

8-
type Permission = typeof roleRights[keyof typeof roleRights][number]
9-
type Role = keyof typeof roleRights
10-
11-
export { roles, roleRights, Permission, Role }
8+
export type Permission = typeof roleRights[keyof typeof roleRights][number]
9+
export type Role = keyof typeof roleRights

src/config/tokens.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const tokenTypes = {
1+
export const tokenTypes = {
22
ACCESS: 'access',
33
REFRESH: 'refresh',
44
RESET_PASSWORD: 'resetPassword',
55
VERIFY_EMAIL: 'verifyEmail'
66
} as const
77

8-
type TokenType = typeof tokenTypes[keyof typeof tokenTypes]
9-
export { tokenTypes, TokenType }
8+
export type TokenType = typeof tokenTypes[keyof typeof tokenTypes]

src/controllers/auth/auth.controller.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as tokenService from '../../services/token.service'
99
import * as userService from '../../services/user.service'
1010
import * as authValidation from '../../validations/auth.validation'
1111

12-
const register: Handler<{ Bindings: Bindings }> = async (c) => {
12+
export const register: Handler<{ Bindings: Bindings }> = async (c) => {
1313
const config = getConfig(c.env)
1414
const bodyParse = await c.req.json()
1515
const body = await authValidation.register.parseAsync(bodyParse)
@@ -18,7 +18,7 @@ const register: Handler<{ Bindings: Bindings }> = async (c) => {
1818
return c.json({ user, tokens }, httpStatus.CREATED as StatusCode)
1919
}
2020

21-
const login: Handler<{ Bindings: Bindings }> = async (c) => {
21+
export const login: Handler<{ Bindings: Bindings }> = async (c) => {
2222
const config = getConfig(c.env)
2323
const bodyParse = await c.req.json()
2424
const { email, password } = authValidation.login.parse(bodyParse)
@@ -27,15 +27,15 @@ const login: Handler<{ Bindings: Bindings }> = async (c) => {
2727
return c.json({ user, tokens }, httpStatus.OK as StatusCode)
2828
}
2929

30-
const refreshTokens: Handler<{ Bindings: Bindings }> = async (c) => {
30+
export const refreshTokens: Handler<{ Bindings: Bindings }> = async (c) => {
3131
const config = getConfig(c.env)
3232
const bodyParse = await c.req.json()
3333
const { refresh_token } = authValidation.refreshTokens.parse(bodyParse)
3434
const tokens = await authService.refreshAuth(refresh_token, config)
3535
return c.json({ ...tokens }, httpStatus.OK as StatusCode)
3636
}
3737

38-
const forgotPassword: Handler<{ Bindings: Bindings }> = async (c) => {
38+
export const forgotPassword: Handler<{ Bindings: Bindings }> = async (c) => {
3939
const bodyParse = await c.req.json()
4040
const config = getConfig(c.env)
4141
const { email } = authValidation.forgotPassword.parse(bodyParse)
@@ -56,7 +56,7 @@ const forgotPassword: Handler<{ Bindings: Bindings }> = async (c) => {
5656
return c.body(null)
5757
}
5858

59-
const resetPassword: Handler<{ Bindings: Bindings }> = async (c) => {
59+
export const resetPassword: Handler<{ Bindings: Bindings }> = async (c) => {
6060
const queryParse = c.req.query()
6161
const bodyParse = await c.req.json()
6262
const config = getConfig(c.env)
@@ -69,15 +69,15 @@ const resetPassword: Handler<{ Bindings: Bindings }> = async (c) => {
6969
return c.body(null)
7070
}
7171

72-
const sendVerificationEmail: Handler<{ Bindings: Bindings }> = async (c) => {
72+
export const sendVerificationEmail: Handler<{ Bindings: Bindings }> = async (c) => {
7373
const config = getConfig(c.env)
7474
const payload = c.get('payload') as JwtPayload
7575
const userId = Number(payload.sub)
7676
// Don't let bad actors know if the email is registered by returning an error if the email
7777
// is already verified
7878
try {
7979
const user = await userService.getUserById(userId, config.database)
80-
if (user.is_email_verified) {
80+
if (!user || user.is_email_verified) {
8181
throw new Error()
8282
}
8383
const verifyEmailToken = await tokenService.generateVerifyEmailToken(user, config.jwt)
@@ -94,21 +94,11 @@ const sendVerificationEmail: Handler<{ Bindings: Bindings }> = async (c) => {
9494
return c.body(null)
9595
}
9696

97-
const verifyEmail: Handler<{ Bindings: Bindings }> = async (c) => {
97+
export const verifyEmail: Handler<{ Bindings: Bindings }> = async (c) => {
9898
const config = getConfig(c.env)
9999
const queryParse = c.req.query()
100100
const { token } = authValidation.verifyEmail.parse(queryParse)
101101
await authService.verifyEmail(token, config)
102102
c.status(httpStatus.NO_CONTENT as StatusCode)
103103
return c.body(null)
104-
}
105-
106-
export {
107-
register,
108-
login,
109-
refreshTokens,
110-
sendVerificationEmail,
111-
forgotPassword,
112-
resetPassword,
113-
verifyEmail
114-
}
104+
}

0 commit comments

Comments
 (0)