From f194424adb1da2ed779fb000db484706dbe8c268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20S=2E=20=C5=81ukasiewicz?= Date: Tue, 31 May 2022 11:40:52 +0200 Subject: [PATCH] enhance: Add axios to hook function --- packages/common-server/src/types/index.ts | 3 + packages/engine-server/src/topics/hooks.ts | 2 + .../engine-server/topics/hooks.spec.ts | 58 +++++++++++++++++++ .../src/commands/CreateHookCommand.ts | 3 +- 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/packages/common-server/src/types/index.ts b/packages/common-server/src/types/index.ts index 9ec08d7339..c33cb40f23 100644 --- a/packages/common-server/src/types/index.ts +++ b/packages/common-server/src/types/index.ts @@ -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; diff --git a/packages/engine-server/src/topics/hooks.ts b/packages/engine-server/src/topics/hooks.ts index 5b7c41074d..80b977479a 100644 --- a/packages/engine-server/src/topics/hooks.ts +++ b/packages/engine-server/src/topics/hooks.ts @@ -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"; @@ -94,6 +95,7 @@ export class HookUtils { wsRoot, note: { ...note }, execa, + axios, _, NoteUtils, }); diff --git a/packages/engine-test-utils/src/__tests__/engine-server/topics/hooks.spec.ts b/packages/engine-test-utils/src/__tests__/engine-server/topics/hooks.spec.ts index fc122682fe..1d1d1dbf15 100644 --- a/packages/engine-test-utils/src/__tests__/engine-server/topics/hooks.spec.ts +++ b/packages/engine-test-utils/src/__tests__/engine-server/topics/hooks.spec.ts @@ -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) => { @@ -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 }) => { diff --git a/packages/plugin-core/src/commands/CreateHookCommand.ts b/packages/plugin-core/src/commands/CreateHookCommand.ts index 91711b7b06..4a8f72722d 100644 --- a/packages/plugin-core/src/commands/CreateHookCommand.ts +++ b/packages/plugin-core/src/commands/CreateHookCommand.ts @@ -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}; };