Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(url-obj): fix data loss during conversion
- Loading branch information
Showing
1 changed file
with
12 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |