-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated PayloadBuilder to not wipe $meta from BoundWitnessBuilder
- Loading branch information
Showing
7 changed files
with
140 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/protocol/packages/payload/packages/builder/src/BuilderBase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { assertEx } from '@xylabs/assert' | ||
import { AnyObject, JsonObject } from '@xylabs/object' | ||
import { Promisable } from '@xylabs/promise' | ||
import { deepOmitPrefixedFields, removeEmptyFields } from '@xyo-network/hash' | ||
import { Payload, Schema, WithMeta } from '@xyo-network/payload-model' | ||
|
||
import { PayloadBuilderOptions } from './Options' | ||
|
||
export class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> { | ||
protected _$meta?: JsonObject | ||
protected _fields?: Omit<T, 'schema' | '$hash' | '$meta'> | ||
protected _schema: Schema | ||
|
||
constructor(readonly options: O) { | ||
const { schema, fields, meta } = options | ||
this._schema = schema | ||
this._fields = fields | ||
this._$meta = meta | ||
} | ||
|
||
$meta(meta?: JsonObject) { | ||
this._$meta = meta ?? (this._fields as WithMeta<T>).$meta | ||
return this | ||
} | ||
|
||
async dataHashableFields(): Promise<T> { | ||
return deepOmitPrefixedFields(await this.hashableFields(), '$') | ||
} | ||
|
||
//we do not require sending in $hash since it will be generated anyway | ||
fields(fields: Omit<WithMeta<T>, '$hash' | 'schema'> & Partial<Pick<WithMeta<T>, '$hash' | 'schema'>>) { | ||
if (fields) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { $meta, $hash, schema, ...fieldsOnly } = fields as WithMeta<T> | ||
if ($meta) { | ||
this.$meta($meta) | ||
} | ||
if (schema) { | ||
this.schema(schema) | ||
} | ||
this._fields = { ...this._fields, ...removeEmptyFields(fieldsOnly) } | ||
} | ||
return this | ||
} | ||
|
||
hashableFields(): Promisable<T> { | ||
const schema = assertEx(this._schema, 'Payload: Missing Schema') | ||
return { | ||
...removeEmptyFields(deepOmitPrefixedFields(this._fields ?? {}, '_')), | ||
schema, | ||
} as T | ||
} | ||
|
||
schema(value: Schema) { | ||
this._schema = value | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/protocol/packages/payload/packages/builder/src/Options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Logger } from '@xylabs/logger' | ||
import { JsonObject } from '@xylabs/object' | ||
import { Schema } from '@xyo-network/payload-model' | ||
|
||
export interface PayloadBuilderOptions<T> { | ||
readonly fields?: Omit<T, 'schema' | '$hash' | '$meta'> | ||
readonly logger?: Logger | ||
readonly meta?: JsonObject | ||
readonly schema: Schema | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/protocol/packages/payload/packages/builder/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './Builder' | ||
export * from './BuilderBase' | ||
export * from './Options' |