Skip to content

Commit

Permalink
fix: memos upgrade openId to token
Browse files Browse the repository at this point in the history
Signed-off-by: EINDEX <snowstarlbk@gmail.com>
  • Loading branch information
EINDEX committed Sep 17, 2023
1 parent 50e53bb commit 741a384
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/memos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class MemosSync {
* syncMemos
*/
public async syncMemos(mode = "Manual") {
const { host, openId }: any = logseq.settings;
if (!host || !openId) {
const { host, token ,openId }: any = logseq.settings;
if (!host || (!openId && !token)) {
logseq.UI.showMsg("Memos Setting up needed.");
logseq.showSettingsUI();
}
Expand Down Expand Up @@ -147,8 +147,8 @@ class MemosSync {
}

private async choosingClient() {
const { host, openId }: any = logseq.settings;
const client = new MemosGeneralClient(host, openId);
const { host, token, openId }: any = logseq.settings;
const client = new MemosGeneralClient(host, token, openId);
try {
this.memosClient = await client.getClient();
} catch (error) {
Expand Down Expand Up @@ -227,7 +227,7 @@ class MemosSync {

private memosFitler(memos: Array<Memo>): Array<Memo> {
if (!memos) {
return []
return [];
}
return memos.filter((memo) => {
if (this.tagFilterList!.length === 0) {
Expand Down
12 changes: 6 additions & 6 deletions src/memos/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface MemosClient {
getMemos(
limit: number,
offset: number,
includeArchive: boolean,
includeArchive: boolean
): Promise<Memo[]>;
updateMemo(memoId: number, payload: Record<string, any>): Promise<Memo>;
createMemo(content: string, visibility: string): Promise<Memo>;
Expand All @@ -16,12 +16,12 @@ export default class MemosGeneralClient {
private v1: MemosClientV1;
private v0: MemosClientV0;

constructor(host: string, openId: string) {
if (!openId) {
throw "OpenId not exist";
constructor(host: string, token: string, openId?: string) {
if (!openId && !token) {
throw "Token not exist";
}
this.v1 = new MemosClientV1(host, openId);
this.v0 = new MemosClientV0(host, openId);
this.v1 = new MemosClientV1(host, token, openId);
this.v0 = new MemosClientV0(host, token, openId);
}

public async getClient(): Promise<MemosClient> {
Expand Down
26 changes: 16 additions & 10 deletions src/memos/impls/clientV0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,32 @@ type MemosAPIResponse<T> = {
};

export default class MemosClientV0 implements MemosClient {
private openId: string;
private openId: string | undefined;
private host: string;
private token: string;

constructor(host:string, openId: string) {
constructor(host: string, token: string, openId?: string) {
this.host = host;
this.openId = openId;
this.token = token;
}

private async request<T>(
url: string,
url: URL,
method: Method,
payload: any
): Promise<T> {
try {
if (this.openId) {
url.searchParams.append("openId", String(this.openId));
}
const resp: AxiosResponse<MemosAPIResponse<T>> = await axios({
method: method,
url: url,
url: url.toString(),
data: payload,
headers: {
Authorization: `Bearer ${this.token}`,
},
});
if (resp.status !== 200) {
throw "Something wrong!";
Expand All @@ -42,17 +50,16 @@ export default class MemosClientV0 implements MemosClient {
public async getMemos(
limit: number,
offset: number,
includeArchive: boolean,
includeArchive: boolean
): Promise<Memo[]> {
const url = new URL(`${this.host}/api/memo`);
url.searchParams.append("openId", String(this.openId));
if (!includeArchive) {
url.searchParams.append("rowStatus", "NORMAL");
}
url.searchParams.append("limit", limit.toString());
url.searchParams.append("offset", offset.toString());
try {
return await this.request<Memo[]>(url.toString(), "GET", {});
return await this.request<Memo[]>(url, "GET", {});
} catch (error) {
throw new Error(`Failed to get memos, ${error}`);
}
Expand All @@ -63,9 +70,8 @@ export default class MemosClientV0 implements MemosClient {
payload: Record<string, any>
): Promise<Memo> {
const url = new URL(`${this.host}/api/memo/${memoId}`);
url.searchParams.append("openId", String(this.openId));
try {
return await this.request<Memo>(url.toString(), "PATCH", payload);
return await this.request<Memo>(url, "PATCH", payload);
} catch (error) {
throw new Error(`Failed to update memo, ${error}.`);
}
Expand All @@ -79,7 +85,7 @@ export default class MemosClientV0 implements MemosClient {
const url = new URL(`${this.host}/api/memo`);
url.searchParams.append("openId", String(this.openId));
try {
return await this.request<Memo>(url.toString(), "POST", payload);
return await this.request<Memo>(url, "POST", payload);
} catch (error) {
throw new Error(`Failed to create memo, ${error}.`);
}
Expand Down
15 changes: 11 additions & 4 deletions src/memos/impls/clientV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Memo } from "../type";
import { MemosClient } from "../client";

export default class MemosClientV1 implements MemosClient {
private openId: string;
private openId: string | undefined;
private host: string;
private token: string;

constructor(host: string, openId: string) {
constructor(host: string, token: string, openId?: string) {
this.host = host;
this.token = token;
this.openId = openId;
}

Expand All @@ -17,15 +19,20 @@ export default class MemosClientV1 implements MemosClient {
payload: any = null
): Promise<T> {
try {
url.searchParams.append("openId", String(this.openId));
if (this.openId) {
url.searchParams.append("openId", String(this.openId));
}
const resp: AxiosResponse<T> = await axios({
method: method,
url: url.toString(),
data: payload,
headers: {
"Authorization": `Bearer ${this.token}`,
}
});
if (resp.status >= 400) {
// @ts-ignore
throw resp.data?.message || "Error occurred";
throw resp.message || "Error occurred";
} else if (resp.status >= 300) {
throw "Something wrong!";
}
Expand Down
9 changes: 8 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ export default function settingSchema() {
description: "example: memos.com:8080",
default: "",
},
{
key: "token",
type: "string",
title: "Token",
description: " Your memos API token, you can find this in memos setting",
default: "",
},
{
key: "openId",
type: "string",
title: "Open Id",
description: "Memos Open Id, you can find this in memos setting",
description: "Please upgrade memos to v0.15.0 switch to Memos Token. Memos OpenId, you can find this in memos setting.",
default: "",
},
{
Expand Down

0 comments on commit 741a384

Please sign in to comment.