简体   繁体   中英

How to use `request.js` by Octokit on browser?

I am a beginner with JavaScript. Even though I am directly running the examples on the readme , it doesn't work for me.
Here is a simple case where I try to get the number of my (user's) repositories.

I have the following html file (sans the api token ) -

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <script type="module">
        import { request } from "https://cdn.pika.dev/@octokit/request";
    </script>
    <script> 
        const octokitRequest = require('@octokit/request');
        myAsyncMethod()
        async function myAsyncMethod () {
            const result = await request("GET /users/:user/repos", {
                headers: {
                    authorization: "token <your token>"
                },
                user: "armsp"
                });
            console.log(`${result.data.length} repos found.`);
        }
    </script>   
</body>

ERROR

require.min.js:1 Uncaught Error: Module name "@octokit/request" has not been loaded yet for context: _. Use require([])
https://requirejs.org/docs/errors.html#notloaded
    at makeError (require.min.js:1)
    at Object.s [as require] (require.min.js:1)
    at requirejs (require.min.js:1)
    at github-api-test.html:11

github-api-test.html:1 Uncaught SyntaxError: The requested module '/universal-user-agent/^4.0.0/es2019/universal-user-agent.js' does not provide an export named 'default'

Then I tried to download the js file itself and include it in script tag the usual way, but I don't see any request.js file in src folder, all are .ts files. I don't know how to make sense of it.

Any help would be appreciated.

After talking with the authors of the library, it seems like it was an issue with a bad release. It has been fixed with version 5.3.1. The following example works now -

    <script type="module">
      import { request } from "https://cdn.pika.dev/@octokit/request";

      myAsyncMethod();

      async function myAsyncMethod() {
        const result = await request("GET /");
        console.log(result.data);
      }
    </script>

Live Demo here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM