Skip to content

Commit

Permalink
handle array paths better
Browse files Browse the repository at this point in the history
  • Loading branch information
trashhalo committed Nov 14, 2021
1 parent d3d9110 commit 20ab956
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
18 changes: 17 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,25 @@ webhookApp.post("/:key", async (req: any, res: any) => {
today.getDate() + 7
);

let path: string;
const qPath: unknown = req.query.path;
if (typeof qPath === "string") {
path = qPath;
} else if (Array.isArray(qPath)) {
path = qPath[0];
} else {
return res
.status(422)
.send(
`path not a valid format. expected string recieved ${JSON.stringify(
qPath
)}`
);
}

const buffer = {
id: crypto.randomBytes(16).toString("hex"),
path: req.query.path,
path,
exp,
data: req.body,
};
Expand Down
15 changes: 14 additions & 1 deletion plugin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,21 @@ export default class ObsidianWebhooksPlugin extends Plugin {
await wipe(value);
}

async applyEvent({ data, path }: { data: string; path: string }) {
async applyEvent({
data,
path: pathOrArr,
}: {
data: string;
path: string | Array<string>;
}) {
const fs = this.app.vault.adapter;
let path: string;
if (typeof pathOrArr === "string") {
path = pathOrArr;
} else {
path = Object.values(pathOrArr).first();
}

let dirPath = path.replace(/\/*$/, "").replace(/^(.+)\/[^\/]*?$/, "$1");
if (dirPath !== path) {
// == means its in the root
Expand Down

0 comments on commit 20ab956

Please # to comment.