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

Importing sdk in express js project #65

Open
fubelli opened this issue Dec 28, 2022 · 7 comments
Open

Importing sdk in express js project #65

fubelli opened this issue Dec 28, 2022 · 7 comments

Comments

@fubelli
Copy link

fubelli commented Dec 28, 2022

I am trying to develop an expressjs app and inside my project I would like to use the following sdk: https://loopring-1.gitbook.io/loopring-dev-docs/sdk/sdk-guides

Following the instructions, I have installed the package via npm and I created a file inside my project named loop.js where I have pasted the starting code provided in the website:

import * as sdk from "@loopring-web/loopring-sdk";
export class LoopringAPIClass {
  public static userAPI: UserAPI;
  public static exchangeAPI: ExchangeAPI;
  public static ammpoolAPI: AmmpoolAPI;
  public static walletAPI: WalletAPI;
  public static wsAPI: WsAPI;
  public static nftAPI: NFTAPI;
  public static delegate: DelegateAPI;
  public static globalAPI: GlobalAPI;
  public static contractAPI: typeof ContractAPI;
  public static __chainId__: sdk.ChainId;
  public static InitApi = (chainId: sdk.ChainId) => {
    LoopringAPI.userAPI = new UserAPI({ chainId });
    LoopringAPI.exchangeAPI = new ExchangeAPI({ chainId });
    LoopringAPI.globalAPI = new GlobalAPI({ chainId });
    LoopringAPI.ammpoolAPI = new AmmpoolAPI({ chainId });
    LoopringAPI.walletAPI = new WalletAPI({ chainId });
    LoopringAPI.wsAPI = new WsAPI({ chainId });
    LoopringAPI.nftAPI = new NFTAPI({ chainId });
    LoopringAPI.delegate = new DelegateAPI({ chainId });
    LoopringAPI.__chainId__ = chainId;
    LoopringAPI.contractAPI = ContractAPI;
  };
}
/* env:
 * test:  sdk.ChainId.GOERLI 
 * eth:  sdk.ChainId.MAINNET 
 */
LoopringAPIClass.InitApi({sdk.ChainId.MAINNET}); 

When I try to run the code in nodejs I get the following error: error1

I have tried to add the "type": "module" line in package.json but then I got this different kind of error by running the import line:

error2

Could you please help me to integrate the sdk in my project?

@ZeeshanAhmadKhalil
Copy link

try removing LoopringAPIClass.InitApi({sdk.ChainId.MAINNET}); from above file and calling it from your server.js or index.js file

@fubelli
Copy link
Author

fubelli commented Dec 29, 2022

Hello, I've tried the following setup but I keep getting the same error.

loop.js

import * as sdk from "@loopring-web/loopring-sdk";
export class LoopringAPIClass {
  public static userAPI: UserAPI;
  public static exchangeAPI: ExchangeAPI;
  public static ammpoolAPI: AmmpoolAPI;
  public static walletAPI: WalletAPI;
  public static wsAPI: WsAPI;
  public static nftAPI: NFTAPI;
  public static delegate: DelegateAPI;
  public static globalAPI: GlobalAPI;
  public static contractAPI: typeof ContractAPI;
  public static __chainId__: sdk.ChainId;
  public static InitApi = (chainId: sdk.ChainId) => {
    LoopringAPI.userAPI = new UserAPI({ chainId });
    LoopringAPI.exchangeAPI = new ExchangeAPI({ chainId });
    LoopringAPI.globalAPI = new GlobalAPI({ chainId });
    LoopringAPI.ammpoolAPI = new AmmpoolAPI({ chainId });
    LoopringAPI.walletAPI = new WalletAPI({ chainId });
    LoopringAPI.wsAPI = new WsAPI({ chainId });
    LoopringAPI.nftAPI = new NFTAPI({ chainId });
    LoopringAPI.delegate = new DelegateAPI({ chainId });
    LoopringAPI.__chainId__ = chainId;
    LoopringAPI.contractAPI = ContractAPI;
  };
}

server.js

const LoopringAPIClass = require("./loop")

And finally I've added the "type": "module" line in the package.json file in order to avoid the first kind of error.

The error I get by running the line inside server.js is the following:

screen3

@akwodkiewicz
Copy link

akwodkiewicz commented Nov 20, 2023

There are several issues with this library:

  1. The library is built as an ESM package, but the "main" in package.json points to a CommonJS file. This is what causes the original error for the author of this issue. FYI, the "module" entry in the manifest is not understood by Node, it's probably for the bundler.

    You would either need to replace the content of the main entry and point to the ESM version of the file. But if people rely on the CJS implementation that is imported from index.js, then you should probably remove the main entry and replace it with a modern exports entry, with separate import and require keys (for ESM version and CommonJS version respectively).

  2. The library imports a lot of core-js, but it's not in the source code, and it's not mentioned in the package.json's dependencies. This is mostly because the core-js is added during the bundling process. I don't know if it's required at all for Node. Anyway, when you fix the problem no. 1, you still cannot use the package, because the package does not define its dependencies correctly.

    You should either get rid of the core-js polyfill library or make it an explicit dependency somehow (not sure which is better here).

  3. The TS typings are wrong -- VSCode only highlights 4 top-level exports, nothing more. There's no way to correctly import the types of all the APIs and other utils that are exported from nested subdirectories.

    Screenshot 2023-11-17 at 18 03 04

    I don't yet know how to fix this issue.

@windatang
Copy link
Contributor

can you support a environment for me can reproduce your you problem

@akwodkiewicz
Copy link

Yes, I'll try to deliver it later this week. I also want to provide several PRs that should resolve some of these issues or improve DX of this project. But first, the simple example repo!

@akwodkiewicz
Copy link

akwodkiewicz commented Nov 22, 2023

Here's the example app. It's basically a "hello world" with a couple of imports.

https://github.com/akwodkiewicz/loopring-sdk-usage-example

There are 2 versions of the app on 2 branches: commonjs and esm. The issue is on both of them.

@windatang
Copy link
Contributor

old version package cjs with js wrap.
new version 3.8+ will dirctory package with cjs.
you can directory use required.

Code change in package.json

  "source": "src/index.ts",
  "exports": {
    "require": "./dist/index.cjs",
    "import": "./dist/index.esm.js"
  },```
  demo code 
  ```  info('imported default:', nodeFetch)
  const library = require('@loopring-web/loopring-sdk') //await import(); // runtime error
  const globalAPI = new library.GlobalAPI({ chainId: 5 })
  // Dynamic import of @loopring-web/loopring-sdk that claims to be ESM
  info('imported default:', globalAPI)```

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

4 participants