Hi I’m looking for how to make some bookmarklets to browse a few things in a different way. Is there some way to at least get a json of a post/community/user to view some info that isn’t readily available on the UI?

  • csos95@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    1 year ago

    There’s an API section1 in the Lemmy documentation that describes some basic information and links to other resources.
    The LemmyHttp class2 in the linked lemmy-js-client documentation lists out the endpoints, parameters, and return values.

    • God@sh.itjust.worksOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      1 year ago

      been playing with it for like 20 minutes and it seems like the documentation and the code are different, so i can’t implement any of what they say i can, been trying to just find the answers in the code with no luck cuz apparently the repo is all typescript and there’s no code in the main repo, idk where this client’s source code is.

      have u been able to do any requests w it?

    • God@sh.itjust.worksOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      1 year ago

      nevermind, i actually did it now 4 minutes after complaining because i saw there’s code examples here:

      https://github.com/LemmyNet/lemmy/blob/ecc9469a02e63eab4e19093007c7ba6db0dca079/api_tests/src/shared.ts#L4

      this works:

      import { GetPost, GetPostResponse, LemmyHttp, Login } from "lemmy-js-client";
      
      export interface API {
        client: LemmyHttp;
        auth: string;
      }
      
      export let alpha: API = {
        client: new LemmyHttp("https://sh.itjust.works"),
        auth: "",
      };
      
      let formAlpha: Login = {
        username_or_email: "username",
        password: "password",
      };
      
      export async function getPost(
        api: API,
        post_id: number
      ): Promise<GetPostResponse> {
        let form: GetPost = {
          id: post_id,
          auth: api.auth,
        };
        return api.client.getPost(form);
      }
      
      getPost(alpha, 1).then((res) => {
        console.log(res);
      });