(
ack: unknown,
options: VersionedAckOptions<TValue, ValueKey>
)
| 21 | } |
| 22 | |
| 23 | export const applyVersionedAck = <TValue, ValueKey extends string>( |
| 24 | ack: unknown, |
| 25 | options: VersionedAckOptions<TValue, ValueKey> |
| 26 | ): void => { |
| 27 | if (!isRecord(ack)) { |
| 28 | throw new Error(options.invalidResponseMessage) |
| 29 | } |
| 30 | |
| 31 | const result = ack.result |
| 32 | if (result === 'success' || result === 'version-mismatch') { |
| 33 | const version = ack.version |
| 34 | if (typeof version !== 'number') { |
| 35 | throw new Error(options.invalidResponseMessage) |
| 36 | } |
| 37 | |
| 38 | const rawValue = ack[options.valueKey] |
| 39 | if (rawValue == null) { |
| 40 | options.applyValue(null) |
| 41 | } else { |
| 42 | const parsed = options.parseValue(rawValue) |
| 43 | if (parsed === null) { |
| 44 | options.logInvalidValue(result, version) |
| 45 | } else { |
| 46 | options.applyValue(parsed) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | options.applyVersion(version) |
| 51 | |
| 52 | if (result === 'version-mismatch') { |
| 53 | throw new Error(options.versionMismatchMessage) |
| 54 | } |
| 55 | |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | if (result === 'error') { |
| 60 | const reason = typeof ack.reason === 'string' ? ack.reason : 'unknown' |
| 61 | throw new Error(`${options.errorMessage} (${reason})`) |
| 62 | } |
| 63 | |
| 64 | throw new Error(options.invalidResponseMessage) |
| 65 | } |
no test coverage detected