envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
297 lines (284 loc) • 7.73 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
function alert(message) {
if ((typeof window !== 'undefined' && window.alert)) {
return (window.alert(message));
}
}
function mixinMissingIssue(envSafe, issue) {
let missingIssues = envSafe.maybeMissingIssues;
if (missingIssues !== undefined) {
missingIssues.push(issue);
} else {
envSafe.maybeMissingIssues = [issue];
}
}
function mixinInvalidIssue(envSafe, issue) {
let invalidIssues = envSafe.maybeInvalidIssues;
if (invalidIssues !== undefined) {
invalidIssues.push(issue);
} else {
envSafe.maybeInvalidIssues = [issue];
}
}
function make(envOpt) {
let env = envOpt !== undefined ? envOpt : process.env;
return {
env: env,
isLocked: false,
maybeMissingIssues: undefined,
maybeInvalidIssues: undefined
};
}
function close(envSafe) {
if (envSafe.isLocked) {
throw new Error(`[rescript-envsafe] ` + "EnvSafe is already closed.");
}
envSafe.isLocked = true;
let match = envSafe.maybeMissingIssues;
let match$1 = envSafe.maybeInvalidIssues;
if (match === undefined && match$1 === undefined) {
return;
}
let line = "========================================";
let output = [line];
if (match$1 !== undefined) {
let invalidIssues = Primitive_option.valFromOption(match$1);
output.push("❌ Invalid environment variables:");
invalidIssues.forEach(issue => {
output.push(` ` + issue.name + `: ` + S$RescriptSchema.$$Error.message(issue.error));
});
}
if (match !== undefined) {
let missingIssues = Primitive_option.valFromOption(match);
output.push("💨 Missing environment variables:");
missingIssues.forEach(issue => {
let match = issue.input;
let tmp;
tmp = match === "" ? "Disallowed empty string" : "Missing value";
output.push(` ` + issue.name + `: ` + tmp);
});
}
output.push(line);
let text = output.join("\n");
console.error(text);
alert(text);
throw new TypeError(text);
}
function boolCoerce(string) {
switch (string) {
case "0" :
case "f" :
case "false" :
return false;
case "1" :
case "t" :
case "true" :
return true;
default:
return string;
}
}
function numberCoerce(string) {
let float = (+string);
if (Number.isNaN(float)) {
return string;
} else {
return float;
}
}
function bigintCoerce(string) {
try {
return BigInt(string);
} catch (exn) {
return string;
}
}
function jsonCoerce(string) {
try {
return JSON.parse(string);
} catch (exn) {
return string;
}
}
function get(envSafe, name, schema, allowEmptyOpt, maybeFallback, maybeDevFallback, maybeInlinedInput) {
let allowEmpty = allowEmptyOpt !== undefined ? allowEmptyOpt : false;
if (envSafe.isLocked) {
throw new Error(`[rescript-envsafe] ` + "EnvSafe is closed. Make a new one to get access to environment variables.");
}
let input = maybeInlinedInput !== undefined ? Primitive_option.valFromOption(maybeInlinedInput) : envSafe.env[name];
let isMissing = input !== undefined ? (
input === "" ? !allowEmpty : false
) : true;
let match = schema.t;
let isOptional;
isOptional = typeof match !== "object" ? false : match.TAG === "option";
if (isMissing && !isOptional) {
if (maybeDevFallback !== undefined && envSafe.env["NODE_ENV"] !== "production") {
return Primitive_option.valFromOption(maybeDevFallback);
}
if (maybeFallback !== undefined) {
return Primitive_option.valFromOption(maybeFallback);
} else {
mixinMissingIssue(envSafe, {
name: name,
input: input
});
return undefined;
}
}
let optionalSchema = schema.t;
let tagged;
tagged = typeof optionalSchema !== "object" || optionalSchema.TAG !== "option" ? optionalSchema : optionalSchema._0.t;
let input$1;
if (input !== undefined) {
let exit = 0;
if (input === "" && !allowEmpty) {
input$1 = undefined;
} else {
exit = 1;
}
if (exit === 1) {
let tmp;
if (typeof tagged !== "object") {
switch (tagged) {
case "never" :
case "string" :
tmp = input;
break;
case "int32" :
case "number" :
tmp = numberCoerce(input);
break;
case "bigint" :
tmp = bigintCoerce(input);
break;
case "boolean" :
tmp = boolCoerce(input);
break;
default:
tmp = jsonCoerce(input);
}
} else {
switch (tagged.TAG) {
case "literal" :
switch (tagged._0.kind) {
case "String" :
tmp = input;
break;
case "Number" :
tmp = numberCoerce(input);
break;
case "Boolean" :
tmp = boolCoerce(input);
break;
case "BigInt" :
tmp = bigintCoerce(input);
break;
default:
tmp = jsonCoerce(input);
}
break;
case "union" :
tmp = input;
break;
default:
tmp = jsonCoerce(input);
}
}
input$1 = tmp;
}
} else {
input$1 = undefined;
}
let schema$1;
schema$1 = typeof tagged !== "object" || tagged.TAG !== "union" ? schema : S$RescriptSchema.preprocess(schema, s => {
let optionalSchema = s.schema.t;
let tagged;
tagged = typeof optionalSchema !== "object" || optionalSchema.TAG !== "option" ? optionalSchema : optionalSchema._0.t;
let exit = 0;
if (typeof tagged !== "object") {
switch (tagged) {
case "never" :
case "string" :
return {};
case "int32" :
case "number" :
exit = 4;
break;
case "bigint" :
exit = 3;
break;
case "boolean" :
exit = 2;
break;
default:
exit = 1;
}
} else {
switch (tagged.TAG) {
case "literal" :
switch (tagged._0.kind) {
case "String" :
return {};
case "Number" :
exit = 4;
break;
case "Boolean" :
exit = 2;
break;
case "BigInt" :
exit = 3;
break;
default:
exit = 1;
}
break;
case "union" :
return {};
default:
exit = 1;
}
}
switch (exit) {
case 1 :
return {
p: jsonCoerce
};
case 2 :
return {
p: boolCoerce
};
case 3 :
return {
p: bigintCoerce
};
case 4 :
return {
p: numberCoerce
};
}
});
try {
return S$RescriptSchema.parseOrThrow(input$1, schema$1);
} catch (raw_error) {
let error = Primitive_exceptions.internalToException(raw_error);
if (error.RE_EXN_ID === S$RescriptSchema.Raised) {
mixinInvalidIssue(envSafe, {
name: name,
error: error._1,
input: input$1
});
return undefined;
}
throw error;
}
}
export {
make,
close,
get,
}
/* S-RescriptSchema Not a pure module */