@xyo-network/payload-builder 3.5.2 → 3.6.0-rc.1

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.
@@ -1,98 +1,81 @@
1
1
  import { assertEx } from '@xylabs/assert'
2
- import type { Hash } from '@xylabs/hex'
3
- import type { AnyObject, JsonObject } from '@xylabs/object'
2
+ import type { AnyObject } from '@xylabs/object'
4
3
  import {
5
4
  isJsonObject, omitBy, toJson,
6
5
  } from '@xylabs/object'
7
6
  import type { Promisable } from '@xylabs/promise'
8
7
  import { removeEmptyFields } from '@xyo-network/hash'
9
- import type {
10
- Payload, Schema, WithMeta, WithOptionalMeta,
11
- } from '@xyo-network/payload-model'
8
+ import type { Payload, Schema } from '@xyo-network/payload-model'
12
9
 
10
+ import { PayloadBuilder } from './Builder.ts'
13
11
  import type { PayloadBuilderOptions } from './Options.ts'
14
12
 
15
13
  export type WithOptionalSchema<T extends Payload> = Omit<T, 'schema'> & Partial<T>
16
14
 
17
15
  export type WithoutSchema<T extends WithOptionalSchema<Payload>> = Omit<T, 'schema'>
18
16
 
19
- export type WithoutMeta<T extends WithOptionalMeta<Payload>> = Omit<T, '$hash' | '$meta'>
20
-
21
- export const removeMetaAndSchema = <T extends Payload>(payload: WithOptionalSchema<WithOptionalMeta<T>>): WithoutSchema<WithoutMeta<T>> => {
22
- const { ...result } = payload
23
- delete result.$hash
24
- delete result.$meta
17
+ export const removeMetaAndSchema = <T extends Payload>(payload: Partial<WithOptionalSchema<T>>): WithoutSchema<T> => {
18
+ const { ...result } = PayloadBuilder.omitMeta(payload as T) as WithOptionalSchema<T>
25
19
  delete result.schema
26
20
  return result as Omit<T, 'schema'>
27
21
  }
28
22
 
29
- const omitByPredicate = (prefix: string) => (_: unknown, key: string) => {
23
+ const omitByPrefixPredicate = (prefix: string) => (_: unknown, key: string) => {
30
24
  assertEx(typeof key === 'string', () => `Invalid key type [${key}, ${typeof key}]`)
31
25
  return key.startsWith(prefix)
32
26
  }
33
27
 
34
28
  export class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extends PayloadBuilderOptions<T> = PayloadBuilderOptions<T>> {
35
- protected _$meta?: JsonObject
36
- protected _fields?: WithoutSchema<WithoutMeta<T>>
29
+ protected _fields?: Partial<WithoutSchema<T>>
37
30
  protected _schema: Schema
38
31
 
39
32
  constructor(readonly options: O) {
40
- const {
41
- schema, fields, meta,
42
- } = options
33
+ const { schema, fields } = options
43
34
  this._schema = schema
44
- this._fields = removeEmptyFields(fields ?? {}) as WithoutSchema<WithoutMeta<T>>
45
- this._$meta = meta
35
+ this._fields = removeMetaAndSchema(removeEmptyFields(structuredClone(fields ?? {})))
46
36
  }
47
37
 
48
- static dataHashableFields<T extends Payload = Payload<AnyObject>>(
49
- schema: string,
50
- fields?: WithoutSchema<WithoutMeta<T>>,
51
- ): Promisable<Omit<T, '$hash' | '$meta'>> {
52
- const cleanFields = fields ? removeEmptyFields(fields) : undefined
38
+ static dataHashableFields<T extends Payload>(
39
+ schema: Schema,
40
+ payload: WithoutSchema<T>,
41
+
42
+ ): Promisable<Payload> {
43
+ const cleanFields = removeEmptyFields({ ...payload, schema })
53
44
  assertEx(
54
45
  cleanFields === undefined || isJsonObject(cleanFields),
55
46
  () => `Fields must be JsonObject: ${JSON.stringify(toJson(cleanFields), null, 2)}`,
56
47
  )
57
- return omitBy(omitBy({ schema, ...cleanFields }, omitByPredicate('$')), omitByPredicate('_')) as unknown as T
48
+ return this.omitMeta(cleanFields) as T
58
49
  }
59
50
 
60
- protected static metaFields(dataHash: Hash, otherMeta?: JsonObject, stamp = true): Promisable<JsonObject> {
61
- const meta: JsonObject = { ...otherMeta }
62
-
63
- if (!meta.timestamp && stamp) {
64
- meta.timestamp = meta.timestamp ?? Date.now()
65
- }
51
+ static omitClientMeta<T extends Payload>(payload: T, maxDepth = 100): T {
52
+ return omitBy(payload, omitByPrefixPredicate('$'), maxDepth) as T
53
+ }
66
54
 
67
- return meta
55
+ static omitMeta<T extends Payload>(payload: T, maxDepth = 100): T {
56
+ return this.omitStorageMeta(this.omitClientMeta(payload, maxDepth), maxDepth)
68
57
  }
69
58
 
70
- $meta(meta?: JsonObject) {
71
- this._$meta = meta ?? (this._fields as WithMeta<T>).$meta
72
- return this
59
+ static omitStorageMeta<T extends Payload>(payload: T, maxDepth = 100): T {
60
+ return omitBy(payload, omitByPrefixPredicate('_'), maxDepth) as T
73
61
  }
74
62
 
75
63
  async dataHashableFields() {
76
64
  return await PayloadBuilderBase.dataHashableFields(
77
65
  assertEx(this._schema, () => 'Payload: Missing Schema'),
78
- this._fields,
66
+ // TDOD: Add verification that required fields are present
67
+ this._fields as T,
79
68
  )
80
69
  }
81
70
 
82
- // we do not require sending in $hash since it will be generated anyway
83
- fields(fields: WithOptionalSchema<WithOptionalMeta<T>>) {
71
+ fields(fields: WithOptionalSchema<T>) {
84
72
  if (fields) {
85
- const {
86
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
87
- $meta, $hash, schema, ...fieldsOnly
88
- } = fields
89
- if ($meta) {
90
- this.$meta($meta)
91
- }
73
+ const fieldsClone = structuredClone(fields)
74
+ const { schema } = fieldsClone
92
75
  if (schema) {
93
76
  this.schema(schema)
94
77
  }
95
- this._fields = removeMetaAndSchema<T>(fields)
78
+ this._fields = removeEmptyFields(removeMetaAndSchema<T>(fieldsClone))
96
79
  }
97
80
  return this
98
81
  }
@@ -100,8 +83,4 @@ export class PayloadBuilderBase<T extends Payload = Payload<AnyObject>, O extend
100
83
  schema(value: Schema) {
101
84
  this._schema = value
102
85
  }
103
-
104
- protected async metaFields(dataHash: Hash, stamp = true): Promise<JsonObject> {
105
- return await PayloadBuilderBase.metaFields(dataHash, this._$meta, stamp)
106
- }
107
86
  }
package/src/Options.ts CHANGED
@@ -3,7 +3,7 @@ import type { JsonObject } from '@xylabs/object'
3
3
  import type { Schema } from '@xyo-network/payload-model'
4
4
 
5
5
  export interface PayloadBuilderOptions<T> {
6
- readonly fields?: Omit<T, 'schema' | '$hash' | '$meta'>
6
+ readonly fields?: Partial<T>
7
7
  readonly logger?: Logger
8
8
  readonly meta?: JsonObject
9
9
  readonly schema: Schema