🩹 back: tolerate extraneous /s in env vars

This commit is contained in:
Eric Doughty-Papassideris
2024-11-16 06:08:56 +01:00
committed by Anton Shepilov
parent 00f819fb6d
commit dcadfa4231
2 changed files with 26 additions and 1 deletions
@@ -16,6 +16,7 @@ import path from "path";
import { existsSync } from "fs";
import axios from "axios";
import short, { Translator } from "short-uuid";
import { joinURL } from "../../../../utils/urls";
export default class EmailPusherClass
extends TdriveService<EmailPusherAPI>
@@ -86,7 +87,14 @@ export default class EmailPusherClass
const encodedCompanyId = translator.fromUUID(data.notifications[0].item.company_id);
const encodedItemId = translator.fromUUID(data.notifications[0].item.id);
const previewType = data.notifications[0].item.is_directory ? "d" : "preview";
const encodedUrl = `${this.platformUrl}/client/${encodedCompanyId}/v/shared_with_me/${previewType}/${encodedItemId}`;
const encodedUrl = joinURL([
this.platformUrl,
"client",
encodedCompanyId,
"v/shared_with_me",
previewType,
encodedItemId,
]);
if (!existsSync(templatePath)) {
throw Error(`template not found: ${templatePath}`);
+17
View File
@@ -0,0 +1,17 @@
/** Suitable type for query arguments */
export type QueryParams = { [key: string]: string | number };
/**
* Compose a URL removing and adding slashes and query parameters as warranted.
* Does not encode paths.
*/
export function joinURL(path: string[], params?: QueryParams) {
let joinedPath = path.map(x => x.replace(/(?:^\/+)|(?:\/+$)/g, "")).join("/");
if (path[path.length - 1].endsWith("/")) joinedPath += "/";
const paramEntries = Object.entries(params || {}).filter(
([, value]) => value !== undefined && value !== null,
);
if (paramEntries.length === 0) return joinedPath;
const query = paramEntries.map(p => p.map(encodeURIComponent).join("=")).join("&");
return joinedPath + (joinedPath.indexOf("?") > -1 ? "&" : "?") + query;
}