Skip to content

Commit 4b7b12b

Browse files
committed
refactor: use HyperErr from hyper-utils
1 parent d845225 commit 4b7b12b

File tree

4 files changed

+12
-72
lines changed

4 files changed

+12
-72
lines changed

adapter.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { crocks, R } from "./deps.js";
1+
import { crocks, HyperErr, R } from "./deps.js";
22

33
import {
44
bulkPath,
@@ -14,7 +14,6 @@ import {
1414
bulkToEsBulk,
1515
esErrToHyperErr,
1616
handleHyperErr,
17-
HyperErr,
1817
mappingsToEsMappings,
1918
moveUnderscoreId,
2019
queryToEsQuery,
@@ -185,7 +184,9 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
185184
headers,
186185
method: "GET",
187186
},
188-
).chain(handleResponse((res) => res.status === 404))
187+
).chain(
188+
handleResponse((res) => res.status === 404),
189+
)
189190
.bichain(
190191
() =>
191192
Async.Rejected(HyperErr({
@@ -227,6 +228,7 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
227228
* @returns {Promise<Response>}
228229
*/
229230
function getDoc({ index, key }) {
231+
// Will 404 if doc or index is not found, but with different error codes in body
230232
return asyncFetch(
231233
getDocPath(config.origin, index, key),
232234
{

deps.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
export * as R from "https://cdn.skypack.dev/ramda@0.28.0";
22
export { default as crocks } from "https://cdn.skypack.dev/crocks@0.12.4";
33

4+
export {
5+
HyperErr,
6+
isHyperErr,
7+
} from "https://x.nest.land/hyper-utils@0.1.0/hyper-err.js";
8+
49
export { encode as base64Encode } from "https://deno.land/std@0.127.0/encoding/base64.ts";

deps_lock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"https://arweave.net/afjNOdumqZhfVMIjRw_8fCYZoecSB6XtHGZpNNUNIr8/hyper-err.js": "434ce4bd1cc58e220aad95148bfb04ad7c43738cb5f771a02a45ea8608ac71cc",
23
"https://cdn.skypack.dev/-/crocks@v0.12.4-Mje8nEhNx2rmIpwz3ROp/dist=es2019,mode=imports/optimized/crocks.js": "93d587d18dc5f124f30e5b38de37a6471eb65309c94ef2ffc7a36dc40ab394da",
34
"https://cdn.skypack.dev/-/ramda@v0.28.0-vDL6KbwpPkq7DJAM324O/dist=es2019,mode=imports/optimized/common/_Set-d915dfa1.js": "85edd0c60ae74c28bfd7a6d831d1274b2299de6c46cc19ddfed24f593365f0c8",
45
"https://cdn.skypack.dev/-/ramda@v0.28.0-vDL6KbwpPkq7DJAM324O/dist=es2019,mode=imports/optimized/common/_arity-fefc01f2.js": "dd07d98af13cc6d84a8fc656ae901b590bbeebfcdff21a1eb9903f3d22a1dbc9",

utils.js

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
import { crocks, R } from "./deps.js";
1+
import { crocks, HyperErr, isHyperErr, R } from "./deps.js";
22

33
const { Async } = crocks;
44
const {
5-
__,
65
assoc,
76
dissoc,
8-
isEmpty,
97
ifElse,
108
defaultTo,
11-
propEq,
12-
cond,
13-
is,
149
identity,
15-
T,
16-
complement,
17-
isNil,
1810
compose,
1911
has,
20-
allPass,
21-
anyPass,
22-
filter,
2312
evolve,
2413
applyTo,
2514
propOr,
@@ -37,63 +26,6 @@ const {
3726

3827
export const underscoreIdAlias = "__movedUnderscoreId63__";
3928

40-
const isDefined = complement(isNil);
41-
const isEmptyObject = allPass([
42-
complement(is(Array)), // not an array
43-
is(Object),
44-
isEmpty,
45-
]);
46-
const rejectNil = filter(isDefined);
47-
48-
/**
49-
* Constructs a hyper-esque error
50-
*
51-
* @typedef {Object} HyperErrArgs
52-
* @property {string} msg
53-
* @property {string?} status
54-
*
55-
* @typedef {Object} NotOk
56-
* @property {false} ok
57-
*
58-
* @param {(HyperErrArgs | string)} argsOrMsg
59-
* @returns {NotOk & HyperErrArgs} - the hyper-esque error
60-
*/
61-
export const HyperErr = (argsOrMsg) =>
62-
compose(
63-
({ ok, msg, status }) => rejectNil({ ok, msg, status }), // pick and filter nil
64-
assoc("ok", false),
65-
cond([
66-
// string
67-
[is(String), assoc("msg", __, {})],
68-
// { msg?, status? }
69-
[
70-
anyPass([
71-
isEmptyObject,
72-
has("msg"),
73-
has("status"),
74-
]),
75-
identity,
76-
],
77-
// Fallthrough to error
78-
[T, () => {
79-
throw new Error(
80-
"HyperErr args must be a string or an object with msg or status",
81-
);
82-
}],
83-
]),
84-
defaultTo({}),
85-
)(argsOrMsg);
86-
87-
export const isHyperErr = allPass([
88-
propEq("ok", false),
89-
/**
90-
* should not have an _id.
91-
* Otherwise it's a document ie data.retrieveDocument
92-
* or cache.getDoc
93-
*/
94-
complement(has("_id")),
95-
]);
96-
9729
export const handleHyperErr = ifElse(
9830
isHyperErr,
9931
Async.Resolved,

0 commit comments

Comments
 (0)