primate 0.16.2 → 0.16.3
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.
- package/README.md +3 -208
- package/package.json +1 -1
- package/src/hooks/handle.js +2 -1
package/README.md
CHANGED
|
@@ -1,210 +1,5 @@
|
|
|
1
|
-
# Primate
|
|
1
|
+
# Primate core framework
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This package contains the core framework code.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Run `npx -y primate@latest create` to create a project structure.
|
|
8
|
-
|
|
9
|
-
Create a route in `routes/index.js`
|
|
10
|
-
|
|
11
|
-
```js
|
|
12
|
-
export default {
|
|
13
|
-
get() {
|
|
14
|
-
return "Hello, world!";
|
|
15
|
-
},
|
|
16
|
-
};
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Run `npm i && npm start` and visit http://localhost:6161 in your browser.
|
|
20
|
-
|
|
21
|
-
## Table of Contents
|
|
22
|
-
|
|
23
|
-
- [Serving content](#serving-content)
|
|
24
|
-
- [Plain text](#plain-text)
|
|
25
|
-
- [JSON](#json)
|
|
26
|
-
- [Streams](#streams)
|
|
27
|
-
- [Response](#response)
|
|
28
|
-
- [HTML](#html)
|
|
29
|
-
- [Routing](#routing)
|
|
30
|
-
- [Basic](#basic)
|
|
31
|
-
- [The request object](#the-request-object)
|
|
32
|
-
- [Accessing the request body](#accessing-the-request-body)
|
|
33
|
-
- [Parameterized routes](#parameterized-routes)
|
|
34
|
-
- [Explicit handlers](#explicit-handlers)
|
|
35
|
-
|
|
36
|
-
## Serving content
|
|
37
|
-
|
|
38
|
-
Create a file in `routes/index.js` to handle the special `/` route.
|
|
39
|
-
|
|
40
|
-
### Plain text
|
|
41
|
-
|
|
42
|
-
```js
|
|
43
|
-
// routes/index.js handles the `/` route
|
|
44
|
-
export default {
|
|
45
|
-
get() {
|
|
46
|
-
// strings are served as plain text
|
|
47
|
-
return "Donald";
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
### JSON
|
|
54
|
-
|
|
55
|
-
```js
|
|
56
|
-
// routes/index.js handles the `/` route
|
|
57
|
-
export default {
|
|
58
|
-
get() {
|
|
59
|
-
// proper JavaScript objects are served as JSON
|
|
60
|
-
return [
|
|
61
|
-
{name: "Donald"},
|
|
62
|
-
{name: "Ryan"},
|
|
63
|
-
];
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
### Streams
|
|
70
|
-
|
|
71
|
-
```js
|
|
72
|
-
import {File} from "runtime-compat/filesystem";
|
|
73
|
-
|
|
74
|
-
// routes/index.js handles the `/` route
|
|
75
|
-
export default {
|
|
76
|
-
get() {
|
|
77
|
-
// ReadableStream or Blob objects are streamed to the client
|
|
78
|
-
return new File("users.json");
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### Response
|
|
85
|
-
|
|
86
|
-
```js
|
|
87
|
-
import {Response} from "runtime-compat/http";
|
|
88
|
-
|
|
89
|
-
// routes/index.js handles the `/` route
|
|
90
|
-
export default {
|
|
91
|
-
get() {
|
|
92
|
-
// use a Response object for custom response status
|
|
93
|
-
return new Response("created!", {status: 201});
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
### HTML
|
|
100
|
-
|
|
101
|
-
```js
|
|
102
|
-
import {html} from "primate";
|
|
103
|
-
|
|
104
|
-
// routes/index.js handles the `/` route
|
|
105
|
-
export default {
|
|
106
|
-
get() {
|
|
107
|
-
// to serve HTML, import and use the html handler
|
|
108
|
-
return html("<p>Hello, world!</p>");
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
## Routing
|
|
115
|
-
|
|
116
|
-
Primate uses filesystem-based routes. Every path a client accesses is mapped to
|
|
117
|
-
a route under `routes`.
|
|
118
|
-
|
|
119
|
-
* `index.js` handles the root route (`/`)
|
|
120
|
-
* `post.js` handles the `/post` route
|
|
121
|
-
* `post/{postId}.js` handles a parameterized route where `{postId}` can
|
|
122
|
-
be mapped to anything, such as `/post/1`
|
|
123
|
-
|
|
124
|
-
### Basic
|
|
125
|
-
|
|
126
|
-
```js
|
|
127
|
-
import {redirect} from "primate";
|
|
128
|
-
|
|
129
|
-
// routes/site/login.js handles the `/site/login` route
|
|
130
|
-
export default {
|
|
131
|
-
get() {
|
|
132
|
-
// strings are served as plain text
|
|
133
|
-
return "Hello, world!";
|
|
134
|
-
},
|
|
135
|
-
// other HTTP verbs are also available
|
|
136
|
-
post() {
|
|
137
|
-
return redirect("/");
|
|
138
|
-
},
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
### The request object
|
|
144
|
-
|
|
145
|
-
```js
|
|
146
|
-
// routes/site/login.js handles the `/site/login` route
|
|
147
|
-
export default {
|
|
148
|
-
get(request) {
|
|
149
|
-
// will serve `["site", "login"]` as JSON
|
|
150
|
-
return request.path;
|
|
151
|
-
},
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
### Accessing the request body
|
|
157
|
-
|
|
158
|
-
For requests containing a body, Primate will attempt to parse the body according
|
|
159
|
-
to the content type sent along the request. Currently supported are
|
|
160
|
-
`application/x-www-form-urlencoded` (typically for form submission) and
|
|
161
|
-
`application/json`.
|
|
162
|
-
|
|
163
|
-
```js
|
|
164
|
-
// routes/site/login.js handles the `/site/login` route
|
|
165
|
-
export default {
|
|
166
|
-
get(request) {
|
|
167
|
-
return `username submitted: ${request.body.username}`;
|
|
168
|
-
},
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### Parameterized routes
|
|
174
|
-
|
|
175
|
-
```js
|
|
176
|
-
// routes/user/{userId}.js handles all routes of the sort `/user/{userId}`
|
|
177
|
-
// where {userId} can be anything
|
|
178
|
-
export default {
|
|
179
|
-
get(request) {
|
|
180
|
-
return `user id: ${request.named.userId}`;
|
|
181
|
-
},
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
### Explicit handlers
|
|
187
|
-
|
|
188
|
-
Often we can figure out the content type to respond with based on the return
|
|
189
|
-
type from the handler. For other cases, we need to use an explicit handler.
|
|
190
|
-
|
|
191
|
-
```js
|
|
192
|
-
import {redirect} from "primate";
|
|
193
|
-
|
|
194
|
-
// routes/source.js handles the `/source` route
|
|
195
|
-
export default {
|
|
196
|
-
get() {
|
|
197
|
-
return redirect("/target");
|
|
198
|
-
},
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
## Resources
|
|
204
|
-
|
|
205
|
-
* Website: https://primatejs.com
|
|
206
|
-
* IRC: Join the `#primate` channel on `irc.libera.chat`.
|
|
207
|
-
|
|
208
|
-
## License
|
|
209
|
-
|
|
210
|
-
MIT
|
|
5
|
+
See the [Getting started][getting-started] guide for documentation.
|
package/package.json
CHANGED
package/src/hooks/handle.js
CHANGED
|
@@ -131,10 +131,11 @@ export default async app => {
|
|
|
131
131
|
|
|
132
132
|
const parseRequest = async request => {
|
|
133
133
|
const cookies = request.headers.get("cookie");
|
|
134
|
+
const {url} = request;
|
|
134
135
|
|
|
135
136
|
return {
|
|
136
137
|
request,
|
|
137
|
-
url: new URL(
|
|
138
|
+
url: new URL(url.endsWith("/") ? url.slice(0, -1) : url),
|
|
138
139
|
body: await parseBody(request),
|
|
139
140
|
cookies: fromNull(cookies === null
|
|
140
141
|
? {}
|