primate 0.29.3 → 0.29.5
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/package.json +2 -2
- package/src/app.js +1 -1
- package/src/hooks/handle.js +1 -1
- package/src/hooks/respond.js +30 -0
- package/src/loaders/common.js +3 -3
- package/src/run.js +1 -1
- package/types/index.d.ts +68 -29
- package/src/hooks/respond/duck.js +0 -4
- package/src/hooks/respond/exports.js +0 -1
- package/src/hooks/respond/respond.js +0 -26
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "primate",
|
|
3
|
-
"version": "0.29.
|
|
3
|
+
"version": "0.29.5",
|
|
4
4
|
"description": "Polymorphic development platform",
|
|
5
5
|
"homepage": "https://primatejs.com",
|
|
6
6
|
"bugs": "https://github.com/primatejs/primate/issues",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"directory": "packages/primate"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"rcompat": "^0.8.
|
|
22
|
+
"rcompat": "^0.8.3"
|
|
23
23
|
},
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": ">=18"
|
package/src/app.js
CHANGED
|
@@ -80,7 +80,7 @@ export default async (log, root, config) => {
|
|
|
80
80
|
root,
|
|
81
81
|
log,
|
|
82
82
|
error: {
|
|
83
|
-
default: await error.exists() ?
|
|
83
|
+
default: await error.exists() ? await error.import("default") : undefined,
|
|
84
84
|
},
|
|
85
85
|
handlers: { ...handlers },
|
|
86
86
|
extensions: {
|
package/src/hooks/handle.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Response, Status, MediaType } from "rcompat/http";
|
|
2
2
|
import { cascade, tryreturn } from "rcompat/async";
|
|
3
|
-
import
|
|
3
|
+
import respond from "./respond.js";
|
|
4
4
|
import { error as clientError } from "../handlers.js";
|
|
5
5
|
|
|
6
6
|
const guard_error = Symbol("guard_error");
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Blob, s_streamable } from "rcompat/fs";
|
|
2
|
+
import { URL, Response } from "rcompat/http";
|
|
3
|
+
import { identity } from "rcompat/function";
|
|
4
|
+
import { text, json, stream, redirect } from "primate";
|
|
5
|
+
|
|
6
|
+
const not_found = value => {
|
|
7
|
+
throw new Error(`no handler found for ${value}`);
|
|
8
|
+
};
|
|
9
|
+
const is_text = value => typeof value === "string";
|
|
10
|
+
const is_non_null_object = value => typeof value === "object" && value !== null;
|
|
11
|
+
const is_instance = of => value => value instanceof of;
|
|
12
|
+
const is_response = is_instance(globalThis.Response);
|
|
13
|
+
const is_fake_response = is_instance(Response);
|
|
14
|
+
const is_streamable =
|
|
15
|
+
value => value instanceof Blob || value?.streamable === s_streamable;
|
|
16
|
+
|
|
17
|
+
// [if, then]
|
|
18
|
+
const guesses = [
|
|
19
|
+
[is_instance(URL), redirect],
|
|
20
|
+
[is_streamable, value => stream(value.stream())],
|
|
21
|
+
[is_instance(ReadableStream), stream],
|
|
22
|
+
[value => is_response(value) || is_fake_response(value), value => _ => value],
|
|
23
|
+
[is_non_null_object, json],
|
|
24
|
+
[is_text, text],
|
|
25
|
+
[not_found, identity],
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
const guess = value => guesses.find(([check]) => check(value))?.[1](value);
|
|
29
|
+
|
|
30
|
+
export default result => typeof result === "function" ? result : guess(result);
|
package/src/loaders/common.js
CHANGED
|
@@ -19,9 +19,9 @@ export default async ({
|
|
|
19
19
|
const objects = directory === undefined ? [] : await Promise.all(
|
|
20
20
|
(await File.collect(directory, /^.*.js$/u, { recursive }))
|
|
21
21
|
.filter(filter)
|
|
22
|
-
.map(async
|
|
23
|
-
`${
|
|
24
|
-
await import(
|
|
22
|
+
.map(async file => [
|
|
23
|
+
`${file}`.replace(directory, _ => "").slice(1, -ending.length),
|
|
24
|
+
await file.import(),
|
|
25
25
|
]));
|
|
26
26
|
warn && await directory.exists() && empty(log)(objects, name, directory);
|
|
27
27
|
|
package/src/run.js
CHANGED
|
@@ -15,7 +15,7 @@ const get_config = async root => {
|
|
|
15
15
|
const config = root.join(name);
|
|
16
16
|
return await config.exists()
|
|
17
17
|
? tryreturn(async _ => {
|
|
18
|
-
const imported =
|
|
18
|
+
const imported = await config.import("default");
|
|
19
19
|
|
|
20
20
|
(imported === undefined || Object.keys(imported).length === 0) &&
|
|
21
21
|
errors.EmptyConfigFile.warn(logger, config);
|
package/types/index.d.ts
CHANGED
|
@@ -1,30 +1,69 @@
|
|
|
1
|
-
declare module
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
1
|
+
declare module "primate" {
|
|
2
|
+
type App = any;
|
|
3
|
+
|
|
4
|
+
interface MinOptions {
|
|
5
|
+
status: number,
|
|
6
|
+
headers: Headers | {},
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface ErrorOptions extends MinOptions {
|
|
10
|
+
page: string,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface Options extends ErrorOptions {
|
|
14
|
+
placeholders: {},
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Dispatcher = {
|
|
18
|
+
get(property: string): string,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type RequestFacade = {
|
|
22
|
+
body: {}
|
|
23
|
+
path: Dispatcher,
|
|
24
|
+
query: Dispatcher,
|
|
25
|
+
cookies: Dispatcher,
|
|
26
|
+
headers: Dispatcher,
|
|
27
|
+
original: Request,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type ResponseFn = (app: App, ...rest: any) => Response;
|
|
31
|
+
type ResponseFacade =
|
|
32
|
+
string
|
|
33
|
+
| object
|
|
34
|
+
| URL
|
|
35
|
+
| Blob
|
|
36
|
+
| ReadableStream
|
|
37
|
+
| Response
|
|
38
|
+
| ResponseFn;
|
|
39
|
+
|
|
40
|
+
type RouteFunction = (request?: RequestFacade) => ResponseFacade;
|
|
41
|
+
|
|
42
|
+
type Streamable = ReadableStream | Blob;
|
|
43
|
+
|
|
44
|
+
export type Route = {
|
|
45
|
+
get?: RouteFunction,
|
|
46
|
+
post?: RouteFunction,
|
|
47
|
+
put?: RouteFunction,
|
|
48
|
+
delete?: RouteFunction,
|
|
49
|
+
};
|
|
29
50
|
|
|
30
|
-
|
|
51
|
+
export function text(body: string, options?: MinOptions): ResponseFn;
|
|
52
|
+
|
|
53
|
+
export function json(body: {}, options?: MinOptions): ResponseFn;
|
|
54
|
+
|
|
55
|
+
export function stream(body: Streamable, options?: MinOptions): ResponseFn;
|
|
56
|
+
|
|
57
|
+
export function redirect(location: string, options?: MinOptions): ResponseFn;
|
|
58
|
+
|
|
59
|
+
export function html(name: string, options?: MinOptions): ResponseFn;
|
|
60
|
+
|
|
61
|
+
export function view(name: string, props: {}, options?: Options): ResponseFn;
|
|
62
|
+
|
|
63
|
+
export function error(body: string, options?: ErrorOptions): ResponseFn;
|
|
64
|
+
|
|
65
|
+
export function sse(implementation: {
|
|
66
|
+
open?: () => void,
|
|
67
|
+
close?: () => void,
|
|
68
|
+
}, options?: MinOptions): ResponseFn;
|
|
69
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default as respond } from "./respond.js";
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Blob } from "rcompat/fs";
|
|
2
|
-
import { URL } from "rcompat/http";
|
|
3
|
-
import { text, json, stream, redirect } from "primate";
|
|
4
|
-
import is_response_duck from "./duck.js";
|
|
5
|
-
|
|
6
|
-
const is_text = value => {
|
|
7
|
-
if (typeof value === "string") {
|
|
8
|
-
return text(value);
|
|
9
|
-
}
|
|
10
|
-
throw new Error(`no handler found for ${value}`);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const is_non_null_object = value => typeof value === "object" && value !== null;
|
|
14
|
-
const is_object = value => is_non_null_object(value)
|
|
15
|
-
? json(value) : is_text(value);
|
|
16
|
-
const is_response = value => is_response_duck(value)
|
|
17
|
-
? _ => value : is_object(value);
|
|
18
|
-
const is_stream = value => value instanceof ReadableStream
|
|
19
|
-
? stream(value) : is_response(value);
|
|
20
|
-
const is_blob = value => value instanceof Blob
|
|
21
|
-
? stream(value.stream()) : is_stream(value);
|
|
22
|
-
const is_URL = value => value instanceof URL
|
|
23
|
-
? redirect(value.href) : is_blob(value);
|
|
24
|
-
const guess = value => is_URL(value);
|
|
25
|
-
|
|
26
|
-
export default result => typeof result === "function" ? result : guess(result);
|