Frontend validation

This commit is contained in:
Mutzi 2022-08-25 14:16:29 +02:00
parent 65464e762a
commit 23ba777e5a
9 changed files with 40 additions and 29 deletions

View File

@ -1,4 +1,4 @@
import { BaseResponse, SuccessResponse } from './base';
import { SuccessResponse } from './base';
import { IsBase32, IsJWT, IsNotEmpty } from 'class-validator';
export type TfaRequiredResponse = SuccessResponse;
@ -8,17 +8,13 @@ export type TfaCompletedResponse = SuccessResponse;
export type SignupResponse = SuccessResponse;
export type RefreshResponse = LoginResponse;
export class LoginResponse extends BaseResponse {
statusCode: 200;
export class LoginResponse extends SuccessResponse {
@IsNotEmpty()
@IsJWT()
jwt: string;
}
export class RequestTotpTfaResponse extends BaseResponse {
statusCode: 200;
export class RequestTotpTfaResponse extends SuccessResponse {
@IsNotEmpty()
qrCode: string;

View File

@ -1,12 +1,20 @@
export class BaseResponse {
constructor(statusCode: number) {
this.statusCode = statusCode;
}
statusCode: number;
}
export class SuccessResponse extends BaseResponse {
statusCode: 200;
constructor() {
super(200);
}
declare statusCode: 200;
}
export class ErrorResponse extends BaseResponse {
statusCode: 400 | 401 | 403;
declare statusCode: 400 | 401 | 403;
message?: string;
}

View File

@ -1,21 +1,17 @@
import { BaseResponse, SuccessResponse } from './base';
import { SuccessResponse } from './base';
import { IsBoolean, IsInt, IsNotEmpty, Min } from 'class-validator';
export type UploadFileResponse = SuccessResponse;
export type DeleteResponse = SuccessResponse;
export type CreateFileResponse = CreateFolderResponse;
export class GetRootResponse extends BaseResponse {
statusCode: 200;
export class GetRootResponse extends SuccessResponse {
@IsInt()
@Min(1)
rootId: number;
}
export class GetNodeResponse extends BaseResponse {
statusCode: 200;
export class GetNodeResponse extends SuccessResponse {
@IsInt()
@Min(1)
id: number;
@ -28,21 +24,19 @@ export class GetNodeResponse extends BaseResponse {
parent: number | null;
@IsInt({ each: true })
@Min(1, { each: true })
children?: number[];
size?: number;
}
export class GetPathResponse extends BaseResponse {
statusCode: 200;
export class GetPathResponse extends SuccessResponse {
@IsNotEmpty()
path: string;
}
export class CreateFolderResponse extends BaseResponse {
statusCode: 200;
export class CreateFolderResponse extends SuccessResponse {
@IsInt()
@Min(1)
id: number;

View File

@ -11,7 +11,8 @@
"outDir": "./dist",
"baseUrl": "./src",
"incremental": true,
"skipLibCheck": true
"skipLibCheck": true,
"strictPropertyInitialization": false
},
"exclude": ["node_modules", "dist"]
}

View File

@ -27,4 +27,4 @@ export const auth_signup = (
export const refresh_token = (
token: string
): Promise<Responses.Auth.RefreshResponse | Responses.ErrorResponse> =>
post_token('/api/auth/refresh', '', token);
post_token('/api/auth/refresh', {}, token);

View File

@ -1,5 +1,7 @@
import axios from 'axios';
import { Requests, Responses } from 'dto';
export { Requests, Responses };
import { validateSync } from 'class-validator';
export const post = <T extends Requests.BaseRequest>(url: string, data: T) =>
axios
@ -9,12 +11,17 @@ export const post = <T extends Requests.BaseRequest>(url: string, data: T) =>
.then((res) => res.data)
.catch((err) => err.response.data);
export const post_token = <T extends Requests.BaseRequest>(
export function post_token<T extends Requests.BaseRequest>(
url: string,
data: T,
token: string
) =>
axios
) {
const errors = validateSync(data);
if (errors.length > 0) {
console.error('Validation failed, errors: ', errors);
throw new Error('Validation failed');
}
return axios
.post(url, data, {
headers: {
Authorization: 'Bearer ' + token,
@ -23,6 +30,7 @@ export const post_token = <T extends Requests.BaseRequest>(
})
.then((res) => res.data)
.catch((err) => err.response.data);
}
export const post_token_form = (
url: string,

View File

@ -13,7 +13,7 @@ router.replace({ path: '/' });
</script>
<template>
<router-link to="home">Click here to go home</router-link>
<router-link to="/">Click here to go home</router-link>
</template>
<style scoped></style>

View File

@ -11,6 +11,9 @@
"forceConsistentCasingInFileNames": true,
"useDefineForClassFields": true,
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"baseUrl": ".",
"types": [
"webpack-env"

View File

@ -12,7 +12,8 @@
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"strictPropertyInitialization": false
},
"exclude": ["node_modules", "dist", "test", "**/*spec.ts", "frontend"]
}