@sortlist/cli 0.39.0 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +55 -24
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -290,6 +290,19 @@ import {
|
|
|
290
290
|
// src/graphql/client.ts
|
|
291
291
|
import { print } from "graphql";
|
|
292
292
|
|
|
293
|
+
// src/lib/api-headers.ts
|
|
294
|
+
var API_CLIENT = "sl-cli";
|
|
295
|
+
function userAgent() {
|
|
296
|
+
return `${API_CLIENT}/${pkg.version} (node/${process.versions.node})`;
|
|
297
|
+
}
|
|
298
|
+
function baseRequestHeaders() {
|
|
299
|
+
return {
|
|
300
|
+
"Content-Type": "application/json",
|
|
301
|
+
"X-API-Client": API_CLIENT,
|
|
302
|
+
"User-Agent": userAgent()
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
293
306
|
// src/lib/token-claims.ts
|
|
294
307
|
function tokenClaims(accessToken) {
|
|
295
308
|
try {
|
|
@@ -537,7 +550,7 @@ var GraphQLRequestError = class extends Error {
|
|
|
537
550
|
async function refreshAccessToken(creds, endpoint) {
|
|
538
551
|
const res = await fetch(endpoint, {
|
|
539
552
|
method: "POST",
|
|
540
|
-
headers:
|
|
553
|
+
headers: baseRequestHeaders(),
|
|
541
554
|
body: JSON.stringify({
|
|
542
555
|
query: print(RefreshToken),
|
|
543
556
|
variables: { input: { refreshToken: creds.refreshToken } }
|
|
@@ -781,10 +794,7 @@ async function executeQuery(query, variables, options = {}) {
|
|
|
781
794
|
level: "info"
|
|
782
795
|
});
|
|
783
796
|
if (attempt.verbose) logRequest(query, variables, attempt);
|
|
784
|
-
const headers =
|
|
785
|
-
"Content-Type": "application/json",
|
|
786
|
-
"X-API-Client": "sl-cli"
|
|
787
|
-
};
|
|
797
|
+
const headers = baseRequestHeaders();
|
|
788
798
|
if (authenticated) {
|
|
789
799
|
const { token, kind } = await resolveAccessToken(options, attempt);
|
|
790
800
|
headers["Authorization"] = `Bearer ${token}`;
|
|
@@ -850,6 +860,7 @@ import {
|
|
|
850
860
|
isScalarType,
|
|
851
861
|
isUnionType
|
|
852
862
|
} from "graphql";
|
|
863
|
+
var CLI_OPERATION_PREFIX = "SlCli_";
|
|
853
864
|
function unwrap(type) {
|
|
854
865
|
if (isNonNullType(type)) return unwrap(type.ofType);
|
|
855
866
|
if (isListType(type)) return unwrap(type.ofType);
|
|
@@ -862,7 +873,7 @@ function ucfirst(s) {
|
|
|
862
873
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
863
874
|
}
|
|
864
875
|
var PAYLOAD_TYPE_PATTERN = /(?:Response|Payload|MutationPayload)$/;
|
|
865
|
-
function buildSelections(type, schema, depth, exclude, prefix, visited) {
|
|
876
|
+
function buildSelections(type, schema, depth, exclude, prefix, totalCount, visited) {
|
|
866
877
|
if (!isObjectType(type)) return [];
|
|
867
878
|
if (visited.has(type.name)) return [];
|
|
868
879
|
visited.add(type.name);
|
|
@@ -871,8 +882,16 @@ function buildSelections(type, schema, depth, exclude, prefix, visited) {
|
|
|
871
882
|
const isPayload = PAYLOAD_TYPE_PATTERN.test(type.name);
|
|
872
883
|
if (isConnection(type)) {
|
|
873
884
|
const nodesType = unwrap(fields.nodes.type);
|
|
874
|
-
const nodeSelections = buildSelections(
|
|
875
|
-
|
|
885
|
+
const nodeSelections = buildSelections(
|
|
886
|
+
nodesType,
|
|
887
|
+
schema,
|
|
888
|
+
depth,
|
|
889
|
+
exclude,
|
|
890
|
+
`${prefix}nodes.`,
|
|
891
|
+
totalCount,
|
|
892
|
+
new Set(visited)
|
|
893
|
+
);
|
|
894
|
+
return connectionSelections(type, nodeSelections, totalCount);
|
|
876
895
|
}
|
|
877
896
|
for (const [name, field] of Object.entries(fields)) {
|
|
878
897
|
if (name.startsWith("__")) continue;
|
|
@@ -885,7 +904,15 @@ function buildSelections(type, schema, depth, exclude, prefix, visited) {
|
|
|
885
904
|
if (isLeaf(namedType)) {
|
|
886
905
|
lines.push(name);
|
|
887
906
|
} else if (isObjectType(namedType) && depth > 0) {
|
|
888
|
-
const subSelections = buildSelections(
|
|
907
|
+
const subSelections = buildSelections(
|
|
908
|
+
namedType,
|
|
909
|
+
schema,
|
|
910
|
+
depth - 1,
|
|
911
|
+
exclude,
|
|
912
|
+
`${fullPath}.`,
|
|
913
|
+
totalCount,
|
|
914
|
+
new Set(visited)
|
|
915
|
+
);
|
|
889
916
|
if (subSelections.length > 0) {
|
|
890
917
|
lines.push(`${fieldCall(name, field)} {`);
|
|
891
918
|
for (const sel of subSelections) lines.push(` ${sel}`);
|
|
@@ -928,7 +955,7 @@ function edgeExtras(connection) {
|
|
|
928
955
|
}
|
|
929
956
|
return lines.length === 0 ? null : { edge, lines };
|
|
930
957
|
}
|
|
931
|
-
function connectionSelections(connection, nodeSelections) {
|
|
958
|
+
function connectionSelections(connection, nodeSelections, totalCount) {
|
|
932
959
|
if (nodeSelections.length === 0) return [];
|
|
933
960
|
const lines = [];
|
|
934
961
|
const extras = edgeExtras(connection);
|
|
@@ -948,7 +975,7 @@ function connectionSelections(connection, nodeSelections) {
|
|
|
948
975
|
lines.push(" hasNextPage");
|
|
949
976
|
lines.push(" endCursor");
|
|
950
977
|
lines.push("}");
|
|
951
|
-
if (connection.getFields().totalCount) lines.push("totalCount");
|
|
978
|
+
if (totalCount && connection.getFields().totalCount) lines.push("totalCount");
|
|
952
979
|
for (const extra of connectionExtras(connection)) lines.push(extra);
|
|
953
980
|
return lines;
|
|
954
981
|
}
|
|
@@ -991,6 +1018,7 @@ function buildQuery(schema, kind, fieldName, variables, options = {}) {
|
|
|
991
1018
|
if (!field) throw new Error(`Unknown ${kind} field: ${fieldName}`);
|
|
992
1019
|
const depth = options.depth ?? 0;
|
|
993
1020
|
const exclude = new Set(options.exclude ?? []);
|
|
1021
|
+
const wantsTotalCount = options.totalCount === true;
|
|
994
1022
|
const returnType = unwrap(field.type);
|
|
995
1023
|
const allArgs = field.args;
|
|
996
1024
|
const varDefsInner = allArgs.map((a) => `$${a.name}: ${a.type.toString()}`).join(", ");
|
|
@@ -1003,9 +1031,9 @@ function buildQuery(schema, kind, fieldName, variables, options = {}) {
|
|
|
1003
1031
|
} else {
|
|
1004
1032
|
let selections;
|
|
1005
1033
|
if (options.fields && options.fields.length > 0) {
|
|
1006
|
-
selections = buildFieldsFromList(returnType, schema, options.fields);
|
|
1034
|
+
selections = buildFieldsFromList(returnType, schema, options.fields, wantsTotalCount);
|
|
1007
1035
|
} else {
|
|
1008
|
-
selections = buildSelections(returnType, schema, depth, exclude, "", /* @__PURE__ */ new Set());
|
|
1036
|
+
selections = buildSelections(returnType, schema, depth, exclude, "", wantsTotalCount, /* @__PURE__ */ new Set());
|
|
1009
1037
|
}
|
|
1010
1038
|
if (selections.length === 0) {
|
|
1011
1039
|
selectionBody = ` ${fieldName}${fieldArgs} {
|
|
@@ -1019,20 +1047,25 @@ ${body}
|
|
|
1019
1047
|
}`;
|
|
1020
1048
|
}
|
|
1021
1049
|
}
|
|
1022
|
-
const opName = ucfirst(fieldName)
|
|
1050
|
+
const opName = `${CLI_OPERATION_PREFIX}${ucfirst(fieldName)}`;
|
|
1023
1051
|
const query = `${kind} ${opName}${varDefs} {
|
|
1024
1052
|
${selectionBody}
|
|
1025
1053
|
}`;
|
|
1026
1054
|
return { query, variables, fieldName };
|
|
1027
1055
|
}
|
|
1028
|
-
function buildFieldsFromList(type, schema, fieldPaths) {
|
|
1056
|
+
function buildFieldsFromList(type, schema, fieldPaths, totalCount) {
|
|
1029
1057
|
if (!isObjectType(type)) return [];
|
|
1030
1058
|
const fields = type.getFields();
|
|
1031
1059
|
if (isConnection(type)) {
|
|
1032
1060
|
const nodesType = unwrap(fields.nodes.type);
|
|
1033
1061
|
const withId = isObjectType(nodesType) && nodesType.getFields().id && !fieldPaths.includes("id");
|
|
1034
|
-
const nodeSelections = buildFieldsFromList(
|
|
1035
|
-
|
|
1062
|
+
const nodeSelections = buildFieldsFromList(
|
|
1063
|
+
nodesType,
|
|
1064
|
+
schema,
|
|
1065
|
+
withId ? ["id", ...fieldPaths] : fieldPaths,
|
|
1066
|
+
totalCount
|
|
1067
|
+
);
|
|
1068
|
+
return connectionSelections(type, nodeSelections, totalCount);
|
|
1036
1069
|
}
|
|
1037
1070
|
const lines = [];
|
|
1038
1071
|
const grouped = /* @__PURE__ */ new Map();
|
|
@@ -1048,7 +1081,7 @@ function buildFieldsFromList(type, schema, fieldPaths) {
|
|
|
1048
1081
|
if (isLeaf(namedType)) {
|
|
1049
1082
|
lines.push(name);
|
|
1050
1083
|
} else if (isObjectType(namedType) && subPaths.length > 0) {
|
|
1051
|
-
const subSelections = buildFieldsFromList(namedType, schema, subPaths);
|
|
1084
|
+
const subSelections = buildFieldsFromList(namedType, schema, subPaths, totalCount);
|
|
1052
1085
|
if (subSelections.length > 0) {
|
|
1053
1086
|
lines.push(`${fieldCall(name, field)} {`);
|
|
1054
1087
|
for (const sel of subSelections) lines.push(` ${sel}`);
|
|
@@ -1391,7 +1424,7 @@ function buildCommand(schema, fieldName, kind, description, service) {
|
|
|
1391
1424
|
cmd.option(flag, describeArg(a));
|
|
1392
1425
|
}
|
|
1393
1426
|
}
|
|
1394
|
-
cmd.option("--depth <depth>", "Selection depth (default: 0)", parseInt).option("--fields <fields>", "Comma-separated fields to include").option("--exclude <fields>", "Comma-separated fields to exclude");
|
|
1427
|
+
cmd.option("--depth <depth>", "Selection depth (default: 0)", parseInt).option("--fields <fields>", "Comma-separated fields to include").option("--exclude <fields>", "Comma-separated fields to exclude").option("--total-count", "Also count every match, not just this page (slow on a broad filter)");
|
|
1395
1428
|
cmd.action(async function() {
|
|
1396
1429
|
const opts = this.optsWithGlobals();
|
|
1397
1430
|
const ctx = getUserContext();
|
|
@@ -1426,7 +1459,8 @@ function buildCommand(schema, fieldName, kind, description, service) {
|
|
|
1426
1459
|
built = buildQuery(schema, kind, fieldName, variables, {
|
|
1427
1460
|
depth: opts.depth,
|
|
1428
1461
|
fields: opts.fields?.split(","),
|
|
1429
|
-
exclude: opts.exclude?.split(",")
|
|
1462
|
+
exclude: opts.exclude?.split(","),
|
|
1463
|
+
totalCount: opts.totalCount === true
|
|
1430
1464
|
});
|
|
1431
1465
|
} catch (buildError) {
|
|
1432
1466
|
if (!(buildError instanceof UnknownFieldError)) throw buildError;
|
|
@@ -1959,10 +1993,7 @@ function writeCache(endpoint, token, introspection, raw) {
|
|
|
1959
1993
|
}
|
|
1960
1994
|
}
|
|
1961
1995
|
async function fetchIntrospection(endpoint, token) {
|
|
1962
|
-
const headers =
|
|
1963
|
-
"Content-Type": "application/json",
|
|
1964
|
-
"X-API-Client": "sl-cli"
|
|
1965
|
-
};
|
|
1996
|
+
const headers = baseRequestHeaders();
|
|
1966
1997
|
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
1967
1998
|
const res = await fetch(endpoint, {
|
|
1968
1999
|
method: "POST",
|