Skip to content

Commit c681e26

Browse files
committed
feat: rewrite the entire project
1 parent 72c91e6 commit c681e26

37 files changed

+4966
-3556
lines changed

.vscode/settings.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

LICENSE

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
MIT License
22

3-
Copyright (c) 2018-2023 the oak authors
3+
Copyright (c) 2018-2024 the oak authors
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
1111

1212
The above copyright notice and this permission notice shall be included in all
1313
copies or substantial portions of the Software.
1414

1515
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 1 addition & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,6 @@
11
# acorn
22

3-
[![ci](https://github.com/oakserver/acorn/workflows/ci/badge.svg)](https://github.com/oakserver/acorn)
4-
5-
Rapidly develop and iterate on RESTful APIs using a strongly typed router
6-
designed for Deno CLI, Deno Deploy and Bun.
7-
8-
## Usage
9-
10-
### Under Deno runtime or Deploy
11-
12-
Add the package to your project:
13-
14-
```
15-
deno add @oak/acorn
16-
```
17-
18-
### Under Node.js and Cloudflare Workers
19-
20-
Install the package using your chosen package manager:
21-
22-
#### npm
23-
24-
```
25-
npx jsr add @oak/acorn
26-
```
27-
28-
#### Yarn
29-
30-
```
31-
yarn dlx jsr add @oak/acorn
32-
```
33-
34-
#### pnpm
35-
36-
```
37-
pnpm dlx jsr add @oak/acorn
38-
```
39-
40-
### Under Bun
41-
42-
```
43-
bunx jsr add @oak/acorn
44-
```
45-
46-
### Listening
47-
48-
For Deno, Node.js, and Bun the router needs to be configured and then start
49-
listening for requests:
50-
51-
```ts
52-
import { Router } from "@oak/acorn";
53-
54-
const BOOKS = {
55-
"1": { id: 1, title: "The Hound of the Baskervilles" },
56-
"2": { id: 2, title: "It" },
57-
};
58-
59-
const router = new Router();
60-
61-
router.get("/", () => ({ hello: "world" }));
62-
router.get("/books/:id", (ctx) => BOOKS[ctx.params.id]);
63-
64-
router.listen({ port: 5000 });
65-
```
66-
67-
### Fetch handlers
68-
69-
For Cloudflare Workers the router needs to be configured and router needs to be
70-
the default export:
71-
72-
```ts
73-
import { Router } from "@oak/acorn";
74-
75-
const BOOKS = {
76-
"1": { id: 1, title: "The Hound of the Baskervilles" },
77-
"2": { id: 2, title: "It" },
78-
};
79-
80-
const router = new Router();
81-
82-
router.get("/", () => ({ hello: "world" }));
83-
router.get("/books/:id", (ctx) => BOOKS[ctx.params.id]);
84-
85-
router.listen({ port: 5000 });
86-
87-
export default router;
88-
```
89-
90-
## Philosophy
91-
92-
After having spent years working on [oak](https://jsr.io/@oak/oak) and extensive
93-
experience with building Deno, that really when people were looking at
94-
middleware type of solution, really what they were looking fore was a straight
95-
forward router that made it easy to handle JSON payloads.
96-
97-
Also, oak was created in the early days of Deno, before it even had a native
98-
HTTP server, and that server supported the web standard `Request` and
99-
`Response`.
100-
101-
Acorn was the culmination of that need. It makes it easy to have route handlers
102-
that are straight forward and focuses on staying closer to the native
103-
implementations of Deno constructs that have evolved over time.
104-
105-
## Routes
106-
107-
An instance of a router has several methods for registering a handler for a
108-
route. The methods correspond to one or many HTTP methods or verbs. When a
109-
request is handled by the router that matches a route and the HTTP method(s), it
110-
will invoke the registered handler.
111-
112-
The handler is provided with a context which contains information about the
113-
request:
114-
115-
```ts
116-
interface Context<Params extends Record<string, string>, BodyType> {
117-
readonly addr: Addr;
118-
readonly cookies: SecureCookieMap;
119-
readonly params: Params;
120-
readonly request: Request;
121-
readonly searchParams: Record<string, string>;
122-
body(): Promise<BodyType | undefined>;
123-
url(): URL;
124-
}
125-
```
126-
127-
The `.params` property provides any parameters (named captures) parsed out when
128-
matching the route string.
129-
130-
The `.searchParams` property provides any search parameters associated with the
131-
request.
132-
133-
The `.addr` property provides the remote address associated with the request.
134-
135-
The `.url()` method returns an instance of URL associated with the request.
136-
137-
The `.body()` method is a convenience method to deal with decoding a JSON string
138-
body. It can be used with an optional
139-
[deserializer](https://deno.land/x/[email protected]/mod.ts?s=Deserializer) which can
140-
do advanced decoding of the body, or it will attempted to be decoded from the
141-
JSON string.
142-
143-
More advanced request body handling can be handled via the `.request` property.
144-
145-
The handler is then expected to have a return value which can be a `Request`
146-
instance, a value that is a [`BodyInit`](https://deno.land/api?s=BodyInit), or
147-
any other value. If an optional
148-
[serializer](https://deno.land/x/[email protected]/mod.ts?s=Serializer) is provided
149-
and the response is not a `Request` instance or of the type `BodyInit`, the
150-
value will be passed to the serializer. If no serializer is present then
151-
[`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
152-
will be used to attempt to convert the value to a JSON string. If `undefined` is
153-
returned, then a `404 NotFound` response will be generated.
154-
155-
The handling of the request differs significantly from middleware solutions like
156-
[oak](https://oakserver.github.io/oak/). In most cases you will want only one
157-
handler per route and HTTP method combination. There is nothing that prevents
158-
multiple registrations, but the first handler registered that returns a non
159-
`undefined` value will be used and any remaining handlers will not be called.
160-
161-
Underneath, the router matches route strings using the browser standard
162-
[URL Pattern API](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API)
163-
and matches the pathname part of the URL.
3+
A focused Javascript/TypeScript framework for creating RESTful services.
1644

1655
---
1666

0 commit comments

Comments
 (0)