1
1
import { Env } from '../../worker-configuration' ;
2
2
import { Request , Response } from '@cloudflare/workers-types' ;
3
3
4
- export type Asset = { name : string ; browser_download_url : string } ;
4
+ export type Asset = {
5
+ name : string ;
6
+ browser_download_url : string ;
7
+ } ;
5
8
6
- export const getReleases = async (
9
+ type Release = {
10
+ tag_name : string ;
11
+ assets : Asset [ ] ;
12
+ body : string ;
13
+ published_at : string ;
14
+ } ;
15
+
16
+ /**
17
+ * Get the latest release from the GitHub API
18
+ * @param request The Worker Request object from the fetch event
19
+ * @param env The Worker environment object
20
+ * @returns Response object from the GitHub API
21
+ */
22
+ export async function getReleases (
7
23
request : Request ,
8
24
env : Env
9
- ) : Promise < Response > => {
10
- const reqUrl = new URL (
11
- `https://api.github.com/repos/${ env . GITHUB_ACCOUNT } /${ env . GITHUB_REPO } /releases/latest`
12
- ) ;
25
+ ) : Promise < Response > {
26
+ // build the request headers conditionally
13
27
const headers = new Headers ( {
14
28
Accept : 'application/vnd.github.preview' ,
15
29
'User-Agent' : request . headers . get ( 'User-Agent' ) as string
16
30
} ) ;
17
31
18
- if ( env . GITHUB_API_TOKEN ?. length )
32
+ if ( env . GITHUB_API_TOKEN ?. length ) {
19
33
headers . set ( 'Authorization' , `token ${ env . GITHUB_API_TOKEN } ` ) ;
34
+ }
20
35
21
- const response = await fetch ( reqUrl . toString ( ) , {
22
- method : 'GET' ,
23
- headers
24
- } ) ;
25
-
26
- return response ;
27
- } ;
28
-
29
- type Release = {
30
- tag_name : string ;
31
- assets : Asset [ ] ;
32
- body : string ;
33
- published_at : string ;
34
- } ;
36
+ // @ts -expect-error - Fetch does not have the webSocket property
37
+ return await fetch (
38
+ `https://api.github.com/repos/${ env . GITHUB_ACCOUNT } /${ env . GITHUB_REPO } /releases/latest` ,
39
+ {
40
+ method : 'GET' ,
41
+ headers
42
+ }
43
+ ) ;
44
+ }
35
45
36
- export const getLatestRelease = async (
46
+ /**
47
+ * Get the latest release from the GitHub API as a Release object
48
+ * @param request The Worker Request object from the fetch event
49
+ * @param env The Worker environment object
50
+ * @returns The latest release as a Release object
51
+ */
52
+ export async function getLatestRelease (
37
53
request : Request ,
38
54
env : Env
39
- ) : Promise < Release > => {
40
- const releases = await getReleases ( request , env ) ;
41
-
55
+ ) : Promise < Release > {
56
+ const releases : Response = await getReleases ( request , env ) ;
42
57
return ( await releases . json ( ) ) as Release ;
43
- } ;
58
+ }
44
59
60
+ /**
61
+ * Find the signature file for a given asset
62
+ * @param fileName The name of the file to find the signature for
63
+ * @param assets The assets to search for the signature
64
+ * @returns The signature as a string or undefined if not found
65
+ */
45
66
export async function findAssetSignature (
46
67
fileName : string ,
47
68
assets : Asset [ ]
@@ -61,6 +82,6 @@ export async function findAssetSignature(
61
82
if ( response . status !== 200 ) {
62
83
return undefined ;
63
84
}
64
- const signature = await response . text ( ) ;
65
- return signature ;
85
+
86
+ return await response . text ( ) ;
66
87
}
0 commit comments