Skip to content

Commit

Permalink
feat: HTSXUtils, errors handler, all http methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xlsft committed Mar 21, 2024
1 parent 3f7ea83 commit a3ab670
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 97 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

277 changes: 222 additions & 55 deletions main.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import HTSX, { HTSXProps, HTSXView, HTSXApi } from './main.ts'
import HTSX, { HTSXProps, HTSXView, HTSXApi, HTSXUtils } from './main.ts'

export {
HTSX, type HTSXView, type HTSXApi, type HTSXProps
HTSX, HTSXUtils, type HTSXView, type HTSXApi, type HTSXProps
}
71 changes: 59 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<br/>
<br/>
<div align="middle">
<img src="https://i.imgur.com/X0ou5EY.png" height=120>
<img src="https://i.imgur.com/aowfT0x.png" height=120>
</div>

#
Expand All @@ -23,6 +23,8 @@
* <a href="#start">Quickstart</a>
* <a href="#usecase">Use case</a>
* <a href="#license">License</a>
* <a href="https://marketplace.visualstudio.com/items?itemName=pushqrdx.inline-html">Inline HTML Extension for VS Code</a>
* <a href="https://deno.land/x/htsx/mod.ts">API Reference</a>

<h2 id="install"><strong>💾 Installation</strong></h2>

Expand All @@ -47,9 +49,12 @@ import { Router } from 'https://deno.land/x/oak/router.ts'
```
Root
│ main.ts (main file)
└───web
│ +root.ts (<App/> like component)
│ +root.css (global css)
│ +error.ts (error component)
│ +root.ts (error css)
├───components
│ Button.ts (pure HTML component)
Expand All @@ -61,8 +66,11 @@ Root
└───api
└───v1/user
+get.ts (GET handler)
+post.ts (POST handler)
+users.ts (simple users array for demonstartion)
+get.ts (GET handler)
+post.ts (POST handler)
│ ...+<method>.ts (supported http method handler)
```
For starters, let's create basic Oak server and pass app and router in `HTSX` class

Expand All @@ -77,10 +85,10 @@ const router = new Router()

await new HTSX({
root: './web',
server: { app, router, () => { app.listen({ port: 8080 })} },
server: { app, router, init: () => { app.listen({ port: 8080 }); console.log('Server listening on 8080') } },
props: {
payload: async (_ctx: Context) => {
return { name: 'John' };
payload: async (ctx: Context) => {
return { user: "John" }
}
}
})
Expand All @@ -106,6 +114,13 @@ export default (props: HTSXProps) => { return /*html*/`
</html>
`}
```
If you need to style root component, create:
`web/+root.css`
```css
body {
background: lightblue;
}
```

Basic config is over, now you can create endpoints for your server in `routes` folder, for example - view endpoint on `/` and `Button` component

Expand Down Expand Up @@ -172,9 +187,31 @@ h1 {

<img src="https://i.imgur.com/OFYmSle.png">

#### Also you can specify error page when status !== 200

`web/+error.ts`
```ts
import { HTSXProps } from "../mod.ts";

export default (props: HTSXProps) => { return /*html*/`
<h1>ERROR: ${props.ctx?.response.status}</h1>
`}
```

`web/+error.css`
```css
body {
background: red;
color: white;
}
```
<img src="https://i.imgur.com/bn8rhbq.png">



#### Now let's create REST API endpoint

Create `+get.ts` or `+post.ts` in any directory inside routest directory, for example
Create any supported `+<method>.ts` in any directory inside routes directory, for example:

`/web/routes/api/v1/user/users.ts`
```ts
Expand Down Expand Up @@ -212,10 +249,20 @@ export default (ctx: Context, props: HTSXProps) => {
return { id: 1, name: 'John Doe', age: 28, }
}
```
<img src="https://i.imgur.com/FJ8Vmic.png">
<img src="https://i.imgur.com/CqGG3ro.png">
<img src="https://i.imgur.com/tv4R3dj.png">
<img src="https://i.imgur.com/MzxrCI6.png">
<img src="https://i.imgur.com/FJ8Vmic.png" width="350">
<img src="https://i.imgur.com/CqGG3ro.png" width="350">
<img src="https://i.imgur.com/tv4R3dj.png" width="350">
<img src="https://i.imgur.com/MzxrCI6.png" width="350">

Supported methods:
- `get`
- `head`
- `patch`
- `options`
- `delete`
- `post`
- `put`


#### So here is example how to quickly create basic API and HTML site with cool DX

Expand Down
12 changes: 8 additions & 4 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { HTSX } from './mod.ts'
import { Application, Context } from 'https://deno.land/x/[email protected]/mod.ts'
import { Application } from 'https://deno.land/x/[email protected]/mod.ts'
import { Router } from 'https://deno.land/x/[email protected]/router.ts'

const app = new Application()
const router = new Router()

await new HTSX({
root: './web',
server: { app, router, init: () => { app.listen({ port: 8080 })} },
server: { app, router, init: () => { app.listen({ port: 8080 }); console.log('Server listening on 8080') } },
logger: (ctx) => {
console.log(`New ${ctx.request.method} request! ${ctx.request.url} ${ctx.response.status === 200 ? `` : `, error: ${ctx.response.status}`}`)
},
props: {
payload: () => {
payload: async (ctx) => {
return { user: "John" }
}
}
})
})

3 changes: 0 additions & 3 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
Other HTTP verbs for api endpoints
+root.css
utils for convinient work with arrays, awaits and more (each, await, etc...)
13 changes: 0 additions & 13 deletions web/+root.ts

This file was deleted.

5 changes: 0 additions & 5 deletions web/routes/404/+view.ts

This file was deleted.

0 comments on commit a3ab670

Please sign in to comment.