primate 0.29.3 → 0.29.4
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 +1 -1
- package/src/app.js +1 -1
- package/src/hooks/respond/respond.js +23 -19
- package/src/loaders/common.js +3 -3
- package/src/run.js +1 -1
- package/types/index.d.ts +68 -29
package/package.json
CHANGED
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: {
|
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
import { Blob } from "rcompat/fs";
|
|
2
|
-
import { URL } from "rcompat/http";
|
|
1
|
+
import { Blob, s_streamable } from "rcompat/fs";
|
|
2
|
+
import { URL, Response } from "rcompat/http";
|
|
3
|
+
import { identity } from "rcompat/function";
|
|
3
4
|
import { text, json, stream, redirect } from "primate";
|
|
4
|
-
import is_response_duck from "./duck.js";
|
|
5
5
|
|
|
6
|
-
const
|
|
7
|
-
if (typeof value === "string") {
|
|
8
|
-
return text(value);
|
|
9
|
-
}
|
|
6
|
+
const not_found = value => {
|
|
10
7
|
throw new Error(`no handler found for ${value}`);
|
|
11
8
|
};
|
|
12
|
-
|
|
9
|
+
const is_text = value => typeof value === "string";
|
|
13
10
|
const is_non_null_object = value => typeof value === "object" && value !== null;
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
const is_instance = of => value => value instanceof of;
|
|
12
|
+
const is_response = is_instance(Response);
|
|
13
|
+
const is_global_response = is_instance(globalThis.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_global_response(value), identity],
|
|
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);
|
|
25
29
|
|
|
26
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
|
+
}
|