itty-router 5.0.0 → 5.0.2

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 (187) hide show
  1. package/AutoRouter.d.ts +2 -0
  2. package/{dist/AutoRouter.js.map → AutoRouter.js.map} +1 -1
  3. package/{dist/AutoRouter.mjs.map → AutoRouter.mjs.map} +1 -1
  4. package/IttyRouter.d.ts +2 -0
  5. package/{dist/IttyRouter.js.map → IttyRouter.js.map} +1 -1
  6. package/{dist/IttyRouter.mjs.map → IttyRouter.mjs.map} +1 -1
  7. package/README.md +2 -2
  8. package/Router.d.ts +2 -0
  9. package/{dist/Router.js.map → Router.js.map} +1 -1
  10. package/{dist/Router.mjs.map → Router.mjs.map} +1 -1
  11. package/{dist/index.js.map → index.js.map} +1 -1
  12. package/{dist/index.mjs.map → index.mjs.map} +1 -1
  13. package/package.json +1 -1
  14. package/types/AutoRouterType.d.ts +8 -0
  15. package/{dist/types.d.ts → types.d.ts} +1 -0
  16. package/.eslintrc +0 -28
  17. package/.github/FUNDING.yml +0 -12
  18. package/.github/ISSUE_TEMPLATE/issue_template.md +0 -52
  19. package/.github/ISSUE_TEMPLATE/question.md +0 -16
  20. package/.github/pull_request_template.md +0 -17
  21. package/.github/workflows/coveralls.yml +0 -20
  22. package/.github/workflows/lint.yml +0 -21
  23. package/.github/workflows/verify.yml +0 -22
  24. package/.nyc_output/17f58b46-7631-4765-9ac8-c567ad984881.json +0 -1
  25. package/.nyc_output/37b99878-3734-4671-b7ad-9716c334e6da.json +0 -1
  26. package/.nyc_output/522b1502-594a-4786-90b9-0b52ce677765.json +0 -1
  27. package/.nyc_output/5ad76e3a-7bd8-4131-b761-9eda43350422.json +0 -1
  28. package/.nyc_output/8518a247-c46c-4288-b558-73a9da27d039.json +0 -1
  29. package/.nyc_output/8cdba743-d03a-4ae0-80a9-be1698a6621a.json +0 -1
  30. package/.nyc_output/a5a529a4-8c20-4fc6-8e19-68f85c854251.json +0 -1
  31. package/.nyc_output/a9e151d4-d47a-4440-885b-1ab50a6ac1a0.json +0 -1
  32. package/.nyc_output/bfc97de7-26ae-4d9c-a9a6-2021b608d83b.json +0 -1
  33. package/.nyc_output/c26290ed-5973-4907-a3f1-29678d97224a.json +0 -1
  34. package/.nyc_output/c6b55c94-7cf5-4f08-86e7-ce60b0d7064c.json +0 -1
  35. package/.nyc_output/e2b16e51-822d-4c53-ad21-bb54668ec04d.json +0 -1
  36. package/.nyc_output/e548baf2-1d0a-4dc2-abe4-4a52ae914581.json +0 -1
  37. package/.nyc_output/processinfo/17f58b46-7631-4765-9ac8-c567ad984881.json +0 -16
  38. package/.nyc_output/processinfo/37b99878-3734-4671-b7ad-9716c334e6da.json +0 -19
  39. package/.nyc_output/processinfo/522b1502-594a-4786-90b9-0b52ce677765.json +0 -16
  40. package/.nyc_output/processinfo/5ad76e3a-7bd8-4131-b761-9eda43350422.json +0 -16
  41. package/.nyc_output/processinfo/8518a247-c46c-4288-b558-73a9da27d039.json +0 -17
  42. package/.nyc_output/processinfo/8cdba743-d03a-4ae0-80a9-be1698a6621a.json +0 -16
  43. package/.nyc_output/processinfo/a5a529a4-8c20-4fc6-8e19-68f85c854251.json +0 -17
  44. package/.nyc_output/processinfo/a9e151d4-d47a-4440-885b-1ab50a6ac1a0.json +0 -20
  45. package/.nyc_output/processinfo/bfc97de7-26ae-4d9c-a9a6-2021b608d83b.json +0 -16
  46. package/.nyc_output/processinfo/c26290ed-5973-4907-a3f1-29678d97224a.json +0 -16
  47. package/.nyc_output/processinfo/c6b55c94-7cf5-4f08-86e7-ce60b0d7064c.json +0 -16
  48. package/.nyc_output/processinfo/e2b16e51-822d-4c53-ad21-bb54668ec04d.json +0 -17
  49. package/.nyc_output/processinfo/e548baf2-1d0a-4dc2-abe4-4a52ae914581.json +0 -16
  50. package/.nyc_output/processinfo/index.json +0 -70
  51. package/.prettierignore +0 -1
  52. package/.prettierrc +0 -4
  53. package/CHANGELOG.md +0 -78
  54. package/CODE_OF_CONDUCT.md +0 -128
  55. package/CONTRIBUTING.md +0 -92
  56. package/dist/AutoRouter.d.ts +0 -2
  57. package/dist/IttyRouter.d.ts +0 -2
  58. package/dist/LICENSE +0 -21
  59. package/dist/Router.d.ts +0 -2
  60. package/example/bun-autorouter-advanced.ts +0 -35
  61. package/example/bun-autorouter.ts +0 -30
  62. package/example/bun-router.ts +0 -26
  63. package/example/bun.ts +0 -15
  64. package/example/cors-types.ts +0 -92
  65. package/example/custom-router-props.ts +0 -11
  66. package/example/index.ts +0 -57
  67. package/example/node-import.js +0 -20
  68. package/example/node-require.js +0 -20
  69. package/example/request-types.ts +0 -101
  70. package/example/runtimes/bun.ts +0 -15
  71. package/example/runtimes/express.js +0 -16
  72. package/example/runtimes/node.js +0 -20
  73. package/example/types/content.ts +0 -22
  74. package/example/types/extra-props.ts +0 -13
  75. package/example/types/global-and-route-level-request.ts +0 -17
  76. package/example/types/global-request.ts +0 -16
  77. package/example/types/middleware.ts +0 -41
  78. package/example/types/requestlike.ts +0 -11
  79. package/example/types/route-level-request.ts +0 -16
  80. package/example/types/router.ts +0 -8
  81. package/lib/index.ts +0 -72
  82. package/rollup.config.mjs +0 -68
  83. package/tsconfig.json +0 -28
  84. package/vitest.config.ts +0 -20
  85. package/yarn-error.log +0 -4335
  86. /package/{dist/AutoRouter.js → AutoRouter.js} +0 -0
  87. /package/{dist/AutoRouter.mjs → AutoRouter.mjs} +0 -0
  88. /package/{dist/IttyRouter.js → IttyRouter.js} +0 -0
  89. /package/{dist/IttyRouter.mjs → IttyRouter.mjs} +0 -0
  90. /package/{dist/Router.js → Router.js} +0 -0
  91. /package/{dist/Router.mjs → Router.mjs} +0 -0
  92. /package/{dist/StatusError.d.ts → StatusError.d.ts} +0 -0
  93. /package/{dist/StatusError.js → StatusError.js} +0 -0
  94. /package/{dist/StatusError.js.map → StatusError.js.map} +0 -0
  95. /package/{dist/StatusError.mjs → StatusError.mjs} +0 -0
  96. /package/{dist/StatusError.mjs.map → StatusError.mjs.map} +0 -0
  97. /package/{dist/cors.d.ts → cors.d.ts} +0 -0
  98. /package/{dist/cors.js → cors.js} +0 -0
  99. /package/{dist/cors.js.map → cors.js.map} +0 -0
  100. /package/{dist/cors.mjs → cors.mjs} +0 -0
  101. /package/{dist/cors.mjs.map → cors.mjs.map} +0 -0
  102. /package/{dist/createResponse.d.ts → createResponse.d.ts} +0 -0
  103. /package/{dist/createResponse.js → createResponse.js} +0 -0
  104. /package/{dist/createResponse.js.map → createResponse.js.map} +0 -0
  105. /package/{dist/createResponse.mjs → createResponse.mjs} +0 -0
  106. /package/{dist/createResponse.mjs.map → createResponse.mjs.map} +0 -0
  107. /package/{dist/error.d.ts → error.d.ts} +0 -0
  108. /package/{dist/error.js → error.js} +0 -0
  109. /package/{dist/error.js.map → error.js.map} +0 -0
  110. /package/{dist/error.mjs → error.mjs} +0 -0
  111. /package/{dist/error.mjs.map → error.mjs.map} +0 -0
  112. /package/{dist/html.d.ts → html.d.ts} +0 -0
  113. /package/{dist/html.js → html.js} +0 -0
  114. /package/{dist/html.js.map → html.js.map} +0 -0
  115. /package/{dist/html.mjs → html.mjs} +0 -0
  116. /package/{dist/html.mjs.map → html.mjs.map} +0 -0
  117. /package/{dist/index.d.ts → index.d.ts} +0 -0
  118. /package/{dist/index.js → index.js} +0 -0
  119. /package/{dist/index.mjs → index.mjs} +0 -0
  120. /package/{dist/jpeg.d.ts → jpeg.d.ts} +0 -0
  121. /package/{dist/jpeg.js → jpeg.js} +0 -0
  122. /package/{dist/jpeg.js.map → jpeg.js.map} +0 -0
  123. /package/{dist/jpeg.mjs → jpeg.mjs} +0 -0
  124. /package/{dist/jpeg.mjs.map → jpeg.mjs.map} +0 -0
  125. /package/{dist/json.d.ts → json.d.ts} +0 -0
  126. /package/{dist/json.js → json.js} +0 -0
  127. /package/{dist/json.js.map → json.js.map} +0 -0
  128. /package/{dist/json.mjs → json.mjs} +0 -0
  129. /package/{dist/json.mjs.map → json.mjs.map} +0 -0
  130. /package/{dist/png.d.ts → png.d.ts} +0 -0
  131. /package/{dist/png.js → png.js} +0 -0
  132. /package/{dist/png.js.map → png.js.map} +0 -0
  133. /package/{dist/png.mjs → png.mjs} +0 -0
  134. /package/{dist/png.mjs.map → png.mjs.map} +0 -0
  135. /package/{dist/status.d.ts → status.d.ts} +0 -0
  136. /package/{dist/status.js → status.js} +0 -0
  137. /package/{dist/status.js.map → status.js.map} +0 -0
  138. /package/{dist/status.mjs → status.mjs} +0 -0
  139. /package/{dist/status.mjs.map → status.mjs.map} +0 -0
  140. /package/{dist/text.d.ts → text.d.ts} +0 -0
  141. /package/{dist/text.js → text.js} +0 -0
  142. /package/{dist/text.js.map → text.js.map} +0 -0
  143. /package/{dist/text.mjs → text.mjs} +0 -0
  144. /package/{dist/text.mjs.map → text.mjs.map} +0 -0
  145. /package/{dist/types → types}/AutoRouterOptions.d.ts +0 -0
  146. /package/{dist/types → types}/CustomRoutes.d.ts +0 -0
  147. /package/{dist/types → types}/ErrorFormatter.d.ts +0 -0
  148. /package/{dist/types → types}/ErrorHandler.d.ts +0 -0
  149. /package/{dist/types → types}/GenericTraps.d.ts +0 -0
  150. /package/{dist/types → types}/HasContent.d.ts +0 -0
  151. /package/{dist/types → types}/IRequest.d.ts +0 -0
  152. /package/{dist/types → types}/IRequestStrict.d.ts +0 -0
  153. /package/{dist/types → types}/IttyRouterOptions.d.ts +0 -0
  154. /package/{dist/types → types}/IttyRouterType.d.ts +0 -0
  155. /package/{dist/types → types}/RequestHandler.d.ts +0 -0
  156. /package/{dist/types → types}/RequestLike.d.ts +0 -0
  157. /package/{dist/types → types}/ResponseFormatter.d.ts +0 -0
  158. /package/{dist/types → types}/ResponseHandler.d.ts +0 -0
  159. /package/{dist/types → types}/Route.d.ts +0 -0
  160. /package/{dist/types → types}/RouteEntry.d.ts +0 -0
  161. /package/{dist/types → types}/RouterOptions.d.ts +0 -0
  162. /package/{dist/types → types}/RouterType.d.ts +0 -0
  163. /package/{dist/webp.d.ts → webp.d.ts} +0 -0
  164. /package/{dist/webp.js → webp.js} +0 -0
  165. /package/{dist/webp.js.map → webp.js.map} +0 -0
  166. /package/{dist/webp.mjs → webp.mjs} +0 -0
  167. /package/{dist/webp.mjs.map → webp.mjs.map} +0 -0
  168. /package/{dist/websocket.d.ts → websocket.d.ts} +0 -0
  169. /package/{dist/websocket.js → websocket.js} +0 -0
  170. /package/{dist/websocket.js.map → websocket.js.map} +0 -0
  171. /package/{dist/websocket.mjs → websocket.mjs} +0 -0
  172. /package/{dist/websocket.mjs.map → websocket.mjs.map} +0 -0
  173. /package/{dist/withContent.d.ts → withContent.d.ts} +0 -0
  174. /package/{dist/withContent.js → withContent.js} +0 -0
  175. /package/{dist/withContent.js.map → withContent.js.map} +0 -0
  176. /package/{dist/withContent.mjs → withContent.mjs} +0 -0
  177. /package/{dist/withContent.mjs.map → withContent.mjs.map} +0 -0
  178. /package/{dist/withCookies.d.ts → withCookies.d.ts} +0 -0
  179. /package/{dist/withCookies.js → withCookies.js} +0 -0
  180. /package/{dist/withCookies.js.map → withCookies.js.map} +0 -0
  181. /package/{dist/withCookies.mjs → withCookies.mjs} +0 -0
  182. /package/{dist/withCookies.mjs.map → withCookies.mjs.map} +0 -0
  183. /package/{dist/withParams.d.ts → withParams.d.ts} +0 -0
  184. /package/{dist/withParams.js → withParams.js} +0 -0
  185. /package/{dist/withParams.js.map → withParams.js.map} +0 -0
  186. /package/{dist/withParams.mjs → withParams.mjs} +0 -0
  187. /package/{dist/withParams.mjs.map → withParams.mjs.map} +0 -0
@@ -1,92 +0,0 @@
1
- import { IRequest, IRequestStrict, Router, createCors } from '../src'
2
-
3
- type FooRequest = {
4
- foo: string
5
- } & IRequest
6
-
7
- // extends IRequestStrict, meaning no undefined attributes off Request
8
- type BarRequest = {
9
- bar: number
10
- } & IRequestStrict
11
-
12
- type Env = {
13
- KV: string
14
- }
15
-
16
- type CF = [env: Env, ctx: ExecutionContext]
17
-
18
- // this router defines a global signature of <BarRequest, CF>
19
- const custom = Router<BarRequest, CF>()
20
-
21
- const { preflight, corsify } = createCors()
22
-
23
- custom
24
- .all('*', preflight)
25
-
26
- // should not be able to access request.foo
27
- .get('/foo/:bar', (request, env, ctx) => {
28
- request.bar
29
- request.foo
30
- env.KV
31
- ctx.waitUntil
32
- })
33
-
34
- const router = Router({ base: '/' })
35
-
36
- router
37
- .all('*', preflight)
38
-
39
- // call custom HTTP method
40
- .puppy('/cat', (request) => {
41
- // supports standard Request by default
42
- request.arrayBuffer()
43
- })
44
-
45
- // standard request
46
- .get('*', (request, env: Env, ctx: ExecutionContext) => {
47
- request.url
48
- env.KV
49
- ctx.waitUntil
50
- })
51
-
52
- // custom request from handler argument
53
- .get('*', (request: FooRequest) => {
54
- request.foo
55
- })
56
-
57
- // custom request from handler argument
58
- .get('*', (request: BarRequest) => {
59
- request.bar
60
- })
61
-
62
- // custom request from handler argument
63
- .get<BarRequest>('*', (request) => {
64
- request.bar
65
- })
66
-
67
- // custom request from Route
68
- .get<BarRequest, CF>('*', ({ bar, foo }, env, ctx) => {
69
- env.KV
70
- ctx.waitUntil
71
- })
72
-
73
- // how to return another custom router method
74
- .get<FooRequest, CF>('*', (request) => {
75
- request.foo
76
- })
77
-
78
- // call custom HTTP method again (to ensure preserved through chain)
79
- .puppy('/cat', () => {})
80
-
81
- // undeclared HTTP method?
82
- .kitten<FooRequest>('/', (request) => {
83
- request.foo
84
- })
85
-
86
- .handle<CF>({ method: 'GET', url: 'foo.bar' }, {}, 'asd')
87
-
88
- type CFfetch = [request: Request, env: Env, ctx: ExecutionContext]
89
-
90
- export default {
91
- fetch: (...args: CFfetch) => router.handle(...args),
92
- }
@@ -1,11 +0,0 @@
1
- import { Router, error, json, text, withParams } from '../src/index'
2
-
3
- const router = Router({ port: 8080 })
4
-
5
- router
6
- .all('*', withParams)
7
- .get('/test', () => text('Success!'))
8
- .get('/foo/:bar/:baz?', ({ bar, baz }) => json({ bar, baz }))
9
- .all('*', () => error(404, 'Are you sure about that?'))
10
-
11
- export default router
package/example/index.ts DELETED
@@ -1,57 +0,0 @@
1
- import { withParams } from 'itty-router'
2
- import {
3
- Router, // the router itself
4
- IRequest, // lightweight/generic Request type
5
- RouterType, // generic Router type
6
- Route, // generic Route type
7
- createCors,
8
- json,
9
- error,
10
- } from '../src'
11
-
12
- const myCustomMiddleware = (request: IRequest) => {
13
- if (true) {
14
- return false
15
- }
16
- }
17
-
18
- // declare a custom Request type to allow request injection from middleware
19
- type RequestWithAuthors = {
20
- authors?: string[]
21
- } & IRequest
22
-
23
- // middleware that modifies the request
24
- const withAuthors = (request: IRequest) => {
25
- request.authors = ['foo', 'bar']
26
- }
27
-
28
- const { corsify, preflight } = createCors()
29
-
30
- const router = Router({ base: '/' })
31
-
32
- router
33
- .all('*', preflight, withParams)
34
- .get('/authors', withAuthors, (request: RequestWithAuthors) => {
35
- return request.authors?.[0]
36
- })
37
- .puppy('/:name', (request) => {
38
- const name = request.params.name
39
- const foo = request.query.foo
40
- })
41
- .get('/whatever/:foo/:bar',
42
- myCustomMiddleware,
43
- myCustomMiddleware,
44
- ({ foo, bar }) => ({ foo, bar })
45
- )
46
- .all('*', () => error(404))
47
-
48
- // CF ES6 module syntax
49
- export default {
50
- fetch: (request: IRequest, env: object, context: object) =>
51
- router.handle(request, env, context).then(json).catch(error).then(corsify),
52
- }
53
-
54
- // test traditional eventListener Worker syntax
55
- addEventListener('fetch', (event: FetchEvent) => {
56
- event.respondWith(router.handle(event.request))
57
- })
@@ -1,20 +0,0 @@
1
- import { createServerAdapter } from '@whatwg-node/server'
2
- import { createServer } from 'http'
3
- import 'isomorphic-fetch'
4
- import { Router, error, json } from '../dist/index.js'
5
-
6
- const router = Router()
7
-
8
- router.get('/', () => 'Success!').all('*', () => error(404))
9
-
10
- const ittyServer = createServerAdapter((...args) =>
11
- router
12
- .handle(...args)
13
- .then(json)
14
- .catch(error)
15
- )
16
-
17
- // Then use it in any environment
18
- const httpServer = createServer(ittyServer)
19
- httpServer.listen(3001)
20
- console.log('listening at https://localhost:3001')
@@ -1,20 +0,0 @@
1
- const { createServerAdapter } = require('@whatwg-node/server')
2
- const { createServer } = require('http')
3
- require('isomorphic-fetch')
4
- const { Router, error, json } = require('../dist/index.js')
5
-
6
- const router = Router()
7
-
8
- router.get('/', () => 'Success!').all('*', () => error(404))
9
-
10
- const ittyServer = createServerAdapter((...args) =>
11
- router
12
- .handle(...args)
13
- .then(json)
14
- .catch(error)
15
- )
16
-
17
- // Then use it in any environment
18
- const httpServer = createServer(ittyServer)
19
- httpServer.listen(3001)
20
- console.log('listening at https://localhost:3001')
@@ -1,101 +0,0 @@
1
- import { IRequest, IRequestStrict, IttyRouter } from '../src/IttyRouter'
2
-
3
- type FooRequest = {
4
- foo: string
5
- } & IRequest
6
-
7
- // extends IRequestStrict, meaning no undefined attributes off Request
8
- type BarRequest = {
9
- bar: number
10
- } & IRequestStrict
11
-
12
- type Env = {
13
- KV: string
14
- }
15
-
16
- type CF = [env: Env, ctx: ExecutionContext]
17
-
18
- // this router defines a global signature of <BarRequest, CF>
19
- const custom = IttyRouter<BarRequest, CF>()
20
-
21
- custom
22
- .get('/', ({ bar, json }) => {
23
- console.log('bar', bar)
24
- })
25
-
26
- // should not be able to access request.foo
27
- .get('/foo/:bar', (request, env, ctx) => {
28
- request.params
29
- request.bar
30
- request.foo
31
- env.KV
32
- ctx.waitUntil
33
- })
34
-
35
- .get<FooRequest>('/', (request) => {
36
- request.foo // found in FooRequest override
37
- })
38
-
39
- const router = IttyRouter({ base: '/' })
40
-
41
- router
42
- // call custom HTTP method
43
- .puppy('/cat/:id', (request) => {
44
- // supports standard Request by default
45
- request.headers // valid
46
- request.foo // valid (generic traps)
47
- })
48
- .puppy<IRequestStrict>('/cat/:id', (request) => {
49
- // supports standard Request by default
50
- request.headers // valid
51
- request.foo // invalid
52
- })
53
-
54
- // standard request
55
- .get('*', (request, env: Env, ctx: ExecutionContext) => {
56
- request.url
57
- env.KV
58
- ctx.waitUntil
59
- })
60
-
61
- // custom request from handler argument
62
- .get('*', (request: FooRequest) => {
63
- request.foo
64
- })
65
-
66
- // custom request from handler argument
67
- .get('*', (request: BarRequest) => {
68
- request.bar
69
- })
70
-
71
- // custom request from handler argument
72
- .get<BarRequest>('*', (request) => {
73
- request.bar
74
- })
75
-
76
- // custom request from Route
77
- .get<BarRequest, CF>('*', ({ bar, foo }, env, ctx) => {
78
- env.KV
79
- ctx.waitUntil
80
- })
81
-
82
- // how to return another custom router method
83
- .get<FooRequest, CF>('*', (request) => {
84
- request.foo
85
- })
86
-
87
- // call custom HTTP method again (to ensure preserved through chain)
88
- .puppy('/cat', () => {})
89
-
90
- // undeclared HTTP method?
91
- .kitten<FooRequest>('/', (request) => {
92
- request.foo
93
- })
94
-
95
- .handle<CF>({ method: 'GET', url: 'foo.bar' }, {}, 'asd')
96
-
97
- type CFfetch = [request: Request, env: Env, ctx: ExecutionContext]
98
-
99
- export default {
100
- fetch: (...args: CFfetch) => router.handle(...args),
101
- }
@@ -1,15 +0,0 @@
1
- import { Router, error, json, withParams } from 'itty-router'
2
-
3
- const router = Router()
4
-
5
- router
6
- .all('*', withParams)
7
- .get('/test', () => 'Success!')
8
- .get('/foo/:bar/:baz?', ({ bar, baz }) => ({ bar, baz }))
9
- .all('*', () => error(404))
10
-
11
- export default {
12
- port: 3001,
13
- fetch: (request, env, ctx) =>
14
- router.handle(request, env, ctx).then(json).catch(error),
15
- }
@@ -1,16 +0,0 @@
1
- import express from 'express'
2
- import { Router, error, json } from 'itty-router'
3
- import 'isomorphic-fetch'
4
-
5
- const app = express()
6
-
7
- const router = Router()
8
-
9
- router.get('/', () => 'Success!').all('*', () => error(404))
10
-
11
- const handle = (request) => router.handle(request).then(json).catch(error)
12
-
13
- app.use(handle)
14
-
15
- app.listen(3001)
16
- console.log('listening at https://localhost:3001')
@@ -1,20 +0,0 @@
1
- import { createServerAdapter } from '@whatwg-node/server'
2
- import { createServer } from 'http'
3
- import 'isomorphic-fetch'
4
- import { Router, error, json } from '../dist/index.js'
5
-
6
- const router = Router()
7
-
8
- router.get('/', () => 'Success!').all('*', () => error(404))
9
-
10
- const ittyServer = createServerAdapter((...args) =>
11
- router
12
- .handle(...args)
13
- .then(json)
14
- .catch(error)
15
- )
16
-
17
- // Then use it in any environment
18
- const httpServer = createServer(ittyServer)
19
- httpServer.listen(3001)
20
- console.log('listening at https://localhost:3001')
@@ -1,22 +0,0 @@
1
- import { IRequestStrict, IRequest, RequestHandler } from 'types'
2
- import { IttyRouter } from 'IttyRouter'
3
- import { withContent } from 'withContent'
4
- import { HasContent } from 'types'
5
-
6
- type User = {
7
- user: string
8
- }
9
-
10
- const router = IttyRouter()
11
-
12
- router
13
- // upstream request sees the request as IRequest (default), so anything goes
14
- .post<HasContent<User>>('/', withContent, ({ content }) => {
15
- content.user
16
- })
17
-
18
- .get('/pollution', (request) => {
19
- request.content.user
20
- })
21
-
22
- export default router
@@ -1,13 +0,0 @@
1
- import { IRequestStrict, IRequest, RequestHandler } from 'types'
2
- import { IttyRouter } from 'IttyRouter'
3
- import { withContent } from 'withContent'
4
- import { HasContent } from 'types'
5
-
6
- const router = IttyRouter({
7
- port: 3000,
8
- })
9
-
10
- router.port = 4000
11
- router.port.toLowerCase()
12
- router.puppy() // should not be typed... RIP
13
-
@@ -1,17 +0,0 @@
1
- import { IRequestStrict } from 'IttyRouter'
2
- import { Router } from 'Router'
3
-
4
- const router = Router<IRequestStrict>()
5
-
6
- type FooRequest = {
7
- foo: string
8
- } & IRequestStrict
9
-
10
- router
11
- .get('/basic', () => new Response('Success!'))
12
- .get('/text', () => 'Success!')
13
- // .get('/params/:foo', ({ foo }) => foo) // should NOT work
14
- .get<FooRequest>('/params/:foo', ({ foo }) => foo) // should work
15
- .get('/json', () => ({ foo: 'bar' }))
16
-
17
- export default router
@@ -1,16 +0,0 @@
1
- import { IRequestStrict } from 'IttyRouter'
2
- import { Router } from 'Router'
3
-
4
- type FooRequest = {
5
- foo: string
6
- } & IRequestStrict
7
-
8
- const router = Router<FooRequest>()
9
-
10
- router
11
- .get('/', (request) => {
12
- request.foo // should work
13
- // request.bar // should NOT work
14
- })
15
-
16
- export default router
@@ -1,41 +0,0 @@
1
- import { IRequestStrict, IRequest, RequestHandler } from '../../src/types'
2
- import { IttyRouter } from 'IttyRouter'
3
-
4
- type UserRequest = {
5
- user: string
6
- } & IRequestStrict
7
-
8
- const router = IttyRouter<IRequestStrict>()
9
-
10
- const withUser: RequestHandler = (request) => {
11
- request.user = 'Kevin'
12
- }
13
-
14
- router
15
- // upstream request sees the request as IRequest (default), so anything goes
16
- .get('/', (request) => {
17
- request.user = 123 // not OK
18
- })
19
-
20
- // then we add the middleware defined above as <UserRequest>
21
- .all('*', withUser, (request) => {
22
- request.user = 'Kevin'
23
- request.user = 123 // NOT ok
24
- })
25
-
26
- // and if we ever need to restore control, add the generic back in
27
- .get<UserRequest, []>('/', (request) => {
28
- request.user = 'Kevin'
29
- request.user = 123 // NOT ok
30
- })
31
-
32
- .get('/', (request) => {
33
- request.user = 'Kevin' // still ok
34
- request.user = 123 // NOT ok
35
- })
36
-
37
- router.get('/', (request) => {
38
- request.user = 123 // NOT ok
39
- })
40
-
41
- export default router
@@ -1,11 +0,0 @@
1
- import { IttyRouter } from 'IttyRouter'
2
-
3
- const router = IttyRouter()
4
-
5
- const request = new Request('https://foo.bar')
6
-
7
- router.fetch(request) // should be OK
8
- router.fetch({ method: 'GET', url: 'foo' }) // should be OK
9
- router.fetch({ method: 'GET' }) // should NOT be OK
10
-
11
-
@@ -1,16 +0,0 @@
1
- import { IRequestStrict } from 'IttyRouter'
2
- import { Router } from 'Router'
3
-
4
- type FooRequest = {
5
- foo: string
6
- } & IRequestStrict
7
-
8
- const router = Router()
9
-
10
- router
11
- .get<FooRequest>('/', (request) => {
12
- request.foo // should work
13
- // request.bar // should NOT work
14
- })
15
-
16
- export default router
@@ -1,8 +0,0 @@
1
- import { Router } from 'Router'
2
-
3
- const router = Router({
4
- port: 4000,
5
- before: [],
6
- })
7
-
8
- router.finally = () => {} // should NOT work
package/lib/index.ts DELETED
@@ -1,72 +0,0 @@
1
- /* istanbul ignore file */
2
- import { expect, it, vi } from 'vitest'
3
-
4
- // generates a request from a string like:
5
- // GET /whatever
6
- // /foo
7
- export const toReq = (methodAndPath: string, options: RequestInit = {}) => {
8
- let [method, path] = methodAndPath.split(' ')
9
- if (!path) {
10
- path = method
11
- method = 'GET'
12
- }
13
-
14
- return new Request(`https://example.com${path}`, { method, ...options })
15
- }
16
-
17
- export const extract = ({ params, query }) => ({ params, query })
18
-
19
- const testRoute = async (
20
- { route, path, method = 'get', returns = true, log = false },
21
- Router
22
- ) => {
23
- const routes = []
24
- const router = Router({ routes })
25
- const handler = vi.fn((req) => req.params)
26
-
27
- // register route
28
- router[method](route, handler)
29
-
30
- log && console.log({
31
- routes,
32
- route,
33
- path,
34
- })
35
-
36
- await router.fetch(toReq(`${method.toUpperCase()} ${path}`))
37
-
38
- if (!returns) {
39
- expect(handler).not.toHaveBeenCalled()
40
- } else {
41
- expect(handler).toHaveBeenCalled()
42
-
43
- if (typeof returns === 'object') {
44
- expect(handler).toHaveReturnedWith(returns)
45
- }
46
- }
47
- }
48
-
49
- export const runTests = (tests, Router) => {
50
- for (let test of tests) {
51
- let { route, path, returns = true, description } = test
52
- const matchNote = returns
53
- ? typeof returns === 'object'
54
- ? `returns params ${JSON.stringify(returns)
55
- .replace('{', '{ ')
56
- .replace('}', ' }')
57
- .replace(/"(\w+)":/g, '$1: ')
58
- .replace(',', ', ')} from`
59
- : 'matches'
60
- : 'does NOT match'
61
- description = description || `route "${route}" ${matchNote} path "${path}"`
62
-
63
- it(description, async () => {
64
- await testRoute(test, Router)
65
- })
66
- }
67
- }
68
-
69
- export const createTestRunner =
70
- (Router) =>
71
- (...args) =>
72
- runTests(...args, Router)
package/rollup.config.mjs DELETED
@@ -1,68 +0,0 @@
1
- import terser from '@rollup/plugin-terser'
2
- import typescript from '@rollup/plugin-typescript'
3
- import { globby } from 'globby'
4
- import bundleSize from 'rollup-plugin-bundle-size'
5
- import copy from 'rollup-plugin-copy'
6
- import fs from 'fs-extra'
7
-
8
- // scan files to build
9
- const files = (await globby('./src/*.ts', {
10
- ignore: ['**/*.spec.ts', 'example', '**/types.ts'],
11
- })).map(path => ({
12
- path,
13
- shortPath: path.replace(/(\/src)|(\.ts)/g, '').replace('./index', '.'),
14
- esm: path.replace('/src/', '/dist/').replace('.ts', '.mjs'),
15
- cjs: path.replace('/src/', '/dist/').replace('.ts', '.js'),
16
- types: path.replace('/src/', '/dist/').replace('.ts', '.d.ts'),
17
- })).sort((a, b) => a.shortPath.toLowerCase() < b.shortPath.toLowerCase() ? -1 : 1)
18
-
19
-
20
- // read original package.json
21
- const pkg = await fs.readJSON('./package.json')
22
-
23
- // create updated exports list from build files
24
- pkg.exports = files.reduce((acc, file) => {
25
- acc[file.shortPath] = {
26
- import: file.esm.replace('/dist', ''),
27
- require: file.cjs.replace('/dist', ''),
28
- types: file.types.replace('/dist', ''),
29
- }
30
-
31
- return acc
32
- }, {})
33
-
34
- // write updated package.json
35
- await fs.writeJSON('./package.json', pkg, { spaces: 2 })
36
-
37
- export default async () => {
38
- console.log(files.map(f => f.path))
39
-
40
- return files.map(file => ({
41
- input: file.path,
42
- output: [
43
- {
44
- format: 'esm',
45
- file: file.esm,
46
- sourcemap: true,
47
- },
48
- {
49
- format: 'cjs',
50
- file: file.cjs,
51
- sourcemap: true,
52
- },
53
- ],
54
- plugins: [
55
- typescript({ sourceMap: true }),
56
- terser(),
57
- bundleSize(),
58
- copy({
59
- targets: [
60
- {
61
- src: ['LICENSE'],
62
- dest: 'dist',
63
- },
64
- ],
65
- }),
66
- ],
67
- }))
68
- }
package/tsconfig.json DELETED
@@ -1,28 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "allowJs": true,
4
- "allowSyntheticDefaultImports": true,
5
- "baseUrl": "src",
6
- "declaration": true,
7
- "sourceMap": true,
8
- "esModuleInterop": true,
9
- "inlineSourceMap": false,
10
- "lib": ["esnext", "dom", "dom.iterable"],
11
- "listEmittedFiles": false,
12
- "listFiles": false,
13
- "noFallthroughCasesInSwitch": true,
14
- "pretty": true,
15
- // "moduleResolution": "nodeNext", // disabled to be compatible with module: "esnext"
16
- // "resolveJsonModule": true, // disabled to be compatible with module: "esnext"
17
- "rootDir": "src",
18
- "skipLibCheck": true,
19
- "strict": true,
20
- "traceResolution": false,
21
- "outDir": "",
22
- "target": "esnext",
23
- "module": "esnext",
24
- "types": ["@cloudflare/workers-types", "@types/node"]
25
- },
26
- "exclude": ["node_modules", "dist", "**/*.spec.ts", "**/example.ts"],
27
- "include": ["src", "example"]
28
- }