generated from garrettmills/template-npm-typescript
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
617 B
23 lines
617 B
import {EncodedPayload} from './types'
|
|
|
|
/**
|
|
* Wrapper class for managing values encoded in a string format.
|
|
*/
|
|
export class Payload<T> {
|
|
/** Instantiate the payload from a given string. */
|
|
public static from<T>(v: EncodedPayload<T>): Payload<T> {
|
|
const encoded = v.slice('multicrypt:'.length)
|
|
return new Payload<T>(JSON.parse(encoded))
|
|
}
|
|
|
|
/** Instantiate the payload from a given value. */
|
|
constructor(
|
|
public readonly value: T,
|
|
) {}
|
|
|
|
/** Serialize the value. */
|
|
encode(): EncodedPayload<T> {
|
|
return `multicrypt:${JSON.stringify(this.value)}`
|
|
}
|
|
}
|