wexts 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ # WEXTS Environment Variables
2
+ # Copy this file to .env and update values
3
+
4
+ # JWT Authentication
5
+ JWT_SECRET=your-super-secret-jwt-key-change-in-production
6
+ JWT_EXPIRES_IN=7d
7
+
8
+ # Database (Local Development - SQLite)
9
+ DATABASE_URL="file:./apps/api/dev.db"
10
+
11
+ # Database (Production - PostgreSQL)
12
+ # DATABASE_URL="postgresql://user:password@host:5432/dbname"
13
+ # DIRECT_URL="postgresql://user:password@host:5432/dbname"
14
+
15
+ # Server
16
+ PORT=3000
17
+ NODE_ENV=development
@@ -0,0 +1,60 @@
1
+ # Stage 1: Build
2
+ FROM node:20-alpine AS builder
3
+
4
+ # Install pnpm
5
+ RUN npm install -g pnpm
6
+
7
+ # Set working directory
8
+ WORKDIR /app
9
+
10
+ # Copy package files
11
+ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
12
+ COPY apps/web/package.json ./apps/web/
13
+ COPY apps/api/package.json ./apps/api/
14
+
15
+ # Install dependencies
16
+ RUN pnpm install --frozen-lockfile
17
+
18
+ # Copy source code
19
+ COPY . .
20
+
21
+ # Generate Prisma Client
22
+ RUN cd apps/api && npx prisma generate
23
+
24
+ # Build all apps
25
+ RUN pnpm run build
26
+
27
+ # Stage 2: Production
28
+ FROM node:20-alpine AS runner
29
+
30
+ # Install pnpm
31
+ RUN npm install -g pnpm
32
+
33
+ WORKDIR /app
34
+
35
+ # Copy package files
36
+ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
37
+ COPY apps/web/package.json ./apps/web/
38
+ COPY apps/api/package.json ./apps/api/
39
+
40
+ # Install production dependencies only
41
+ RUN pnpm install --prod --frozen-lockfile
42
+
43
+ # Copy built files from builder
44
+ COPY --from=builder /app/dist ./dist
45
+ COPY --from=builder /app/apps/web/.next ./apps/web/.next
46
+ COPY --from=builder /app/apps/web/public ./apps/web/public
47
+ COPY --from=builder /app/apps/api/dist ./apps/api/dist
48
+ COPY --from=builder /app/apps/api/prisma ./apps/api/prisma
49
+ COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
50
+ COPY --from=builder /app/server.ts ./server.ts
51
+
52
+ # Expose port
53
+ EXPOSE 3000
54
+
55
+ # Set environment
56
+ ENV NODE_ENV=production
57
+ ENV PORT=3000
58
+
59
+ # Start application
60
+ CMD ["node", "dist/server.js"]
@@ -0,0 +1 @@
1
+ web: pnpm start
@@ -0,0 +1,58 @@
1
+ # WEXTS Templates
2
+
3
+ This directory contains all the templates needed to create a WEXTS unified server application.
4
+
5
+ ## 📁 Files Included
6
+
7
+ ### Core Templates
8
+ - `server.ts` - Unified server that runs Next.js + NestJS in one process
9
+ - `api-sdk.ts` - Type-safe SDK for API calls (zero URLs needed!)
10
+ - `root-package.json` - Root package.json with all scripts
11
+ - `tsconfig.json` - TypeScript configuration
12
+ - `.env.example` - Environment variables template
13
+
14
+ ### Deployment Templates
15
+ - `Dockerfile` - Docker multi-stage build
16
+ - `docker-compose.yml` - Docker Compose with PostgreSQL
17
+ - `.dockerignore` - Docker ignore file
18
+ - `nixpacks.toml` - Railway deployment config
19
+ - `Procfile` - Render/Heroku deployment
20
+
21
+ ### App Templates
22
+ - `nestjs-api/` - NestJS backend template
23
+ - `nextjs-web/` - Next.js frontend template
24
+
25
+ ## 🚀 Usage
26
+
27
+ These templates are used by the WEXTS CLI when creating a new project:
28
+
29
+ ```bash
30
+ npx wexts create my-app
31
+ ```
32
+
33
+ ## ✨ Features
34
+
35
+ ✅ **Unified Server** - Single Node.js process for frontend + backend
36
+ ✅ **Zero URLs** - Type-safe SDK without explicit API URLs
37
+ ✅ **Smart Routing** - Automatic routing between Next.js and NestJS
38
+ ✅ **Docker Ready** - Complete Docker setup included
39
+ ✅ **Multi-Platform Deploy** - Railway, Render, Docker, VPS
40
+
41
+ ## 📦 Template Structure
42
+
43
+ ```
44
+ project/
45
+ ├── server.ts ← Unified server
46
+ ├── package.json ← Root package
47
+ ├── tsconfig.json ← TS config
48
+ ├── .env.example ← Env template
49
+ ├── Dockerfile ← Docker build
50
+ ├── docker-compose.yml ← Docker + DB
51
+ ├── apps/
52
+ │ ├── api/ ← From nestjs-api template
53
+ │ └── web/ ← From nextjs-web template
54
+ ```
55
+
56
+ ## 🔧 Customization
57
+
58
+ Templates can be customized before project creation. See CLI documentation for details.
@@ -0,0 +1,115 @@
1
+ /**
2
+ * WEXTS Internal SDK
3
+ * Type-safe API client - ZERO URLs needed!
4
+ * Works in both Client and Server Components
5
+ */
6
+
7
+ // HTTP client - uses relative paths only
8
+ async function request<T>(method: string, path: string, data?: any): Promise<T> {
9
+ const url = `/api${path}`;
10
+
11
+ const options: RequestInit = {
12
+ method,
13
+ headers: {
14
+ 'Content-Type': 'application/json',
15
+ },
16
+ credentials: 'include',
17
+ };
18
+
19
+ if (data) {
20
+ options.body = JSON.stringify(data);
21
+ }
22
+
23
+ const response = await fetch(url, options);
24
+
25
+ if (!response.ok) {
26
+ const error = await response.json().catch(() => ({ message: response.statusText }));
27
+ throw new Error(error.message || 'Request failed');
28
+ }
29
+
30
+ return response.json();
31
+ }
32
+
33
+ // ==========================================
34
+ // TYPE-SAFE API - NO URLs ANYWHERE!
35
+ // ==========================================
36
+
37
+ export const api = {
38
+ /**
39
+ * Authentication API
40
+ */
41
+ auth: {
42
+ /**
43
+ * Register new user
44
+ * @example await api.auth.register({ email, password, name })
45
+ */
46
+ register: (data: { email: string; password: string; name?: string }) =>
47
+ request<{ user: any; access_token: string }>('POST', '/auth/register', data),
48
+
49
+ /**
50
+ * Login user
51
+ * @example await api.auth.login({ email, password })
52
+ */
53
+ login: (data: { email: string; password: string }) =>
54
+ request<{ user: any; access_token: string }>('POST', '/auth/login', data),
55
+
56
+ /**
57
+ * Get current user
58
+ * @example const user = await api.auth.me()
59
+ */
60
+ me: () => request<any>('GET', '/auth/me'),
61
+ },
62
+
63
+ /**
64
+ * Users API
65
+ */
66
+ users: {
67
+ /**
68
+ * Get current user profile
69
+ * @example const profile = await api.users.me()
70
+ */
71
+ me: () => request<any>('GET', '/users/me'),
72
+ },
73
+
74
+ /**
75
+ * Todos API
76
+ */
77
+ todos: {
78
+ /**
79
+ * Get all todos
80
+ * @example const todos = await api.todos.findAll()
81
+ */
82
+ findAll: () => request<any[]>('GET', '/todos'),
83
+
84
+ /**
85
+ * Get single todo
86
+ * @example const todo = await api.todos.findOne('123')
87
+ */
88
+ findOne: (id: string) => request<any>('GET', `/todos/${id}`),
89
+
90
+ /**
91
+ * Create new todo
92
+ * @example await api.todos.create({ title: 'Task', description: 'Do it' })
93
+ */
94
+ create: (data: { title: string; description?: string }) =>
95
+ request<any>('POST', '/todos', data),
96
+
97
+ /**
98
+ * Update todo
99
+ * @example await api.todos.update('123', { completed: true })
100
+ */
101
+ update: (id: string, data: { title?: string; description?: string; completed?: boolean }) =>
102
+ request<any>('PUT', `/todos/${id}`, data),
103
+
104
+ /**
105
+ * Delete todo
106
+ * @example await api.todos.delete('123')
107
+ */
108
+ delete: (id: string) => request<void>('DELETE', `/todos/${id}`),
109
+ },
110
+ };
111
+
112
+ /**
113
+ * Export type-safe API
114
+ */
115
+ export type API = typeof api;
@@ -0,0 +1,34 @@
1
+ version: '3.8'
2
+
3
+ services:
4
+ # WEXTS Application
5
+ app:
6
+ build:
7
+ context: .
8
+ dockerfile: Dockerfile
9
+ ports:
10
+ - "3000:3000"
11
+ environment:
12
+ - NODE_ENV=production
13
+ - DATABASE_URL=postgresql://wexts:wexts_password@postgres:5432/wexts_db
14
+ - JWT_SECRET=your-super-secret-jwt-key-change-this
15
+ - PORT=3000
16
+ depends_on:
17
+ - postgres
18
+ restart: unless-stopped
19
+
20
+ # PostgreSQL Database
21
+ postgres:
22
+ image: postgres:16-alpine
23
+ ports:
24
+ - "5432:5432"
25
+ environment:
26
+ - POSTGRES_USER=wexts
27
+ - POSTGRES_PASSWORD=wexts_password
28
+ - POSTGRES_DB=wexts_db
29
+ volumes:
30
+ - postgres_data:/var/lib/postgresql/data
31
+ restart: unless-stopped
32
+
33
+ volumes:
34
+ postgres_data:
@@ -1,27 +1,24 @@
1
- import { Controller, Post, Get, Body, UseGuards, Request } from '@nestjs/common';
2
- import { FusionController as WextsController, FusionPost as WextsPost, FusionGet as WextsGet } from 'wexts/nest';
1
+ import { Controller, Post, Get, Body, UseGuards, Request, Inject } from '@nestjs/common';
3
2
  import { AuthService } from './auth.service';
4
3
  import { JwtAuthGuard } from './guards/jwt-auth.guard';
5
4
  import { RegisterDto, LoginDto } from './dto/auth.dto';
6
5
 
7
- @WextsController('auth')
8
6
  @Controller('auth')
9
7
  export class AuthController {
10
- constructor(private authService: AuthService) { }
8
+ constructor(
9
+ @Inject(AuthService) private readonly authService: AuthService
10
+ ) { }
11
11
 
12
- @WextsPost()
13
12
  @Post('register')
14
13
  async register(@Body() dto: RegisterDto) {
15
14
  return this.authService.register(dto);
16
15
  }
17
16
 
18
- @WextsPost()
19
17
  @Post('login')
20
18
  async login(@Body() dto: LoginDto) {
21
19
  return this.authService.login(dto);
22
20
  }
23
21
 
24
- @WextsGet()
25
22
  @Get('me')
26
23
  @UseGuards(JwtAuthGuard)
27
24
  async getMe(@Request() req) {
@@ -1,13 +1,16 @@
1
1
  import { Module } from '@nestjs/common';
2
2
  import { JwtModule } from '@nestjs/jwt';
3
3
  import { PassportModule } from '@nestjs/passport';
4
- import { ConfigService } from '@nestjs/config';
4
+ import { ConfigModule, ConfigService } from '@nestjs/config';
5
5
  import { AuthController } from './auth.controller';
6
6
  import { AuthService } from './auth.service';
7
7
  import { JwtStrategy } from './strategies/jwt.strategy';
8
+ import { PrismaModule } from '../prisma/prisma.module';
8
9
 
9
10
  @Module({
10
11
  imports: [
12
+ PrismaModule, // ✅ Important: Import PrismaModule
13
+ ConfigModule,
11
14
  PassportModule,
12
15
  JwtModule.registerAsync({
13
16
  inject: [ConfigService],
@@ -1,18 +1,17 @@
1
- import { Injectable, UnauthorizedException } from '@nestjs/common';
1
+ import { Injectable, UnauthorizedException, Inject } from '@nestjs/common';
2
2
  import { JwtService } from '@nestjs/jwt';
3
3
  import { PrismaService } from '../prisma/prisma.service';
4
- import * as bcrypt from 'bcrypt';
4
+ import * as bcrypt from 'bcryptjs';
5
5
  import { RegisterDto, LoginDto } from './dto/auth.dto';
6
6
 
7
7
  @Injectable()
8
8
  export class AuthService {
9
9
  constructor(
10
- private prisma: PrismaService,
11
- private jwtService: JwtService,
10
+ @Inject(PrismaService) private readonly prisma: PrismaService,
11
+ @Inject(JwtService) private readonly jwtService: JwtService,
12
12
  ) { }
13
13
 
14
14
  async register(dto: RegisterDto) {
15
- // Check if user exists
16
15
  const exists = await this.prisma.user.findUnique({
17
16
  where: { email: dto.email },
18
17
  });
@@ -21,10 +20,8 @@ export class AuthService {
21
20
  throw new UnauthorizedException('Email already registered');
22
21
  }
23
22
 
24
- // Hash password
25
23
  const hashedPassword = await bcrypt.hash(dto.password, 10);
26
24
 
27
- // Create user
28
25
  const user = await this.prisma.user.create({
29
26
  data: {
30
27
  email: dto.email,
@@ -39,13 +36,12 @@ export class AuthService {
39
36
  },
40
37
  });
41
38
 
42
- const token = this.generateToken(user.id, user.email);
39
+ const access_token = this.generateToken(user.id, user.email);
43
40
 
44
- return { user, token };
41
+ return { user, access_token };
45
42
  }
46
43
 
47
44
  async login(dto: LoginDto) {
48
- // Find user
49
45
  const user = await this.prisma.user.findUnique({
50
46
  where: { email: dto.email },
51
47
  });
@@ -54,14 +50,13 @@ export class AuthService {
54
50
  throw new UnauthorizedException('Invalid credentials');
55
51
  }
56
52
 
57
- // Verify password
58
53
  const valid = await bcrypt.compare(dto.password, user.password);
59
54
 
60
55
  if (!valid) {
61
56
  throw new UnauthorizedException('Invalid credentials');
62
57
  }
63
58
 
64
- const token = this.generateToken(user.id, user.email);
59
+ const access_token = this.generateToken(user.id, user.email);
65
60
 
66
61
  return {
67
62
  user: {
@@ -69,7 +64,7 @@ export class AuthService {
69
64
  email: user.email,
70
65
  name: user.name,
71
66
  },
72
- token,
67
+ access_token,
73
68
  };
74
69
  }
75
70
 
@@ -1,34 +1,28 @@
1
1
  import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards, Request } from '@nestjs/common';
2
- import { FusionController as WextsController, FusionGet as WextsGet, FusionPost as WextsPost, FusionPut as WextsPut, FusionDelete as WextsDelete } from 'wexts/nest';
3
2
  import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
4
3
  import { TodosService } from './todos.service';
5
4
  import { CreateTodoDto, UpdateTodoDto } from './dto/todo.dto';
6
5
 
7
- @WextsController('todos')
8
6
  @Controller('todos')
9
7
  @UseGuards(JwtAuthGuard)
10
8
  export class TodosController {
11
9
  constructor(private todosService: TodosService) { }
12
10
 
13
- @WextsGet()
14
11
  @Get()
15
12
  async findAll(@Request() req) {
16
13
  return this.todosService.findAll(req.user.userId);
17
14
  }
18
15
 
19
- @WextsGet()
20
16
  @Get(':id')
21
17
  async findOne(@Param('id') id: string, @Request() req) {
22
18
  return this.todosService.findOne(id, req.user.userId);
23
19
  }
24
20
 
25
- @WextsPost()
26
21
  @Post()
27
22
  async create(@Body() dto: CreateTodoDto, @Request() req) {
28
23
  return this.todosService.create(dto, req.user.userId);
29
24
  }
30
25
 
31
- @WextsPut()
32
26
  @Put(':id')
33
27
  async update(
34
28
  @Param('id') id: string,
@@ -38,7 +32,6 @@ export class TodosController {
38
32
  return this.todosService.update(id, dto, req.user.userId);
39
33
  }
40
34
 
41
- @WextsDelete()
42
35
  @Delete(':id')
43
36
  async remove(@Param('id') id: string, @Request() req) {
44
37
  return this.todosService.remove(id, req.user.userId);
@@ -1,8 +1,10 @@
1
1
  import { Module } from '@nestjs/common';
2
- import { TodosController } from './todos.controller';
3
2
  import { TodosService } from './todos.service';
3
+ import { TodosController } from './todos.controller';
4
+ import { PrismaModule } from '../prisma/prisma.module';
4
5
 
5
6
  @Module({
7
+ imports: [PrismaModule], // ✅ Import PrismaModule
6
8
  controllers: [TodosController],
7
9
  providers: [TodosService],
8
10
  })
@@ -1,15 +1,12 @@
1
1
  import { Controller, Get, UseGuards, Request } from '@nestjs/common';
2
- import { FusionController as WextsController, FusionGet as WextsGet } from 'wexts/nest';
3
2
  import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
4
3
  import { UsersService } from './users.service';
5
4
 
6
- @WextsController('users')
7
5
  @Controller('users')
8
6
  @UseGuards(JwtAuthGuard)
9
7
  export class UsersController {
10
8
  constructor(private usersService: UsersService) { }
11
9
 
12
- @WextsGet()
13
10
  @Get('me')
14
11
  async getMe(@Request() req) {
15
12
  return this.usersService.findById(req.user.userId);
@@ -1,8 +1,10 @@
1
1
  import { Module } from '@nestjs/common';
2
- import { UsersController } from './users.controller';
3
2
  import { UsersService } from './users.service';
3
+ import { UsersController } from './users.controller';
4
+ import { PrismaModule } from '../prisma/prisma.module';
4
5
 
5
6
  @Module({
7
+ imports: [PrismaModule], // ✅ Import PrismaModule
6
8
  controllers: [UsersController],
7
9
  providers: [UsersService],
8
10
  exports: [UsersService],
@@ -2,8 +2,7 @@
2
2
 
3
3
  import { cookies } from 'next/headers';
4
4
  import { redirect } from 'next/navigation';
5
-
6
- const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5050';
5
+ import { api } from '@/lib/api';
7
6
 
8
7
  export type ActionState = {
9
8
  message?: string;
@@ -32,23 +31,10 @@ export async function loginAction(prevState: ActionState, formData: FormData): P
32
31
  }
33
32
 
34
33
  try {
35
- const response = await fetch(`${API_URL}/auth/login`, {
36
- method: 'POST',
37
- headers: {
38
- 'Content-Type': 'application/json',
39
- },
40
- body: JSON.stringify({ email, password }),
41
- });
42
-
43
- const data = await response.json();
44
-
45
- if (!response.ok) {
46
- return {
47
- message: data.message || 'Invalid credentials',
48
- };
49
- }
34
+ // Use SDK - no URLs needed!
35
+ const data = await api.auth.login({ email, password });
50
36
 
51
- // Assuming the API returns { access_token: string }
37
+ // Store JWT token
52
38
  if (data.access_token) {
53
39
  const cookieStore = await cookies();
54
40
  cookieStore.set('wexts_token', data.access_token, {
@@ -62,10 +48,53 @@ export async function loginAction(prevState: ActionState, formData: FormData): P
62
48
  message: 'Login failed: No token received',
63
49
  };
64
50
  }
65
- } catch (error) {
51
+ } catch (error: any) {
66
52
  console.error('Login error:', error);
67
53
  return {
68
- message: 'Something went wrong. Please try again.',
54
+ message: error.message || 'Invalid credentials',
55
+ };
56
+ }
57
+
58
+ redirect('/dashboard');
59
+ }
60
+
61
+ export async function registerAction(prevState: ActionState, formData: FormData): Promise<ActionState> {
62
+ const email = formData.get('email') as string;
63
+ const password = formData.get('password') as string;
64
+ const name = formData.get('name') as string;
65
+
66
+ const errors: { email?: string[]; password?: string[] } = {};
67
+
68
+ if (!email || !email.includes('@')) {
69
+ errors.email = ['Invalid email address'];
70
+ }
71
+
72
+ if (!password || password.length < 6) {
73
+ errors.password = ['Password must be at least 6 characters'];
74
+ }
75
+
76
+ if (Object.keys(errors).length > 0) {
77
+ return { errors };
78
+ }
79
+
80
+ try {
81
+ // Use SDK - no URLs needed!
82
+ const data = await api.auth.register({ email, password, name });
83
+
84
+ // Store JWT token
85
+ if (data.access_token) {
86
+ const cookieStore = await cookies();
87
+ cookieStore.set('wexts_token', data.access_token, {
88
+ httpOnly: true,
89
+ secure: process.env.NODE_ENV === 'production',
90
+ maxAge: 60 * 60 * 24 * 7,
91
+ path: '/',
92
+ });
93
+ }
94
+ } catch (error: any) {
95
+ console.error('Register error:', error);
96
+ return {
97
+ message: error.message || 'Registration failed',
69
98
  };
70
99
  }
71
100