Skip to content
This repository was archived by the owner on Oct 12, 2025. It is now read-only.

Commit ddac9b0

Browse files
committed
Merge branch 'main' of https://github.com/ecency/sdk
2 parents d724dd5 + d47607d commit ddac9b0

14 files changed

+241
-1
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ecency/sdk",
33
"private": false,
4-
"version": "1.0.51",
4+
"version": "1.0.52",
55
"type": "module",
66
"license": "MIT",
77
"main": "./dist/ecency-sdk.umd.js",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./use-account-bookmark-add";
2+
export * from "./use-account-bookmark-delete";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { CONFIG, getAccessToken, getQueryClient } from "@/modules/core";
2+
import { useMutation } from "@tanstack/react-query";
3+
4+
interface Payload {
5+
author: string;
6+
permlink: string;
7+
}
8+
9+
export function useBookmarkAdd(
10+
username: string | undefined,
11+
onSuccess: () => void,
12+
onError: (e: Error) => void
13+
) {
14+
return useMutation({
15+
mutationKey: ["accounts", "bookmarks", "add", username],
16+
mutationFn: async ({ author, permlink }: Payload) => {
17+
if (!username) {
18+
throw new Error("[SDK][Account][Bookmarks] – no active user");
19+
}
20+
21+
const response = await fetch(
22+
CONFIG.privateApiHost + "/private-api/bookmarks-add",
23+
{
24+
method: "POST",
25+
headers: {
26+
"Content-Type": "application/json",
27+
},
28+
body: JSON.stringify({
29+
author,
30+
permlink,
31+
code: getAccessToken(username),
32+
}),
33+
}
34+
);
35+
return response.json();
36+
},
37+
onSuccess: () => {
38+
onSuccess();
39+
getQueryClient().invalidateQueries({
40+
queryKey: ["accounts", "bookmarks", username],
41+
});
42+
},
43+
onError,
44+
});
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { CONFIG, getAccessToken, getQueryClient } from "@/modules/core";
2+
import { useMutation } from "@tanstack/react-query";
3+
4+
export function useBookmarkDelete(
5+
username: string | undefined,
6+
onSuccess: () => void,
7+
onError: (e: Error) => void
8+
) {
9+
return useMutation({
10+
mutationKey: ["accounts", "bookmarks", "delete", username],
11+
mutationFn: async (bookmarkId: string) => {
12+
if (!username) {
13+
throw new Error("[SDK][Account][Bookmarks] – no active user");
14+
}
15+
16+
const response = await fetch(
17+
CONFIG.privateApiHost + "/private-api/bookmarks-delete",
18+
{
19+
method: "POST",
20+
headers: {
21+
"Content-Type": "application/json",
22+
},
23+
body: JSON.stringify({
24+
id: bookmarkId,
25+
code: getAccessToken(username),
26+
}),
27+
}
28+
);
29+
return response.json();
30+
},
31+
onSuccess: () => {
32+
onSuccess();
33+
getQueryClient().invalidateQueries({
34+
queryKey: ["accounts", "bookmarks", username],
35+
});
36+
},
37+
onError,
38+
});
39+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./use-account-favourite-add";
2+
export * from "./use-account-favourite-delete";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { CONFIG, getAccessToken, getQueryClient } from "@/modules/core";
2+
import { useMutation } from "@tanstack/react-query";
3+
4+
export function useAccountFavouriteAdd(
5+
username: string | undefined,
6+
onSuccess: () => void,
7+
onError: (e: Error) => void
8+
) {
9+
return useMutation({
10+
mutationKey: ["accounts", "favourites", "add", username],
11+
mutationFn: async (account: string) => {
12+
if (!username) {
13+
throw new Error("[SDK][Account][Bookmarks] – no active user");
14+
}
15+
16+
const response = await fetch(
17+
CONFIG.privateApiHost + "/private-api/favorites-add",
18+
{
19+
method: "POST",
20+
headers: {
21+
"Content-Type": "application/json",
22+
},
23+
body: JSON.stringify({
24+
account,
25+
code: getAccessToken(username),
26+
}),
27+
}
28+
);
29+
return response.json();
30+
},
31+
onSuccess: () => {
32+
onSuccess();
33+
getQueryClient().invalidateQueries({
34+
queryKey: ["accounts", "favourites", username],
35+
});
36+
},
37+
onError,
38+
});
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { CONFIG, getAccessToken, getQueryClient } from "@/modules/core";
2+
import { useMutation } from "@tanstack/react-query";
3+
4+
export function useAccountFavouriteDelete(
5+
username: string | undefined,
6+
onSuccess: () => void,
7+
onError: (e: Error) => void
8+
) {
9+
return useMutation({
10+
mutationKey: ["accounts", "favourites", "add", username],
11+
mutationFn: async (account: string) => {
12+
if (!username) {
13+
throw new Error("[SDK][Account][Bookmarks] – no active user");
14+
}
15+
16+
const response = await fetch(
17+
CONFIG.privateApiHost + "/private-api/favorites-delete",
18+
{
19+
method: "POST",
20+
headers: {
21+
"Content-Type": "application/json",
22+
},
23+
body: JSON.stringify({
24+
account,
25+
code: getAccessToken(username),
26+
}),
27+
}
28+
);
29+
return response.json();
30+
},
31+
onSuccess: () => {
32+
onSuccess();
33+
getQueryClient().invalidateQueries({
34+
queryKey: ["accounts", "favourites", username],
35+
});
36+
},
37+
onError,
38+
});
39+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from "./use-account-update";
22
export * from "./use-account-relations-update";
3+
export * from "./bookmarks";
4+
export * from "./favourites";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { CONFIG, getAccessToken } from "@/modules/core";
2+
import { queryOptions } from "@tanstack/react-query";
3+
import { AccountBookmark } from "../types";
4+
5+
export function getActiveAccountBookmarksQueryOptions(
6+
activeUsername: string | undefined
7+
) {
8+
return queryOptions({
9+
queryKey: ["accounts", "bookmarks", activeUsername],
10+
enabled: !!activeUsername,
11+
queryFn: async () => {
12+
if (!activeUsername) {
13+
throw new Error("[SDK][Accounts][Bookmarks] – no active user");
14+
}
15+
const response = await fetch(
16+
CONFIG.privateApiHost + "/private-api/bookmarks",
17+
{
18+
method: "POST",
19+
headers: {
20+
"Content-Type": "application/json",
21+
},
22+
body: JSON.stringify({ code: getAccessToken(activeUsername) }),
23+
}
24+
);
25+
return (await response.json()) as AccountBookmark[];
26+
},
27+
});
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { CONFIG, getAccessToken } from "@/modules/core";
2+
import { queryOptions } from "@tanstack/react-query";
3+
import { AccountFavorite } from "../types";
4+
5+
export function getActiveAccountFavouritesQueryOptions(
6+
activeUsername: string | undefined
7+
) {
8+
return queryOptions({
9+
queryKey: ["accounts", "favourites", activeUsername],
10+
enabled: !!activeUsername,
11+
queryFn: async () => {
12+
if (!activeUsername) {
13+
throw new Error("[SDK][Accounts][Favourites] – no active user");
14+
}
15+
const response = await fetch(
16+
CONFIG.privateApiHost + "/private-api/favorites",
17+
{
18+
method: "POST",
19+
headers: {
20+
"Content-Type": "application/json",
21+
},
22+
body: JSON.stringify({ code: getAccessToken(activeUsername) }),
23+
}
24+
);
25+
return (await response.json()) as AccountFavorite[];
26+
},
27+
});
28+
}

0 commit comments

Comments
 (0)