Skip to content

Commit

Permalink
fix(url-obj): fix data loss during conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Aug 24, 2021
1 parent 0cffc40 commit 2b3ca72
Showing 1 changed file with 12 additions and 54 deletions.
66 changes: 12 additions & 54 deletions src/modules/url-obj.ts
@@ -1,88 +1,46 @@
import JsonURL from "@jsonurl/jsonurl";
import assertNever from "assert-never";
import { ObsidianProtocolData } from "obsidian";
import { stringify as toQs } from "query-string";
import URLParse from "url-parse";

import { PREFIX, PREFIX_REGEX, VERSION } from "../const";
import { DataType, ReturnBody } from "../return";
import { ReturnBody } from "../return";

export const ObjToUrl = (obj: ReturnBody): string => {
const url = URLParse("obsidian://mncomp"),
{ type, sendTime, last, data } = obj,
qsObj: QsObj = {
version: VERSION,
type,
sendTime,
last: JsonURL.stringify(last) as string,
data: JsonURL.stringify(data) as string,
body: JsonURL.stringify(obj) as string,
},
qs = toQs(qsObj);
url.set("query", qs);
return url.toString();
};
export const ObjToJson = (obj: ReturnBody): string =>
PREFIX + JSON.stringify(obj);

type QsObj = {
version: string;
type: DataType;
sendTime: number;
last: string;
data: string;
body: string;
};
const dts = ["sel", "note", "toc"];
const keys: (keyof QsObj)[] = ["version", "type", "sendTime", "last", "data"];
const isDataType = (str: string): str is DataType => dts.includes(str);

export const UrlToObj = (
params: ObsidianProtocolData,
): [version: string, body: ReturnBody] | null => {
let obj = {} as any,
version: string = "";
for (const key of keys) {
if (!params[key]) {
console.error("param empty, key: " + key);
return null;
} else {
switch (key) {
case "version":
version = params[key];
continue;
case "type": {
let type = params[key];
if (isDataType(type)) obj[key] = type;
else {
console.error("invaild type: " + type);
return null;
}
continue;
}
case "sendTime":
obj[key] = +params[key];
continue;
case "last":
case "data":
obj[key] = JsonURL.parse(params[key]);
continue;
default:
assertNever(key);
}
}
}
if (version === "") {
if (params.version === "") {
console.error("version empty");
return null;
}
return [version, obj as ReturnBody];
const { version, body } = params as ObsidianProtocolData & QsObj;
if (!version || !body) return null;
return [version, JsonURL.parse(body) as ReturnBody];
};

export const ObjToJson = (obj: ReturnBody): string =>
PREFIX + JSON.stringify(obj);
export const JsonToObj = (
src: string,
): [version: string, body: ReturnBody] | null => {
let match;
if ((match = src.match(PREFIX_REGEX))) {
const version = match[1];
const json = src.replace(PREFIX_REGEX, "");
const version = match[1],
json = src.replace(PREFIX_REGEX, "");
return [version, JSON.parse(json)];
} else return null;
};

0 comments on commit 2b3ca72

Please sign in to comment.