Skip to content

Commit

Permalink
enhance: Add axios to hook function
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarrk committed May 31, 2022
1 parent c540ff2 commit f194424
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/common-server/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { NoteProps } from "@dendronhq/common-all";
import execa from "execa";
import axios from "axios";
import _ from "lodash";

export { CommentJSONObject, CommentJSONValue } from "comment-json";

export type DHookFunction = (opts: {
note: NoteProps;
execa: typeof execa;
axios: typeof axios;
_: typeof _;
}) => Promise<NoteProps>;
2 changes: 2 additions & 0 deletions packages/engine-server/src/topics/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ConfigUtils,
} from "@dendronhq/common-all";
import { createLogger } from "@dendronhq/common-server";
import axios from "axios";
import execa from "execa";
import fs from "fs-extra";
import _ from "lodash";
Expand Down Expand Up @@ -94,6 +95,7 @@ export class HookUtils {
wsRoot,
note: { ...note },
execa,
axios,
_,
NoteUtils,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ const execaHookPayload = `module.exports = async function({note, execa, _}) {
return {note};
};
`;

const axiosHookPayload = `module.exports = async function({note, axios}) {
// Get some axios property that's very unlikely to change
// expected here: 'application/json'
const contentType = axios.defaults.headers.common.Accept.split(",")[0];
note.body = note.body + " " + contentType;
return {note};
};
`;

const { writeJSHook } = TestHookUtils;

const writeExecaHook = (root: string, fname: string) => {
Expand Down Expand Up @@ -204,6 +214,54 @@ describe("engine", () => {
}
);

testWithEngine(
"axios available",
async ({ engine, vaults }) => {
const vault = _.find(vaults, { fsPath: "vault1" })!;
const note = NoteUtils.create({
id: "hooked",
fname: "hooked",
body: "hooked body",
vault,
});
await engine.writeNote(note, { newNode: true });
const ent = engine.notes["hooked"];
expect(
await AssertUtils.assertInString({
body: ent.body,
match: ["hooked body contentType application/json"],
})
).toBeTruthy();
},
{
initHooks: true,
preSetupHook: async ({ wsRoot }) => {
const hookPath = path.join(
wsRoot,
CONSTANTS.DENDRON_HOOKS_BASE,
"content.js"
);
fs.writeFileSync(hookPath, axiosHookPayload);
TestConfigUtils.withConfig(
(config) => {
const hooks: DHookDict = {
onCreate: [
{
id: "content",
pattern: "*",
type: "js",
},
],
};
ConfigUtils.setHooks(config, hooks);
return config;
},
{ wsRoot }
);
},
}
);

test("custom filter rules", async () => {
await runEngineTestV5(
async ({ engine, vaults }) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-core/src/commands/CreateHookCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const hookTemplate = `
@params note: Object with following properties https://github.com/dendronhq/dendron/blob/master/packages/common-all/src/types/foundation.ts#L66:L66
@params NoteUtils: utilities for working with notes. [code](https://github.com/dendronhq/dendron/blob/master/packages/common-all/src/dnode.ts#L323:L323)
@params execa: instance of [execa](https://github.com/sindresorhus/execa#execacommandcommand-options)
@params axios: instance of [axios](https://axios-http.com/docs/example)
@params _: instance of [lodash](https://lodash.com/docs)
*/
module.exports = async function({wsRoot, note, NoteUtils, execa, _}) {
module.exports = async function({wsRoot, note, NoteUtils, execa, axios, _}) {
// do some changes
return {note};
};
Expand Down

0 comments on commit f194424

Please sign in to comment.