-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfetch.ts
More file actions
113 lines (101 loc) · 2.78 KB
/
fetch.ts
File metadata and controls
113 lines (101 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import {
info,
exportVariable,
setOutput as setEnvironmentOutput,
debug
} from '@actions/core'
import {mkdirP} from '@actions/io'
import {promises as fs} from 'fs'
import Mustache from 'mustache'
import retryRequest from 'async-retry'
import type {DataInterface, ExportInterface} from './constants.js'
import {Status} from './constants.js'
import {parseData} from './util.js'
/**
* Retrieves data from an API endpoint.
*/
export async function retrieveData({
debug: requestDebug,
endpoint,
configuration,
auth,
isTokenRequest,
retry
}: DataInterface): Promise<string> {
try {
info(
isTokenRequest
? 'Fetching credentials from the token endpoint… 🎟️'
: 'Fetching the requested data… 📦'
)
const settings = configuration
? JSON.parse(
Mustache.render(configuration, auth ? parseData(auth) : null)
)
: {}
if (settings.body) {
// Ensures the body is stringified in the case of a post request being made.
settings.body = JSON.stringify(settings.body)
}
return await retryRequest(
async () => {
// If anything throws the request is retried.
const response = await fetch(endpoint, settings)
const data = await response.text()
if (!response.ok) {
throw new Error(data)
}
if (requestDebug) {
info('📡 Request Response Debug: ')
info(JSON.stringify(data))
}
return data
},
{
retries: retry ? 3 : 0,
onRetry: (error: Error) => {
debug(error.message)
info(`There was an error with the request, retrying… ⏳`)
}
}
)
} catch (error) {
throw new Error(`There was an error fetching from the API: ${error} ❌`)
}
}
/**
* Generates an export file from the data provided.
*/
export async function generateExport({
data,
encoding,
format,
saveLocation,
saveName,
setOutput,
variableName
}: ExportInterface): Promise<Status> {
info('Saving the data... 📁')
const file = `${saveLocation ? saveLocation : 'fetch-api-data-action'}/${
saveName ? saveName : 'data'
}.${format ? format : 'json'}`
const dataEncoding = encoding ? encoding : 'utf8'
const defaultVariableName = 'fetchApiData'
const environmentVariableName = variableName
? variableName
: defaultVariableName
try {
await mkdirP(`${saveLocation ? saveLocation : 'fetch-api-data-action'}`)
await fs.writeFile(file, data, dataEncoding)
info(`Saved ${file} 💾`)
if (setOutput) {
exportVariable(environmentVariableName, data)
setEnvironmentOutput(defaultVariableName, data)
}
return Status.SUCCESS
} catch (error) {
throw new Error(
`There was an error generating the export file: ${error} ❌`
)
}
}