canx-starter-api 1.0.0
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.
- package/README.md +74 -0
- package/package.json +37 -0
- package/src/app.ts +15 -0
- package/src/controllers/AuthController.ts +33 -0
- package/src/controllers/UserController.ts +13 -0
- package/src/routes.ts +13 -0
- package/tsconfig.json +14 -0
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Canx Starter API
|
|
2
|
+
|
|
3
|
+
⚡ Minimal REST API template for CanxJS with JWT authentication.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ **JWT Authentication** - Token-based auth with refresh tokens
|
|
8
|
+
- ✅ **API Routes** - Clean REST API structure
|
|
9
|
+
- ✅ **Validation** - Request validation with built-in validators
|
|
10
|
+
- ✅ **Error Handling** - Consistent JSON error responses
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# Clone or copy this starter
|
|
16
|
+
bunx degit chandafa/canx-starters/api my-api
|
|
17
|
+
|
|
18
|
+
# Install dependencies
|
|
19
|
+
cd my-api
|
|
20
|
+
bun install
|
|
21
|
+
|
|
22
|
+
# Start development server
|
|
23
|
+
bun run dev
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Project Structure
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
src/
|
|
30
|
+
├── controllers/ # API controllers
|
|
31
|
+
│ ├── AuthController.ts
|
|
32
|
+
│ └── UserController.ts
|
|
33
|
+
├── middlewares/ # Custom middleware
|
|
34
|
+
│ └── auth.ts
|
|
35
|
+
├── routes.ts # API route definitions
|
|
36
|
+
└── app.ts # Application entry
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API Endpoints
|
|
40
|
+
|
|
41
|
+
| Method | Endpoint | Description |
|
|
42
|
+
| ------ | -------------------- | -------------------------- |
|
|
43
|
+
| POST | `/api/auth/register` | Register new user |
|
|
44
|
+
| POST | `/api/auth/login` | Login and get token |
|
|
45
|
+
| POST | `/api/auth/refresh` | Refresh access token |
|
|
46
|
+
| GET | `/api/users` | List all users (protected) |
|
|
47
|
+
| GET | `/api/users/:id` | Get user by ID (protected) |
|
|
48
|
+
|
|
49
|
+
## Available Scripts
|
|
50
|
+
|
|
51
|
+
| Command | Description |
|
|
52
|
+
| --------------- | ---------------------------------------- |
|
|
53
|
+
| `bun run dev` | Start development server with hot reload |
|
|
54
|
+
| `bun run build` | Build for production |
|
|
55
|
+
| `bun run start` | Run production build |
|
|
56
|
+
|
|
57
|
+
## Configuration
|
|
58
|
+
|
|
59
|
+
Create a `.env` file:
|
|
60
|
+
|
|
61
|
+
```env
|
|
62
|
+
PORT=3000
|
|
63
|
+
JWT_SECRET=your-jwt-secret
|
|
64
|
+
JWT_EXPIRES_IN=7d
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Learn More
|
|
68
|
+
|
|
69
|
+
- [CanxJS Documentation](https://docs-canxjs.netlify.app)
|
|
70
|
+
- [API Reference](https://docs-canxjs.netlify.app/docs/api)
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "canx-starter-api",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Minimal API Starter Kit for CanxJS - REST API with JWT Authentication",
|
|
5
|
+
"author": "CanxJS Team",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"canxjs",
|
|
9
|
+
"starter",
|
|
10
|
+
"template",
|
|
11
|
+
"api",
|
|
12
|
+
"rest",
|
|
13
|
+
"bun",
|
|
14
|
+
"jwt"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/chandafa/canx-starters.git",
|
|
19
|
+
"directory": "api"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"dev": "bun --watch src/app.ts",
|
|
23
|
+
"build": "bun build src/app.ts --outdir dist",
|
|
24
|
+
"start": "bun run dist/app.js"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"canxjs": "^1.2.1"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^20.0.0",
|
|
31
|
+
"bun-types": "latest",
|
|
32
|
+
"typescript": "^5.3.0"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"bun": ">=1.0.0"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/app.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Canx } from "canxjs";
|
|
2
|
+
import { routes } from "./routes";
|
|
3
|
+
|
|
4
|
+
const app = new Canx();
|
|
5
|
+
|
|
6
|
+
// Register routes
|
|
7
|
+
app.routes(routes);
|
|
8
|
+
|
|
9
|
+
// Start the server
|
|
10
|
+
app.start({
|
|
11
|
+
port: 3001,
|
|
12
|
+
development: true,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
console.log("Canx API Starter running on http://localhost:3001");
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BaseController, Request, Response } from "canxjs";
|
|
2
|
+
|
|
3
|
+
export class AuthController extends BaseController {
|
|
4
|
+
|
|
5
|
+
public async login() {
|
|
6
|
+
const { email, password } = await this.body<{email: string, password: string}>();
|
|
7
|
+
|
|
8
|
+
// Mock authentication logic
|
|
9
|
+
if (email === "test@example.com" && password === "password") {
|
|
10
|
+
return this.json({
|
|
11
|
+
token: "mock-jwt-token-123456",
|
|
12
|
+
user: {
|
|
13
|
+
id: 1,
|
|
14
|
+
name: "Test User",
|
|
15
|
+
email: "test@example.com"
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return this.json({ message: "Invalid credentials" }, 401);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public async register() {
|
|
24
|
+
const { name, email, password } = await this.body<{name: string, email: string, password: string}>();
|
|
25
|
+
|
|
26
|
+
// Mock registration
|
|
27
|
+
return this.json({
|
|
28
|
+
message: "User registered successfully",
|
|
29
|
+
user: { id: 2, name, email },
|
|
30
|
+
token: "mock-jwt-token-new-user"
|
|
31
|
+
}, 201);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseController, Request, Response } from "canxjs";
|
|
2
|
+
|
|
3
|
+
export class UserController extends BaseController {
|
|
4
|
+
|
|
5
|
+
public async me() {
|
|
6
|
+
// In real app, user is attached to request by auth middleware
|
|
7
|
+
return this.json({
|
|
8
|
+
id: 1,
|
|
9
|
+
name: "Test User",
|
|
10
|
+
email: "test@example.com"
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/routes.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Router } from "canxjs";
|
|
2
|
+
import { AuthController } from "./controllers/AuthController";
|
|
3
|
+
import { UserController } from "./controllers/UserController";
|
|
4
|
+
|
|
5
|
+
export const routes = (router: Router) => {
|
|
6
|
+
// API Routes
|
|
7
|
+
router.post("/api/login", [AuthController, "login"]);
|
|
8
|
+
router.post("/api/register", [AuthController, "register"]);
|
|
9
|
+
|
|
10
|
+
// Protected Routes
|
|
11
|
+
// middleware: ['auth:api']
|
|
12
|
+
router.get("/api/user", [UserController, "me"]);
|
|
13
|
+
};
|
package/tsconfig.json
ADDED