Skip to content

Commit

Permalink
Unwrapping updated
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Jan 26, 2024
1 parent 70bbf42 commit 2363308
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { BoundWitnessValidator } from '@xyo-network/boundwitness-validator'
import { PayloadHasher } from '@xyo-network/hash'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import { Payload, WithMeta } from '@xyo-network/payload-model'
import { PayloadWrapper, PayloadWrapperBase } from '@xyo-network/payload-wrapper'
import { isPayloadWrapperBase, PayloadWrapper, PayloadWrapperBase } from '@xyo-network/payload-wrapper'

export const isBoundWitnessWrapper = (value?: unknown): value is PayloadWrapper => {
if (isPayloadWrapperBase(value)) {
return typeof (value as BoundWitnessWrapper).payloadsDataHashMap === 'function'
}
return false
}

export class BoundWitnessWrapper<
TBoundWitness extends BoundWitness<{ schema: string }> = BoundWitness,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { PayloadBuilder } from '@xyo-network/payload-builder'
import { Payload } from '@xyo-network/payload-model'
import { PayloadValidator } from '@xyo-network/payload-validator'

import { PayloadLoaderFactory, PayloadWrapperBase } from './PayloadWrapperBase'
import { isPayloadWrapperBase, PayloadLoaderFactory, PayloadWrapperBase } from './PayloadWrapperBase'

export const isPayloadWrapper = (value?: unknown): value is PayloadWrapper => {
return isPayloadWrapperBase(value)
}

export class PayloadWrapper<TPayload extends Payload = Payload> extends PayloadWrapperBase<TPayload> {
private static loaderFactory: PayloadLoaderFactory | null = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { assertEx } from '@xylabs/assert'
import { Promisable } from '@xylabs/promise'
import { PayloadHasher } from '@xyo-network/hash'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import { Payload, WithMeta } from '@xyo-network/payload-model'
import { isAnyPayload, isPayload, Payload, WithMeta } from '@xyo-network/payload-model'

export type PayloadLoader = (address: string) => Promise<Payload | null>
export type PayloadLoaderFactory = () => PayloadLoader

export const isPayloadWrapperBase = (value?: unknown): value is PayloadWrapperBase => {
if (typeof value === 'object' && !Array.isArray(value)) {
return typeof (value as PayloadWrapperBase).jsonPayload === 'function'
}
return false
}

export class PayloadWrapperBase<TPayload extends Payload = Payload> extends PayloadHasher<TPayload> {
private _errors?: Error[]

Expand Down Expand Up @@ -41,10 +48,22 @@ export class PayloadWrapperBase<TPayload extends Payload = Payload> extends Payl
}

if (!(typeof payload === 'object')) {
throw 'Can not unwrap class that is not extended from object'
throw new TypeError('Can not unwrap value that is not extended from object')
}

if (Array.isArray(payload)) {
throw new TypeError('Can not unwrap value that is an array')
}

if (isPayloadWrapperBase(payload)) {
return await PayloadBuilder.build(payload.jsonPayload())
}

if (isAnyPayload(payload)) {
return await PayloadBuilder.build(payload)
}

return await PayloadBuilder.build(payload instanceof PayloadWrapperBase ? payload.jsonPayload() : payload)
throw new TypeError('Can not unwrap an object that is not a PayloadWrapper or Payload')
}

/** @deprecated use jsonPayload instead */
Expand Down

0 comments on commit 2363308

Please sign in to comment.