Skip to content

Commit

Permalink
Merge pull request #7 from rui2015/dynamic-imports-test
Browse files Browse the repository at this point in the history
Add Node detection
  • Loading branch information
FranciscoRibeiro03 authored Jul 29, 2021
2 parents 64ff8e8 + 4c0bda1 commit 948840b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ api.downloadMapByHash('hash', directory).then(fileLocation => {}).catch(err => {
You need to make sure that the App Version on the `BeatSaverAPI` constructor is valid [SemVer](https://semver.org/)
## Important Notes
- You can't use the `downloadMapByKey` and the `downloadMapByHash` functions if you're using this library on a browser.
- While using this library with Node.js, all requests made to BeatSaver are made with the User-Agent `AppName/Version` (so, in the example case, the User-Agent sent to BeatSaver will be `Application Name/1.0.0`). However, if you're using this library on a browser, the requests will use your browser's User-Agent.
## Important for users using webpack
If you try to use [webpack](https://webpack.js.org/) for front-end use with this library, it will throw two errors, due to the fact that the `fs` module is not available.
To stop these errors, you need to add one of the following to your `webpack.config.js` file:
```js
// Classic
module.exports = {
...
node: {
fs: "empty"
}
}
```
```js
// Symfony
// You edit the end of the file with the following
let config = Encore.getWebpackConfig();
config.node = {
fs: "empty"
}
module.exports = config
```
Thank you [KriKrixs](https://github.com/KriKrixs) for this
## Help
If you need help using this module or if you found an error with it, you can always contact me on my [Discord Server](https://discord.gg/qjKhqA3)
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
"homepage": "https://github.com/rui2015/beatsaver-api#readme",
"dependencies": {
"axios": "^0.21.1",
"detect-node": "^2.1.0",
"semver": "^7.3.5",
"tslib": "^2.3.0"
},
"devDependencies": {
"@types/detect-node": "^2.0.0",
"@types/jest": "^26.0.24",
"@types/node": "^16.4.1",
"@types/semver": "^7.3.7",
Expand Down
16 changes: 11 additions & 5 deletions src/BeatSaverAPI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { AxiosInstance } from 'axios';
import { valid } from 'semver';
import downloadMapByHash from './api/downloadMapByHash';
import downloadMapByKey from './api/downloadMapByKey';
import isNode from 'detect-node';

import getMapDetailsByHash from './api/getMapDetailsByHash';
import getMapDetailsByKey from './api/getMapDetailsByKey';
import getMapsByUploader from './api/getMapsByUploader';
Expand Down Expand Up @@ -31,9 +31,11 @@ class BeatSaverAPI {

this.axiosInstance = axios.create({
baseURL: 'https://beatsaver.com/api',
headers: {
'User-Agent': `${this.appName}/${this.appVersion}`,
},
headers: !isNode
? {}
: {
'User-Agent': `${this.appName}/${this.appVersion}`,
},
});
}

Expand Down Expand Up @@ -74,10 +76,14 @@ class BeatSaverAPI {
}

public async downloadMapByHash(hash: string, directory: string) {
if (!isNode) return "downloadMapByHash function can't be used in a browser";
const { default: downloadMapByHash } = await import('./api/downloadMapByHash');
return downloadMapByHash(hash, directory, this.axiosInstance);
}

public async downloadMapByKey(key: string, directory: string) {
if (!isNode) return "downloadMapByKey function can't be used in a browser";
const { default: downloadMapByKey } = await import('./api/downloadMapByKey');
return downloadMapByKey(key, directory, this.axiosInstance);
}
}
Expand Down

0 comments on commit 948840b

Please sign in to comment.