utils/templates.js 5.8 K raw
1
export const honoRpcTemplate = `import { Hono } from "hono";
2
import { cors } from "hono/cors";
3
import type { ApiResponse } from "shared/dist";
4
import { hc } from "hono/client";
5
6
export const app = new Hono()
7
8
.use(cors())
9
10
.get("/", (c) => {
11
	return c.text("Hello Hono!");
12
})
13
14
.get("/hello", async (c) => {
15
	const data: ApiResponse = {
16
		message: "Hello BHVR!",
17
		success: true,
18
	};
19
20
	return c.json(data, { status: 200 });
21
});
22
23
const client = hc<typeof app>("");
24
export type Client = typeof client;
25
26
export const hcWithType = (...args: Parameters<typeof hc>): Client =>
27
hc<typeof app>(...args);
28
29
export default app;`;
30
31
export const tailwindTemplate = `import { useState } from 'react'
32
import beaver from './assets/beaver.svg'
33
import { hcWithType } from 'server'
34
35
const SERVER_URL = import.meta.env.VITE_SERVER_URL || "http://localhost:3000"
36
37
type ResponseType = Awaited<ReturnType<typeof client.hello.$get>>;
38
39
const client = hcWithType(SERVER_URL);
40
41
function App() {
42
  const [data, setData] = useState<Awaited<ReturnType<ResponseType["json"]>> | undefined>()
43
44
  async function sendRequest() {
45
    try {
46
      const res = await client.hello.$get()
47
      if (!res.ok) {
48
        console.log("Error fetching data")
49
        return
50
      }
51
      const data = await res.json()
52
      setData(data)
53
    } catch (error) {
54
      console.log(error)
55
    }
56
  }
57
58
  return (
59
    <div className="max-w-xl mx-auto flex flex-col gap-6 items-center justify-center min-h-screen">
60
      <a href="https://github.com/stevedylandev/bhvr" target="_blank">
61
        <img
62
          src={beaver}
63
          className="w-16 h-16 cursor-pointer"
64
          alt="beaver logo"
65
        />
66
      </a>
67
      <h1 className="text-5xl font-black">bhvr</h1>
68
      <h2 className="text-2xl font-bold">Bun + Hono + Vite + React</h2>
69
      <p>A typesafe fullstack monorepo</p>
70
      <div className='flex items-center gap-4'>
71
        <button
72
          onClick={sendRequest}
73
          className="bg-black text-white px-2.5 py-1.5 rounded-md"
74
        >
75
          Call API
76
        </button>
77
        <a target='_blank' href="https://bhvr.dev" className='border-1 border-black text-black px-2.5 py-1.5 rounded-md'>
78
          Docs
79
        </a>
80
      </div>
81
        {data && (
82
          <pre className="bg-gray-100 p-4 rounded-md">
83
            <code>
84
            Message: {data.message} <br />
85
            Success: {data.success.toString()}
86
            </code>
87
          </pre>
88
        )}
89
    </div>
90
  )
91
}
92
93
export default App`;
94
95
export const shadcnTemplate = `import { useState } from 'react'
96
import beaver from './assets/beaver.svg'
97
import { Button } from './components/ui/button'
98
import { hcWithType } from 'server'
99
100
const SERVER_URL = import.meta.env.VITE_SERVER_URL || "http://localhost:3000"
101
102
const client = hcWithType(SERVER_URL);
103
104
type ResponseType = Awaited<ReturnType<typeof client.hello.$get>>;
105
106
function App() {
107
  const [data, setData] = useState<Awaited<ReturnType<ResponseType["json"]>> | undefined>()
108
109
  async function sendRequest() {
110
    try {
111
      const res = await client.hello.$get()
112
      if (!res.ok) {
113
        console.log("Error fetching data")
114
        return
115
      }
116
      const data = await res.json()
117
      setData(data)
118
    } catch (error) {
119
      console.log(error)
120
    }
121
  }
122
123
  return (
124
    <div className="max-w-xl mx-auto flex flex-col gap-6 items-center justify-center min-h-screen">
125
      <a href="https://github.com/stevedylandev/bhvr" target="_blank">
126
        <img
127
          src={beaver}
128
          className="w-16 h-16 cursor-pointer"
129
          alt="beaver logo"
130
        />
131
      </a>
132
      <h1 className="text-5xl font-black">bhvr</h1>
133
      <h2 className="text-2xl font-bold">Bun + Hono + Vite + React</h2>
134
      <p>A typesafe fullstack monorepo</p>
135
      <div className='flex items-center gap-4'>
136
        <Button
137
          onClick={sendRequest}
138
        >
139
          Call API
140
        </Button>
141
        <Button
142
          variant='secondary'
143
          asChild
144
        >
145
          <a target='_blank' href="https://bhvr.dev">
146
          Docs
147
          </a>
148
        </Button>
149
      </div>
150
        {data && (
151
          <pre className="bg-gray-100 p-4 rounded-md">
152
            <code>
153
            Message: {data.message} <br />
154
            Success: {data.success.toString()}
155
            </code>
156
          </pre>
157
        )}
158
    </div>
159
  )
160
}
161
162
export default App`;
163
164
export const defaultTemplate = `import { useState } from 'react'
165
import beaver from './assets/beaver.svg'
166
import { hcWithType } from 'server'
167
import './App.css'
168
169
const SERVER_URL = import.meta.env.VITE_SERVER_URL || "http://localhost:3000"
170
171
const client = hcWithType(SERVER_URL);
172
173
type ResponseType = Awaited<ReturnType<typeof client.hello.$get>>;
174
175
function App() {
176
  const [data, setData] = useState<Awaited<ReturnType<ResponseType["json"]>> | undefined>()
177
178
  async function sendRequest() {
179
    try {
180
      const res = await client.hello.$get()
181
      if (!res.ok) {
182
        console.log("Error fetching data")
183
        return
184
      }
185
      const data = await res.json()
186
      setData(data)
187
    } catch (error) {
188
      console.log(error)
189
    }
190
  }
191
192
  return (
193
    <>
194
      <div>
195
        <a href="https://github.com/stevedylandev/bhvr" target="_blank">
196
          <img src={beaver} className="logo" alt="beaver logo" />
197
        </a>
198
      </div>
199
      <h1>bhvr</h1>
200
      <h2>Bun + Hono + Vite + React</h2>
201
      <p>A typesafe fullstack monorepo</p>
202
      <div className="card">
203
        <div className='button-container'>
204
          <button onClick={sendRequest}>
205
            Call API
206
          </button>
207
          <a className='docs-link' target='_blank' href="https://bhvr.dev">Docs</a>
208
        </div>
209
        {data && (
210
          <pre className='response'>
211
            <code>
212
            Message: {data.message} <br />
213
            Success: {data.success.toString()}
214
            </code>
215
          </pre>
216
        )}
217
      </div>
218
    </>
219
  )
220
}
221
222
export default App`;