create-acrylblends 0.0.0-stage → 0.1.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.
Files changed (106) hide show
  1. package/README.md +61 -2
  2. package/bin/create-acrylblends.js +4 -0
  3. package/dist/index.d.ts +56 -0
  4. package/dist/index.js +274 -0
  5. package/dist/index.js.map +1 -0
  6. package/docs/framework/LICENSE +23 -0
  7. package/docs/framework/README.md +63 -0
  8. package/docs/framework/UPSTREAM.md +3 -0
  9. package/docs/framework/agents/index.md +24 -0
  10. package/docs/framework/api/acryl.md +259 -0
  11. package/docs/framework/api/context.md +527 -0
  12. package/docs/framework/api/exception.md +80 -0
  13. package/docs/framework/api/index.md +12 -0
  14. package/docs/framework/api/presets.md +65 -0
  15. package/docs/framework/api/request.md +396 -0
  16. package/docs/framework/api/routing.md +303 -0
  17. package/docs/framework/api/summary.md +140 -0
  18. package/docs/framework/concepts/benchmarks.md +26 -0
  19. package/docs/framework/concepts/developer-experience.md +5 -0
  20. package/docs/framework/concepts/middleware.md +21 -0
  21. package/docs/framework/concepts/motivation.md +14 -0
  22. package/docs/framework/concepts/routers.md +63 -0
  23. package/docs/framework/concepts/stacks.md +235 -0
  24. package/docs/framework/concepts/web-standard.md +37 -0
  25. package/docs/framework/cordis/acryl-in-a-cordis-app.md +146 -0
  26. package/docs/framework/cordis/concepts.md +105 -0
  27. package/docs/framework/cordis/middleware.md +8 -0
  28. package/docs/framework/cordis/migration-from-0-3.md +111 -0
  29. package/docs/framework/cordis/migration-from-0-4.md +14 -0
  30. package/docs/framework/cordis/migration-from-dsh-webserver.md +109 -0
  31. package/docs/framework/cordis/mounting.md +85 -0
  32. package/docs/framework/cordis/node-server.md +75 -0
  33. package/docs/framework/cordis/plugin-lifecycle.md +111 -0
  34. package/docs/framework/cordis/surfaces.md +71 -0
  35. package/docs/framework/cordis/transport-and-cancellation.md +57 -0
  36. package/docs/framework/cordis/validation.md +135 -0
  37. package/docs/framework/getting-started/basic.md +196 -0
  38. package/docs/framework/getting-started/bun.md +221 -0
  39. package/docs/framework/getting-started/nodejs.md +344 -0
  40. package/docs/framework/getting-started/other-runtimes.md +23 -0
  41. package/docs/framework/guides/best-practices.md +231 -0
  42. package/docs/framework/guides/create-acrylblends.md +79 -0
  43. package/docs/framework/guides/examples.md +3 -0
  44. package/docs/framework/guides/faq.md +19 -0
  45. package/docs/framework/guides/helpers.md +33 -0
  46. package/docs/framework/guides/jsx-dom.md +299 -0
  47. package/docs/framework/guides/jsx.md +440 -0
  48. package/docs/framework/guides/middleware.md +253 -0
  49. package/docs/framework/guides/rpc.md +886 -0
  50. package/docs/framework/guides/testing.md +133 -0
  51. package/docs/framework/guides/validation.md +383 -0
  52. package/docs/framework/helpers/accepts.md +60 -0
  53. package/docs/framework/helpers/adapter.md +87 -0
  54. package/docs/framework/helpers/conninfo.md +112 -0
  55. package/docs/framework/helpers/cookie.md +232 -0
  56. package/docs/framework/helpers/css.md +285 -0
  57. package/docs/framework/helpers/dev.md +67 -0
  58. package/docs/framework/helpers/factory.md +180 -0
  59. package/docs/framework/helpers/html.md +123 -0
  60. package/docs/framework/helpers/jwt.md +184 -0
  61. package/docs/framework/helpers/proxy.md +118 -0
  62. package/docs/framework/helpers/route.md +143 -0
  63. package/docs/framework/helpers/ssg.md +393 -0
  64. package/docs/framework/helpers/streaming.md +119 -0
  65. package/docs/framework/helpers/testing.md +101 -0
  66. package/docs/framework/helpers/websocket.md +215 -0
  67. package/docs/framework/middleware/builtin/basic-auth.md +165 -0
  68. package/docs/framework/middleware/builtin/bearer-auth.md +138 -0
  69. package/docs/framework/middleware/builtin/body-limit.md +71 -0
  70. package/docs/framework/middleware/builtin/cache.md +122 -0
  71. package/docs/framework/middleware/builtin/combine.md +94 -0
  72. package/docs/framework/middleware/builtin/compress.md +59 -0
  73. package/docs/framework/middleware/builtin/context-storage.md +78 -0
  74. package/docs/framework/middleware/builtin/cors.md +135 -0
  75. package/docs/framework/middleware/builtin/csrf.md +105 -0
  76. package/docs/framework/middleware/builtin/etag.md +54 -0
  77. package/docs/framework/middleware/builtin/ip-restriction.md +69 -0
  78. package/docs/framework/middleware/builtin/jsx-renderer.md +286 -0
  79. package/docs/framework/middleware/builtin/jwk.md +192 -0
  80. package/docs/framework/middleware/builtin/jwt.md +145 -0
  81. package/docs/framework/middleware/builtin/language.md +232 -0
  82. package/docs/framework/middleware/builtin/logger.md +74 -0
  83. package/docs/framework/middleware/builtin/loopback-only.md +31 -0
  84. package/docs/framework/middleware/builtin/method-not-allowed.md +48 -0
  85. package/docs/framework/middleware/builtin/method-override.md +80 -0
  86. package/docs/framework/middleware/builtin/mount.md +106 -0
  87. package/docs/framework/middleware/builtin/pretty-json.md +53 -0
  88. package/docs/framework/middleware/builtin/request-id.md +94 -0
  89. package/docs/framework/middleware/builtin/secure-headers.md +247 -0
  90. package/docs/framework/middleware/builtin/timeout.md +98 -0
  91. package/docs/framework/middleware/builtin/timing.md +101 -0
  92. package/docs/framework/middleware/builtin/trailing-slash.md +74 -0
  93. package/docs/framework/middleware/third-party.md +4 -0
  94. package/docs/framework/upstream.json +9 -0
  95. package/package.json +47 -3
  96. package/templates/bun/gitignore +1 -0
  97. package/templates/bun/package.json +19 -0
  98. package/templates/bun/src/index.ts +7 -0
  99. package/templates/bun/test/app.test.ts +8 -0
  100. package/templates/bun/tsconfig.json +15 -0
  101. package/templates/nodejs/gitignore +1 -0
  102. package/templates/nodejs/package.json +22 -0
  103. package/templates/nodejs/src/app.ts +7 -0
  104. package/templates/nodejs/src/index.ts +9 -0
  105. package/templates/nodejs/test/app.test.ts +9 -0
  106. package/templates/nodejs/tsconfig.json +16 -0
@@ -0,0 +1,259 @@
1
+ # App - Acryl
2
+
3
+ `Acryl` is the primary object.
4
+ It will be imported first and used until the end.
5
+
6
+ ```ts twoslash
7
+ import { Acryl } from 'acrylblends'
8
+
9
+ const app = new Acryl()
10
+ //...
11
+
12
+ export default app // for Cloudflare Workers or Bun
13
+ ```
14
+
15
+ ## Methods
16
+
17
+ An instance of `Acryl` has the following methods.
18
+
19
+ - app.**HTTP_METHOD**(\[path,\]handler|middleware...)
20
+ - app.**all**(\[path,\]handler|middleware...)
21
+ - app.**on**(method|method[], path|path[], handler|middleware...)
22
+ - app.**use**(\[path,\]middleware)
23
+ - app.**route**(path, \[app\])
24
+ - app.**basePath**(path)
25
+ - app.**notFound**(handler)
26
+ - app.**onError**(err, handler)
27
+ - app.**mount**(path, anotherApp, \[options\])
28
+ - app.**fire**()
29
+ - app.**fetch**(request, env, event)
30
+ - app.**request**(path, options)
31
+
32
+ The first part of them is used for routing, please refer to the [routing section](routing.md).
33
+
34
+ ## Not Found
35
+
36
+ `app.notFound` allows you to customize a Not Found Response.
37
+
38
+ ```ts twoslash
39
+ import { Acryl } from 'acrylblends'
40
+ const app = new Acryl()
41
+ // ---cut---
42
+ app.notFound((c) => {
43
+ return c.text('Custom 404 Message', 404)
44
+ })
45
+ ```
46
+
47
+ > **Warning.** The `notFound` method is only called from the top-level app. For more information, see this issue.
48
+
49
+ ## Error Handling
50
+
51
+ `app.onError` allows you to handle uncaught errors and return a custom Response.
52
+
53
+ ```ts twoslash
54
+ import { Acryl } from 'acrylblends'
55
+ const app = new Acryl()
56
+ // ---cut---
57
+ app.onError((err, c) => {
58
+ console.error(`${err}`)
59
+ return c.text('Custom Error Message', 500)
60
+ })
61
+ ```
62
+
63
+ > **Note.** If both a parent app and its routes have `onError` handlers, the route-level handlers get priority.
64
+
65
+ ## fire()
66
+
67
+ > **Warning.** **`app.fire()` is deprecated**. Use `fire()` from `acrylblends/service-worker` instead. See the Service Worker documentation for details.
68
+
69
+ `app.fire()` automatically adds a global `fetch` event listener.
70
+
71
+ This can be useful for environments that adhere to the [Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API), such as [non-ES module Cloudflare Workers](https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/).
72
+
73
+ `app.fire()` executes the following for you:
74
+
75
+ ```ts
76
+ addEventListener('fetch', (event: FetchEventLike): void => {
77
+ event.respondWith(this.dispatch(...))
78
+ })
79
+ ```
80
+
81
+ ## fetch()
82
+
83
+ `app.fetch` will be the entry point of your application.
84
+
85
+ For Cloudflare Workers, you can use the following:
86
+
87
+ ```ts twoslash
88
+ import { Acryl } from 'acrylblends'
89
+ const app = new Acryl()
90
+ type Env = any
91
+ type ExecutionContext = any
92
+ // ---cut---
93
+ export default {
94
+ fetch(request: Request, env: Env, ctx: ExecutionContext) {
95
+ return app.fetch(request, env, ctx)
96
+ },
97
+ }
98
+ ```
99
+
100
+ or just do:
101
+
102
+ ```ts twoslash
103
+ import { Acryl } from 'acrylblends'
104
+ const app = new Acryl()
105
+ // ---cut---
106
+ export default app
107
+ ```
108
+
109
+ Bun:
110
+
111
+ <!-- prettier-ignore -->
112
+ ```ts
113
+ export default {
114
+ port: 3000,
115
+ fetch: app.fetch,
116
+ }
117
+ ```
118
+
119
+ ## request()
120
+
121
+ `request` is a useful method for testing.
122
+
123
+ You can pass a URL or pathname to send a GET request.
124
+ `app` will return a `Response` object.
125
+
126
+ ```ts twoslash
127
+ import { Acryl } from 'acrylblends'
128
+ const app = new Acryl()
129
+ declare const test: (name: string, fn: () => void) => void
130
+ declare const expect: (value: any) => any
131
+ // ---cut---
132
+ test('GET /hello is ok', async () => {
133
+ const res = await app.request('/hello')
134
+ expect(res.status).toBe(200)
135
+ })
136
+ ```
137
+
138
+ You can also pass a `Request` object:
139
+
140
+ ```ts twoslash
141
+ import { Acryl } from 'acrylblends'
142
+ const app = new Acryl()
143
+ declare const test: (name: string, fn: () => void) => void
144
+ declare const expect: (value: any) => any
145
+ // ---cut---
146
+ test('POST /message is ok', async () => {
147
+ const req = new Request('Hello!', {
148
+ method: 'POST',
149
+ })
150
+ const res = await app.request(req)
151
+ expect(res.status).toBe(201)
152
+ })
153
+ ```
154
+
155
+ ## mount()
156
+
157
+ > **Warning.** **`app.mount()` is deprecated**. Use the [Mount Middleware](../middleware/builtin/mount.md) instead.
158
+
159
+ The `mount()` allows you to mount applications built with other frameworks into your Acryl application.
160
+
161
+ ```ts
162
+ import { Router as IttyRouter } from 'itty-router'
163
+ import { Acryl } from 'acrylblends'
164
+
165
+ // Create itty-router application
166
+ const ittyRouter = IttyRouter()
167
+
168
+ // Handle `GET /itty-router/hello`
169
+ ittyRouter.get('/hello', () => new Response('Hello from itty-router'))
170
+
171
+ // Acryl application
172
+ const app = new Acryl()
173
+
174
+ // Mount!
175
+ app.mount('/itty-router', ittyRouter.handle)
176
+ ```
177
+
178
+ By default, `mount()` passes a new `Request` with the mount path removed from its URL. Provide a `replaceRequest` function to control which `Request` is passed to the mounted application:
179
+
180
+ ```ts twoslash
181
+ import { Acryl } from 'acrylblends'
182
+ const app = new Acryl()
183
+ const handler = (request: Request) => new Response(request.url)
184
+ // ---cut---
185
+ app.mount('/app', handler, {
186
+ replaceRequest: (originalRequest) => originalRequest,
187
+ })
188
+ ```
189
+
190
+ To pass the original `Request` unchanged, set `replaceRequest` to `false` as a shorthand for the function above:
191
+
192
+ ```ts twoslash
193
+ import { Acryl } from 'acrylblends'
194
+ const app = new Acryl()
195
+ const handler = (request: Request) => new Response(request.url)
196
+ // ---cut---
197
+ app.mount('/app', handler, {
198
+ replaceRequest: false,
199
+ })
200
+ ```
201
+
202
+ ## strict mode
203
+
204
+ Strict mode defaults to `true` and distinguishes the following routes.
205
+
206
+ - `/hello`
207
+ - `/hello/`
208
+
209
+ `app.get('/hello')` will not match `GET /hello/`.
210
+
211
+ By setting strict mode to `false`, both paths will be treated equally.
212
+
213
+ ```ts twoslash
214
+ import { Acryl } from 'acrylblends'
215
+ // ---cut---
216
+ const app = new Acryl({ strict: false })
217
+ ```
218
+
219
+ ## router option
220
+
221
+ The `router` option specifies which router to use. The default router is `SmartRouter`. If you want to use `RegExpRouter`, pass it to a new `Acryl` instance:
222
+
223
+ ```ts twoslash
224
+ import { Acryl } from 'acrylblends'
225
+ // ---cut---
226
+ import { RegExpRouter } from 'acrylblends/router/reg-exp-router'
227
+
228
+ const app = new Acryl({ router: new RegExpRouter() })
229
+ ```
230
+
231
+ ## Generics
232
+
233
+ You can pass Generics to specify the types of Cloudflare Workers Bindings and variables used in `c.set`/`c.get`.
234
+
235
+ ```ts twoslash
236
+ import { Acryl } from 'acrylblends'
237
+ type User = any
238
+ declare const user: User
239
+ // ---cut---
240
+ type Bindings = {
241
+ TOKEN: string
242
+ }
243
+
244
+ type Variables = {
245
+ user: User
246
+ }
247
+
248
+ const app = new Acryl<{
249
+ Bindings: Bindings
250
+ Variables: Variables
251
+ }>()
252
+
253
+ app.use('/auth/*', async (c, next) => {
254
+ const token = c.env.TOKEN // token is `string`
255
+ // ...
256
+ c.set('user', user) // user should be `User`
257
+ await next()
258
+ })
259
+ ```