Skip to content

fix: add status and message on error #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 97 additions & 83 deletions adapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { R } from "./deps.js";
import { crocks, R } from "./deps.js";

import {
bulkPath,
Expand All @@ -25,64 +25,77 @@ const {
toPairs,
} = R;

const { Async } = crocks;

/**
*
* @typedef {Object} IndexInfo
* @property {string} index - index name
* @property {Object} mappings
*
* @typedef {Object} BulkSearchDoc
* @property {boolean} ok
* @property {string} [msg]
* @property {Array<any>} results
*
* @typedef {Object} SearchDoc
* @property {string} index
* @property {string} key
* @property {Object} doc
*
* @typedef {Object} SearchInfo
* @property {string} index
* @property {string} key
*
* @typedef {Object} SearchOptions
* @property {Array<string>} fields
* @property {Object} boost
* @property {boolean} prefix
*
* @typedef {Object} SearchQuery
* @property {string} index
* @property {string} query
* @property {SearchOptions} [options]
*
* @typedef {Object} Response
* @property {boolean} ok
* @property {string} [msg]
*
* @typedef {Object} ResponseWithResults
* @property {boolean} ok
* @property {string} [msg]
* @property {Array<any>} results
*
* @typedef {Object} ResponseWithMatches
* @property {boolean} ok
* @property {string} [msg]
* @property {Array<any>} matches
* @typedef {Object} IndexInfo
* @property {string} index - index name
* @property {Object} mappings
*
* @typedef {Object} BulkSearchDoc
* @property {boolean} ok
* @property {string} [msg]
* @property {Array<any>} results
*
* @typedef {Object} SearchDoc
* @property {string} index
* @property {string} key
* @property {Object} doc
*
* @typedef {Object} SearchInfo
* @property {string} index
* @property {string} key
*
* @typedef {Object} SearchOptions
* @property {Array<string>} fields
* @property {Object} boost
* @property {boolean} prefix
*
* @typedef {Object} SearchQuery
* @property {string} index
* @property {string} query
* @property {SearchOptions} [options]
*
* @typedef {Object} Response
* @property {boolean} ok
* @property {string} [msg]
*
* @typedef {Object} ResponseWithResults
* @property {boolean} ok
* @property {string} [msg]
* @property {Array<any>} results
*
* @typedef {Object} ResponseWithMatches
* @property {boolean} ok
* @property {string} [msg]
* @property {Array<any>} matches
*/

const handleRejectedResponse = (res) =>
Async.of(res)
.chain(Async.fromPromise((res) => res.json()))
.bichain(
() => Async.Rejected({ ok: false, status: res.status }), // not json body, so no message
(body) =>
Async.Rejected({
ok: false,
status: res.status,
msg: JSON.stringify(body),
}),
);

/**
* TODO:
* - Sanitize inputs ie. index names
* - Map Port api to Elasticsearch api for creating an index
* - Enable monitoring ie. with bimap(tap(console.err), tap(console.log))
* - How to support different versions of Elasticsearch?
* - ? Should we expose Elasticsearch response in result as res?
*/
* TODO:
* - Sanitize inputs ie. index names
* - Map Port api to Elasticsearch api for creating an index
* - Enable monitoring ie. with bimap(tap(console.err), tap(console.log))
* - How to support different versions of Elasticsearch?
* - ? Should we expose Elasticsearch response in result as res?
*/
export default function ({ config, asyncFetch, headers, handleResponse }) {
/**
* @param {IndexInfo}
* @returns {Promise<Response>}
*
*/
function createIndex({ index, mappings }) {
const properties = mappings.fields.reduce(
Expand All @@ -101,12 +114,10 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
}),
},
)
.chain(
handleResponse((res) => res.status < 400),
)
.bimap(
(res) => ({ ok: false, msg: JSON.stringify(res) }),
always({ ok: true }),
.chain(handleResponse((res) => res.status < 400))
.bichain(
handleRejectedResponse,
always(Async.Resolved({ ok: true })),
)
.toPromise();
}
Expand All @@ -126,9 +137,9 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
.chain(
handleResponse((res) => res.status === 200),
)
.bimap(
always({ ok: false }),
always({ ok: true }),
.bichain(
handleRejectedResponse,
always(Async.Resolved({ ok: true })),
)
.toPromise();
}
Expand All @@ -149,9 +160,9 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
.chain(
handleResponse((res) => res.status < 400),
)
.bimap(
always({ ok: false }),
always({ ok: true }),
.bichain(
handleRejectedResponse,
always(Async.Resolved({ ok: true })),
)
.toPromise();
}
Expand All @@ -171,9 +182,9 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
.chain(
handleResponse((res) => res.status < 400),
)
.bimap(
always({ ok: false }),
(res) => ({ ok: true, key, doc: res }),
.bichain(
handleRejectedResponse,
(res) => Async.Resolved({ ok: true, key, doc: res }),
)
.toPromise();
}
Expand All @@ -194,9 +205,9 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
.chain(
handleResponse((res) => res.status < 400),
)
.bimap(
always({ ok: false }),
always({ ok: true }),
.bichain(
handleRejectedResponse,
always(Async.Resolved({ ok: true })),
)
.toPromise();
}
Expand All @@ -216,9 +227,9 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
.chain(
handleResponse((res) => res.status < 400),
)
.bimap(
always({ ok: false }),
always({ ok: true }),
.bichain(
handleRejectedResponse,
always(Async.Resolved({ ok: true })),
)
.toPromise();
}
Expand Down Expand Up @@ -255,15 +266,14 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
.chain(
handleResponse((res) => res.status < 400),
)
.bimap(
always({ ok: false }),
(res) => ({ ok: true, results: res.items }),
.bichain(
handleRejectedResponse,
(res) => Async.Resolved({ ok: true, results: res.items }),
)
.toPromise();
}

/**
*
* @param {SearchQuery}
* @returns {Promise<ResponseWithMatches>}
*/
Expand Down Expand Up @@ -294,13 +304,17 @@ export default function ({ config, asyncFetch, headers, handleResponse }) {
},
)
.chain(handleResponse((res) => res.status < 400))
.bimap(
// TODO: what should message be for a failed query?
(res) => ({ ok: false, msg: JSON.stringify(res) }),
(res) => ({
ok: true,
matches: pluck("_source", res.hits.hits),
}),
.bichain(
// query failure
handleRejectedResponse,
// Success
(res) =>
Async.Resolved(
({
ok: true,
matches: pluck("_source", res.hits.hits),
}),
),
)
.toPromise();
}
Expand Down
7 changes: 4 additions & 3 deletions async-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const asyncFetch = (fetch) => Async.fromPromise(fetch);
const createHeaders = (username, password) =>
pipe(
assoc("Content-Type", "application/json"),
assoc("Accept", "application/json"),
ifElse(
() => username && password,
assoc(
Expand All @@ -24,10 +25,10 @@ const createHeaders = (username, password) =>
const handleResponse = (pred) =>
ifElse(
(res) => pred(res),
(res) => Async.fromPromise(() => res.json())(),
(res) =>
Async.fromPromise(() => res.json())()
.chain(Async.Rejected),
Async.of(res)
.chain(Async.fromPromise((res) => res.json())),
(res) => Async.Rejected(res),
);

export { asyncFetch, createHeaders, handleResponse };
2 changes: 1 addition & 1 deletion deps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as R from "https://cdn.skypack.dev/ramda@^0.27.1";
export { default as crocks } from "https://cdn.skypack.dev/crocks@^0.12.4";

export { encode as base64Encode } from "https://deno.land/std@0.116.0/encoding/base64.ts";
export { encode as base64Encode } from "https://deno.land/std@0.117.0/encoding/base64.ts";
2 changes: 1 addition & 1 deletion deps_lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"https://cdn.skypack.dev/-/[email protected]/dist=es2019,mode=imports/optimized/ramda.js": "0e51cd76039fc9669f9179d935b261bcbb19ecbb11d49e7e60cfbc14360a85d2",
"https://cdn.skypack.dev/crocks@^0.12.4": "fc624b776247aa182539fed6fe1b21a578609d1c2c0e31830c4469f2bcd099cd",
"https://cdn.skypack.dev/ramda@^0.27.1": "a87add6080b004efb8072f81063306b03dac107cd2b6e5fca726f1a8bbb969b4",
"https://deno.land/std@0.116.0/encoding/base64.ts": "0b58bd6477214838bf711eef43eac21e47ba9e5c81b2ce185fe25d9ecab3ebb3"
"https://deno.land/std@0.117.0/encoding/base64.ts": "0b58bd6477214838bf711eef43eac21e47ba9e5c81b2ce185fe25d9ecab3ebb3"
}
2 changes: 1 addition & 1 deletion dev_deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export {
assertEquals,
assertObjectMatch,
assertThrows,
} from "https://deno.land/std@0.116.0/testing/asserts.ts";
} from "https://deno.land/std@0.117.0/testing/asserts.ts";
export { spy } from "https://deno.land/x/[email protected]/mod.ts";
6 changes: 3 additions & 3 deletions dev_deps_lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"https://deno.land/[email protected]/fmt/colors.ts": "8368ddf2d48dfe413ffd04cdbb7ae6a1009cf0dccc9c7ff1d76259d9c61a0621",
"https://deno.land/[email protected]/testing/_diff.ts": "e6a10d2aca8d6c27a9c5b8a2dbbf64353874730af539707b5b39d4128140642d",
"https://deno.land/[email protected]/testing/asserts.ts": "a1fef0239a2c343b0baa49c77dcdd7412613c46f3aba2887c331a2d7ed1f645e",
"https://deno.land/std@0.116.0/fmt/colors.ts": "8368ddf2d48dfe413ffd04cdbb7ae6a1009cf0dccc9c7ff1d76259d9c61a0621",
"https://deno.land/std@0.116.0/testing/_diff.ts": "e6a10d2aca8d6c27a9c5b8a2dbbf64353874730af539707b5b39d4128140642d",
"https://deno.land/std@0.116.0/testing/asserts.ts": "a1fef0239a2c343b0baa49c77dcdd7412613c46f3aba2887c331a2d7ed1f645e",
"https://deno.land/std@0.117.0/fmt/colors.ts": "8368ddf2d48dfe413ffd04cdbb7ae6a1009cf0dccc9c7ff1d76259d9c61a0621",
"https://deno.land/std@0.117.0/testing/_diff.ts": "e6a10d2aca8d6c27a9c5b8a2dbbf64353874730af539707b5b39d4128140642d",
"https://deno.land/std@0.117.0/testing/asserts.ts": "a1fef0239a2c343b0baa49c77dcdd7412613c46f3aba2887c331a2d7ed1f645e",
"https://deno.land/x/[email protected]/common.ts": "34e8367e3696c3f872ae417d7c421fa908a5a2125a1c4cb259f7dee9561a7096",
"https://deno.land/x/[email protected]/comparators.ts": "08563340dbb0051f032bacdcf854bcabd13d607d2e8cb1889826417419df89d0",
"https://deno.land/x/[email protected]/trees/bs_node.ts": "854d39f6d60cdcb47e1183f0fa67091e6bad59dd2b13252a8b38b1b37269fa67",
Expand Down