Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with implementing in a Nodejs environment #87

Closed
Atitlanio opened this issue Nov 26, 2022 · 5 comments
Closed

Help with implementing in a Nodejs environment #87

Atitlanio opened this issue Nov 26, 2022 · 5 comments

Comments

@Atitlanio
Copy link

Atitlanio commented Nov 26, 2022

Hey, I'm having trouble getting this to work with the code provided in the README. I would like to have the capability to add posts from a nodejs backend.

I've played around with the README code a bit. I added packages including @sniptt/monads, class-transformer, [email protected] and reflect-metadata. I wasn't quite sure what needs to be included with in the headers?

I have the following and I'm attempting to log in using an administrator account. What's the purpose of retrieving a jwt once logged in?

import {{ LemmyHttp }} from 'lemmy-js-client';

var loginForm = {
    username_or_email: "username",
    password: "password"
};

var baseUrl = 'https://mydomain.io';
var client = new LemmyHttp(baseUrl);
var jwt = await client.login(loginForm).jwt;

Apologies if I'm missing something simple! Appreciate any guidance 🙏

@Atitlanio
Copy link
Author

Atitlanio commented Nov 30, 2022

okay, so I understand the jwt (used the unwrap() function to get the token) is for the auth and seem to have gotten it to work with the following code

import { LemmyHttp } from 'lemmy-js-client';

var loginForm = {
    username_or_email: "username",
    password: "password"
};

var baseUrl = 'https://mydomain.io';
var client = new LemmyHttp(baseUrl);

async function getLogin() {
  const login = await client.login(loginForm);
  return login
}

getLogin().then( (res) => {
    const jwt = res.jwt.unwrap()
    client.createPost({auth: jwt, body:"this is a test post", name: "test post", community_id: 2 })
    .then( (res) => console.log(res))
})

Curious if there's a way I can more easily obtain a list of community_id's

Also, is there anyway for the body to have some formatting to it? e.g., a new line? \n didn't do anything for me

@dessalines
Copy link
Member

Much easier to make every function async, and then call .await, rather than endlessly chaining .then. Read about async / await for js.

What list of communities are you looking for?

Also, is there anyway for the body to have some formatting to it? e.g., a new line? \n didn't do anything for me

You're creating a post? \n\n should be for newlines, a single \n doesn't make one in commonmark.

@Atitlanio
Copy link
Author

Yeah I was trying that initially with the await keyword, however it was returning undefined so I moved it into the .then structure. Maybe I was doing something funky, I'll have to try it out again as it does seem cleaner.

Nevermind about the community_id question! I didn't realize at first that you can pass a name argument into client.getCommunity(), I thought it only allowed the id #.

the double \n\n worked, thank you! I am going to try implementing the code on my server and see if it works out. Appreciate the help!

@Atitlanio
Copy link
Author

Atitlanio commented Dec 3, 2022

Cool, okay I got everything working!! I'm using it to allow people to post help questions @ https://atitlan.io/help (just a mockup for now, this link might change in the future)

One thing I noticed in my setup was you guys have "node-fetch": "^3.2.9" in your dependencies.

I run into an error when I use this (same with v3.3.0), I get the following error:

.../node_modules/lemmy-js-client/dist/http.js:52
var node_fetch_1 = require("node-fetch");
                   ^

Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/node-fetch/src/index.js from .../node_modules/lemmy-js-client/dist/http.js not supported.
Instead change the require of index.js in /.../node_modules/lemmy-js-client/dist/http.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (.../node_modules/lemmy-js-client/dist/http.js:52:20
    at Object.<anonymous> (.../node_modules/lemmy-js-client/dist/index.js:17:14)
    at async Promise.all (index 0) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.8.0

According to stackoverflow it's simply a problem with using require instead of import.

I downgraded to node-fetch v2 (specifically 2.6.7) and it works!

Thanks for your help and highly appreciate your guys work on this 🚀

@dessalines
Copy link
Member

dessalines commented Dec 9, 2022

I've had weird problems with node-fetch too, sry about that. Your PR is merged.

I would recommend using import tho and not require, and of course using typescript and not javascript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants