@xyo-network/payload-builder 2.92.7 → 2.92.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,25 +3,39 @@ import { Hash } from '@xylabs/hex'
3
3
  import { AnyObject, isJsonObject, JsonObject, toJson } from '@xylabs/object'
4
4
  import { Promisable } from '@xylabs/promise'
5
5
  import { deepOmitPrefixedFields, removeEmptyFields } from '@xyo-network/hash'
6
- import { Payload, Schema, WithMeta } from '@xyo-network/payload-model'
6
+ import { Payload, Schema, WithMeta, WithOptionalMeta } from '@xyo-network/payload-model'
7
7
 
8
8
  import { PayloadBuilderOptions } from './Options'
9
9
 
10
+ export type WithOptionalSchema<T extends Payload> = Omit<T, 'schema'> & Partial<T>
11
+
12
+ export type WithoutSchema<T extends WithOptionalSchema<Payload>> = Omit<T, 'schema'>
13
+
14
+ export type WithoutMeta<T extends WithOptionalMeta<Payload>> = Omit<T, '$hash' | '$meta'>
15
+
16
+ export const removeMetaAndSchema = <T extends Payload>(payload: WithOptionalSchema<WithOptionalMeta<T>>): WithoutSchema<WithoutMeta<T>> => {
17
+ const { ...result } = payload
18
+ delete result.$hash
19
+ delete result.$meta
20
+ delete result.schema
21
+ return result as Omit<T, 'schema'>
22
+ }
23
+
10
24
  export class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> {
11
25
  protected _$meta?: JsonObject
12
- protected _fields?: Omit<T, 'schema' | '$hash' | '$meta'>
26
+ protected _fields?: WithoutSchema<WithoutMeta<T>>
13
27
  protected _schema: Schema
14
28
 
15
29
  constructor(readonly options: O) {
16
30
  const { schema, fields, meta } = options
17
31
  this._schema = schema
18
- this._fields = removeEmptyFields(fields ?? {}) as Omit<T, 'schema' | '$hash' | '$meta'>
32
+ this._fields = removeEmptyFields(fields ?? {}) as WithoutSchema<WithoutMeta<T>>
19
33
  this._$meta = meta
20
34
  }
21
35
 
22
36
  static dataHashableFields<T extends Payload = Payload<AnyObject>>(
23
37
  schema: string,
24
- fields?: Omit<T, 'schema' | '$hash' | '$meta'>,
38
+ fields?: WithoutSchema<WithoutMeta<T>>,
25
39
  ): Promisable<Omit<T, '$hash' | '$meta'>> {
26
40
  const cleanFields = fields ? removeEmptyFields(fields) : undefined
27
41
  assertEx(
@@ -45,11 +59,14 @@ export class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extend
45
59
  }
46
60
 
47
61
  async dataHashableFields() {
48
- return await PayloadBuilderBase.dataHashableFields(assertEx(this._schema, 'Payload: Missing Schema'), this._fields)
62
+ return await PayloadBuilderBase.dataHashableFields(
63
+ assertEx(this._schema, () => 'Payload: Missing Schema'),
64
+ this._fields,
65
+ )
49
66
  }
50
67
 
51
68
  //we do not require sending in $hash since it will be generated anyway
52
- fields(fields: Omit<WithMeta<T>, '$hash' | 'schema' | '$meta'> & Partial<Pick<WithMeta<T>, '$hash' | 'schema' | '$meta'>>) {
69
+ fields(fields: WithOptionalSchema<WithOptionalMeta<T>>) {
53
70
  if (fields) {
54
71
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
55
72
  const { $meta, $hash, schema, ...fieldsOnly } = fields
@@ -59,7 +76,7 @@ export class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extend
59
76
  if (schema) {
60
77
  this.schema(schema)
61
78
  }
62
- this._fields = { ...this._fields, ...removeEmptyFields(fieldsOnly) } as T
79
+ this._fields = removeMetaAndSchema<T>(fields)
63
80
  }
64
81
  return this
65
82
  }