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'; import { IsBase32, IsJWT, IsNotEmpty } from 'class-validator';
export type TfaRequiredResponse = SuccessResponse; export type TfaRequiredResponse = SuccessResponse;
@ -8,17 +8,13 @@ export type TfaCompletedResponse = SuccessResponse;
export type SignupResponse = SuccessResponse; export type SignupResponse = SuccessResponse;
export type RefreshResponse = LoginResponse; export type RefreshResponse = LoginResponse;
export class LoginResponse extends BaseResponse { export class LoginResponse extends SuccessResponse {
statusCode: 200;
@IsNotEmpty() @IsNotEmpty()
@IsJWT() @IsJWT()
jwt: string; jwt: string;
} }
export class RequestTotpTfaResponse extends BaseResponse { export class RequestTotpTfaResponse extends SuccessResponse {
statusCode: 200;
@IsNotEmpty() @IsNotEmpty()
qrCode: string; qrCode: string;

View File

@ -1,12 +1,20 @@
export class BaseResponse { export class BaseResponse {
constructor(statusCode: number) {
this.statusCode = statusCode;
}
statusCode: number; statusCode: number;
} }
export class SuccessResponse extends BaseResponse { export class SuccessResponse extends BaseResponse {
statusCode: 200; constructor() {
super(200);
}
declare statusCode: 200;
} }
export class ErrorResponse extends BaseResponse { export class ErrorResponse extends BaseResponse {
statusCode: 400 | 401 | 403; declare statusCode: 400 | 401 | 403;
message?: string; 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'; import { IsBoolean, IsInt, IsNotEmpty, Min } from 'class-validator';
export type UploadFileResponse = SuccessResponse; export type UploadFileResponse = SuccessResponse;
export type DeleteResponse = SuccessResponse; export type DeleteResponse = SuccessResponse;
export type CreateFileResponse = CreateFolderResponse; export type CreateFileResponse = CreateFolderResponse;
export class GetRootResponse extends BaseResponse { export class GetRootResponse extends SuccessResponse {
statusCode: 200;
@IsInt() @IsInt()
@Min(1) @Min(1)
rootId: number; rootId: number;
} }
export class GetNodeResponse extends BaseResponse { export class GetNodeResponse extends SuccessResponse {
statusCode: 200;
@IsInt() @IsInt()
@Min(1) @Min(1)
id: number; id: number;
@ -28,21 +24,19 @@ export class GetNodeResponse extends BaseResponse {
parent: number | null; parent: number | null;
@IsInt({ each: true })
@Min(1, { each: true })
children?: number[]; children?: number[];
size?: number; size?: number;
} }
export class GetPathResponse extends BaseResponse { export class GetPathResponse extends SuccessResponse {
statusCode: 200;
@IsNotEmpty() @IsNotEmpty()
path: string; path: string;
} }
export class CreateFolderResponse extends BaseResponse { export class CreateFolderResponse extends SuccessResponse {
statusCode: 200;
@IsInt() @IsInt()
@Min(1) @Min(1)
id: number; id: number;

View File

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

View File

@ -27,4 +27,4 @@ export const auth_signup = (
export const refresh_token = ( export const refresh_token = (
token: string token: string
): Promise<Responses.Auth.RefreshResponse | Responses.ErrorResponse> => ): 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 axios from 'axios';
import { Requests, Responses } from 'dto'; import { Requests, Responses } from 'dto';
export { Requests, Responses };
import { validateSync } from 'class-validator';
export const post = <T extends Requests.BaseRequest>(url: string, data: T) => export const post = <T extends Requests.BaseRequest>(url: string, data: T) =>
axios axios
@ -9,12 +11,17 @@ export const post = <T extends Requests.BaseRequest>(url: string, data: T) =>
.then((res) => res.data) .then((res) => res.data)
.catch((err) => err.response.data); .catch((err) => err.response.data);
export const post_token = <T extends Requests.BaseRequest>( export function post_token<T extends Requests.BaseRequest>(
url: string, url: string,
data: T, data: T,
token: string 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, { .post(url, data, {
headers: { headers: {
Authorization: 'Bearer ' + token, Authorization: 'Bearer ' + token,
@ -23,6 +30,7 @@ export const post_token = <T extends Requests.BaseRequest>(
}) })
.then((res) => res.data) .then((res) => res.data)
.catch((err) => err.response.data); .catch((err) => err.response.data);
}
export const post_token_form = ( export const post_token_form = (
url: string, url: string,

View File

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

View File

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

View File

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