Skip to content

Commit

Permalink
conditional header for v4 shell commands during transition period
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilde345 committed Jul 22, 2024
1 parent a1453ee commit 800c6bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/lib/fauna-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ class FaunaCommand extends Command {
super.error(message, { exit: 1 });
}

_getHeaders() {
const headers = {
"X-Fauna-Source": "Fauna Shell",
};
if (!["ShellCommand", "EvalCommand"].includes(this.constructor.name)) {
headers["x-fauna-shell-builtin"] = "true";
}
return headers;
}

/**
* !!! use getClient instead
* Runs the function in the context of a database connection.
Expand All @@ -74,9 +84,7 @@ class FaunaCommand extends Command {
// Force http1. See getClient.
fetch: fetch,

headers: {
"X-Fauna-Source": "Fauna Shell",
},
headers: this._getHeaders(),
});
await client.query(q.Now());
//TODO this should return a Promise
Expand Down Expand Up @@ -142,9 +150,7 @@ class FaunaCommand extends Command {
// TODO: Remove and just connect to a docker container.
fetch: fetch,

headers: {
"X-Fauna-Source": "Fauna Shell",
},
headers: this._getHeaders(),
});

// validate the client settings
Expand Down
5 changes: 4 additions & 1 deletion test/commands/databases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ describe("database test", () => {
nock(getEndpoint(), { allowUnmocked: true })
.persist()
.post("/", matchFqlReq(q.Now()))
.reply(200, new Date())
.reply(200, function () {
expect(this.req.headers["x-fauna-shell-builtin"]).to.equal("true");
return new Date();
})
.post("/", matchFqlReq(q.Paginate(q.Databases(), { size: 1000 })))
.reply(200, { resource: { data: databases } });
const { stdout } = await runCommand(withOpts(["list-databases"]));
Expand Down
5 changes: 5 additions & 0 deletions test/commands/eval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const {
} = require("../helpers/utils.js");
const { query: q } = require("faunadb");

afterEach(() => {
nock.cleanAll();
});

describe("eval", () => {
it("runs eval on root db", async () => {
const scope = nock(getEndpoint(), { allowUnmocked: true });
Expand Down Expand Up @@ -180,6 +184,7 @@ function mockQuery(api) {
.post("/", matchFqlReq(q.Paginate(q.Collections())))
.reply(200, function () {
const auth = this.req.headers.authorization.split(" ")[1].split(":");
expect(this.req.headers["x-fauna-shell-builtin"]).to.not.exist;
return {
resource: {
data: [
Expand Down
5 changes: 4 additions & 1 deletion test/commands/keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ describe("keys test", () => {
nock(getEndpoint(), { allowUnmocked: true })
.persist()
.post("/", matchFqlReq(q.Now()))
.reply(200, new Date())
.reply(200, function () {
expect(this.req.headers["x-fauna-shell-builtin"]).to.equal("true");
return new Date();
})
.post("/", matchFqlReq(q.Paginate(q.Keys(), { size: 100 })))
.reply(200, { resource: currentKeys })
.post("/", matchFqlReq(q.Paginate(q.Databases(), { size: 100 })))
Expand Down

0 comments on commit 800c6bd

Please sign in to comment.