@xyo-network/payload-builder
Advanced tools
Comparing version
import type { Hash } from '@xylabs/hex'; | ||
import type { AnyObject, JsonObject } from '@xylabs/object'; | ||
import type { Payload, PayloadWithMeta, WithMeta } from '@xyo-network/payload-model'; | ||
import type { WithoutMeta, WithoutSchema } from './BuilderBase.ts'; | ||
import type { AnyObject } from '@xylabs/object'; | ||
import type { Payload } from '@xyo-network/payload-model'; | ||
import { PayloadBuilderBase } from './BuilderBase.ts'; | ||
import type { PayloadBuilderOptions } from './Options.ts'; | ||
export interface BuildOptions { | ||
stamp?: boolean; | ||
validate?: boolean; | ||
} | ||
export declare class PayloadBuilder<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> extends PayloadBuilderBase<T, O> { | ||
static build<T extends Payload = Payload<AnyObject>>(payload: T, options?: BuildOptions): Promise<WithMeta<T>>; | ||
static build<T extends Payload = Payload<AnyObject>>(payload: T[], options?: BuildOptions): Promise<WithMeta<T>[]>; | ||
static dataHash<T extends Payload>(payload: T, options?: BuildOptions): Promise<Hash>; | ||
static dataHashPairs<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<[WithMeta<T>, Hash][]>; | ||
static dataHashes(payloads: undefined, options?: BuildOptions): Promise<undefined>; | ||
static dataHashes<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<Hash[]>; | ||
static dataHash<T extends Payload>(payload: T): Promise<Hash>; | ||
static dataHashPairs<T extends Payload>(payloads: T[]): Promise<[T, Hash][]>; | ||
static dataHashes(payloads: undefined): Promise<undefined>; | ||
static dataHashes<T extends Payload>(payloads: T[]): Promise<Hash[]>; | ||
static filterExclude<T extends Payload>(payloads: T[] | undefined, hash: Hash[] | Hash): Promise<T[]>; | ||
@@ -22,3 +15,3 @@ static filterExcludeByDataHash<T extends Payload>(payloads: T[] | undefined, hash: Hash[] | Hash): Promise<T[]>; | ||
static findByDataHash<T extends Payload>(payloads: T[] | undefined, hash: Hash): Promise<T | undefined>; | ||
static hash<T extends Payload>(payload: T, options?: BuildOptions): Promise<Hash>; | ||
static hash<T extends Payload>(payload: T): Promise<Hash>; | ||
/** | ||
@@ -29,8 +22,8 @@ * Creates an array of payload/hash tuples based on the payloads passed in | ||
*/ | ||
static hashPairs<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<[WithMeta<T>, Hash][]>; | ||
static hashableFields<T extends Payload = Payload<AnyObject>>(schema: string, fields?: WithoutSchema<WithoutMeta<T>>, $meta?: JsonObject, $hash?: Hash, timestamp?: number, stamp?: boolean): Promise<WithMeta<T>>; | ||
static hashPairs<T extends Payload>(payloads: T[]): Promise<[T, Hash][]>; | ||
static hashableFields<T extends Payload>(payload: T): T; | ||
static hashes(payloads: undefined): Promise<undefined>; | ||
static hashes<T extends Payload>(payloads: T[]): Promise<Hash[]>; | ||
static toAllHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static toDataHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static toAllHashMap<T extends Payload>(payloads: T[]): Promise<Record<Hash, T>>; | ||
static toDataHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, T>>; | ||
/** | ||
@@ -41,9 +34,5 @@ * Creates an object map of payload hashes to payloads based on the payloads passed in | ||
*/ | ||
static toHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static withoutMeta(payload: undefined): undefined; | ||
static withoutMeta<T extends PayloadWithMeta>(payload: T): Omit<T, '$meta'>; | ||
static withoutMeta<T extends PayloadWithMeta>(payloads: T[]): Omit<T, '$meta'>[]; | ||
build(options?: BuildOptions): Promise<WithMeta<T>>; | ||
hashableFields(): Promise<WithMeta<T>>; | ||
static toHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, T>>; | ||
build(): T; | ||
} | ||
//# sourceMappingURL=Builder.d.ts.map |
@@ -1,24 +0,23 @@ | ||
import type { Hash } from '@xylabs/hex'; | ||
import type { AnyObject, JsonObject } from '@xylabs/object'; | ||
import type { AnyObject } from '@xylabs/object'; | ||
import type { Promisable } from '@xylabs/promise'; | ||
import type { Payload, Schema, WithOptionalMeta } from '@xyo-network/payload-model'; | ||
import type { Payload, Schema } from '@xyo-network/payload-model'; | ||
import type { PayloadBuilderOptions } from './Options.ts'; | ||
export type WithOptionalSchema<T extends Payload> = Omit<T, 'schema'> & Partial<T>; | ||
export type WithoutSchema<T extends WithOptionalSchema<Payload>> = Omit<T, 'schema'>; | ||
export type WithoutMeta<T extends WithOptionalMeta<Payload>> = Omit<T, '$hash' | '$meta'>; | ||
export declare const removeMetaAndSchema: <T extends Payload>(payload: WithOptionalSchema<WithOptionalMeta<T>>) => WithoutSchema<WithoutMeta<T>>; | ||
export declare const removeMetaAndSchema: <T extends Payload>(payload: Partial<WithOptionalSchema<T>>) => WithoutSchema<T>; | ||
export declare class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> { | ||
readonly options: O; | ||
protected _$meta?: JsonObject; | ||
protected _fields?: WithoutSchema<WithoutMeta<T>>; | ||
protected _fields?: Partial<WithoutSchema<T>>; | ||
protected _schema: Schema; | ||
constructor(options: O); | ||
static dataHashableFields<T extends Payload = Payload<AnyObject>>(schema: string, fields?: WithoutSchema<WithoutMeta<T>>): Promisable<Omit<T, '$hash' | '$meta'>>; | ||
protected static metaFields(dataHash: Hash, otherMeta?: JsonObject, stamp?: boolean): Promisable<JsonObject>; | ||
$meta(meta?: JsonObject): this; | ||
dataHashableFields(): Promise<Omit<T, "$hash" | "$meta">>; | ||
fields(fields: WithOptionalSchema<WithOptionalMeta<T>>): this; | ||
static dataHashableFields<T extends Payload>(schema: Schema, payload: WithoutSchema<T>): Promisable<Payload>; | ||
static omitClientMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
static omitMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
static omitStorageMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
dataHashableFields(): Promise<{ | ||
schema: string; | ||
} & import("@xyo-network/payload-model").PayloadFields>; | ||
fields(fields: WithOptionalSchema<T>): this; | ||
schema(value: Schema): void; | ||
protected metaFields(dataHash: Hash, stamp?: boolean): Promise<JsonObject>; | ||
} | ||
//# sourceMappingURL=BuilderBase.d.ts.map |
@@ -5,3 +5,3 @@ import type { Logger } from '@xylabs/logger'; | ||
export interface PayloadBuilderOptions<T> { | ||
readonly fields?: Omit<T, 'schema' | '$hash' | '$meta'>; | ||
readonly fields?: Partial<T>; | ||
readonly logger?: Logger; | ||
@@ -8,0 +8,0 @@ readonly meta?: JsonObject; |
import type { Hash } from '@xylabs/hex'; | ||
import type { AnyObject, JsonObject } from '@xylabs/object'; | ||
import type { Payload, PayloadWithMeta, WithMeta } from '@xyo-network/payload-model'; | ||
import type { WithoutMeta, WithoutSchema } from './BuilderBase.ts'; | ||
import type { AnyObject } from '@xylabs/object'; | ||
import type { Payload } from '@xyo-network/payload-model'; | ||
import { PayloadBuilderBase } from './BuilderBase.ts'; | ||
import type { PayloadBuilderOptions } from './Options.ts'; | ||
export interface BuildOptions { | ||
stamp?: boolean; | ||
validate?: boolean; | ||
} | ||
export declare class PayloadBuilder<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> extends PayloadBuilderBase<T, O> { | ||
static build<T extends Payload = Payload<AnyObject>>(payload: T, options?: BuildOptions): Promise<WithMeta<T>>; | ||
static build<T extends Payload = Payload<AnyObject>>(payload: T[], options?: BuildOptions): Promise<WithMeta<T>[]>; | ||
static dataHash<T extends Payload>(payload: T, options?: BuildOptions): Promise<Hash>; | ||
static dataHashPairs<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<[WithMeta<T>, Hash][]>; | ||
static dataHashes(payloads: undefined, options?: BuildOptions): Promise<undefined>; | ||
static dataHashes<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<Hash[]>; | ||
static dataHash<T extends Payload>(payload: T): Promise<Hash>; | ||
static dataHashPairs<T extends Payload>(payloads: T[]): Promise<[T, Hash][]>; | ||
static dataHashes(payloads: undefined): Promise<undefined>; | ||
static dataHashes<T extends Payload>(payloads: T[]): Promise<Hash[]>; | ||
static filterExclude<T extends Payload>(payloads: T[] | undefined, hash: Hash[] | Hash): Promise<T[]>; | ||
@@ -22,3 +15,3 @@ static filterExcludeByDataHash<T extends Payload>(payloads: T[] | undefined, hash: Hash[] | Hash): Promise<T[]>; | ||
static findByDataHash<T extends Payload>(payloads: T[] | undefined, hash: Hash): Promise<T | undefined>; | ||
static hash<T extends Payload>(payload: T, options?: BuildOptions): Promise<Hash>; | ||
static hash<T extends Payload>(payload: T): Promise<Hash>; | ||
/** | ||
@@ -29,8 +22,8 @@ * Creates an array of payload/hash tuples based on the payloads passed in | ||
*/ | ||
static hashPairs<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<[WithMeta<T>, Hash][]>; | ||
static hashableFields<T extends Payload = Payload<AnyObject>>(schema: string, fields?: WithoutSchema<WithoutMeta<T>>, $meta?: JsonObject, $hash?: Hash, timestamp?: number, stamp?: boolean): Promise<WithMeta<T>>; | ||
static hashPairs<T extends Payload>(payloads: T[]): Promise<[T, Hash][]>; | ||
static hashableFields<T extends Payload>(payload: T): T; | ||
static hashes(payloads: undefined): Promise<undefined>; | ||
static hashes<T extends Payload>(payloads: T[]): Promise<Hash[]>; | ||
static toAllHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static toDataHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static toAllHashMap<T extends Payload>(payloads: T[]): Promise<Record<Hash, T>>; | ||
static toDataHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, T>>; | ||
/** | ||
@@ -41,9 +34,5 @@ * Creates an object map of payload hashes to payloads based on the payloads passed in | ||
*/ | ||
static toHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static withoutMeta(payload: undefined): undefined; | ||
static withoutMeta<T extends PayloadWithMeta>(payload: T): Omit<T, '$meta'>; | ||
static withoutMeta<T extends PayloadWithMeta>(payloads: T[]): Omit<T, '$meta'>[]; | ||
build(options?: BuildOptions): Promise<WithMeta<T>>; | ||
hashableFields(): Promise<WithMeta<T>>; | ||
static toHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, T>>; | ||
build(): T; | ||
} | ||
//# sourceMappingURL=Builder.d.ts.map |
@@ -1,24 +0,23 @@ | ||
import type { Hash } from '@xylabs/hex'; | ||
import type { AnyObject, JsonObject } from '@xylabs/object'; | ||
import type { AnyObject } from '@xylabs/object'; | ||
import type { Promisable } from '@xylabs/promise'; | ||
import type { Payload, Schema, WithOptionalMeta } from '@xyo-network/payload-model'; | ||
import type { Payload, Schema } from '@xyo-network/payload-model'; | ||
import type { PayloadBuilderOptions } from './Options.ts'; | ||
export type WithOptionalSchema<T extends Payload> = Omit<T, 'schema'> & Partial<T>; | ||
export type WithoutSchema<T extends WithOptionalSchema<Payload>> = Omit<T, 'schema'>; | ||
export type WithoutMeta<T extends WithOptionalMeta<Payload>> = Omit<T, '$hash' | '$meta'>; | ||
export declare const removeMetaAndSchema: <T extends Payload>(payload: WithOptionalSchema<WithOptionalMeta<T>>) => WithoutSchema<WithoutMeta<T>>; | ||
export declare const removeMetaAndSchema: <T extends Payload>(payload: Partial<WithOptionalSchema<T>>) => WithoutSchema<T>; | ||
export declare class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> { | ||
readonly options: O; | ||
protected _$meta?: JsonObject; | ||
protected _fields?: WithoutSchema<WithoutMeta<T>>; | ||
protected _fields?: Partial<WithoutSchema<T>>; | ||
protected _schema: Schema; | ||
constructor(options: O); | ||
static dataHashableFields<T extends Payload = Payload<AnyObject>>(schema: string, fields?: WithoutSchema<WithoutMeta<T>>): Promisable<Omit<T, '$hash' | '$meta'>>; | ||
protected static metaFields(dataHash: Hash, otherMeta?: JsonObject, stamp?: boolean): Promisable<JsonObject>; | ||
$meta(meta?: JsonObject): this; | ||
dataHashableFields(): Promise<Omit<T, "$hash" | "$meta">>; | ||
fields(fields: WithOptionalSchema<WithOptionalMeta<T>>): this; | ||
static dataHashableFields<T extends Payload>(schema: Schema, payload: WithoutSchema<T>): Promisable<Payload>; | ||
static omitClientMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
static omitMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
static omitStorageMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
dataHashableFields(): Promise<{ | ||
schema: string; | ||
} & import("@xyo-network/payload-model").PayloadFields>; | ||
fields(fields: WithOptionalSchema<T>): this; | ||
schema(value: Schema): void; | ||
protected metaFields(dataHash: Hash, stamp?: boolean): Promise<JsonObject>; | ||
} | ||
//# sourceMappingURL=BuilderBase.d.ts.map |
@@ -5,3 +5,3 @@ import type { Logger } from '@xylabs/logger'; | ||
export interface PayloadBuilderOptions<T> { | ||
readonly fields?: Omit<T, 'schema' | '$hash' | '$meta'>; | ||
readonly fields?: Partial<T>; | ||
readonly logger?: Logger; | ||
@@ -8,0 +8,0 @@ readonly meta?: JsonObject; |
import type { Hash } from '@xylabs/hex'; | ||
import type { AnyObject, JsonObject } from '@xylabs/object'; | ||
import type { Payload, PayloadWithMeta, WithMeta } from '@xyo-network/payload-model'; | ||
import type { WithoutMeta, WithoutSchema } from './BuilderBase.ts'; | ||
import type { AnyObject } from '@xylabs/object'; | ||
import type { Payload } from '@xyo-network/payload-model'; | ||
import { PayloadBuilderBase } from './BuilderBase.ts'; | ||
import type { PayloadBuilderOptions } from './Options.ts'; | ||
export interface BuildOptions { | ||
stamp?: boolean; | ||
validate?: boolean; | ||
} | ||
export declare class PayloadBuilder<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> extends PayloadBuilderBase<T, O> { | ||
static build<T extends Payload = Payload<AnyObject>>(payload: T, options?: BuildOptions): Promise<WithMeta<T>>; | ||
static build<T extends Payload = Payload<AnyObject>>(payload: T[], options?: BuildOptions): Promise<WithMeta<T>[]>; | ||
static dataHash<T extends Payload>(payload: T, options?: BuildOptions): Promise<Hash>; | ||
static dataHashPairs<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<[WithMeta<T>, Hash][]>; | ||
static dataHashes(payloads: undefined, options?: BuildOptions): Promise<undefined>; | ||
static dataHashes<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<Hash[]>; | ||
static dataHash<T extends Payload>(payload: T): Promise<Hash>; | ||
static dataHashPairs<T extends Payload>(payloads: T[]): Promise<[T, Hash][]>; | ||
static dataHashes(payloads: undefined): Promise<undefined>; | ||
static dataHashes<T extends Payload>(payloads: T[]): Promise<Hash[]>; | ||
static filterExclude<T extends Payload>(payloads: T[] | undefined, hash: Hash[] | Hash): Promise<T[]>; | ||
@@ -22,3 +15,3 @@ static filterExcludeByDataHash<T extends Payload>(payloads: T[] | undefined, hash: Hash[] | Hash): Promise<T[]>; | ||
static findByDataHash<T extends Payload>(payloads: T[] | undefined, hash: Hash): Promise<T | undefined>; | ||
static hash<T extends Payload>(payload: T, options?: BuildOptions): Promise<Hash>; | ||
static hash<T extends Payload>(payload: T): Promise<Hash>; | ||
/** | ||
@@ -29,8 +22,8 @@ * Creates an array of payload/hash tuples based on the payloads passed in | ||
*/ | ||
static hashPairs<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<[WithMeta<T>, Hash][]>; | ||
static hashableFields<T extends Payload = Payload<AnyObject>>(schema: string, fields?: WithoutSchema<WithoutMeta<T>>, $meta?: JsonObject, $hash?: Hash, timestamp?: number, stamp?: boolean): Promise<WithMeta<T>>; | ||
static hashPairs<T extends Payload>(payloads: T[]): Promise<[T, Hash][]>; | ||
static hashableFields<T extends Payload>(payload: T): T; | ||
static hashes(payloads: undefined): Promise<undefined>; | ||
static hashes<T extends Payload>(payloads: T[]): Promise<Hash[]>; | ||
static toAllHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static toDataHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static toAllHashMap<T extends Payload>(payloads: T[]): Promise<Record<Hash, T>>; | ||
static toDataHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, T>>; | ||
/** | ||
@@ -41,9 +34,5 @@ * Creates an object map of payload hashes to payloads based on the payloads passed in | ||
*/ | ||
static toHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>>; | ||
static withoutMeta(payload: undefined): undefined; | ||
static withoutMeta<T extends PayloadWithMeta>(payload: T): Omit<T, '$meta'>; | ||
static withoutMeta<T extends PayloadWithMeta>(payloads: T[]): Omit<T, '$meta'>[]; | ||
build(options?: BuildOptions): Promise<WithMeta<T>>; | ||
hashableFields(): Promise<WithMeta<T>>; | ||
static toHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, T>>; | ||
build(): T; | ||
} | ||
//# sourceMappingURL=Builder.d.ts.map |
@@ -1,24 +0,23 @@ | ||
import type { Hash } from '@xylabs/hex'; | ||
import type { AnyObject, JsonObject } from '@xylabs/object'; | ||
import type { AnyObject } from '@xylabs/object'; | ||
import type { Promisable } from '@xylabs/promise'; | ||
import type { Payload, Schema, WithOptionalMeta } from '@xyo-network/payload-model'; | ||
import type { Payload, Schema } from '@xyo-network/payload-model'; | ||
import type { PayloadBuilderOptions } from './Options.ts'; | ||
export type WithOptionalSchema<T extends Payload> = Omit<T, 'schema'> & Partial<T>; | ||
export type WithoutSchema<T extends WithOptionalSchema<Payload>> = Omit<T, 'schema'>; | ||
export type WithoutMeta<T extends WithOptionalMeta<Payload>> = Omit<T, '$hash' | '$meta'>; | ||
export declare const removeMetaAndSchema: <T extends Payload>(payload: WithOptionalSchema<WithOptionalMeta<T>>) => WithoutSchema<WithoutMeta<T>>; | ||
export declare const removeMetaAndSchema: <T extends Payload>(payload: Partial<WithOptionalSchema<T>>) => WithoutSchema<T>; | ||
export declare class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> { | ||
readonly options: O; | ||
protected _$meta?: JsonObject; | ||
protected _fields?: WithoutSchema<WithoutMeta<T>>; | ||
protected _fields?: Partial<WithoutSchema<T>>; | ||
protected _schema: Schema; | ||
constructor(options: O); | ||
static dataHashableFields<T extends Payload = Payload<AnyObject>>(schema: string, fields?: WithoutSchema<WithoutMeta<T>>): Promisable<Omit<T, '$hash' | '$meta'>>; | ||
protected static metaFields(dataHash: Hash, otherMeta?: JsonObject, stamp?: boolean): Promisable<JsonObject>; | ||
$meta(meta?: JsonObject): this; | ||
dataHashableFields(): Promise<Omit<T, "$hash" | "$meta">>; | ||
fields(fields: WithOptionalSchema<WithOptionalMeta<T>>): this; | ||
static dataHashableFields<T extends Payload>(schema: Schema, payload: WithoutSchema<T>): Promisable<Payload>; | ||
static omitClientMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
static omitMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
static omitStorageMeta<T extends Payload>(payload: T, maxDepth?: number): T; | ||
dataHashableFields(): Promise<{ | ||
schema: string; | ||
} & import("@xyo-network/payload-model").PayloadFields>; | ||
fields(fields: WithOptionalSchema<T>): this; | ||
schema(value: Schema): void; | ||
protected metaFields(dataHash: Hash, stamp?: boolean): Promise<JsonObject>; | ||
} | ||
//# sourceMappingURL=BuilderBase.d.ts.map |
@@ -5,3 +5,3 @@ import type { Logger } from '@xylabs/logger'; | ||
export interface PayloadBuilderOptions<T> { | ||
readonly fields?: Omit<T, 'schema' | '$hash' | '$meta'>; | ||
readonly fields?: Partial<T>; | ||
readonly logger?: Logger; | ||
@@ -8,0 +8,0 @@ readonly meta?: JsonObject; |
{ | ||
"name": "@xyo-network/payload-builder", | ||
"version": "3.5.2", | ||
"version": "3.6.0-rc.1", | ||
"description": "Primary SDK for using XYO Protocol 2.0", | ||
@@ -32,9 +32,9 @@ "homepage": "https://xyo.network", | ||
"dependencies": { | ||
"@xylabs/assert": "^4.4.9", | ||
"@xylabs/hex": "^4.4.9", | ||
"@xylabs/logger": "^4.4.9", | ||
"@xylabs/object": "^4.4.9", | ||
"@xylabs/promise": "^4.4.9", | ||
"@xyo-network/hash": "^3.5.2", | ||
"@xyo-network/payload-model": "^3.5.2" | ||
"@xylabs/assert": "^4.4.12", | ||
"@xylabs/hex": "^4.4.12", | ||
"@xylabs/logger": "^4.4.12", | ||
"@xylabs/object": "^4.4.12", | ||
"@xylabs/promise": "^4.4.12", | ||
"@xyo-network/hash": "^3.6.0-rc.1", | ||
"@xyo-network/payload-model": "^3.6.0-rc.1" | ||
}, | ||
@@ -44,9 +44,10 @@ "devDependencies": { | ||
"@xylabs/tsconfig": "^4.2.4", | ||
"@xylabs/vitest-extended": "^4.4.9", | ||
"@xylabs/vitest-extended": "^4.4.12", | ||
"typescript": "^5.7.2", | ||
"vitest": "^2.1.5" | ||
"vitest": "^2.1.8" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
}, | ||
"stableVersion": "3.5.2" | ||
} |
@@ -1,26 +0,9 @@ | ||
import { assertEx } from '@xylabs/assert' | ||
import type { Hash } from '@xylabs/hex' | ||
import type { | ||
AnyObject, JsonArray, JsonObject, | ||
} from '@xylabs/object' | ||
import { isJsonObject, omitBy } from '@xylabs/object' | ||
import { PayloadHasher } from '@xyo-network/hash' | ||
import type { | ||
Payload, PayloadWithMeta, WithMeta, | ||
} from '@xyo-network/payload-model' | ||
import type { AnyObject } from '@xylabs/object' | ||
import { ObjectHasher } from '@xyo-network/hash' | ||
import type { Payload } from '@xyo-network/payload-model' | ||
import type { WithoutMeta, WithoutSchema } from './BuilderBase.ts' | ||
import { PayloadBuilderBase, removeMetaAndSchema } from './BuilderBase.ts' | ||
import { PayloadBuilderBase } from './BuilderBase.ts' | ||
import type { PayloadBuilderOptions } from './Options.ts' | ||
export interface BuildOptions { | ||
stamp?: boolean | ||
validate?: boolean | ||
} | ||
const omitByPredicate = <T extends object>(prefix: string) => (_: T[keyof T], key: keyof T) => { | ||
assertEx(typeof key === 'string', () => `Invalid key type [${String(key)}, ${typeof key}]`) | ||
return (key as string).startsWith(prefix) | ||
} | ||
export class PayloadBuilder< | ||
@@ -30,47 +13,11 @@ T extends Payload = Payload<AnyObject>, | ||
> extends PayloadBuilderBase<T, O> { | ||
static async build<T extends Payload = Payload<AnyObject>>(payload: T, options?: BuildOptions): Promise<WithMeta<T>> | ||
static async build<T extends Payload = Payload<AnyObject>>(payload: T[], options?: BuildOptions): Promise<WithMeta<T>[]> | ||
static async build<T extends Payload = Payload<AnyObject>>(payload: T | T[], options: BuildOptions = {}) { | ||
if (Array.isArray(payload)) { | ||
return await Promise.all(payload.map(payload => this.build(payload, options))) | ||
} else { | ||
const { stamp = false, validate = true } = options | ||
const { | ||
schema, $hash: incomingDataHash, $meta: incomingMeta = {}, | ||
} = payload as WithMeta<T> | ||
// check for legacy signatures | ||
const { _signatures } = payload as { _signatures?: JsonArray } | ||
if (_signatures && !incomingMeta.signatures) { | ||
incomingMeta.signatures = _signatures | ||
} | ||
const fields = removeMetaAndSchema(payload) | ||
const dataHashableFields = await PayloadBuilder.dataHashableFields(schema, fields) | ||
const $hash = validate || incomingDataHash === undefined ? await PayloadHasher.hash(dataHashableFields) : incomingDataHash | ||
const $meta: JsonObject = { ...incomingMeta } | ||
if ($meta.timestamp === undefined && stamp) { | ||
$meta.timestamp = Date.now() | ||
} | ||
const hashableFields: WithMeta<Payload> = { | ||
...dataHashableFields, $hash, schema, | ||
} | ||
if (Object.keys($meta).length > 0) { | ||
hashableFields.$meta = $meta | ||
} | ||
return hashableFields as WithMeta<T> | ||
} | ||
static async dataHash<T extends Payload>(payload: T): Promise<Hash> { | ||
return await ObjectHasher.hash(this.omitMeta(payload)) | ||
} | ||
static async dataHash<T extends Payload>(payload: T, options?: BuildOptions): Promise<Hash> { | ||
return (await this.build(payload, options)).$hash | ||
} | ||
static async dataHashPairs<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<[WithMeta<T>, Hash][]> { | ||
static async dataHashPairs<T extends Payload>(payloads: T[]): Promise<[T, Hash][]> { | ||
return await Promise.all( | ||
payloads.map(async (payload) => { | ||
const built = await PayloadBuilder.build(payload, options) | ||
return [built, built.$hash] | ||
const dataHash = await this.dataHash(payload) | ||
return [payload, dataHash] | ||
}), | ||
@@ -80,10 +27,9 @@ ) | ||
static async dataHashes(payloads: undefined, options?: BuildOptions): Promise<undefined> | ||
static async dataHashes<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<Hash[]> | ||
static async dataHashes<T extends Payload>(payloads?: T[], options?: BuildOptions): Promise<Hash[] | undefined> { | ||
static async dataHashes(payloads: undefined): Promise<undefined> | ||
static async dataHashes<T extends Payload>(payloads: T[]): Promise<Hash[]> | ||
static async dataHashes<T extends Payload>(payloads?: T[]): Promise<Hash[] | undefined> { | ||
return payloads | ||
? await Promise.all( | ||
payloads.map(async (payload) => { | ||
const built = await PayloadBuilder.build(payload, options) | ||
return built.$hash | ||
return await PayloadBuilder.dataHash(payload) | ||
}), | ||
@@ -95,3 +41,3 @@ ) | ||
static async filterExclude<T extends Payload>(payloads: T[] = [], hash: Hash[] | Hash): Promise<T[]> { | ||
return await PayloadHasher.filterExcludeByHash(await this.filterExcludeByDataHash(payloads, hash), hash) | ||
return await ObjectHasher.filterExcludeByHash(await this.filterExcludeByDataHash(payloads, hash), hash) | ||
} | ||
@@ -113,4 +59,4 @@ | ||
static async hash<T extends Payload>(payload: T, options?: BuildOptions): Promise<Hash> { | ||
return await PayloadHasher.hash(await PayloadBuilder.build(payload, options)) | ||
static async hash<T extends Payload>(payload: T): Promise<Hash> { | ||
return await ObjectHasher.hash(this.omitStorageMeta(payload)) | ||
} | ||
@@ -123,7 +69,6 @@ | ||
*/ | ||
static async hashPairs<T extends Payload>(payloads: T[], options?: BuildOptions): Promise<[WithMeta<T>, Hash][]> { | ||
static async hashPairs<T extends Payload>(payloads: T[]): Promise<[T, Hash][]> { | ||
return await Promise.all( | ||
payloads.map<Promise<[WithMeta<T>, Hash]>>(async (payload) => { | ||
const built = await PayloadBuilder.build(payload, options) | ||
return [built, await PayloadBuilder.hash(built)] | ||
payloads.map<Promise<[T, Hash]>>(async (payload) => { | ||
return [payload, await PayloadBuilder.hash(payload)] | ||
}), | ||
@@ -133,36 +78,6 @@ ) | ||
static async hashableFields<T extends Payload = Payload<AnyObject>>( | ||
schema: string, | ||
fields?: WithoutSchema<WithoutMeta<T>>, | ||
$meta?: JsonObject, | ||
$hash?: Hash, | ||
timestamp?: number, | ||
stamp = false, | ||
): Promise<WithMeta<T>> { | ||
const dataFields = await this.dataHashableFields<T>(schema, fields) | ||
assertEx($meta === undefined || isJsonObject($meta), () => '$meta must be JsonObject') | ||
const result: WithMeta<T> = omitBy( | ||
{ | ||
...dataFields, | ||
$hash: $hash ?? (await PayloadBuilder.dataHash(dataFields)), | ||
schema, | ||
} as WithMeta<T>, | ||
omitByPredicate('_'), | ||
) as WithMeta<T> | ||
const clonedMeta = { ...$meta } | ||
if (timestamp) { | ||
clonedMeta.timestamp = timestamp | ||
} | ||
if (clonedMeta.timestamp === undefined && stamp) { | ||
clonedMeta.timestamp = Date.now() | ||
} | ||
if (Object.keys(clonedMeta).length > 0) { | ||
result.$meta = clonedMeta | ||
} | ||
return result | ||
static hashableFields<T extends Payload>( | ||
payload: T, | ||
): T { | ||
return this.omitStorageMeta(payload) | ||
} | ||
@@ -173,10 +88,11 @@ | ||
static async hashes<T extends Payload>(payloads?: T[]): Promise<Hash[] | undefined> { | ||
return await PayloadHasher.hashes(payloads) | ||
return await ObjectHasher.hashes(payloads) | ||
} | ||
static async toAllHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>> { | ||
const result: Record<Hash, WithMeta<T>> = {} | ||
for (const pair of await this.hashPairs(objs)) { | ||
static async toAllHashMap<T extends Payload>(payloads: T[]): Promise<Record<Hash, T>> { | ||
const result: Record<Hash, T> = {} | ||
for (const pair of await this.hashPairs(payloads)) { | ||
const dataHash = await this.dataHash(pair[0]) | ||
result[pair[1]] = pair[0] | ||
result[pair[0].$hash] = pair[0] | ||
result[dataHash] = pair[0] | ||
} | ||
@@ -186,4 +102,4 @@ return result | ||
static async toDataHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>> { | ||
const result: Record<Hash, WithMeta<T>> = {} | ||
static async toDataHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, T>> { | ||
const result: Record<Hash, T> = {} | ||
for (const pair of await this.dataHashPairs(objs)) { | ||
@@ -200,4 +116,4 @@ result[pair[1]] = pair[0] | ||
*/ | ||
static async toHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, WithMeta<T>>> { | ||
const result: Record<Hash, WithMeta<T>> = {} | ||
static async toHashMap<T extends Payload>(objs: T[]): Promise<Record<Hash, T>> { | ||
const result: Record<Hash, T> = {} | ||
for (const pair of await this.hashPairs(objs)) { | ||
@@ -209,31 +125,8 @@ result[pair[1]] = pair[0] | ||
static withoutMeta(payload: undefined): undefined | ||
static withoutMeta<T extends PayloadWithMeta>(payload: T): Omit<T, '$meta'> | ||
static withoutMeta<T extends PayloadWithMeta>(payloads: T[]): Omit<T, '$meta'>[] | ||
static withoutMeta<T extends PayloadWithMeta>(payloads: T | T[]) { | ||
if (Array.isArray(payloads)) { | ||
return payloads.map(payload => this.withoutMeta(payload)) | ||
} else { | ||
if (payloads) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { $meta, ...result } = payloads | ||
return result as Omit<T, '$meta'> | ||
} | ||
} | ||
build(): T { | ||
return { | ||
schema: this._schema, | ||
...this._fields, | ||
} as T | ||
} | ||
async build(options?: BuildOptions): Promise<WithMeta<T>> { | ||
const dataHashableFields = await this.dataHashableFields() | ||
return await PayloadBuilder.build<T>({ | ||
...dataHashableFields, $meta: this._$meta, schema: this._schema, | ||
} as Payload as T, options) | ||
} | ||
async hashableFields() { | ||
return await PayloadBuilder.hashableFields( | ||
assertEx(this._schema, () => 'Payload: Missing Schema'), | ||
this._fields, | ||
this._$meta, | ||
) | ||
} | ||
} |
import { assertEx } from '@xylabs/assert' | ||
import type { Hash } from '@xylabs/hex' | ||
import type { AnyObject, JsonObject } from '@xylabs/object' | ||
import type { AnyObject } from '@xylabs/object' | ||
import { | ||
@@ -9,6 +8,5 @@ isJsonObject, omitBy, toJson, | ||
import { removeEmptyFields } from '@xyo-network/hash' | ||
import type { | ||
Payload, Schema, WithMeta, WithOptionalMeta, | ||
} from '@xyo-network/payload-model' | ||
import type { Payload, Schema } from '@xyo-network/payload-model' | ||
import { PayloadBuilder } from './Builder.ts' | ||
import type { PayloadBuilderOptions } from './Options.ts' | ||
@@ -20,8 +18,4 @@ | ||
export type WithoutMeta<T extends WithOptionalMeta<Payload>> = Omit<T, '$hash' | '$meta'> | ||
export const removeMetaAndSchema = <T extends Payload>(payload: WithOptionalSchema<WithOptionalMeta<T>>): WithoutSchema<WithoutMeta<T>> => { | ||
const { ...result } = payload | ||
delete result.$hash | ||
delete result.$meta | ||
export const removeMetaAndSchema = <T extends Payload>(payload: Partial<WithOptionalSchema<T>>): WithoutSchema<T> => { | ||
const { ...result } = PayloadBuilder.omitMeta(payload as T) as WithOptionalSchema<T> | ||
delete result.schema | ||
@@ -31,3 +25,3 @@ return result as Omit<T, 'schema'> | ||
const omitByPredicate = (prefix: string) => (_: unknown, key: string) => { | ||
const omitByPrefixPredicate = (prefix: string) => (_: unknown, key: string) => { | ||
assertEx(typeof key === 'string', () => `Invalid key type [${key}, ${typeof key}]`) | ||
@@ -38,20 +32,17 @@ return key.startsWith(prefix) | ||
export class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> { | ||
protected _$meta?: JsonObject | ||
protected _fields?: WithoutSchema<WithoutMeta<T>> | ||
protected _fields?: Partial<WithoutSchema<T>> | ||
protected _schema: Schema | ||
constructor(readonly options: O) { | ||
const { | ||
schema, fields, meta, | ||
} = options | ||
const { schema, fields } = options | ||
this._schema = schema | ||
this._fields = removeEmptyFields(fields ?? {}) as WithoutSchema<WithoutMeta<T>> | ||
this._$meta = meta | ||
this._fields = removeMetaAndSchema(removeEmptyFields(structuredClone(fields ?? {}))) | ||
} | ||
static dataHashableFields<T extends Payload = Payload<AnyObject>>( | ||
schema: string, | ||
fields?: WithoutSchema<WithoutMeta<T>>, | ||
): Promisable<Omit<T, '$hash' | '$meta'>> { | ||
const cleanFields = fields ? removeEmptyFields(fields) : undefined | ||
static dataHashableFields<T extends Payload>( | ||
schema: Schema, | ||
payload: WithoutSchema<T>, | ||
): Promisable<Payload> { | ||
const cleanFields = removeEmptyFields({ ...payload, schema }) | ||
assertEx( | ||
@@ -61,18 +52,15 @@ cleanFields === undefined || isJsonObject(cleanFields), | ||
) | ||
return omitBy(omitBy({ schema, ...cleanFields }, omitByPredicate('$')), omitByPredicate('_')) as unknown as T | ||
return this.omitMeta(cleanFields) as T | ||
} | ||
protected static metaFields(dataHash: Hash, otherMeta?: JsonObject, stamp = true): Promisable<JsonObject> { | ||
const meta: JsonObject = { ...otherMeta } | ||
static omitClientMeta<T extends Payload>(payload: T, maxDepth = 100): T { | ||
return omitBy(payload, omitByPrefixPredicate('$'), maxDepth) as T | ||
} | ||
if (!meta.timestamp && stamp) { | ||
meta.timestamp = meta.timestamp ?? Date.now() | ||
} | ||
return meta | ||
static omitMeta<T extends Payload>(payload: T, maxDepth = 100): T { | ||
return this.omitStorageMeta(this.omitClientMeta(payload, maxDepth), maxDepth) | ||
} | ||
$meta(meta?: JsonObject) { | ||
this._$meta = meta ?? (this._fields as WithMeta<T>).$meta | ||
return this | ||
static omitStorageMeta<T extends Payload>(payload: T, maxDepth = 100): T { | ||
return omitBy(payload, omitByPrefixPredicate('_'), maxDepth) as T | ||
} | ||
@@ -83,20 +71,15 @@ | ||
assertEx(this._schema, () => 'Payload: Missing Schema'), | ||
this._fields, | ||
// TDOD: Add verification that required fields are present | ||
this._fields as T, | ||
) | ||
} | ||
// we do not require sending in $hash since it will be generated anyway | ||
fields(fields: WithOptionalSchema<WithOptionalMeta<T>>) { | ||
fields(fields: WithOptionalSchema<T>) { | ||
if (fields) { | ||
const { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
$meta, $hash, schema, ...fieldsOnly | ||
} = fields | ||
if ($meta) { | ||
this.$meta($meta) | ||
} | ||
const fieldsClone = structuredClone(fields) | ||
const { schema } = fieldsClone | ||
if (schema) { | ||
this.schema(schema) | ||
} | ||
this._fields = removeMetaAndSchema<T>(fields) | ||
this._fields = removeEmptyFields(removeMetaAndSchema<T>(fieldsClone)) | ||
} | ||
@@ -109,6 +92,2 @@ return this | ||
} | ||
protected async metaFields(dataHash: Hash, stamp = true): Promise<JsonObject> { | ||
return await PayloadBuilderBase.metaFields(dataHash, this._$meta, stamp) | ||
} | ||
} |
@@ -6,3 +6,3 @@ import type { Logger } from '@xylabs/logger' | ||
export interface PayloadBuilderOptions<T> { | ||
readonly fields?: Omit<T, 'schema' | '$hash' | '$meta'> | ||
readonly fields?: Partial<T> | ||
readonly logger?: Logger | ||
@@ -9,0 +9,0 @@ readonly meta?: JsonObject |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
91593
-32.49%883
-33.51%1
Infinity%Updated
Updated
Updated
Updated
Updated