primate 0.29.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "primate",
3
- "version": "0.29.2",
3
+ "version": "0.29.4",
4
4
  "description": "Polymorphic development platform",
5
5
  "homepage": "https://primatejs.com",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
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() ? (await import(error)).default : undefined,
83
+ default: await error.exists() ? await error.import("default") : undefined,
84
84
  },
85
85
  handlers: { ...handlers },
86
86
  extensions: {
@@ -113,7 +113,8 @@ export default async (log, root, config) => {
113
113
  const { location: { server, client, components } } = this.config;
114
114
 
115
115
  const source = this.path.components;
116
- const compile = this.extensions[component.fullExtension]?.compile;
116
+ const compile = this.extensions[component.fullExtension]?.compile
117
+ ?? this.extensions[component.extension]?.compile;
117
118
  if (compile === undefined) {
118
119
  const debased = `${component.path}`.replace(source, "");
119
120
 
@@ -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 is_text = value => {
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 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);
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);
@@ -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 path => [
23
- `${path}`.replace(directory, _ => "").slice(1, -ending.length),
24
- await import(path),
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 = (await import(config)).default;
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 'primate' {
2
- function _default(command: any): Promise<any>;
3
- export default _default;
4
- export function text(body: any, options: any): (app: any) => any;
5
- export function json(body: any, options: any): (app: any) => any;
6
- export function stream(body: any, options: any): (app: any) => any;
7
- export function redirect(Location: any, { status }?: {
8
- status?: any;
9
- }): (app: any) => any;
10
- export function error(body?: string, { status, page }?: {
11
- status?: any;
12
- page: any;
13
- }): (app: any) => any;
14
- export function html(name: any, options: any): (app: any) => Promise<any>;
15
- /**
16
- * Render a component using handler for the given filename extension.
17
- * @param name component filename
18
- * @param props props passed to component
19
- * @param options rendering options
20
- */
21
- export function view(name: string, props: object, options: object): (app: any, ...rest: any[]) => Promise<any>;
22
- export function ws(implementation: any): ({ server }: {
23
- server: any;
24
- }, _: any, { original }: {
25
- original: any;
26
- }) => any;
27
- export function sse(body: any, options: any): (app: any) => any;
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
- //# sourceMappingURL=index.d.ts.map
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
+ }