-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
4,966 additions
and
3,556 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018-2023 the oak authors | ||
Copyright (c) 2018-2024 the oak authors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,166 +1,6 @@ | ||
# acorn | ||
|
||
[![ci](https://github.com/oakserver/acorn/workflows/ci/badge.svg)](https://github.com/oakserver/acorn) | ||
|
||
Rapidly develop and iterate on RESTful APIs using a strongly typed router | ||
designed for Deno CLI, Deno Deploy and Bun. | ||
|
||
## Usage | ||
|
||
### Under Deno runtime or Deploy | ||
|
||
Add the package to your project: | ||
|
||
``` | ||
deno add @oak/acorn | ||
``` | ||
|
||
### Under Node.js and Cloudflare Workers | ||
|
||
Install the package using your chosen package manager: | ||
|
||
#### npm | ||
|
||
``` | ||
npx jsr add @oak/acorn | ||
``` | ||
|
||
#### Yarn | ||
|
||
``` | ||
yarn dlx jsr add @oak/acorn | ||
``` | ||
|
||
#### pnpm | ||
|
||
``` | ||
pnpm dlx jsr add @oak/acorn | ||
``` | ||
|
||
### Under Bun | ||
|
||
``` | ||
bunx jsr add @oak/acorn | ||
``` | ||
|
||
### Listening | ||
|
||
For Deno, Node.js, and Bun the router needs to be configured and then start | ||
listening for requests: | ||
|
||
```ts | ||
import { Router } from "@oak/acorn"; | ||
|
||
const BOOKS = { | ||
"1": { id: 1, title: "The Hound of the Baskervilles" }, | ||
"2": { id: 2, title: "It" }, | ||
}; | ||
|
||
const router = new Router(); | ||
|
||
router.get("/", () => ({ hello: "world" })); | ||
router.get("/books/:id", (ctx) => BOOKS[ctx.params.id]); | ||
|
||
router.listen({ port: 5000 }); | ||
``` | ||
|
||
### Fetch handlers | ||
|
||
For Cloudflare Workers the router needs to be configured and router needs to be | ||
the default export: | ||
|
||
```ts | ||
import { Router } from "@oak/acorn"; | ||
|
||
const BOOKS = { | ||
"1": { id: 1, title: "The Hound of the Baskervilles" }, | ||
"2": { id: 2, title: "It" }, | ||
}; | ||
|
||
const router = new Router(); | ||
|
||
router.get("/", () => ({ hello: "world" })); | ||
router.get("/books/:id", (ctx) => BOOKS[ctx.params.id]); | ||
|
||
router.listen({ port: 5000 }); | ||
|
||
export default router; | ||
``` | ||
|
||
## Philosophy | ||
|
||
After having spent years working on [oak](https://jsr.io/@oak/oak) and extensive | ||
experience with building Deno, that really when people were looking at | ||
middleware type of solution, really what they were looking fore was a straight | ||
forward router that made it easy to handle JSON payloads. | ||
|
||
Also, oak was created in the early days of Deno, before it even had a native | ||
HTTP server, and that server supported the web standard `Request` and | ||
`Response`. | ||
|
||
Acorn was the culmination of that need. It makes it easy to have route handlers | ||
that are straight forward and focuses on staying closer to the native | ||
implementations of Deno constructs that have evolved over time. | ||
|
||
## Routes | ||
|
||
An instance of a router has several methods for registering a handler for a | ||
route. The methods correspond to one or many HTTP methods or verbs. When a | ||
request is handled by the router that matches a route and the HTTP method(s), it | ||
will invoke the registered handler. | ||
|
||
The handler is provided with a context which contains information about the | ||
request: | ||
|
||
```ts | ||
interface Context<Params extends Record<string, string>, BodyType> { | ||
readonly addr: Addr; | ||
readonly cookies: SecureCookieMap; | ||
readonly params: Params; | ||
readonly request: Request; | ||
readonly searchParams: Record<string, string>; | ||
body(): Promise<BodyType | undefined>; | ||
url(): URL; | ||
} | ||
``` | ||
|
||
The `.params` property provides any parameters (named captures) parsed out when | ||
matching the route string. | ||
|
||
The `.searchParams` property provides any search parameters associated with the | ||
request. | ||
|
||
The `.addr` property provides the remote address associated with the request. | ||
|
||
The `.url()` method returns an instance of URL associated with the request. | ||
|
||
The `.body()` method is a convenience method to deal with decoding a JSON string | ||
body. It can be used with an optional | ||
[deserializer](https://deno.land/x/[email protected]/mod.ts?s=Deserializer) which can | ||
do advanced decoding of the body, or it will attempted to be decoded from the | ||
JSON string. | ||
|
||
More advanced request body handling can be handled via the `.request` property. | ||
|
||
The handler is then expected to have a return value which can be a `Request` | ||
instance, a value that is a [`BodyInit`](https://deno.land/api?s=BodyInit), or | ||
any other value. If an optional | ||
[serializer](https://deno.land/x/[email protected]/mod.ts?s=Serializer) is provided | ||
and the response is not a `Request` instance or of the type `BodyInit`, the | ||
value will be passed to the serializer. If no serializer is present then | ||
[`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) | ||
will be used to attempt to convert the value to a JSON string. If `undefined` is | ||
returned, then a `404 NotFound` response will be generated. | ||
|
||
The handling of the request differs significantly from middleware solutions like | ||
[oak](https://oakserver.github.io/oak/). In most cases you will want only one | ||
handler per route and HTTP method combination. There is nothing that prevents | ||
multiple registrations, but the first handler registered that returns a non | ||
`undefined` value will be used and any remaining handlers will not be called. | ||
|
||
Underneath, the router matches route strings using the browser standard | ||
[URL Pattern API](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API) | ||
and matches the pathname part of the URL. | ||
A focused Javascript/TypeScript framework for creating RESTful services. | ||
|
||
--- | ||
|
||
|
Oops, something went wrong.