neet / masto.js

🐘 Universal Mastodon API client for JavaScript
https://neet.github.io/masto.js/
MIT License
737 stars 51 forks source link

react-native support #341

Closed greyivy closed 6 months ago

greyivy commented 3 years ago

Has anyone had any luck getting this to run on react-native?

More specifically, the dependence on native modules and a regular expression seems to be breaking things. I'm fairly new to react-native so I don't have additional details yet.

neet commented 3 years ago

Happy holidays @greyivy, I tested Masto.js only on Node.js and browsers, but I would like to make it work on more devices 😄 Could you provide a specific error you've got and/or reproduction of the code?

neet commented 3 years ago

Closing due to inactivity

shamoons commented 2 years ago
image

Seems to not work on React Native

shamoons commented 2 years ago

Thanks for looking @neet

shamoons commented 2 years ago

Just wanted to see if we had any traction here?

neet commented 2 years ago

Hi @shamoons , sorry for the lack of information

Although I'm unfamiliar with React Native, I'm guessing that the error seems to be caused by Masto.js trying to import Node.js module querystring which is seemingly unavailable in React Native environment.

I'm not sure whether it might be the case, but I opened PR #680 to make it work without querystring. So would you verify that it resolves the error? You can try it by installing the beta verison

yarn add masto@4.6.2-rc1
# or if you're using npm
npm install masto@4.6.2-rc1
shamoons commented 2 years ago

@neet - getting a new issue:

Error: ENOENT: no such file or directory, open '/Users/shamoon/Sites/stampede/HttpAxiosImpl@http:/192.168.5.48:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false'
    at Object.openSync (fs.js:498:3)
    at Object.readFileSync (fs.js:394:35)

I assume masto is using axios somewhere?

neet commented 2 years ago

@shamoons Would you provide a reproduction repository for it? so that I can run it locally and inspect your error. That error message does not seem to be thrown from Masto.

343max commented 2 years ago

I managed to use masto.js 4.4.1 inside a expo project using this call:

import {
  HttpNativeImpl,
  InstanceRepository,
  MastoClient,
  MastoConfig,
  SerializerNativeImpl,
  WsNodejsImpl,
} from "masto"

export const mastoLogin = async (config: MastoConfig): Promise<MastoClient> => {
  const serializer = new SerializerNativeImpl()
  const http = new HttpNativeImpl(config, serializer)
  const instance = await new InstanceRepository(http, "1.0.0").fetch()
  const ws = new WsNodejsImpl(
    instance.urls.streamingApi,
    instance.version,
    config,
    serializer
  )

  return new MastoClient(http, ws, instance.version, config)
}

It doesn't work with the latest 4.6.2 though ('Content-Type' not set, the headers are all wrapped in a object called Map, something like: Map: {'content-type': 'text/json...'}

shamoons commented 2 years ago
node_modules/masto/dist/index.js: querystring could not be found within the project or in these directories:
  node_modules/masto/node_modules
  node_modules
   5 | var axios = require('axios');
   6 | var changeCase = require('change-case');
>  7 | var querystring = require('querystring');
     |                            ^
   8 | var EventEmitter = require('eventemitter3');
   9 | var WebSocket$1 = require('isomorphic-ws');

This is with "masto": "4.4.1",

neet commented 2 years ago

@343max 's workaround is clever to opt out of Node.js dependency in the old version but still have no idea why getting problems with 4.6.2.

@shamoons To clarify, just using 4.4.1 will not solve the error because @343max 's snippet above is using internal modules to customise the login().

Again, could you please show me a repository to reproduce the error you are getting? I have no experience in React Native so I can't figure out what is actual issue here just by pasting error messages 🙏

343max commented 2 years ago

@neet I added PR #682 that will revert a change you introduced recently that broke React Native compatibility.

aarononeal commented 1 year ago

@neet Can't determine why but out of the box in my Expo / React Native app I'm getting the no such file or directory error reported by shamoons and it is worked around using what 343max suggested to avoid the Axios dependency. Is there a different way to switch to fetch without relying on internal dependencies?

shamoons commented 1 year ago

I tried 4.6.1, no dice

shamoons commented 1 year ago

Anyone have any ideas?

aarononeal commented 1 year ago

I got it working using what 343max proposed, though not ideal. What issue are you running into with that workaround?

shamoons commented 1 year ago

@aarononeal - are using version 4.6.1?

aarononeal commented 1 year ago

@shamoons 4.6.6

neet commented 6 months ago

I'm closing this issue as it seems that it doesn't happen anymore. For anyone who faced a similar problem, feel free to create another issue but don't forget to provide a minimal reproducible example.