@@ -21,7 +21,9 @@ export type SocketJsonError = {
2121 code ?: number
2222}
2323
24- export type SocketJsonResponse < T = unknown > = SocketJsonSuccess < T > | SocketJsonError
24+ export type SocketJsonResponse < T = unknown > =
25+ | SocketJsonSuccess < T >
26+ | SocketJsonError
2527
2628/**
2729 * Validates that a string contains valid JSON matching Socket CLI response format.
@@ -31,7 +33,7 @@ export type SocketJsonResponse<T = unknown> = SocketJsonSuccess<T> | SocketJsonE
3133 */
3234export function validateSocketJson < T = unknown > (
3335 jsonString : string ,
34- expectedExitCode : number
36+ expectedExitCode : number ,
3537) : SocketJsonResponse < T > {
3638 let parsed : any
3739
@@ -44,29 +46,41 @@ export function validateSocketJson<T = unknown>(
4446
4547 // Check for required ok field.
4648 if ( typeof parsed . ok !== 'boolean' ) {
47- throw new Error ( `JSON output missing required 'ok' boolean field: ${ jsonString } ` )
49+ throw new Error (
50+ `JSON output missing required 'ok' boolean field: ${ jsonString } ` ,
51+ )
4852 }
4953
5054 // Validate based on exit code expectation.
5155 if ( expectedExitCode === 0 ) {
5256 if ( parsed . ok !== true ) {
53- throw new Error ( `JSON output 'ok' should be true when exit code is 0: ${ jsonString } ` )
57+ throw new Error (
58+ `JSON output 'ok' should be true when exit code is 0: ${ jsonString } ` ,
59+ )
5460 }
5561 // Success response must have data field.
5662 if ( parsed . data === undefined || parsed . data === null ) {
57- throw new Error ( `JSON output missing required 'data' field when ok is true: ${ jsonString } ` )
63+ throw new Error (
64+ `JSON output missing required 'data' field when ok is true: ${ jsonString } ` ,
65+ )
5866 }
5967 } else {
6068 if ( parsed . ok !== false ) {
61- throw new Error ( `JSON output 'ok' should be false when exit code is non-zero: ${ jsonString } ` )
69+ throw new Error (
70+ `JSON output 'ok' should be false when exit code is non-zero: ${ jsonString } ` ,
71+ )
6272 }
6373 // Error response must have message field.
6474 if ( typeof parsed . message !== 'string' || parsed . message . length === 0 ) {
65- throw new Error ( `JSON output missing required 'message' field when ok is false: ${ jsonString } ` )
75+ throw new Error (
76+ `JSON output missing required 'message' field when ok is false: ${ jsonString } ` ,
77+ )
6678 }
6779 // If code exists, it must be a number.
6880 if ( parsed . code !== undefined && typeof parsed . code !== 'number' ) {
69- throw new Error ( `JSON output 'code' field must be a number: ${ jsonString } ` )
81+ throw new Error (
82+ `JSON output 'code' field must be a number: ${ jsonString } ` ,
83+ )
7084 }
7185 }
7286
@@ -77,7 +91,7 @@ export function validateSocketJson<T = unknown>(
7791 * Helper to check if response is a success response.
7892 */
7993export function isSocketJsonSuccess < T = unknown > (
80- response : SocketJsonResponse < T >
94+ response : SocketJsonResponse < T > ,
8195) : response is SocketJsonSuccess < T > {
8296 return response . ok === true
8397}
@@ -86,7 +100,7 @@ export function isSocketJsonSuccess<T = unknown>(
86100 * Helper to check if response is an error response.
87101 */
88102export function isSocketJsonError < T = unknown > (
89- response : SocketJsonResponse < T >
103+ response : SocketJsonResponse < T > ,
90104) : response is SocketJsonError {
91105 return response . ok === false
92- }
106+ }
0 commit comments