refactor(document): extract file route builder and normalize query composition
This commit is contained in:
@@ -58,6 +58,32 @@ const loadRelayClientModule = (injected = {}) => {
|
||||
return context.module.exports;
|
||||
};
|
||||
|
||||
const loadFileRouteBuilderModule = (injected = {}) => {
|
||||
const filePath = path.join(
|
||||
rootDir,
|
||||
"actions",
|
||||
"clients",
|
||||
"fileRouteBuilder.js"
|
||||
);
|
||||
let source = fs.readFileSync(filePath, "utf8");
|
||||
|
||||
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
|
||||
source = source.replace(/export const\s+/g, "const ");
|
||||
source +=
|
||||
"\nmodule.exports = { buildFileQuery, withBaseUrl, appendQuerySuffix };\n";
|
||||
|
||||
const context = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
require,
|
||||
encodeURIComponent,
|
||||
...injected
|
||||
};
|
||||
|
||||
vm.runInNewContext(source, context, { filename: filePath });
|
||||
return context.module.exports;
|
||||
};
|
||||
|
||||
const tests = [];
|
||||
const test = (name, fn) => tests.push({ name, fn });
|
||||
|
||||
@@ -150,6 +176,43 @@ test("clients/relayClient rethrows when browser hash call fails and no HASHKEY",
|
||||
);
|
||||
});
|
||||
|
||||
test("clients/fileRouteBuilder builds query with optional encoding and suffix helpers", async () => {
|
||||
const mod = loadFileRouteBuilderModule();
|
||||
|
||||
const unencoded = mod.buildFileQuery("/api/file/getbloblist", {
|
||||
container: "abc",
|
||||
casefolderID: "x/y"
|
||||
});
|
||||
const encoded = mod.buildFileQuery(
|
||||
"/api/file/deleteblob",
|
||||
{
|
||||
container: "abc",
|
||||
casefolderID: "x/y",
|
||||
blobname: "doc one.pdf"
|
||||
},
|
||||
{
|
||||
encode: true
|
||||
}
|
||||
);
|
||||
|
||||
assert.strictEqual(
|
||||
unencoded,
|
||||
"/api/file/getbloblist?container=abc&casefolderID=x/y"
|
||||
);
|
||||
assert.strictEqual(
|
||||
encoded,
|
||||
"/api/file/deleteblob?container=abc&casefolderID=x%2Fy&blobname=doc%20one.pdf"
|
||||
);
|
||||
assert.strictEqual(
|
||||
mod.withBaseUrl("http://example.local", unencoded),
|
||||
"http://example.local/api/file/getbloblist?container=abc&casefolderID=x/y"
|
||||
);
|
||||
assert.strictEqual(
|
||||
mod.appendQuerySuffix(unencoded, "&hash=123"),
|
||||
"/api/file/getbloblist?container=abc&casefolderID=x/y&hash=123"
|
||||
);
|
||||
});
|
||||
|
||||
const run = async () => {
|
||||
let passed = 0;
|
||||
|
||||
|
||||
@@ -126,6 +126,33 @@ const loadServiceModule = (fileName, injected = {}) => {
|
||||
});
|
||||
};
|
||||
|
||||
const defaultBuildFileQuery = (pathValue, params = {}, options = {}) => {
|
||||
const { encode = false } = options;
|
||||
const entries = Object.entries(params).filter(([, value]) => {
|
||||
return value !== undefined && value !== null;
|
||||
});
|
||||
|
||||
if (entries.length === 0) {
|
||||
return pathValue;
|
||||
}
|
||||
|
||||
const query = entries
|
||||
.map(([key, value]) => {
|
||||
if (!encode) {
|
||||
return `${key}=${String(value)}`;
|
||||
}
|
||||
|
||||
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`;
|
||||
})
|
||||
.join("&");
|
||||
|
||||
return `${pathValue}?${query}`;
|
||||
};
|
||||
|
||||
const defaultWithBaseUrl = (baseUrl, route) => `${baseUrl}${route}`;
|
||||
const defaultAppendQuerySuffix = (route, suffix = "") =>
|
||||
`${route}${suffix}`;
|
||||
|
||||
const context = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
@@ -147,6 +174,10 @@ const loadServiceModule = (fileName, injected = {}) => {
|
||||
downloadFileBlob: injected.downloadFileBlob || defaultDownloadFileBlob,
|
||||
buildHashedQueryUrl:
|
||||
injected.buildHashedQueryUrl || defaultBuildHashedQueryUrl,
|
||||
buildFileQuery: injected.buildFileQuery || defaultBuildFileQuery,
|
||||
withBaseUrl: injected.withBaseUrl || defaultWithBaseUrl,
|
||||
appendQuerySuffix:
|
||||
injected.appendQuerySuffix || defaultAppendQuerySuffix,
|
||||
...injected
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user