0.0.9 • Published 2 years ago
@p.zarkov/hotstuff v0.0.9
🔥 Hot Stuff 🔥
Various NodeJS utils
Table of Contents
📮 fetchService
for everything http
usage
import { fetchService } from "@p.zarkov/hotstuff";
const myRes = await fetchService({
url: "https://www.yoururl.com/",
method: "POST",
payload: { some: "payload" },
options: {
path: "/somePath/{pathParamToReplace}",
pathParams: {
pathParamToReplace: "myDynamicPathParam"
},
queryParams: {
someQueryParam: true
}
}
});
response is one of the two:
type HttpSuccessResponse<T> = {
isGood: true;
statusCode: number;
elapsed: number;
response: T;
};
type HttpErrorResponse<T> = {
isGood: false;
error: string;
elapsed: number;
statusCode: number;
response?: T;
};
⏲️ Stopwatch
for timing
usage
import { Stopwatch } from "@p.zarkov/hotstuff";
const mySW = new Stopwatch();
(() => Promise)(); // Some action
mySW.getElapsedMs(); // elapsed miliseconds since construction
mySW.getElapsedS(); // elapsed seconds since construction
⚙️ UrlUtils
Build your URL from an already existing URL or a string with optional query params
or just use the methods in isolation
- buildQuery
import { UrlUtils } from "@p.zarkov/hotstuff";
// All three result in http://localhost:4444/?query1=val1&query2=true
UrlUtils.instance.buildQuery("http://localhost:4444/", { query1: "val1", query2: true });
UrlUtils.instance.buildQuery("http://localhost:4444", { query1: "val1", query2: true });
UrlUtils.instance.buildQuery(new URL("http://localhost:4444/"), { query1: "val1", query2: true });
// Nullables are ignored
// Results in http://localhost:4444/?query1=val1
UrlUtils.instance.buildQuery(new URL("http://localhost:4444/"), { query1: "val1", query2: undefined });
- replacePathParams - basically a formatUnicorn
import { UrlUtils } from "@p.zarkov/hotstuff";
// Results in "Some text."
UrlUtils.instance.replacePathParams("Some {replacement}.", { replacement: "text" });
- buildFromString - builds a new URL from string
import { UrlUtils } from "@p.zarkov/hotstuff";
// Domain can end with / or not
// Path can start with / or not
// Both result in "http://localhost:4444/some/path/params/"
UrlUtils.instance.buildFromString("http://localhost:4444/", "/some/path/params/");
UrlUtils.instance.buildFromString("http://localhost:4444", "some/path/params/");
- build - combines buildQuery, buildFromString, replacePathParams
import { UrlUtils } from "@p.zarkov/hotstuff";
// Results in "http://localhost:4444/base/path/somedynamicpath/?someQ=1"
UrlUtils.instance.build({
base: "http://localhost:4444/{someBasePath}",
path: "/path/{dynamic}/",
pathParams: { someBasePath: "base", dynamic: "somedynamicpath" },
queryParams: { someQ: "1"}
});
📜 HotLogger
Log all you need
usage
import { HotLogger } from "@p.zarkov/hotstuff";
const myLogger = new HotLogger.createLogger("MyLoggerContext");
// Logs
[{"Message":"Some info msg","LogLevel":"Trace","SourceContext":"MyLoggerContext","data":{"smh":"ye"},"ProcessID":21268,"AppVersion":"0.0.5","AppName":"@p.zarkov/hotstuff","Env":"development","LogTimestamp":"2021-12-08T13:06:01.911Z"}]
myLogger.trace("Some info msg", { data: { smh: "ye"} });
// Logs
[{"Message":"Some err msg","LogLevel":"Error","SourceContext":"WeHot","ExceptionMessage":"yer error","ExceptionStacktrace":"Error: yer error at Object.<anonymous> at (C:\\hotstuff\\index.js:18:40),"ProcessID":15320,"AppVersion":"0.0.5","AppName":"@p.zarkov/hotstuff","Env":"development","LogTimestamp":"2021-12-08T13:32:45.847Z"}]
myLogger.error("Some err msg", { err: new Error("yer error") });