Skip to content
This repository was archived by the owner on May 6, 2023. It is now read-only.

Commit cff5358

Browse files
committed
update readme and github profile
1 parent 2d450a1 commit cff5358

File tree

3 files changed

+136
-12
lines changed

3 files changed

+136
-12
lines changed

.gitfiles/buxt-logo.png

106 KB
Loading

README.md

+130-9
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,152 @@
66

77
---
88

9-
# <p align="center">Buxt</p>
10-
11-
## <p align="center">Filesystem-route based REST API server for Bun.js</p>
12-
13-
<p align="center">If you've ever used NextJs's routing system, then you should already be familiar with this concept. In short, it creates routes based off the directory structure under the folder "routes" in the root of the project. Exporting a defult function is all you need to do to get things working, the rest is handled automatically.
9+
<p align="center">
10+
<img src="./.gitfiles/buxt-logo.png">
11+
12+
<h3 align="center"><b>Lightweight filesystem-based router for creating REST APIs.</b>
1413

1514
---
1615

1716
## <p align="center"> **Note**
1817

19-
<p align="center"> This project is currently not usable - if you try to use it, it will just return 404.
18+
<p align="center"> This project is currently very early development, and I wouldn't recommend it for production. Feel free to use it as you like and if you find any problems then submit an issue via github issues tab.
2019

2120
---
2221

2322
## <p align="center"> Features/Roadmap
2423

25-
- [x] MVP Status
2624
- [x] Nested routing
2725
- [x] Route parameters
2826
- [ ] Catch-all routes on same level as named routes
2927
- [x] Implement response handling logic fully
3028
- [ ] Middleware
3129
- [x] CORS solution
32-
- [ ] More detailed usage instructions
30+
- [ ] Advanced Logging
31+
- [ ] Authentication
32+
- [ ] Websockets
33+
- [ ] ESLint plugin
34+
---
35+
36+
## <b>Installation</b>
37+
`bun a buxt`
38+
39+
## <b>Getting started</b>
40+
Starting a basic server with the default values
41+
42+
``` typescript
43+
//index.ts
44+
import CreateServer from "buxt";
45+
46+
await CreateServer(3000).then(s => s.listen());
47+
48+
//routes/example_endpoint.ts
49+
import type { BuxtRequest, BuxtResponse } from "buxt"; //typings arent required, but useful!
50+
51+
export default async function(req: BuxtRequest, res: BuxtResponse) {
52+
res.send("Hello!");
53+
}
54+
```
55+
Thats it!
56+
57+
---
58+
59+
## <b>Usage</b>
60+
61+
By default, the app will search for exported functions under <b>\<project-root>/routes</b> and <b>\<project-root>/src/routes</b>, unless specified when creating the server.
62+
63+
Aside from the previous example, there are three other ways of creating and starting a buxt server:
64+
65+
66+
### <b><u>Create a server with default route root using port 3000</u></b>
67+
``` typescript
68+
//index.ts
69+
import CreateServer from "buxt";
70+
71+
const server = await CreateServer(3000);
72+
await server.listen();
73+
```
74+
75+
### <u><b>Create a server with default route root using port 3000 and a custom root route path</b></u>
76+
77+
``` typescript
78+
//index.ts
79+
import CreateServer from "buxt";
80+
81+
const server = await CreateServer(3000, "src/api");
82+
await server.listen();
83+
```
84+
85+
### <u><b>Create a server with config object</u></b>
86+
87+
``` typescript
88+
//index.ts
89+
import CreateServer from "buxt";
90+
91+
const server = await CreateServer({
92+
port: 3000,
93+
routeRoot: "api",
94+
cors: true,
95+
corsConfig: {
96+
origins: [ "*" ]
97+
}
98+
});
99+
await server.listen();
100+
```
101+
102+
### Definitions for config
103+
104+
``` typescript
105+
type BuxtConfig = {
106+
port: number,
107+
routeRoot: string,
108+
cors?: boolean = false,
109+
corsConfig?: CorsConfig = null
110+
}
111+
112+
type CorsConfig = {
113+
origins: string[],
114+
allowedMethods?: HttpMethod[] = ["GET", "OPTIONS", "POST"]
115+
}
116+
117+
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH";
118+
119+
```
120+
121+
### <u><b>Route parameters</b></u>
122+
Route parameters work they they do in Next.js - they're denoted by a variable name surrounded by square brackets, eg: routes/user/[user].ts
123+
124+
They can then be accessed on the BuxtRequest object under `req.routeParameters.{variable_name}`
125+
``` typescript
126+
//routes/user/[user].ts
127+
import type { BuxtRequest, BuxtResponse } from "buxt";
128+
129+
export default async function(req: BuxtRequest, res: BuxtResponse) {
130+
res.send("Hello " + req.routeParameters.user);
131+
}
132+
```
133+
134+
### <u><b>Enabling Cors</b></u>
135+
You must create a server using a config object to enable cors responses.
136+
137+
``` typescript
138+
//index.ts
139+
import CreateServer from "buxt";
140+
141+
const server = await CreateServer({
142+
port: 3000,
143+
routeRoot: "api",
144+
cors: true,
145+
corsConfig: {
146+
origins: [ "localhost:3000", "localhost:3001", "https://miaz.xyz/", "http://miaz.xyz" ],
147+
allowedMethods: [ "GET", "POST", "OPTIONS", "PUT", "DELETE"]
148+
}
149+
});
150+
```
151+
152+
Firstly, make sure the cors key is set to `true`, then pass in a `CorsConfig` object. The `CorsConfig`'s `origins` key cannot be null. If you're allowing all origins then simply make it a single item array with `["*"]`.
33153

154+
<b>Reminder</b> that you cannot combine wildcard routes and non-wildcard routes; if you attempt to do this then it will throw an error.
34155

35156
---
36-
##### Big thanks to <a href="https://github.com/lau1944">lau1994</a> and their project <a href="https://github.com/lau1944/bunrest">Bunrest</a> (really nice express-like server built for Bun) which has helped me a lot getting this project started.
157+
###### Big thanks to <a href="https://github.com/lau1944">lau1994</a> and their project <a href="https://github.com/lau1944/bunrest">Bunrest</a> (really nice express-like server built for Bun) which has helped me a lot getting this project started.

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
"types": "index.d.ts",
1313
"exports": {
1414
".": {
15-
"browser": {
15+
"node": {
1616
"default": "./index.ts"
17-
}
17+
},
18+
"import": "./index.ts",
19+
"default": "./index.ts"
1820
}
1921
},
2022
"bugs": {
@@ -30,5 +32,6 @@
3032
"@swc/cli": "^0.1.57",
3133
"@swc/core": "^1.3.14",
3234
"bun-types": "^0.2.2"
33-
}
35+
},
36+
"keywords": ["typescript", "bun", "bunjs", "zigg", "api", "rest", "routes", "framework", "javascript"]
3437
}

0 commit comments

Comments
 (0)