github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@octokit/core/README.md (about)

     1  # core.js
     2  
     3  > Extendable client for GitHub's REST & GraphQL APIs
     4  
     5  [![@latest](https://img.shields.io/npm/v/@octokit/core.svg)](https://www.npmjs.com/package/@octokit/core)
     6  [![Build Status](https://github.com/octokit/core.js/workflows/Test/badge.svg)](https://github.com/octokit/core.js/actions?query=workflow%3ATest+branch%3Amaster)
     7  
     8  <!-- toc -->
     9  
    10  - [Usage](#usage)
    11    - [REST API example](#rest-api-example)
    12    - [GraphQL example](#graphql-example)
    13  - [Options](#options)
    14  - [Defaults](#defaults)
    15  - [Authentication](#authentication)
    16  - [Logging](#logging)
    17  - [Hooks](#hooks)
    18  - [Plugins](#plugins)
    19  - [Build your own Octokit with Plugins and Defaults](#build-your-own-octokit-with-plugins-and-defaults)
    20  - [LICENSE](#license)
    21  
    22  <!-- tocstop -->
    23  
    24  If you need a minimalistic library to utilize GitHub's [REST API](https://developer.github.com/v3/) and [GraphQL API](https://developer.github.com/v4/) which you can extend with plugins as needed, then `@octokit/core` is a great starting point.
    25  
    26  If you don't need the Plugin API then using [`@octokit/request`](https://github.com/octokit/request.js/) or [`@octokit/graphql`](https://github.com/octokit/graphql.js/) directly is a good alternative.
    27  
    28  ## Usage
    29  
    30  <table>
    31  <tbody valign=top align=left>
    32  <tr><th>
    33  Browsers
    34  </th><td width=100%>
    35  Load <code>@octokit/core</code> directly from <a href="https://cdn.skypack.dev">cdn.skypack.dev</a>
    36          
    37  ```html
    38  <script type="module">
    39  import { Octokit } from "https://cdn.skypack.dev/@octokit/core";
    40  </script>
    41  ```
    42  
    43  </td></tr>
    44  <tr><th>
    45  Node
    46  </th><td>
    47  
    48  Install with <code>npm install @octokit/core</code>
    49  
    50  ```js
    51  const { Octokit } = require("@octokit/core");
    52  // or: import { Octokit } from "@octokit/core";
    53  ```
    54  
    55  </td></tr>
    56  </tbody>
    57  </table>
    58  
    59  ### REST API example
    60  
    61  ```js
    62  // Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
    63  const octokit = new Octokit({ auth: `personal-access-token123` });
    64  
    65  const response = await octokit.request("GET /orgs/{org}/repos", {
    66    org: "octokit",
    67    type: "private",
    68  });
    69  ```
    70  
    71  See [`@octokit/request`](https://github.com/octokit/request.js) for full documentation of the `.request` method.
    72  
    73  ### GraphQL example
    74  
    75  ```js
    76  const octokit = new Octokit({ auth: `secret123` });
    77  
    78  const response = await octokit.graphql(
    79    `query ($login: String!) {
    80      organization(login: $login) {
    81        repositories(privacy: PRIVATE) {
    82          totalCount
    83        }
    84      }
    85    }`,
    86    { login: "octokit" }
    87  );
    88  ```
    89  
    90  See [`@octokit/graphql`](https://github.com/octokit/graphql.js) for full documentation of the `.graphql` method.
    91  
    92  ## Options
    93  
    94  <table>
    95    <thead align=left>
    96      <tr>
    97        <th>
    98          name
    99        </th>
   100        <th>
   101          type
   102        </th>
   103        <th width=100%>
   104          description
   105        </th>
   106      </tr>
   107    </thead>
   108    <tbody align=left valign=top>
   109      <tr>
   110        <th>
   111          <code>options.authStrategy</code>
   112        </th>
   113        <td>
   114          <code>Function<code>
   115        </td>
   116        <td>
   117          Defaults to <a href="https://github.com/octokit/auth-token.js#readme"><code>@octokit/auth-token</code></a>. See <a href="#authentication">Authentication</a> below for examples.
   118        </td>
   119      </tr>
   120      <tr>
   121        <th>
   122          <code>options.auth</code>
   123        </th>
   124        <td>
   125          <code>String</code> or <code>Object</code>
   126        </td>
   127        <td>
   128          See <a href="#authentication">Authentication</a> below for examples.
   129        </td>
   130      </tr>
   131      <tr>
   132        <th>
   133          <code>options.baseUrl</code>
   134        </th>
   135        <td>
   136          <code>String</code>
   137        </td>
   138        <td>
   139  
   140  When using with GitHub Enterprise Server, set `options.baseUrl` to the root URL of the API. For example, if your GitHub Enterprise Server's hostname is `github.acme-inc.com`, then set `options.baseUrl` to `https://github.acme-inc.com/api/v3`. Example
   141  
   142  ```js
   143  const octokit = new Octokit({
   144    baseUrl: "https://github.acme-inc.com/api/v3",
   145  });
   146  ```
   147  
   148  </td></tr>
   149      <tr>
   150        <th>
   151          <code>options.previews</code>
   152        </th>
   153        <td>
   154          <code>Array of Strings</code>
   155        </td>
   156        <td>
   157  
   158  Some REST API endpoints require preview headers to be set, or enable
   159  additional features. Preview headers can be set on a per-request basis, e.g.
   160  
   161  ```js
   162  octokit.request("POST /repos/{owner}/{repo}/pulls", {
   163    mediaType: {
   164      previews: ["shadow-cat"],
   165    },
   166    owner,
   167    repo,
   168    title: "My pull request",
   169    base: "master",
   170    head: "my-feature",
   171    draft: true,
   172  });
   173  ```
   174  
   175  You can also set previews globally, by setting the `options.previews` option on the constructor. Example:
   176  
   177  ```js
   178  const octokit = new Octokit({
   179    previews: ["shadow-cat"],
   180  });
   181  ```
   182  
   183  </td></tr>
   184      <tr>
   185        <th>
   186          <code>options.request</code>
   187        </th>
   188        <td>
   189          <code>Object</code>
   190        </td>
   191        <td>
   192  
   193  Set a default request timeout (`options.request.timeout`) or an [`http(s).Agent`](https://nodejs.org/api/http.html#http_class_http_agent) e.g. for proxy usage (Node only, `options.request.agent`).
   194  
   195  There are more `options.request.*` options, see [`@octokit/request` options](https://github.com/octokit/request.js#request). `options.request` can also be set on a per-request basis.
   196  
   197  </td></tr>
   198      <tr>
   199        <th>
   200          <code>options.timeZone</code>
   201        </th>
   202        <td>
   203          <code>String</code>
   204        </td>
   205        <td>
   206  
   207  Sets the `Time-Zone` header which defines a timezone according to the [list of names from the Olson database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
   208  
   209  ```js
   210  const octokit = new Octokit({
   211    timeZone: "America/Los_Angeles",
   212  });
   213  ```
   214  
   215  The time zone header will determine the timezone used for generating the timestamp when creating commits. See [GitHub's Timezones documentation](https://developer.github.com/v3/#timezones).
   216  
   217  </td></tr>
   218      <tr>
   219        <th>
   220          <code>options.userAgent</code>
   221        </th>
   222        <td>
   223          <code>String</code>
   224        </td>
   225        <td>
   226  
   227  A custom user agent string for your app or library. Example
   228  
   229  ```js
   230  const octokit = new Octokit({
   231    userAgent: "my-app/v1.2.3",
   232  });
   233  ```
   234  
   235  </td></tr>
   236    </tbody>
   237  </table>
   238  
   239  ## Defaults
   240  
   241  You can create a new Octokit class with customized default options.
   242  
   243  ```js
   244  const MyOctokit = Octokit.defaults({
   245    auth: "personal-access-token123",
   246    baseUrl: "https://github.acme-inc.com/api/v3",
   247    userAgent: "my-app/v1.2.3",
   248  });
   249  const octokit1 = new MyOctokit();
   250  const octokit2 = new MyOctokit();
   251  ```
   252  
   253  If you pass additional options to your new constructor, the options will be merged shallowly.
   254  
   255  ```js
   256  const MyOctokit = Octokit.defaults({
   257    foo: {
   258      opt1: 1,
   259    },
   260  });
   261  const octokit = new MyOctokit({
   262    foo: {
   263      opt2: 1,
   264    },
   265  });
   266  // options will be { foo: { opt2: 1 }}
   267  ```
   268  
   269  If you need a deep or conditional merge, you can pass a function instead.
   270  
   271  ```js
   272  const MyOctokit = Octokit.defaults((options) => {
   273    return {
   274      foo: Object.assign({}, options.foo, { opt2: 1 }),
   275    };
   276  });
   277  const octokit = new MyOctokit({
   278    foo: { opt2: 1 },
   279  });
   280  // options will be { foo: { opt1: 1, opt2: 1 }}
   281  ```
   282  
   283  Be careful about mutating the `options` object in the `Octokit.defaults` callback, as it can have unforeseen consequences.
   284  
   285  ## Authentication
   286  
   287  Authentication is optional for some REST API endpoints accessing public data, but is required for GraphQL queries. Using authentication also increases your [API rate limit](https://developer.github.com/v3/#rate-limiting).
   288  
   289  By default, Octokit authenticates using the [token authentication strategy](https://github.com/octokit/auth-token.js). Pass in a token using `options.auth`. It can be a personal access token, an OAuth token, an installation access token or a JSON Web Token for GitHub App authentication. The `Authorization` header will be set according to the type of token.
   290  
   291  ```js
   292  import { Octokit } from "@octokit/core";
   293  
   294  const octokit = new Octokit({
   295    auth: "mypersonalaccesstoken123",
   296  });
   297  
   298  const { data } = await octokit.request("/user");
   299  ```
   300  
   301  To use a different authentication strategy, set `options.authStrategy`. A list of authentication strategies is available at [octokit/authentication-strategies.js](https://github.com/octokit/authentication-strategies.js/#readme).
   302  
   303  Example
   304  
   305  ```js
   306  import { Octokit } from "@octokit/core";
   307  import { createAppAuth } from "@octokit/auth-app";
   308  
   309  const appOctokit = new Octokit({
   310    authStrategy: createAppAuth,
   311    auth: {
   312      appId: 123,
   313      privateKey: process.env.PRIVATE_KEY,
   314    },
   315  });
   316  
   317  const { data } = await appOctokit.request("/app");
   318  ```
   319  
   320  The `.auth()` method returned by the current authentication strategy can be accessed at `octokit.auth()`. Example
   321  
   322  ```js
   323  const { token } = await appOctokit.auth({
   324    type: "installation",
   325    installationId: 123,
   326  });
   327  ```
   328  
   329  ## Logging
   330  
   331  There are four built-in log methods
   332  
   333  1. `octokit.log.debug(message[, additionalInfo])`
   334  1. `octokit.log.info(message[, additionalInfo])`
   335  1. `octokit.log.warn(message[, additionalInfo])`
   336  1. `octokit.log.error(message[, additionalInfo])`
   337  
   338  They can be configured using the [`log` client option](client-options). By default, `octokit.log.debug()` and `octokit.log.info()` are no-ops, while the other two call `console.warn()` and `console.error()` respectively.
   339  
   340  This is useful if you build reusable [plugins](#plugins).
   341  
   342  If you would like to make the log level configurable using an environment variable or external option, we recommend the [console-log-level](https://github.com/watson/console-log-level) package. Example
   343  
   344  ```js
   345  const octokit = new Octokit({
   346    log: require("console-log-level")({ level: "info" }),
   347  });
   348  ```
   349  
   350  ## Hooks
   351  
   352  You can customize Octokit's request lifecycle with hooks.
   353  
   354  ```js
   355  octokit.hook.before("request", async (options) => {
   356    validate(options);
   357  });
   358  octokit.hook.after("request", async (response, options) => {
   359    console.log(`${options.method} ${options.url}: ${response.status}`);
   360  });
   361  octokit.hook.error("request", async (error, options) => {
   362    if (error.status === 304) {
   363      return findInCache(error.response.headers.etag);
   364    }
   365  
   366    throw error;
   367  });
   368  octokit.hook.wrap("request", async (request, options) => {
   369    // add logic before, after, catch errors or replace the request altogether
   370    return request(options);
   371  });
   372  ```
   373  
   374  See [before-after-hook](https://github.com/gr2m/before-after-hook#readme) for more documentation on hooks.
   375  
   376  ## Plugins
   377  
   378  Octokit’s functionality can be extended using plugins. The `Octokit.plugin()` method accepts a plugin (or many) and returns a new constructor.
   379  
   380  A plugin is a function which gets two arguments:
   381  
   382  1. the current instance
   383  2. the options passed to the constructor.
   384  
   385  In order to extend `octokit`'s API, the plugin must return an object with the new methods.
   386  
   387  ```js
   388  // index.js
   389  const { Octokit } = require("@octokit/core")
   390  const MyOctokit = Octokit.plugin(
   391    require("./lib/my-plugin"),
   392    require("octokit-plugin-example")
   393  );
   394  
   395  const octokit = new MyOctokit({ greeting: "Moin moin" });
   396  octokit.helloWorld(); // logs "Moin moin, world!"
   397  octokit.request("GET /"); // logs "GET / - 200 in 123ms"
   398  
   399  // lib/my-plugin.js
   400  module.exports = (octokit, options = { greeting: "Hello" }) => {
   401    // hook into the request lifecycle
   402    octokit.hook.wrap("request", async (request, options) => {
   403      const time = Date.now();
   404      const response = await request(options);
   405      console.log(
   406        `${options.method} ${options.url} – ${response.status} in ${Date.now() -
   407          time}ms`
   408      );
   409      return response;
   410    });
   411  
   412    // add a custom method
   413    return {
   414      helloWorld: () => console.log(`${options.greeting}, world!`);
   415    }
   416  };
   417  ```
   418  
   419  ## Build your own Octokit with Plugins and Defaults
   420  
   421  You can build your own Octokit class with preset default options and plugins. In fact, this is mostly how the `@octokit/<context>` modules work, such as [`@octokit/action`](https://github.com/octokit/action.js):
   422  
   423  ```js
   424  const { Octokit } = require("@octokit/core");
   425  const MyActionOctokit = Octokit.plugin(
   426    require("@octokit/plugin-paginate-rest").paginateRest,
   427    require("@octokit/plugin-throttling").throttling,
   428    require("@octokit/plugin-retry").retry
   429  ).defaults({
   430    throttle: {
   431      onAbuseLimit: (retryAfter, options) => {
   432        /* ... */
   433      },
   434      onRateLimit: (retryAfter, options) => {
   435        /* ... */
   436      },
   437    },
   438    authStrategy: require("@octokit/auth-action").createActionAuth,
   439    userAgent: `my-octokit-action/v1.2.3`,
   440  });
   441  
   442  const octokit = new MyActionOctokit();
   443  const installations = await octokit.paginate("GET /app/installations");
   444  ```
   445  
   446  ## LICENSE
   447  
   448  [MIT](LICENSE)