|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +sidebar_label: 🧩 API |
| 4 | +--- |
| 5 | + |
| 6 | +# API |
| 7 | + |
| 8 | +```js |
| 9 | +// Require the module |
| 10 | +const versionCheck = require('@version-checker/core'); |
| 11 | + |
| 12 | +// Or import |
| 13 | +import versionCheck from '@version-checker/core' |
| 14 | +``` |
| 15 | + |
| 16 | +## `function versionCheck(options, [callback])` |
| 17 | +Performs an update check with the given options. The `callback` is optional, can be omitted to return a `Promise`. |
| 18 | + |
| 19 | +### The `options` object |
| 20 | +Option | Description | Default Value | Introduction |
| 21 | +--- | --- | --- | --- |
| 22 | +token | A [personal access token](https://blog.github.com/2013-05-16-personal-api-tokens/) used to access the **Github GraphQL API (v4)**. Can be omitted and instead be read from an env variable called `GITHUB_API_TOKEN`. When no token can be found, the module will fall back to the **Github Rest API (v3)**. | `undefined` | `v2.0.0` |
| 23 | +repo | The name of your Github repository.| **None. Required.** | `v1.0.0` |
| 24 | +owner | The owner of your Github repository (usually your username).| **None. Required.** | `v1.0.0` |
| 25 | +currentVersion | Your app's current version. | **None. Required.** | `v1.0.0` |
| 26 | +fetchTags | Whether to fetch the repositories' git tags instead of the GitHub releases. Useful when no releases are created, but only tags. | `false` | `v1.0.0` |
| 27 | +latestOnly | Setting this to `true` will fetch the latest release only | `false` | `v2.2.0` |
| 28 | +excludePrereleases | Excludes pre-releases from checks. Currently only works when no token is specified. | `false` | `v2.3.0` |
| 29 | +forceRest | Will use the Github REST API (v3) even with a supplied token. | `false` | `v3.0.0` |
| 30 | + |
| 31 | +### The `callback` function (optional) |
| 32 | +Should be of the following form: |
| 33 | +```javascript |
| 34 | +function(error, update) { |
| 35 | + // ...your code |
| 36 | +} |
| 37 | +``` |
| 38 | +* `error`: |
| 39 | + * If an error occurs, this holds the error message. `null` if no error occurs. |
| 40 | +* `update`: |
| 41 | + * An object in the format specified below. `null` if no update was found. |
| 42 | + |
| 43 | +### Return type |
| 44 | + |
| 45 | +The function returns a `CheckResult` which has the following structure: |
| 46 | + |
| 47 | +```typescript |
| 48 | +interface CheckResult { |
| 49 | + src: string |
| 50 | + type: string |
| 51 | + update: ReleaseDescriptor | TagDescriptor | undefined |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +#### Properties |
| 56 | +##### `src` |
| 57 | + |
| 58 | +States which API endpoint has been used. |
| 59 | + |
| 60 | +Possible values: |
| 61 | + |
| 62 | +- `rest` |
| 63 | +- `graphql` |
| 64 | + |
| 65 | +##### `type` |
| 66 | + |
| 67 | +States whether releases or tags have been fetched. |
| 68 | + |
| 69 | +Possible values: |
| 70 | + |
| 71 | +- `releases` |
| 72 | +- `tags` |
| 73 | + |
| 74 | +##### `update` |
| 75 | + |
| 76 | +Holds the actual data on a possible update. For structure details refer to [Object schemes](#object-schemes). |
| 77 | + |
| 78 | +It is `undefined` in case no update could be found. |
| 79 | + |
| 80 | +### Using `Promise` |
| 81 | +You can omit the `callback` function to return a `Promise`, which resolves with the `update` object. |
| 82 | + |
| 83 | +## Object schemes |
| 84 | +### ReleaseDescriptor |
| 85 | +When fetching releases, an object with the following structure will be returned: |
| 86 | +```typescript |
| 87 | +interface ReleaseDescriptor { |
| 88 | + name: string |
| 89 | + tag: TagDescriptor |
| 90 | + isPrerelease: boolean |
| 91 | + isDraft: boolean |
| 92 | + publishedAt: string |
| 93 | + url: string |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### TagDescriptor |
| 98 | +When fetching tags, you will receive an object with the following structure: |
| 99 | +```typescript |
| 100 | +interface TagDescriptor { |
| 101 | + name: string |
| 102 | +} |
| 103 | +``` |
0 commit comments