Skip to content

Commit

Permalink
linter & ts update & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Torge committed Nov 14, 2023
1 parent 4932d48 commit 27f75e1
Show file tree
Hide file tree
Showing 73 changed files with 6,176 additions and 8,707 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"forwardPorts": [8080, 8085, 3030, 9222, 27017],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "npm i"
// "postCreateCommand": ""

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
Expand Down
53 changes: 50 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
const importSettings = {
'import/first': 'error',
'import/no-duplicates': 'error',
'import/no-unresolved': 'off',
'import/newline-after-import': 'error',
'import/order': [
'error',
{
'newlines-between': 'always',
alphabetize: { order: 'asc', caseInsensitive: true },
pathGroups: [
{
pattern: '@/**/*',
group: 'internal',
},
],
},
],
}

module.exports = {
root: true,
env: {
Expand Down Expand Up @@ -26,10 +46,14 @@ module.exports = {
parserOptions: {
parser: '@typescript-eslint/parser',
},
settings: {
'import/internal-regex': '^@codeanker/',
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:vue/vue3-recommended',
'plugin:import/recommended',
'plugin:prettier/recommended',
],
rules: {
Expand All @@ -53,14 +77,33 @@ module.exports = {
'vue/v-on-function-call': 'error',
'vue/no-unused-properties': ['error', { groups: ['props', 'data', 'computed', 'methods', 'setup'] }],
'vue/html-button-has-type': 'error',
'vue/no-ref-object-reactivity-loss': 'error',
'vue/padding-line-between-blocks': 'error',
'vue/define-macros-order': 'error',
'vue/block-order': [
'error',
{
order: ['template', 'script', 'style'],
},
],
'vue/require-macro-variable-name': 'error',
'vue/define-props-declaration': 'error',
'vue/define-emits-declaration': 'error',
'vue/component-api-style': ['error', ['script-setup']],
'vue/block-lang': ['error', { script: { lang: 'ts' } }],
...importSettings,
},
},
{
files: ['**/*.ts'],
plugins: ['@typescript-eslint'],
parser: '@typescript-eslint/parser',
env: {
es2022: true,
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
settings: {
'import/internal-regex': '^@codeanker/',
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
Expand All @@ -77,13 +120,17 @@ module.exports = {
'error',
{ argsIgnorePattern: '^_', destructuredArrayIgnorePattern: '^_' },
],
...importSettings,
},
},
{
files: ['**/*.ts'],
files: ['**/*.js'],
extends: ['eslint:recommended', 'plugin:prettier/recommended'],
rules: {
'no-unused-vars': 'off',
'no-unused-vars': [
'error',
{ argsIgnorePattern: '^_', destructuredArrayIgnorePattern: '^_', ignoreRestSiblings: true },
],
},
},
{
Expand All @@ -99,7 +146,7 @@ module.exports = {
},
},
{
files: ['api/test/**/*.ts'],
files: ['api/test/**/*.js'],
env: {
mocha: true,
},
Expand Down
10 changes: 5 additions & 5 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "@codeanker/api",
"main": "./src/index.ts",
"type": "module",
"scripts": {
"dev": "nodemon -x ts-node src/index.ts",
"createUser": "ts-node src/scripts/createUser.ts"
"dev": "tsx watch --clear-screen=false src/index.ts",
"createUser": "tsx src/scripts/createUser.ts"
},
"dependencies": {
"@prisma/client": "^4.14.0",
Expand All @@ -25,9 +26,8 @@
"@types/koa": "^2.13.6",
"@types/koa-router": "^7.4.4",
"@types/node": "^20.1.4",
"nodemon": "^2.0.22",
"prisma": "^4.14.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"tsx": "^4.1.2",
"typescript": "^5.2.2"
}
}
4 changes: 2 additions & 2 deletions api/src/enumMappings/getEnumOptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EnumMapping } from './defineEnumMapping'
import { type EnumMapping } from './defineEnumMapping'

export function getEnumOptions<T extends EnumMapping>(enumMapping: T) {
return Object.entries(enumMapping).map(([apiKey, mapping]) => {
return {
value: apiKey as keyof T,
label: mapping.human
label: mapping.human,
}
})
}
1 change: 1 addition & 0 deletions api/src/enumMappings/role.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Prisma } from '@prisma/client'

import { defineEnumMapping } from './defineEnumMapping'

export const roleMapping = defineEnumMapping<Prisma.UserCreateInput['role']>({
Expand Down
9 changes: 5 additions & 4 deletions api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { appRouter } from './services'
import * as Koa from 'koa'
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server'
import config from 'config'
import Koa from 'koa'
import { createKoaMiddleware } from 'trpc-koa-adapter'

import { koaRouter } from './middlewares'
import { inferRouterInputs, inferRouterOutputs } from '@trpc/server'
import * as config from 'config'
import { appRouter } from './services'

// Export type router type signature,
// NOT the router itself.
Expand Down
2 changes: 1 addition & 1 deletion api/src/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Router from 'koa-router'
import Router from 'koa-router'

const koaRouter = new Router()

Expand Down
1 change: 1 addition & 0 deletions api/src/scripts/createUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { input, password as passwordInput, select } from '@inquirer/prompts'

import { getEnumOptions, roleMapping } from '../enumMappings'
import prisma from '../prisma'
import { hashPassword } from '../services/authentication'
Expand Down
21 changes: 11 additions & 10 deletions api/src/services/authentication/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { TRPCError } from '@trpc/server'
import bcrypt from 'bcryptjs'
import config from 'config'
import jwt from 'jsonwebtoken'
import { z } from 'zod'
import { publicProcedure, router } from '../../trpc'

import prisma from '../../prisma'
import { TRPCError } from '@trpc/server'
import { sign, verify } from 'jsonwebtoken'
import { hash, compare } from 'bcryptjs'
import * as config from 'config'
import { publicProcedure, router } from '../../trpc'
import exclude from '../../util/prisma-exclude'

const jwtSecret: string = config.get('secret')
Expand Down Expand Up @@ -33,7 +34,7 @@ export const authenticationRouter = router({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password, ...userWithoutPassword } = user

const accessToken = sign(
const accessToken = jwt.sign(
{
userId: user.id,
},
Expand All @@ -53,7 +54,7 @@ export const authenticationRouter = router({
)
.mutation(async (opts) => {
try {
const payload = verify(opts.input.accessToken, jwtSecret)
const payload = jwt.verify(opts.input.accessToken, jwtSecret)
if (payload === null || typeof payload !== 'object') throw new Error('Weird payload')

const user = await prisma.user.findUnique({
Expand Down Expand Up @@ -84,7 +85,7 @@ export const authenticationRouter = router({
})
)
.mutation(async ({ input }) => {
const { password } = await prisma.user.findFirst({
const { password } = await prisma.user.findFirstOrThrow({
where: {
id: input.id,
},
Expand Down Expand Up @@ -118,12 +119,12 @@ export const authenticationRouter = router({
})

export function hashPassword(password: string) {
return hash(password, 10)
return bcrypt.hash(password, 10)
}

async function passwordMatches(hash: string, password: string) {
// find password in entity, this allows for dot notation

if (!hash) return false
return await compare(password, hash)
return await bcrypt.compare(password, hash)
}
1 change: 1 addition & 0 deletions api/src/services/gliederung/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod'

import prisma from '../../prisma'
import { publicProcedure, router } from '../../trpc'

Expand Down
1 change: 1 addition & 0 deletions api/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { router } from '../trpc'

import { authenticationRouter } from './authentication'
import { gliederungRouter } from './gliederung'
import { userRouter } from './user'
Expand Down
9 changes: 5 additions & 4 deletions api/src/services/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Gender } from '@prisma/client'
import { z } from 'zod'

import prisma from '../../prisma'
import { publicProcedure, router } from '../../trpc'
import { hashPassword } from '../authentication'
import exclude from '../../util/prisma-exclude'
import { Gender } from '@prisma/client'
import { hashPassword } from '../authentication'

export const userRouter = router({
create: publicProcedure
Expand All @@ -15,7 +16,7 @@ export const userRouter = router({
role: z.enum(['GLIEDERUNG_ADMIN', 'ADMIN']),
password: z.string(),
birthdate: z.string().nullable(),
gender: z.enum(Object.keys(Gender)).nullable(),
gender: z.nativeEnum(Gender).nullable(),
})
)
.mutation(async (opts) => {
Expand Down Expand Up @@ -70,7 +71,7 @@ export const userRouter = router({
firstname: z.string(),
lastname: z.string(),
birthdate: z.string().nullable(),
gender: z.enum(Object.keys(Gender)).nullable(),
gender: z.nativeEnum(Gender).nullable(),
})
)
.mutation(async ({ input }) => {
Expand Down
13 changes: 9 additions & 4 deletions api/src/util/prisma-exclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
*
* @see https://www.prisma.io/docs/concepts/components/prisma-client/excluding-fields
*/
export default function exclude<Entity, Key extends keyof Entity>(entity: Entity, ...keys: Key[]): Omit<Entity, Key> {
const entries = Object.entries(entity).filter(([key]) => !keys.includes(key))
const result = Object.fromEntries(entries)
return result as Omit<Entity, Key>
export default function exclude<T extends Record<string, unknown>, K extends keyof T>(
entity: T,
...keys: K[]
): Omit<T, K> {
const clone = { ...entity }
for (const key of keys) {
delete clone[key]
}
return clone
}
17 changes: 10 additions & 7 deletions api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"compilerOptions": {
"strict": true
},
"ts-node": {
"swc": true
}
}
"extends": "../tsconfig.base.json",
"compilerOptions": {
"lib": [
"ES2022"
]
},
"include": [
"src"
],
}
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@vueuse/core": "^10.1.2",
"crypto-js": "^4.1.1",
"socket.io-client": "^4.6.1",
"vue": "^3.2.47",
"vue": "^3.3.8",
"vue-router": "^4.1.6"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client'

import type { AppRouter } from '@codeanker/api'

export const apiClient = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
Expand Down
20 changes: 11 additions & 9 deletions frontend/src/components/BasicHeader.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
<script setup lang="ts">
defineProps<{
title?: string
subtitle?: string
}>()
const emit = defineEmits(['back'])
</script>

<template>
<div class="mb-8">
<div
Expand Down Expand Up @@ -34,3 +25,14 @@ const emit = defineEmits(['back'])
</div>
</div>
</template>

<script setup lang="ts">
defineProps<{
title?: string
subtitle?: string
}>()
const emit = defineEmits<{
(event: 'back'): void
}>()
</script>
19 changes: 5 additions & 14 deletions frontend/src/components/BasicInputs/BasicCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,14 @@
</template>

<script setup lang="ts">
import { RuleFunction } from '@codeanker/validation'
import useValidatedModel from '../../composables/useValidatedModel'
import BasicValidationFeedback from './components/BasicValidationFeedback.vue'
import { RequiredRulesParams } from '@codeanker/validation/rules'
import { type BasicInputDefaultProps } from './defaultProps'
const props = defineProps<{
disabled?: boolean
id?: string
label?: string
name?: string
// eslint-disable-next-line vue/no-unused-properties
modelValue: boolean | undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any, vue/no-unused-properties
rules?: RuleFunction[]
// eslint-disable-next-line vue/no-unused-properties
required?: RequiredRulesParams
const props = defineProps<BasicInputDefaultProps<boolean>>()
const emit = defineEmits<{
(event: 'update:modelValue', eventArgs: boolean | undefined): void
}>()
const emit = defineEmits(['update:modelValue'])
const { model, errorMessage } = useValidatedModel(props, emit)
</script>
Loading

0 comments on commit 27f75e1

Please sign in to comment.