upend/tools/upend_js/types.ts

134 lines
2.6 KiB
TypeScript

export type Address = `@${string}`;
export type ADDRESS_TYPE = "Hash" | "Uuid" | "Attribute" | "Url";
export type VALUE_TYPE = "Address" | "String" | "Number" | "Invalid";
export function isAddress(address: string): address is Address {
return address.match(/^@[0-9a-zA-Z]+$/) !== null;
}
export function asAddress(address: string): Address {
const result = address.startsWith("@") ? address : `@${address}`;
if (!isAddress(result)) {
throw new Error(`Invalid address: ${address}`);
}
return result;
}
/**
* A single atomic entry in UpEnd.
*/
export interface IEntry {
/** The entity this entry annotates. */
entity: Address;
/** The attribute of the relation. */
attribute: string;
/** The value of the attribute. */
value: IValue;
/** The origin or provenance of the data entry (e.g. SYSTEM or USER API...) */
provenance: string;
/** The timestamp when the data entry was created in RFC 3339 format. */
timestamp: string;
}
export type IValue =
| {
t: "Address";
c: Address;
}
| {
t: "String";
c: string;
}
| {
t: "Number";
c: number;
}
| {
t: "Null";
c: null;
};
export interface InvariantEntry {
attribute: string;
value: IValue;
}
export type InAddress =
| Address
| { t: "Attribute" | "Url" | "Uuid"; c?: string };
export type InEntry = {
entity: Address;
attribute: string;
value: IValue;
provenance?: string;
};
export type PutInput =
| InEntry
| InEntry[]
| InvariantEntry
| { entity: InAddress };
export interface ListingResult {
[key: Address]: IEntry;
}
// entry address, entity address address
export type PutResult = [Address | undefined, Address];
// export type OrderedListing = [Address, IEntry][];
export type AttributeListingResult = Array<{
name: string;
labels: string[];
}>;
export interface IFile {
hash: string;
path: string;
valid: boolean;
added: string;
size: number;
mtime: string;
}
export interface IJob {
title: string;
job_type: string;
progress: number;
state: "InProgress" | "Done" | "Failed";
}
export interface VaultInfo {
name: string | null;
location: string;
version: string;
desktop: boolean;
}
export interface StoreInfo {
totals: { count: number; size: number };
blobs: {
hash: string;
size: number;
paths: { added: number; valid: boolean; path: string }[];
}[];
}
export type EntityInfo =
| {
t: "Hash" | "Uuid";
}
| {
t: "Attribute" | "Url";
c: string;
};
export interface EntityListing {
entity: EntityInfo;
entries: ListingResult;
}