primate 0.13.0 → 0.13.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.
- package/README.md +12 -15
- package/package.json +1 -1
- package/readme/routing/sharing-logic-across-requests.js +9 -12
- package/readme/template.md +3 -3
- package/src/bundle.js +3 -1
- package/src/config.js +4 -1
- package/src/handlers/html.js +2 -2
- package/src/index.html +1 -0
- package/src/serve.js +11 -2
package/README.md
CHANGED
|
@@ -190,32 +190,29 @@ export default router => {
|
|
|
190
190
|
### Sharing logic across requests
|
|
191
191
|
|
|
192
192
|
```js
|
|
193
|
-
import html from "@primate/html";
|
|
194
|
-
import redirect from "@primate/redirect";
|
|
195
|
-
|
|
196
193
|
export default router => {
|
|
197
|
-
//
|
|
194
|
+
// Declare `"edit-user"` as alias of `"/user/edit/([0-9])+"`
|
|
198
195
|
router.alias("edit-user", "/user/edit/([0-9])+");
|
|
199
196
|
|
|
200
|
-
//
|
|
201
|
-
router.map("edit-user", () =>
|
|
197
|
+
// Pass user instead of request to all verbs on this route
|
|
198
|
+
router.map("edit-user", ({body}) => body?.name ?? "Donald");
|
|
202
199
|
|
|
203
|
-
//
|
|
204
|
-
router.get("edit-user", user =>
|
|
200
|
+
// Show user as plain text
|
|
201
|
+
router.get("edit-user", user => user);
|
|
205
202
|
|
|
206
|
-
//
|
|
207
|
-
router.post("edit-user",
|
|
208
|
-
?
|
|
209
|
-
:
|
|
203
|
+
// Verify or show error
|
|
204
|
+
router.post("edit-user", user => user === "Donald"
|
|
205
|
+
? "Hi Donald!"
|
|
206
|
+
: {message: "Error saving user"});
|
|
210
207
|
};
|
|
211
208
|
|
|
212
209
|
```
|
|
213
210
|
|
|
214
211
|
### Explicit handlers
|
|
215
212
|
|
|
216
|
-
Most often we can figure the content type to respond with based on the
|
|
217
|
-
type from the handler. To handle content not automatically detected, use
|
|
218
|
-
second argument of the exported function.
|
|
213
|
+
Most often we can figure out the content type to respond with based on the
|
|
214
|
+
return type from the handler. To handle content not automatically detected, use
|
|
215
|
+
the second argument of the exported function.
|
|
219
216
|
|
|
220
217
|
```js
|
|
221
218
|
export default (router, {redirect}) => {
|
package/package.json
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import html from "@primate/html";
|
|
2
|
-
import redirect from "@primate/redirect";
|
|
3
|
-
|
|
4
1
|
export default router => {
|
|
5
|
-
//
|
|
2
|
+
// Declare `"edit-user"` as alias of `"/user/edit/([0-9])+"`
|
|
6
3
|
router.alias("edit-user", "/user/edit/([0-9])+");
|
|
7
4
|
|
|
8
|
-
//
|
|
9
|
-
router.map("edit-user", () =>
|
|
5
|
+
// Pass user instead of request to all verbs on this route
|
|
6
|
+
router.map("edit-user", ({body}) => body?.name ?? "Donald");
|
|
10
7
|
|
|
11
|
-
//
|
|
12
|
-
router.get("edit-user", user =>
|
|
8
|
+
// Show user as plain text
|
|
9
|
+
router.get("edit-user", user => user);
|
|
13
10
|
|
|
14
|
-
//
|
|
15
|
-
router.post("edit-user",
|
|
16
|
-
?
|
|
17
|
-
:
|
|
11
|
+
// Verify or show error
|
|
12
|
+
router.post("edit-user", user => user === "Donald"
|
|
13
|
+
? "Hi Donald!"
|
|
14
|
+
: {message: "Error saving user"});
|
|
18
15
|
};
|
package/readme/template.md
CHANGED
|
@@ -117,9 +117,9 @@ to the content type sent along the request. Currently supported are
|
|
|
117
117
|
|
|
118
118
|
### Explicit handlers
|
|
119
119
|
|
|
120
|
-
Most often we can figure the content type to respond with based on the
|
|
121
|
-
type from the handler. To handle content not automatically detected, use
|
|
122
|
-
second argument of the exported function.
|
|
120
|
+
Most often we can figure out the content type to respond with based on the
|
|
121
|
+
return type from the handler. To handle content not automatically detected, use
|
|
122
|
+
the second argument of the exported function.
|
|
123
123
|
|
|
124
124
|
```js
|
|
125
125
|
// routing/explicit-handlers.js
|
package/src/bundle.js
CHANGED
|
@@ -16,6 +16,8 @@ const makePublic = async env => {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export default async env =>
|
|
19
|
+
export default async env => {
|
|
20
|
+
await makePublic(env);
|
|
20
21
|
[...filter("bundle", env.modules), _ => _].reduceRight((acc, handler) =>
|
|
21
22
|
input => handler(input, acc))(env);
|
|
23
|
+
};
|
package/src/config.js
CHANGED
|
@@ -59,7 +59,10 @@ export default async (filename = "primate.config.js") => {
|
|
|
59
59
|
env.handlers[name] = handler;
|
|
60
60
|
},
|
|
61
61
|
handlers: {...handlers},
|
|
62
|
-
render: async
|
|
62
|
+
render: async ({body = "", head = ""} = {}) => {
|
|
63
|
+
const html = await index(env);
|
|
64
|
+
return html.replace("%body%", () => body).replace("%head%", () => head);
|
|
65
|
+
},
|
|
63
66
|
};
|
|
64
67
|
env.log.info(`${package_json.name} \x1b[34m${package_json.version}\x1b[0m`);
|
|
65
68
|
const modules = await Promise.all(config.modules.map(module => module(env)));
|
package/src/handlers/html.js
CHANGED
|
@@ -8,9 +8,9 @@ const getContent = async (env, name) => {
|
|
|
8
8
|
|
|
9
9
|
export default (content, {status = 200, partial = false, adhoc = false} = {}) =>
|
|
10
10
|
async (env, headers) => {
|
|
11
|
-
const
|
|
11
|
+
const body = adhoc ? content : await getContent(env, content);
|
|
12
12
|
return [
|
|
13
|
-
partial ?
|
|
13
|
+
partial ? body : await env.render({body}), {
|
|
14
14
|
status,
|
|
15
15
|
headers: {...headers, "Content-Type": "text/html"},
|
|
16
16
|
},
|
package/src/index.html
CHANGED
package/src/serve.js
CHANGED
|
@@ -67,11 +67,20 @@ export default env => {
|
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
-
const
|
|
71
|
-
const type = contents[
|
|
70
|
+
const parseContentType = (contentType, body) => {
|
|
71
|
+
const type = contents[contentType];
|
|
72
72
|
return type === undefined ? body : type(body);
|
|
73
73
|
};
|
|
74
74
|
|
|
75
|
+
const parseContent = (request, body) => {
|
|
76
|
+
try {
|
|
77
|
+
return parseContentType(request.headers.get("content-type"), body);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
env.log.warn(error);
|
|
80
|
+
return body;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
75
84
|
const {http} = env;
|
|
76
85
|
const modules = filter("serve", env.modules);
|
|
77
86
|
|