Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
EINDEX committed May 28, 2023
2 parents 4067a79 + 5f55458 commit e870c90
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/memos/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import axios, { AxiosResponse } from "axios";
import { ListMemo, Memo, SingleMemo } from "./type";
import { ListMemo, Memo } from "./type";

type MemosAPIResponse<T> = {
data:T;
message: string;
};


export default class MemosClient {
private openId: string;
Expand All @@ -15,23 +21,39 @@ export default class MemosClient {
this.openId = openId;
}


private async request<T>(url: string, method: string, payload: any): Promise<T> {
const resp: AxiosResponse<MemosAPIResponse<T>> = await axios({
method: method,
url: url,
data: payload,
});
if (resp.status !== 200) {
throw "Connect issue";
} else if (resp.status >= 400 && resp.status < 500) {
throw resp.data?.message || "Error occurred";
}
const data = resp.data.data;
return data;
}

public async getMemos(
includeArchive: boolean,
limit: number,
offset: number
): Promise<Array<Memo>> {
): 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());
const resp: AxiosResponse<ListMemo> = await axios.get(url.toString());
if (resp.status !== 200) {
throw "Connect issue";
try {
return await this.request<Memo[]>(url.toString(), "GET", {});
} catch (error) {
throw new Error(`Failed to get memos, ${error}`);
}
return resp.data.data;
}

public async updateMemo(
Expand All @@ -40,16 +62,11 @@ export default class MemosClient {
): Promise<Memo> {
const url = new URL(`${this.host}/api/memo/${memoId}`);
url.searchParams.append("openId", String(this.openId));
const resp: AxiosResponse<SingleMemo> = await axios.patch(
url.toString(),
payload
);
if (resp.status !== 200) {
throw "Connect issue";
} else if (resp.status >= 400 && resp.status < 500) {
throw resp.data.message;
try {
return await this.request<Memo>(url.toString(), "PATCH", payload);
} catch (error) {
throw new Error(`Failed to update memo, ${error}.`);
}
return resp.data.data;
}

public async createMemo(content: string, visibility: string): Promise<Memo> {
Expand All @@ -59,15 +76,10 @@ export default class MemosClient {
};
const url = new URL(`${this.host}/api/memo`);
url.searchParams.append("openId", String(this.openId));
const resp: AxiosResponse<SingleMemo> = await axios.post(
url.toString(),
payload
);
if (resp.status !== 200) {
throw "Connect issue";
} else if (resp.status >= 400 && resp.status < 500) {
throw resp.data.message;
try {
return await this.request<Memo>(url.toString(), "POST", payload);
} catch (error) {
throw new Error(`Failed to create memo, ${error}.`);
}
return resp.data.data;
}
}

0 comments on commit e870c90

Please sign in to comment.