Skip to content

Commit efa1597

Browse files
committed
Adjustments
1 parent 5e9582c commit efa1597

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

.github/workflows/integration.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
persist-credentials: false
2424

2525
- name: Fetch Data 📦
26-
uses: JamesIves/fetch-api-data-action@releases/v2
26+
uses: JamesIves/fetch-api-data-action@v2
2727
with:
2828
endpoint: https://jsonplaceholder.typicode.com/todos/1
2929
save-location: fetch-api-data-custom
@@ -37,3 +37,29 @@ jobs:
3737
folder: fetch-api-data-custom
3838
target-folder: data
3939
ssh-key: ${{ secrets.DEPLOY_KEY }}
40+
41+
refresh-api-data-modified-formatting-encoding:
42+
runs-on: ubuntu-latest
43+
steps:
44+
- name: Checkout 🛎️
45+
uses: actions/checkout@v2.4.0
46+
with:
47+
persist-credentials: false
48+
49+
- name: Fetch Data 📦
50+
uses: JamesIves/fetch-api-data-action@v2
51+
with:
52+
endpoint: https://jsonplaceholder.typicode.com/todos/1
53+
save-location: fetch-api-data-custom-encoding
54+
save-name: todo2
55+
encoding: hex
56+
format: txt
57+
retry: true
58+
59+
- name: Build and Deploy Repo 🚀
60+
uses: JamesIves/github-pages-deploy-action@v4.2.2
61+
with:
62+
branch: gh-pages
63+
folder: fetch-api-data-custom-encoding
64+
target-folder: encoding
65+
ssh-key: ${{ secrets.DEPLOY_KEY }}

.github/workflows/version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ jobs:
1010
name: Update Major Version Tag
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: nowactions/update-majorver@v1
13+
- uses: nowactions/update-majorver@v1.1.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ The following configuration options should be set.
186186
| `save-location` | By default the save location of the file is `fetch-api-data-action/data.json`, if you'd like to override the directory you can do so by specifying a new one with this variable. | `with` | **No** |
187187
| `save-name` | You can override the name of the exported `.json` file by specifying a new one here. You should _not_ include the file extension in your name. | `with` | **No** |
188188
| `format` | Allows you to modify the extension of the file saved from the API response, for example you can set this field to `json` or `txt`. This field defaults to `json`. | `with` | **No** |
189-
| `encoding` | Allows you to modify the encoding of the file saved from the API response, for example you can set this field to `utf8` or `hex`. This field defaults to `hex`. Choose from `ascii` or `utf8` or `utf-8` or `utf16le` or `ucs2` or `ucs-2` or `base64` or `latin1` or `binary` or `hex` | `with` | **No** |
189+
| `encoding` | Allows you to modify the encoding of the file saved from the API response, for example you can set this field to `utf8` or `hex`. This field defaults to `utf8`. Choose from `ascii`, `utf8`, `utf-8`, `utf16le`, `ucs2`, `ucs-2`, `base64`, `latin1`, `binary` or `hex`. | `with` | **No** |
190190
| `debug` | If set to `true` the action will log the API responses it receives in the terminal. | `with` | **No** |
191191

192192
---

__tests__/fetch.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('fetch', () => {
5454
})
5555
} catch (error) {
5656
expect(error instanceof Error && error.message).toBe(
57-
"There was an error fetching from the API: TypeError: Cannot read property 'cat' of null"
57+
"There was an error fetching from the API: TypeError: Cannot read property 'cat' of null"
5858
)
5959
}
6060
})
@@ -77,7 +77,7 @@ describe('fetch', () => {
7777
})
7878
} catch (error) {
7979
expect(error instanceof Error && error.message).toBe(
80-
'There was an error fetching from the API: Error: {"a":1}'
80+
'There was an error fetching from the API: Error: {"a":1}'
8181
)
8282
}
8383
})
@@ -101,7 +101,7 @@ describe('fetch', () => {
101101
})
102102
} catch (error) {
103103
expect(error instanceof Error && error.message).toBe(
104-
'There was an error fetching from the API: FetchError: invalid json response body at https://jives.dev/ reason: Unexpected token < in JSON at position 0'
104+
'There was an error fetching from the API: FetchError: invalid json response body at https://jives.dev/ reason: Unexpected token < in JSON at position 0'
105105
)
106106
}
107107
})
@@ -143,5 +143,20 @@ describe('fetch', () => {
143143
})
144144
expect(process.env['fetch-api-data']).toBe('68656C6C6F21')
145145
})
146+
147+
it('should fail if invalid encoding is used', async () => {
148+
try {
149+
await generateExport({
150+
data: '68656C6C6F21',
151+
encoding: 'hexxxxx' as BufferEncoding,
152+
format: 'txt',
153+
saveName: 'hex-data'
154+
})
155+
} catch (error) {
156+
expect(error instanceof Error && error.message).toBe(
157+
`There was an error generating the export file: TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "hexxxxx" is invalid for option "encoding" ❌`
158+
)
159+
}
160+
})
146161
})
147162
})

src/fetch.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function retrieveData({
5858
}
5959
)
6060
} catch (error) {
61-
throw new Error(`There was an error fetching from the API: ${error}`)
61+
throw new Error(`There was an error fetching from the API: ${error}`)
6262
}
6363
}
6464

@@ -75,12 +75,19 @@ export async function generateExport({
7575
saveName ? saveName : 'data'
7676
}.${format ? format : 'json'}`
7777
const dataEncoding = encoding ? encoding : 'utf8'
78-
await mkdirP(`${saveLocation ? saveLocation : 'fetch-api-data-action'}`)
79-
await fs.writeFile(file, data, dataEncoding)
8078

81-
info(`Saved ${file} 💾`)
79+
try {
80+
await mkdirP(`${saveLocation ? saveLocation : 'fetch-api-data-action'}`)
81+
await fs.writeFile(file, data, dataEncoding)
82+
83+
info(`Saved ${file} 💾`)
8284

83-
exportVariable('fetch-api-data', data)
85+
exportVariable('fetch-api-data', data)
8486

85-
return Status.SUCCESS
87+
return Status.SUCCESS
88+
} catch (error) {
89+
throw new Error(
90+
`There was an error generating the export file: ${error} ❌`
91+
)
92+
}
8693
}

0 commit comments

Comments
 (0)