Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"dependencies": {
"@nanoforge-dev/schematics": "^2.1.3",
"bun": "catalog:core",
"class-transformer": "catalog:libs-back",
"class-validator": "catalog:libs-back",
"dotenv": "catalog:libs-back",
"typescript": "catalog:build"
},
Expand Down
594 changes: 400 additions & 194 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ catalogs:
monaco-editor: ^0.55.1
svelte-sonner: ^1.1.1
core:
'@nanoforge-dev/ecs-lib': ^1.3.0
'@nanoforge-dev/ecs-lib': ^1.3.1
'@sveltejs/adapter-auto': ^7.0.1
'@sveltejs/kit': ^2.62.0
'@sveltejs/kit': ^2.63.0
'@sveltejs/vite-plugin-svelte': ^7.1.2
'@types/bun': ^1.3.14
bun: ^1.3.14
svelte: ^5.56.1
svelte-check: ^4.5.0
svelte: ^5.56.2
svelte-check: ^4.6.0
svelte-kit-sessions: ^0.4.0
vite: ^8.0.16
css:
Expand All @@ -49,6 +49,8 @@ catalogs:
'@iconify-json/solar': ^1.2.5
'@lucide/svelte': ^1.17.0
libs-back:
class-transformer: ^0.5.1
class-validator: ^0.15.1
dotenv: ^17.4.2
libs-front:
'@internationalized/date': ^3.12.2
Expand All @@ -75,6 +77,3 @@ catalogs:
playwright: ^1.60.0
vitest: ^4.1.8
vitest-browser-svelte: ^2.1.1

minimumReleaseAgeExclude:
- '@nanoforge-dev/actions@2.1.3'
14 changes: 14 additions & 0 deletions src/lib/server/actions/project/complete.action.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { Expose } from 'class-transformer';
import { IsBoolean, IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator';

import { resolveSessionFunctions } from '$lib/server/actions/project/load.action';
import { loadProject } from '$lib/server/project';

import { useActionHandler } from '@utils-server/request-handler';

export class CompleteProjectBody {
@Expose()
@IsUUID(8)
@IsNotEmpty()
gatewayId!: string;

@Expose()
@IsString()
@IsOptional()
@IsEnum(['js', 'ts'])
language?: 'js' | 'ts';

@Expose()
@IsBoolean()
@IsOptional()
multiplayerServer?: boolean;
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib/server/actions/project/fs/readdir.action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Expose } from 'class-transformer';
import { IsOptional, IsString } from 'class-validator';

import { useActionHandler } from '@utils-server/request-handler';

export class ReaddirFsBody {
@Expose()
@IsString()
@IsOptional()
path?: string;
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib/server/actions/project/info.action.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Expose } from 'class-transformer';
import { IsOptional, IsString } from 'class-validator';

import { useActionHandler } from '@utils-server/request-handler';

export interface InfoProject {
Expand All @@ -7,6 +10,9 @@ export interface InfoProject {
}

export class InfoProjectBody {
@Expose()
@IsString()
@IsOptional()
name?: string;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';

import type { LibraryPackage } from '$lib/server/project/library';

import { useActionHandler } from '@utils-server/request-handler';

export class InstallLibraryBody {
@Expose()
@IsString()
@IsNotEmpty()
libraryName!: string;
}

Expand Down
17 changes: 17 additions & 0 deletions src/lib/server/actions/project/load.action.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Expose } from 'class-transformer';
import { IsOptional, IsString } from 'class-validator';
import { join } from 'path';

import { loadProject } from '$lib/server/project';
Expand All @@ -9,9 +11,24 @@ import { Exception } from '@utils/exception';
import { type Handler, useActionHandler } from '@utils-server/request-handler';

export class LoadProjectBody {
@Expose()
@IsString()
@IsOptional()
id?: string;

@Expose()
@IsString()
@IsOptional()
path?: string;

@Expose()
@IsString()
@IsOptional()
gitUrl?: string;

@Expose()
@IsString()
@IsOptional()
gatewayId?: string;
}

Expand Down
37 changes: 37 additions & 0 deletions src/lib/server/actions/project/new.action.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import { Expose } from 'class-transformer';
import { IsBoolean, IsEnum, IsNotEmpty, IsOptional, IsString, Matches } from 'class-validator';
import { join } from 'path';

import { loadProject } from '$lib/server/project';

import { useActionHandler } from '@utils-server/request-handler';
import { IsFalseOrString } from '@utils-server/validators';

export class CreateProjectBody {
@Expose()
@IsString()
@IsNotEmpty()
@Matches(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)
projectName!: string;

@Expose()
@IsString()
@IsOptional()
projectPath?: string;

@Expose()
@IsString()
@IsOptional()
@IsEnum(['npm', 'pnpm', 'yarn', 'bun'])
packageManager?: 'npm' | 'pnpm' | 'yarn' | 'bun';

@Expose()
@IsString()
@IsOptional()
@IsEnum(['js', 'ts'])
language?: 'js' | 'ts';

@Expose()
@IsBoolean()
@IsOptional()
multiplayerServer?: boolean;

@Expose()
@IsBoolean()
@IsOptional()
dockerContainerization?: boolean;

@Expose()
@IsBoolean()
@IsOptional()
createGitRepository?: boolean;

@Expose()
@IsFalseOrString()
@IsOptional()
gitRemote?: string | false;
}

Expand All @@ -31,6 +67,7 @@ export const createProjectAction = useActionHandler(
docker: body.dockerContainerization,
git: body.createGitRepository,
gitRemote: body.gitRemote,
lint: false,
});

return await loadProject({ path: join(body.projectPath ?? '', body.projectName) }, handler);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';

import type { ComponentPackage } from '$lib/server/project/package/package.type';

import { useActionHandler } from '@utils-server/request-handler';

export class AddComponentBody {
@Expose()
@IsString({ each: true })
@IsNotEmpty()
componentNames!: [string, ...string[]];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';

import type { SystemPackage } from '$lib/server/project/package/package.type';

import { useActionHandler } from '@utils-server/request-handler';

export class AddSystemBody {
@Expose()
@IsString({ each: true })
@IsNotEmpty()
systemNames!: [string, ...string[]];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';

import type { ComponentPackage } from '$lib/server/project/package/package.type';

import { useActionHandler } from '@utils-server/request-handler';

export class CreateComponentBody {
@Expose()
@IsString()
@IsNotEmpty()
componentName!: string;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';

import type { SystemPackage } from '$lib/server/project/package/package.type';

import { useActionHandler } from '@utils-server/request-handler';

export class CreateSystemBody {
@Expose()
@IsString()
@IsNotEmpty()
systemName!: string;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';

import type { ComponentManifest } from '$lib/server/project/package';

import { useActionHandler } from '@utils-server/request-handler';

export class GetComponentManifestBody {
@Expose()
@IsString({ each: true })
@IsNotEmpty()
componentPaths!: [string, ...string[]];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';

import type { SystemManifest } from '$lib/server/project/package';

import { useActionHandler } from '@utils-server/request-handler';

export class GetSystemManifestBody {
@Expose()
@IsString({ each: true })
@IsNotEmpty()
systemPaths!: [string, ...string[]];
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib/server/actions/project/save/set-save.action.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsObject } from 'class-validator';

import type { Save } from '@utils/types';

import { useActionHandler } from '@utils-server/request-handler';

export class SetSaveBody {
@Expose()
@IsObject()
@IsNotEmpty()
save!: Save;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/utils/request-handler/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const handleError = (e: unknown): ActionFailure<{ error: string; message: unknow
return fail(500, { error: 'Internal Server Error', message: (e as any).toString() });
};

export const useActionHandler = <Body = any>(
export const useActionHandler = <Body extends object = any>(
callback: Callback<Body>,
options?: RequestHandlerOptions<Body>,
): Action => {
return async (event) => {
try {
const context = await getContext(event);

const body = parseFormDataBody<Body>(await event.request.formData(), options?.body);
const body = await parseFormDataBody<Body>(await event.request.formData(), options?.body);

assertRequest(context, options);

Expand Down
26 changes: 21 additions & 5 deletions src/lib/server/utils/request-handler/body.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';

import { Exception } from '@utils/exception';
import type { ClassType } from '@utils/types';

export const parseFormDataBody = <Body = any>(
export const parseFormDataBody = <Body extends object = any>(
rawBody: FormData,
c: ClassType<Body> | undefined,
): Body => {
): Promise<Body> => {
const baseBody = Object.fromEntries(
Array.from(rawBody.entries()).map(([key, value]) => [key, JSON.parse(value)]),
);
return parseBody(baseBody, c);
};

export const parseBody = <Body = any>(baseBody: any, c: ClassType<Body> | undefined): Body => {
// @todo add class validation and class transformation
void c;
export const parseBody = async <Body extends object = any>(
baseBody: any,
c: ClassType<Body> | undefined,
): Promise<Body> => {
if (!c) return baseBody as Body;
const data = plainToInstance(c, baseBody, {
excludeExtraneousValues: true,
});
const errors = await validate(data);
if (errors.length > 0)
throw new Exception(
'Bad Request',
`Invalid body :\n${errors.toString().replace(/,/g, '\n')}`,
400,
);
return baseBody as Body;
};
1 change: 1 addition & 0 deletions src/lib/server/utils/validators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { IsFalseOrString } from './is-string-or-false.validator';
20 changes: 20 additions & 0 deletions src/lib/server/utils/validators/is-string-or-false.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type ValidationOptions, isBoolean, isString, registerDecorator } from 'class-validator';

export const IsFalseOrString = (validationOptions?: ValidationOptions) => {
return (object: object, propertyName: string) => {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
...(validationOptions ? { options: validationOptions } : {}),
constraints: [],
validator: {
validate(value: string) {
return (isBoolean(value) && !value) || isString(value);
},
defaultMessage() {
return `$value must be a valid IP address or FQDN`;
},
},
});
};
};
Loading
Loading