1
1
//@ts -check
2
2
3
- import stripAnsi from "strip-ansi" ;
4
-
5
3
import { container } from "../config/container.mjs" ;
6
4
import { isUnknownError } from "./errors.mjs" ;
7
5
import { faunaToCommandError } from "./fauna.mjs" ;
8
6
import { faunadbToCommandError } from "./faunadb.mjs" ;
9
- import { colorize , Format } from "./formatting/colorize.mjs" ;
10
-
11
- /**
12
- * Regex to match the FQL diagnostic line.
13
- * @type {RegExp }
14
- */
15
- export const FQL_DIAGNOSTIC_REGEX = / ^ ( \s { 2 , } \| ) | ( \s * \d { 1 , } \s \| ) / ;
7
+ import { Format } from "./formatting/colorize.mjs" ;
16
8
17
9
/**
18
10
* Gets a secret for the current credentials.
@@ -103,16 +95,17 @@ export const runQueryFromString = (expression, argv) => {
103
95
* @param {object } opts
104
96
* @param {string } opts.apiVersion - The API version
105
97
* @param {boolean } opts.color - Whether to colorize the error
98
+ * @param {string[] } opts.include - The query info fields to include
106
99
* @returns {string }
107
100
*/
108
- export const formatError = ( err , { apiVersion, color } ) => {
101
+ export const formatError = ( err , { apiVersion, color, include } ) => {
109
102
const faunaV4 = container . resolve ( "faunaClientV4" ) ;
110
103
const faunaV10 = container . resolve ( "faunaClientV10" ) ;
111
104
112
105
if ( apiVersion === "4" ) {
113
- return faunaV4 . formatError ( err , { color } ) ;
106
+ return faunaV4 . formatError ( err , { color, include } ) ;
114
107
} else {
115
- return faunaV10 . formatError ( err , { color } ) ;
108
+ return faunaV10 . formatError ( err , { color, include } ) ;
116
109
}
117
110
} ;
118
111
@@ -132,11 +125,11 @@ export const isQueryable = async (argv) => {
132
125
throw err ;
133
126
}
134
127
135
- const { color } = argv ;
128
+ const { color, include } = argv ;
136
129
if ( argv . apiVersion === "4" ) {
137
- faunadbToCommandError ( { err, color } ) ;
130
+ faunadbToCommandError ( { err, color, include } ) ;
138
131
} else {
139
- faunaToCommandError ( { err, color } ) ;
132
+ faunaToCommandError ( { err, color, include } ) ;
140
133
}
141
134
}
142
135
@@ -153,67 +146,15 @@ export const isQueryable = async (argv) => {
153
146
* @returns {string }
154
147
*/
155
148
export const formatQueryResponse = ( res , { apiVersion, color, format } ) => {
156
- const faunaV4 = container . resolve ( "faunaClientV4" ) ;
157
- const faunaV10 = container . resolve ( "faunaClientV10" ) ;
158
-
159
149
if ( apiVersion === "4" ) {
150
+ const faunaV4 = container . resolve ( "faunaClientV4" ) ;
160
151
return faunaV4 . formatQueryResponse ( res , { format, color } ) ;
161
152
} else {
153
+ const faunaV10 = container . resolve ( "faunaClientV10" ) ;
162
154
return faunaV10 . formatQueryResponse ( res , { format, color } ) ;
163
155
}
164
156
} ;
165
157
166
- /**
167
- * Formats a summary of a query from a fauna
168
- * @param {string } summary - The summary of the query
169
- * @returns {string }
170
- */
171
- export const formatQuerySummary = ( summary ) => {
172
- if ( ! summary || typeof summary !== "string" ) {
173
- return "" ;
174
- }
175
-
176
- try {
177
- const lines = summary . split ( "\n" ) . map ( ( line ) => {
178
- if ( ! line . match ( FQL_DIAGNOSTIC_REGEX ) ) {
179
- return line ;
180
- }
181
- return colorize ( line , { format : Format . FQL } ) ;
182
- } ) ;
183
- return lines . join ( "\n" ) ;
184
- } catch ( err ) {
185
- const logger = container . resolve ( "logger" ) ;
186
- logger . debug ( `Unable to parse performance hint: ${ err } ` ) ;
187
- return summary ;
188
- }
189
- } ;
190
-
191
- const getQueryInfoValue = ( response , field ) => {
192
- switch ( field ) {
193
- case "txnTs" :
194
- return response . txn_ts ;
195
- case "schemaVersion" :
196
- return response . schema_version ?. toString ( ) ;
197
- case "summary" :
198
- return response . summary ;
199
- case "queryTags" :
200
- return response . query_tags ;
201
- case "stats" :
202
- return response . stats ;
203
- default :
204
- return undefined ;
205
- }
206
- } ;
207
-
208
- const getIncludedQueryInfo = ( response , include ) => {
209
- const queryInfo = { } ;
210
- include . forEach ( ( field ) => {
211
- const value = getQueryInfoValue ( response , field ) ;
212
- if ( value ) queryInfo [ field ] = value ;
213
- } ) ;
214
- return queryInfo ;
215
- } ;
216
-
217
158
/**
218
159
*
219
160
* @param {object } response - The v4 or v10 query response with query info
@@ -224,38 +165,11 @@ const getIncludedQueryInfo = (response, include) => {
224
165
* @returns
225
166
*/
226
167
export const formatQueryInfo = ( response , { apiVersion, color, include } ) => {
227
- if ( apiVersion === "4" && include . includes ( "stats" ) ) {
228
- /** @type {import("faunadb").MetricsResponse } */
229
- const metricsResponse = response ;
230
- const colorized = colorize (
231
- { metrics : metricsResponse . metrics } ,
232
- { color, format : Format . YAML } ,
233
- ) ;
234
-
235
- return `${ colorized } \n` ;
236
- } else if ( apiVersion === "10" ) {
237
- const queryInfoToDisplay = getIncludedQueryInfo ( response , include ) ;
238
-
239
- if ( Object . keys ( queryInfoToDisplay ) . length === 0 ) return "" ;
240
-
241
- // We colorize the entire query info object as YAML, but then need to
242
- // colorize the diagnostic lines individually. To simplify this, we
243
- // strip the ansi when we're checking if the line is a diagnostic line.
244
- const colorized = colorize ( queryInfoToDisplay , {
245
- color,
246
- format : Format . YAML ,
247
- } )
248
- . split ( "\n" )
249
- . map ( ( line ) => {
250
- if ( ! stripAnsi ( line ) . match ( FQL_DIAGNOSTIC_REGEX ) ) {
251
- return line ;
252
- }
253
- return colorize ( line , { format : Format . FQL } ) ;
254
- } )
255
- . join ( "\n" ) ;
256
-
257
- return `${ colorized } \n` ;
168
+ if ( apiVersion === "4" ) {
169
+ const faunaV4 = container . resolve ( "faunaClientV4" ) ;
170
+ return faunaV4 . formatQueryInfo ( response , { color, include } ) ;
171
+ } else {
172
+ const faunaV10 = container . resolve ( "faunaClientV10" ) ;
173
+ return faunaV10 . formatQueryInfo ( response , { color, include } ) ;
258
174
}
259
-
260
- return "" ;
261
175
} ;
0 commit comments