Skip to content

Commit

Permalink
0.29.4
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Feb 12, 2024
1 parent e29bb8f commit 7f10943
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@primate/frontend",
"version": "0.13.3",
"version": "0.13.4",
"description": "Primate frontend code",
"bugs": "https://github.com/primatejs/primate/issues",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/primate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "primate",
"version": "0.29.3",
"version": "0.29.4",
"description": "Polymorphic development platform",
"homepage": "https://primatejs.com",
"bugs": "https://github.com/primatejs/primate/issues",
Expand Down
42 changes: 23 additions & 19 deletions packages/primate/src/hooks/respond/respond.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { Blob } from "rcompat/fs";
import { URL } from "rcompat/http";
import { Blob, s_streamable } from "rcompat/fs";
import { URL, Response } from "rcompat/http";
import { identity } from "rcompat/function";
import { text, json, stream, redirect } from "primate";
import is_response_duck from "./duck.js";

const is_text = value => {
if (typeof value === "string") {
return text(value);
}
const not_found = value => {
throw new Error(`no handler found for ${value}`);
};

const is_text = value => typeof value === "string";
const is_non_null_object = value => typeof value === "object" && value !== null;
const is_object = value => is_non_null_object(value)
? json(value) : is_text(value);
const is_response = value => is_response_duck(value)
? _ => value : is_object(value);
const is_stream = value => value instanceof ReadableStream
? stream(value) : is_response(value);
const is_blob = value => value instanceof Blob
? stream(value.stream()) : is_stream(value);
const is_URL = value => value instanceof URL
? redirect(value.href) : is_blob(value);
const guess = value => is_URL(value);
const is_instance = of => value => value instanceof of;
const is_response = is_instance(Response);
const is_global_response = is_instance(globalThis.Response);
const is_streamable =
value => value instanceof Blob || value?.streamable === s_streamable;

// [if, then]
const guesses = [
[is_instance(URL), redirect],
[is_streamable, value => stream(value.stream())],
[is_instance(ReadableStream), stream],
[value => is_response(value) || is_global_response(value), identity],
[is_non_null_object, json],
[is_text, text],
[not_found, identity],
];

const guess = value => guesses.find(([check]) => check(value))?.[1](value);

export default result => typeof result === "function" ? result : guess(result);
2 changes: 1 addition & 1 deletion packages/store/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@primate/store",
"version": "0.22.1",
"version": "0.22.2",
"description": "Primate data store module",
"homepage": "https://primatejs.com/modules/store",
"bugs": "https://github.com/primatejs/primate/issues",
Expand Down

0 comments on commit 7f10943

Please sign in to comment.