diff --git a/Makefile b/Makefile index 9ea1792..736c55f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ TEST_FILES := $(shell find ./**/*.ts) - test: $(TEST_FILES) deno test test.ts - deno check mod.ts \ No newline at end of file + deno check mod.ts diff --git a/healthUtil.ts b/healthUtil.ts new file mode 100644 index 0000000..d2b5e1d --- /dev/null +++ b/healthUtil.ts @@ -0,0 +1,33 @@ +import { Effects, ExpectedExports, ResultType } from "./types.ts"; + +export const checkWebUrl: ( + url: string, +) => (effects: Effects, duration: number) => Promise> = + (url) => { + return async (effects, duration) => { + await guardDurationAboveMinimum({ duration, minimumTime: 5000 }); + + return await effects.fetch(url) + .then((_) => ok) + .catch((e) => { + effects.warn(`Error while fetching URL: ${url}`); + effects.error(JSON.stringify(e)); + effects.error(e.toString()); + return error(`Error while fetching URL: ${url}`); + }); + }; + }; + +// Ensure the starting duration is pass a minimum +const guardDurationAboveMinimum = ( + input: { duration: number; minimumTime: number }, +) => + (input.duration <= input.minimumTime) + ? Promise.reject(errorCode(60, "Starting")) + : null; + +const errorCode = (code: number, error: string) => ({ + "error-code": [code, error] as const, +}); +const error = (error: string) => ({ error }); +const ok = { result: null }; diff --git a/mod.ts b/mod.ts index ac913c8..7cf9766 100644 --- a/mod.ts +++ b/mod.ts @@ -1,6 +1,6 @@ export { matches, YAML } from "./dependencies.ts"; export * as types from "./types.ts"; - export * as compat from "./compat/mod.ts"; export * as migrations from "./migrations.ts"; +export * as healthUtil from "./healthUtil.ts"; export * as util from "./util.ts";