generated from pengx17/logseq-plugin-template-react
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: EINDEX <snowstarlbk@gmail.com>
- Loading branch information
Showing
6 changed files
with
252 additions
and
99 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
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,90 +1,38 @@ | ||
import axios, { AxiosResponse } from "axios"; | ||
import { ListMemo, Memo } from "./type"; | ||
|
||
type MemosAPIResponse<T> = { | ||
data:T; | ||
message: string; | ||
}; | ||
import MemosClientV1 from "./impls/clientV1"; | ||
import MemosClientV0 from "./impls/clientV0"; | ||
import { Memo } from "./type"; | ||
|
||
export interface MemosClient { | ||
getMemos( | ||
limit: number, | ||
offset: number, | ||
includeArchive: boolean, | ||
): Promise<Memo[]>; | ||
updateMemo(memoId: number, payload: Record<string, any>): Promise<Memo>; | ||
createMemo(content: string, visibility: string): Promise<Memo>; | ||
} | ||
|
||
export default class MemosClient { | ||
private openId: string; | ||
private host: string; | ||
export default class MemosGeneralClient { | ||
private v1: MemosClientV1; | ||
private v0: MemosClientV0; | ||
|
||
constructor(openAPI: string) { | ||
const openAPIURL = new URL(openAPI); | ||
this.host = openAPIURL.origin; | ||
const openId = openAPIURL.searchParams.get("openId"); | ||
constructor(host: string, openId: string) { | ||
if (!openId) { | ||
throw "OpenId not exist"; | ||
} | ||
this.openId = openId; | ||
} | ||
|
||
|
||
private async request<T>(url: string, method: string, payload: any): Promise<T> { | ||
try { | ||
const resp: AxiosResponse<MemosAPIResponse<T>> = await axios({ | ||
method: method, | ||
url: url, | ||
data: payload, | ||
}); | ||
if (resp.status !== 200) { | ||
throw "Something wrong!"; | ||
} else if (resp.status >= 400 && resp.status < 500) { | ||
throw resp.data?.message || "Error occurred"; | ||
} | ||
const data = resp.data.data; | ||
return data; | ||
} catch ( error) { | ||
throw "Cannot connect to memos server"; | ||
} | ||
|
||
} | ||
|
||
public async getMemos( | ||
includeArchive: boolean, | ||
limit: number, | ||
offset: number | ||
): 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", {}); | ||
} catch (error) { | ||
throw new Error(`Failed to get memos, ${error}`); | ||
} | ||
this.v1 = new MemosClientV1(host, openId); | ||
this.v0 = new MemosClientV0(host, openId); | ||
} | ||
|
||
public async updateMemo( | ||
memoId: number, | ||
payload: Record<string, any> | ||
): Promise<Memo> { | ||
const url = new URL(`${this.host}/api/memo/${memoId}`); | ||
url.searchParams.append("openId", String(this.openId)); | ||
public async getClient(): Promise<MemosClient> { | ||
try { | ||
return await this.request<Memo>(url.toString(), "PATCH", payload); | ||
await this.v1.me(); | ||
return this.v1; | ||
} catch (error) { | ||
throw new Error(`Failed to update memo, ${error}.`); | ||
} | ||
} | ||
|
||
public async createMemo(content: string, visibility: string): Promise<Memo> { | ||
const payload = { | ||
content: content, | ||
visibility: visibility, | ||
}; | ||
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); | ||
} catch (error) { | ||
throw new Error(`Failed to create memo, ${error}.`); | ||
if (error instanceof Error && error.message.includes("Not Found")) { | ||
return this.v0; | ||
} | ||
throw error; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import axios, { AxiosResponse, Method } from "axios"; | ||
import { Memo } from "../type"; | ||
import { MemosClient } from "../client"; | ||
|
||
type MemosAPIResponse<T> = { | ||
data: T; | ||
message: string; | ||
}; | ||
|
||
export default class MemosClientV0 implements MemosClient { | ||
private openId: string; | ||
private host: string; | ||
|
||
constructor(host:string, openId: string) { | ||
this.host = host; | ||
this.openId = openId; | ||
} | ||
|
||
private async request<T>( | ||
url: string, | ||
method: Method, | ||
payload: any | ||
): Promise<T> { | ||
try { | ||
const resp: AxiosResponse<MemosAPIResponse<T>> = await axios({ | ||
method: method, | ||
url: url, | ||
data: payload, | ||
}); | ||
if (resp.status !== 200) { | ||
throw "Something wrong!"; | ||
} else if (resp.status >= 400 && resp.status < 500) { | ||
throw resp.data?.message || "Error occurred"; | ||
} | ||
const data = resp.data.data; | ||
return data; | ||
} catch (error) { | ||
throw "Cannot connect to memos server"; | ||
} | ||
} | ||
|
||
public async getMemos( | ||
limit: number, | ||
offset: number, | ||
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", {}); | ||
} catch (error) { | ||
throw new Error(`Failed to get memos, ${error}`); | ||
} | ||
} | ||
|
||
public async updateMemo( | ||
memoId: number, | ||
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); | ||
} catch (error) { | ||
throw new Error(`Failed to update memo, ${error}.`); | ||
} | ||
} | ||
|
||
public async createMemo(content: string, visibility: string): Promise<Memo> { | ||
const payload = { | ||
content: content, | ||
visibility: visibility, | ||
}; | ||
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); | ||
} catch (error) { | ||
throw new Error(`Failed to create memo, ${error}.`); | ||
} | ||
} | ||
} |
Oops, something went wrong.