Skip to content

Commit

Permalink
Merge branch 'v3' into FE-6215
Browse files Browse the repository at this point in the history
  • Loading branch information
henryfauna authored Dec 10, 2024
2 parents 86585f0 + 53bf7cc commit a86f48f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 50 deletions.
12 changes: 6 additions & 6 deletions src/commands/query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function queryCommand(argv) {
// get the query handler and run the query
try {
const secret = await getSecret();
const { url, timeout, typecheck, extra, json, apiVersion, color } = argv;
const { url, timeout, typecheck, raw, json, apiVersion, color } = argv;

// If we're writing to a file, don't colorize the output regardless of the user's preference
const useColor = argv.output ? false : color;
Expand All @@ -88,7 +88,7 @@ async function queryCommand(argv) {

const output = formatQueryResponse(results, {
apiVersion,
extra,
raw,
json,
color: useColor,
});
Expand All @@ -105,8 +105,8 @@ async function queryCommand(argv) {
throw err;
}

const { apiVersion, extra, color } = argv;
throw new CommandError(formatError(err, { apiVersion, extra, color }), {
const { apiVersion, raw, color } = argv;
throw new CommandError(formatError(err, { apiVersion, raw, color }), {
cause: err,
});
}
Expand All @@ -131,7 +131,7 @@ function buildQueryCommand(yargs) {
description:
"Path to a file where query results are written. Defaults to stdout.",
},
extra: {
raw: {
type: "boolean",
description:
"Output the full API response, including summary and query stats.",
Expand Down Expand Up @@ -164,7 +164,7 @@ function buildQueryCommand(yargs) {
"Run the query and write the results to a file.",
],
[
"$0 query -i /path/to/queries.fql --extra --output /tmp/result.json --database us/example",
"$0 query -i /path/to/queries.fql --raw --output /tmp/result.json --database us/example",
"Run the query and write the full API response to a file.",
],
]);
Expand Down
16 changes: 7 additions & 9 deletions src/commands/shell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ async function shellCommand(argv) {
},
},
{
cmd: "toggleExtra",
cmd: "toggleRawResponses",
help: "Enable or disable additional output. Disabled by default. If enabled, outputs the full API response, including summary and query stats.",
action: () => {
shell.context.extra = !shell.context.extra;
shell.context.raw = !shell.context.raw;
logger.stderr(
`Additional information in shell: ${shell.context.extra ? "on" : "off"}`,
`Additional information in shell: ${shell.context.raw ? "on" : "off"}`,
);
shell.prompt();
},
Expand All @@ -115,7 +115,7 @@ async function buildCustomEval(argv) {

// These are options used for querying and formatting the response
const { apiVersion, color, json } = argv;
const { extra } = ctx;
const { raw } = ctx;

let res;
try {
Expand All @@ -129,14 +129,12 @@ async function buildCustomEval(argv) {
typecheck,
});
} catch (err) {
logger.stderr(formatError(err, { apiVersion, extra, color }));
logger.stderr(formatError(err, { apiVersion, raw, color }));
return cb(null);
}

// If extra is on, return the full response. Otherwise, return just the data.
logger.stdout(
formatQueryResponse(res, { apiVersion, extra, color, json }),
);
// If raw is on, return the full response. Otherwise, return just the data.
logger.stdout(formatQueryResponse(res, { apiVersion, raw, color, json }));

return cb(null);
} catch (e) {
Expand Down
19 changes: 8 additions & 11 deletions src/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,18 @@ export const runQueryFromString = (expression, argv) => {
* @param {object} err - The error to format
* @param {object} opts
* @param {string} opts.apiVersion - The API version
* @param {boolean} opts.extra - Whether to include extra information
* @param {boolean} opts.raw - Whether to include full response bodies
* @param {boolean} opts.color - Whether to colorize the error
* @returns {object}
*/
export const formatError = (err, { apiVersion, extra, color }) => {
export const formatError = (err, { apiVersion, raw, color }) => {
const faunaV4 = container.resolve("faunaClientV4");
const faunaV10 = container.resolve("faunaClientV10");

if (apiVersion === "4") {
return faunaV4.formatError(err, { extra, color });
return faunaV4.formatError(err, { raw, color });
} else {
return faunaV10.formatError(err, { extra, color });
return faunaV10.formatError(err, { raw, color });
}
};

Expand All @@ -168,21 +168,18 @@ export const formatError = (err, { apiVersion, extra, color }) => {
* @param {object} res - The query response
* @param {object} opts
* @param {string} opts.apiVersion - The API version
* @param {boolean} opts.extra - Whether to include extra information
* @param {boolean} opts.raw - Whether to include full response bodies
* @param {boolean} opts.json - Whether to format the response as JSON
* @param {boolean} opts.color - Whether to colorize the response
* @returns {object}
*/
export const formatQueryResponse = (
res,
{ apiVersion, extra, json, color },
) => {
export const formatQueryResponse = (res, { apiVersion, raw, json, color }) => {
const faunaV4 = container.resolve("faunaClientV4");
const faunaV10 = container.resolve("faunaClientV10");

if (apiVersion === "4") {
return faunaV4.formatQueryResponse(res, { extra, json, color });
return faunaV4.formatQueryResponse(res, { raw, json, color });
} else {
return faunaV10.formatQueryResponse(res, { extra, json, color });
return faunaV10.formatQueryResponse(res, { raw, json, color });
}
};
16 changes: 8 additions & 8 deletions src/lib/fauna.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ export const runQueryFromString = async ({
*
* @param {any} err - An error to format
* @param {object} [opts]
* @param {boolean} [opts.extra] - Whether to include extra information
* @param {boolean} [opts.raw] - Whether to include raw information
* @param {boolean} [opts.color] - Whether to colorize the error
* @returns {string} The formatted error message
*/
export const formatError = (err, opts = {}) => {
const { extra, color } = opts;
const { raw, color } = opts;

// If the error has a queryInfo object with a summary property, we can format it.
// Doing this check allows this code to avoid a fauna direct dependency.
Expand All @@ -137,8 +137,8 @@ export const formatError = (err, opts = {}) => {
typeof err.queryInfo === "object" &&
typeof err.queryInfo.summary === "string"
) {
// If you want extra information, use util.inspect to get the full error object.
if (extra) {
// If you want raw information, use util.inspect to get the full error object.
if (raw) {
return formatFullErrorForShell(err, { color });
}

Expand All @@ -153,16 +153,16 @@ export const formatError = (err, opts = {}) => {
* Formats a V10 Fauna query response.
* @par [ am {import("fauna").QuerySuccess<any>} res
* @param {object} [opts]
* @param {boolean} [opts.extra] - Whether to include extra information
* @param {boolean} [opts.raw] - Whether to include raw information
* @param {boolean} [opts.json] - Whether to return the response as a JSON string
* @param {boolean} [opts.color] - Whether to colorize the response
* @returns {string} The formatted response
*/
export const formatQueryResponse = (res, opts = {}) => {
const { extra } = opts;
const { raw } = opts;

// If extra is set, return the full response object.
const data = extra ? res : res.data;
// If raw is set, return the full response object.
const data = raw ? res : res.data;
return formatObject(data, opts);
};

Expand Down
14 changes: 7 additions & 7 deletions src/lib/faunadb.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ export const runQuery = async ({
* Formats a V4 Fauna error for display.
* @param {any} err - An error to format
* @param {object} [opts]
* @param {boolean} [opts.extra] - Whether to include extra information
* @param {boolean} [opts.raw] - Whether to include full response bodies
* @param {boolean} [opts.color] - Whether to colorize the error
* @returns {string} The formatted error message
*/
export const formatError = (err, opts = {}) => {
const { extra, color } = opts;
const { raw, color } = opts;

// By doing this we can avoid requiring a faunadb direct dependency
if (
Expand All @@ -98,8 +98,8 @@ export const formatError = (err, opts = {}) => {
typeof err.requestResult.responseContent === "object" &&
Array.isArray(err.requestResult.responseContent.errors)
) {
// If extra is on, return the full error.
if (extra) {
// If raw is on, return the full error.
if (raw) {
return formatFullErrorForShell(err, { color });
}

Expand All @@ -123,14 +123,14 @@ export const formatError = (err, opts = {}) => {
* Formats a V4 Fauna query response.
* @param {any} res - The query response to format
* @param {object} [opts]
* @param {boolean} [opts.extra] - Whether to include extra information
* @param {boolean} [opts.raw] - Whether to include full response bodies
* @param {boolean} [opts.json] - Whether to return the response as a JSON string
* @param {boolean} [opts.color] - Whether to colorize the response
* @returns {string} The formatted response
*/
export const formatQueryResponse = (res, opts = {}) => {
const { extra } = opts;
const data = extra ? res : res.value;
const { raw } = opts;
const data = raw ? res : res.value;
return formatObject(data, opts);
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/misc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function formatObjectForShell(obj, { color = true } = {}) {
/**
* Formats an error for display in the shell. Use this when you want to see
* the full error object. Use specific formatting logic in your commands
* if you are creating a summary message. This is best used with --extra.
* if you are creating a summary message. This is best used with --raw.
* @param {any} err - The error to format
* @param {object} [opts] - Options
* @param {boolean} [opts.color] - Whether to colorize the error
Expand Down
16 changes: 8 additions & 8 deletions test/query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe("query", function () {
expect(logger.stderr).to.not.be.called;
});

it("can output additional response fields via --extra", async function () {
it("can output additional response fields via --raw", async function () {
const testData = {
name: "test",
coll: "Database",
Expand All @@ -300,7 +300,7 @@ describe("query", function () {
const testResponse = createV10QuerySuccess(testData);
runQueryFromString.resolves(testResponse);

await run(`query "Database.all()" --extra --secret=foo`, container);
await run(`query "Database.all()" --raw --secret=foo`, container);

expect(logger.stdout).to.have.been.calledWith(
formatObjectForShell(testResponse),
Expand All @@ -320,13 +320,13 @@ describe("query", function () {
expect(logger.stderr).to.have.been.calledWith(sinon.match(/test query/));
});

it("can output the full error object when --extra is used", async function () {
it("can output the full error object when --raw is used", async function () {
const failure = createV10QueryFailure("test query");
const error = new ServiceError(failure);
runQueryFromString.rejects(error);

try {
await run(`query "Database.all()" --extra --secret=foo`, container);
await run(`query "Database.all()" --raw --secret=foo`, container);
} catch (e) {}

expect(logger.stdout).to.not.be.called;
Expand Down Expand Up @@ -376,7 +376,7 @@ describe("query", function () {
expect(logger.stderr).to.not.be.called;
});

it("can output additional response fields via --extra", async function () {
it("can output additional response fields via --raw", async function () {
const testData = {
"@ref": {
id: "test",
Expand All @@ -391,7 +391,7 @@ describe("query", function () {
runQueryFromString.resolves(testResponse);

await run(
`query "Collection('test')" --extra --apiVersion 4 --secret=foo`,
`query "Collection('test')" --raw --apiVersion 4 --secret=foo`,
container,
);

Expand Down Expand Up @@ -426,7 +426,7 @@ describe("query", function () {
);
});

it("can output the full error object when --extra is used", async function () {
it("can output the full error object when --raw is used", async function () {
const testError = createV4QueryFailure({
position: ["paginate", "collections"],
code: "invalid argument",
Expand All @@ -438,7 +438,7 @@ describe("query", function () {

try {
await run(
`query "Paginate(Collection('x'))" --apiVersion 4 --extra --secret=foo`,
`query "Paginate(Collection('x'))" --apiVersion 4 --raw --secret=foo`,
container,
);
} catch (e) {}
Expand Down

0 comments on commit a86f48f

Please sign in to comment.