Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

Commit b2fa7d1

Browse files
committed
Modify the returned data structure
The new type "CheckResult" now contains information about how the data was fetched
1 parent e6d234d commit b2fa7d1

8 files changed

Lines changed: 55 additions & 28 deletions

File tree

packages/api/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ export interface ReleaseDescriptor {
8989
url: string
9090
}
9191

92-
export type CallbackFunction = (error?: Error|string, update?: ReleaseDescriptor | TagDescriptor) => void
92+
export interface CheckResult {
93+
src: 'rest'|'graphql'
94+
type: 'releases'|'tags'
95+
update: ReleaseDescriptor | TagDescriptor | undefined
96+
}
97+
98+
export type CheckFunction = (options: CheckOptions) => Promise<CheckResult>
99+
100+
export type CallbackFunction = (error?: Error|string, update?: CheckResult) => void
93101

94102
export type RestHandlerFunction = (res: IncomingMessage) => void
95103

packages/core/src/check.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { CheckFunction, CheckOptions, CheckResult } from '@github-version-checker/api'
12
import graphql from './query/graphql'
23
import rest from './query/rest'
3-
import { CheckOptions, ReleaseDescriptor, TagDescriptor } from '@github-version-checker/api'
44

55
/**
66
* Checks whether a new version is available. Depending on whether a token is given, the
@@ -9,7 +9,7 @@ import { CheckOptions, ReleaseDescriptor, TagDescriptor } from '@github-version-
99
*
1010
* @param options The options for the version check.
1111
*/
12-
export default async function check(options: CheckOptions): Promise<ReleaseDescriptor|TagDescriptor|undefined> {
12+
const check: CheckFunction = async function(options: CheckOptions): Promise<CheckResult> {
1313
if (options === null || options === undefined) {
1414
throw new Error('options object must not be null or undefined!')
1515
}
@@ -32,12 +32,10 @@ export default async function check(options: CheckOptions): Promise<ReleaseDescr
3232
// decide what to do
3333
// when we have a token supplied, we will call the GraphQL API
3434
if (options.token) {
35-
if (options.excludePrereleases) {
36-
throw new Error('excludePrereleases option currently unsupported when specifying a token.')
37-
}
38-
3935
return await graphql(options)
4036
} else {
4137
return await rest(options)
4238
}
4339
}
40+
41+
export default check

packages/core/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { CallbackFunction, CheckOptions, CheckResult } from '@github-version-checker/api'
12
import check from './check'
2-
import { CallbackFunction, CheckOptions, ReleaseDescriptor, TagDescriptor } from '@github-version-checker/api'
33

44
/**
55
* The exported checking function.
@@ -11,7 +11,7 @@ import { CallbackFunction, CheckOptions, ReleaseDescriptor, TagDescriptor } from
1111
export default function versionCheck(
1212
options: CheckOptions,
1313
callback?: CallbackFunction
14-
): undefined|Promise<ReleaseDescriptor|TagDescriptor|undefined> {
14+
): undefined | Promise<CheckResult> {
1515
if (callback) {
1616
check(options)
1717
.then(update => callback(undefined, update))

packages/core/src/query/graphql.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Octokit } from '@octokit/core'
22
import type { GraphQlQueryResponseData } from '@octokit/graphql';
33
import { gt } from 'semver'
4-
import { CheckOptions, ReleaseDescriptor, TagDescriptor } from '@github-version-checker/api'
4+
import { CheckFunction, CheckOptions, CheckResult } from '@github-version-checker/api'
55
import { releases, tags } from '../util/graphql'
66

77
/**
@@ -12,7 +12,7 @@ import { releases, tags } from '../util/graphql'
1212
*
1313
* @param options The options for the version check.
1414
*/
15-
export default async function graphql(options: CheckOptions): Promise<ReleaseDescriptor|TagDescriptor|undefined> {
15+
const graphql: CheckFunction = async function(options: CheckOptions): Promise<CheckResult> {
1616
const theQuery = options.fetchTags ? tags : releases
1717

1818
const octokit = new Octokit({
@@ -31,35 +31,44 @@ export default async function graphql(options: CheckOptions): Promise<ReleaseDes
3131
return repository
3232
}
3333

34+
const result: CheckResult = {
35+
src: 'graphql',
36+
type: options.fetchTags ? 'tags' : 'releases',
37+
update: undefined
38+
}
39+
3440
let cursor: string|undefined = undefined
35-
const found = false
41+
let found = false
3642
while (! found) {
3743
const repository: any = await fetch(cursor)
3844
const entries: any[] = options.fetchTags ? repository.refs.nodes : repository.releases.nodes
3945

4046
if (entries.length == 0) {
41-
return undefined
47+
return result
4248
}
4349

44-
const skip = (! options.fetchTags && entries[0].isDraft) || (options.excludePrereleases && entries[0].isPrerelease)
45-
if (skip) {
50+
const skip = entries[0].isDraft || (options.excludePrereleases && entries[0].isPrerelease)
51+
if (! options.fetchTags && skip) {
4652
if (repository.releases.pageInfo.hasNextPage) {
4753
cursor = repository.releases.pageInfo.endCursor
4854
continue
4955
} else {
50-
return undefined
56+
return result
5157
}
5258
}
5359

5460
// Retrieve newer version name
5561
const newer = entries[0]
56-
const fetchedVersion = options.fetchTags ? newer.name : newer.tagName
62+
const fetchedVersion = options.fetchTags ? newer.name : newer.tag.name
5763
if (gt(fetchedVersion, options.currentVersion)) {
58-
return newer
64+
result.update = newer
65+
found = true
5966
} else {
60-
return undefined
67+
return result
6168
}
6269
}
6370

64-
return undefined
71+
return result
6572
}
73+
74+
export default graphql

packages/core/src/query/rest.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Octokit } from '@octokit/core'
2-
import { CheckOptions, ReleaseDescriptor, TagDescriptor } from '@github-version-checker/api'
2+
import { CheckFunction, CheckOptions, CheckResult, RestResponseTag } from '@github-version-checker/api'
33
import { compareReleases, compareTags } from '../util/comparator'
44
import { release, tag } from '../util/scheme-mapper'
5+
import semver from 'semver'
56

67
/**
78
* Executes an API call on the Github Rest API (v3) that should return the latest version.
@@ -11,7 +12,7 @@ import { release, tag } from '../util/scheme-mapper'
1112
*
1213
* @param options The options for the version check.
1314
*/
14-
export default async function rest(options: CheckOptions): Promise<ReleaseDescriptor|TagDescriptor|undefined> {
15+
const rest: CheckFunction = async function(options: CheckOptions): Promise<CheckResult> {
1516
// build url
1617
let apiUrl = '/repos/{owner}/{repo}'
1718
if (options.fetchTags) {
@@ -36,9 +37,15 @@ export default async function rest(options: CheckOptions): Promise<ReleaseDescri
3637
}
3738
})
3839

40+
const result: CheckResult = {
41+
src: 'rest',
42+
type: options.fetchTags ? 'tags' : 'releases',
43+
update: undefined
44+
}
45+
3946
// 404 error occurs, when no releases are found
4047
if (response.status === 404) {
41-
return undefined
48+
return result
4249
}
4350

4451
// status codes other than 200 are treated as an error
@@ -47,9 +54,13 @@ export default async function rest(options: CheckOptions): Promise<ReleaseDescri
4754
}
4855

4956
if (options.fetchTags) {
50-
return tag(compareTags(options, response.data))
57+
result.update = tag(compareTags(options, response.data))
5158
}
5259
else {
53-
return release(compareReleases(options, response.data))
60+
result.update = release(compareReleases(options, response.data))
5461
}
62+
63+
return result
5564
}
65+
66+
export default rest

packages/core/test/test-comparator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava'
2-
import { CheckOptions, RestResponseRelease, RestResponseTag } from '../src/types'
2+
import { CheckOptions, RestResponseRelease, RestResponseTag } from '@github-version-checker/api'
33
import { compareReleases, compareTags } from '../src/util/comparator'
44

55
const options: CheckOptions = {

packages/core/test/test-promise-handling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const tester = options => {
77

88
test(`one parameter and correct options returns without errors (${counter})`, t => {
99
t.notThrows(() => {
10-
versionCheck(options).then(update => {
10+
versionCheck(options).then(({update}) => {
1111
t.truthy(update)
1212
t.is(update.name, 'axelrindle/github-version-checker')
1313
})

packages/core/test/test-tags.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import test from 'ava'
22
import versionCheck from '../src/index'
33

44
test('fetching tags returns a more lightweight schema', async t => {
5-
const update = await versionCheck({
5+
const { type, update } = await versionCheck({
66
token: false,
77
owner: 'axelrindle',
88
repo: 'github-version-checker',
99
currentVersion: '0.0.1',
1010
fetchTags: true
1111
})
1212

13+
t.is(type, 'tags')
1314
t.truthy(update)
1415
t.is(Object.keys(update).length, 1)
1516
t.is(Object.keys(update)[0], 'name')

0 commit comments

Comments
 (0)