0.1.1 • Published 1 year ago
nanocluster v0.1.1
nanocluster
Client library for interacting with nanocluster.io.
connectServices
Connects to services in a nanocluster.
import { connectServices, nt } from "nanocluster";
const { hello } = await connectServices(
"https://px2zvh7ykb.nanocluster.io/rpc",
{
hello: {
run: nt.fn()(nt.string),
},
},
);
console.log(await hello.run()); // Hello world!
Throws an exception if the specified protocol is not compatible with the current deployments.
nt
A runtime typing system. (Short for NanoType
.)
import { nt } from "nanocluster";
const LogMessage = nt.object({
level: nt.union(
nt.literal("INFO"),
nt.literal("ERROR"),
),
message: nt.string,
});
type LogMessage = nt.TypeOf<typeof LogMessage>;
function handleUnknownData(data: unknown) {
if (nt.check(data, LogMessage)) {
// TypeScript knows that data is a LogMessage here
console.log(`A log message. Level: ${data.level}.`);
}
}
{
// (string | number) => string
const ActualApi = nt.fn(
nt.union(
nt.string,
nt.number,
),
)(nt.string);
// (string) => string
const RequiredApi = nt.fn(nt.string)(nt.string);
console.log(
// true: it's ok to use ActualApi in place of RequiredApi because the
// string argument will work in ActualApi and the returned string
// matches RequiredApi.
nt.checkAssignable(RequiredApi, ActualApi),
);
}
{
// (string) => string | number
const ActualApi = nt.fn(
nt.string
)(
nt.union(
nt.string,
nt.number,
),
);
// (string) => string
const RequiredApi = nt.fn(nt.string)(nt.string);
console.log(
// false: ActualApi might return number, which is not compatible with
// RequiredApi's return type of string.
nt.checkAssignable(RequiredApi, ActualApi),
);
}
toBuffer
Takes plain data and encodes it as a buffer (Uint8Array
).
Currently this is just a wrapper around msgpackr.pack
, but it will change over time to support the needs of nanocluster (e.g. supporting extra JavaScript types like Date
and bigint
, and things like NanoType
).
fromBuffer
Decodes a buffer produced by toBuffer
.