|
| 1 | +import { SubintentRequestItem } from '../../../schemas' |
| 2 | + |
| 3 | +export type BuildableSubintentRequest = { |
| 4 | + toRequestItem: () => SubintentRequestItem |
| 5 | +} |
| 6 | +/** |
| 7 | + * A builder function for creating a SubintentRequest. |
| 8 | + * |
| 9 | + * @returns An object with methods to configure and build a SubintentRequestItem. |
| 10 | + * |
| 11 | + * @example |
| 12 | + * const builder = SubintentRequestBuilder(); |
| 13 | + * const requestItem = builder |
| 14 | + * .manifest('some-manifest') |
| 15 | + * .setExpiration('atTime', 1234567890) |
| 16 | + * .addBlobs('blob1', 'blob2') |
| 17 | + * .message('This is a message') |
| 18 | +
|
| 19 | + */ |
| 20 | +export const SubintentRequestBuilder = () => { |
| 21 | + let state: Partial<SubintentRequestItem> = { |
| 22 | + discriminator: 'subintent', |
| 23 | + version: 1, |
| 24 | + transactionManifestVersion: 1, |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Sets the expiration for a request. |
| 29 | + * |
| 30 | + * @param type - The type of expiration. Can be 'atTime' for a specific time or 'secondsAfterSignature' for a duration after the signature. |
| 31 | + * @param value - The value associated with the expiration type. For 'atTime', this is a timestamp. For 'secondsAfterSignature', this is the number of seconds. |
| 32 | + * @returns The API object for chaining. |
| 33 | + */ |
| 34 | + const setExpiration = ( |
| 35 | + type: 'atTime' | 'secondsAfterSignature', |
| 36 | + value: number, |
| 37 | + ) => { |
| 38 | + state.expiration = { |
| 39 | + discriminator: |
| 40 | + type === 'atTime' ? 'expireAtTime' : 'expireAfterSignature', |
| 41 | + value, |
| 42 | + } |
| 43 | + return api |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Adds the provided blobs to the state. |
| 48 | + * |
| 49 | + * @param blobs - A list of blob strings to be added to the state. |
| 50 | + * @returns The API object for chaining. |
| 51 | + */ |
| 52 | + const addBlobs = (...blobs: string[]) => { |
| 53 | + state.blobs = blobs |
| 54 | + return api |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Sets the message to be included in the subintent transaction. |
| 59 | + * |
| 60 | + * @param message - The message to be set in the state. |
| 61 | + * @returns The API object for chaining further calls. |
| 62 | + */ |
| 63 | + const message = (message: string) => { |
| 64 | + state.message = message |
| 65 | + return api |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Sets the transaction manifest in the state and returns the API object. |
| 70 | + * |
| 71 | + * @param value - The transaction manifest to be set. |
| 72 | + * @returns The API object for method chaining. |
| 73 | + */ |
| 74 | + const manifest = (value: string) => { |
| 75 | + state.transactionManifest = value |
| 76 | + return api |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Converts the current state to a SubintentRequestItem. |
| 81 | + * |
| 82 | + * @returns {SubintentRequestItem} The current state cast as a SubintentRequestItem. |
| 83 | + */ |
| 84 | + const toRequestItem = () => state as SubintentRequestItem |
| 85 | + |
| 86 | + /** |
| 87 | + * Sets the raw configuration for the builder. |
| 88 | + * |
| 89 | + * @param rawConfig - The raw configuration to set. |
| 90 | + * @returns The API object for method chaining. |
| 91 | + */ |
| 92 | + const rawConfig = ( |
| 93 | + rawConfig: Omit<SubintentRequestItem, 'discriminator'>, |
| 94 | + ) => { |
| 95 | + state = { ...rawConfig, discriminator: 'subintent' } |
| 96 | + return { toRequestItem } |
| 97 | + } |
| 98 | + |
| 99 | + const api = { |
| 100 | + setExpiration, |
| 101 | + addBlobs, |
| 102 | + message, |
| 103 | + toRequestItem, |
| 104 | + } as const |
| 105 | + |
| 106 | + return { manifest, rawConfig } |
| 107 | +} |
0 commit comments