Skip to content

Commit

Permalink
Adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesIves committed Jan 12, 2022
1 parent 5e9582c commit efa1597
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
28 changes: 27 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
persist-credentials: false

- name: Fetch Data 📦
uses: JamesIves/fetch-api-data-action@releases/v2
uses: JamesIves/fetch-api-data-action@v2
with:
endpoint: https://jsonplaceholder.typicode.com/todos/1
save-location: fetch-api-data-custom
Expand All @@ -37,3 +37,29 @@ jobs:
folder: fetch-api-data-custom
target-folder: data
ssh-key: ${{ secrets.DEPLOY_KEY }}

refresh-api-data-modified-formatting-encoding:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/[email protected]
with:
persist-credentials: false

- name: Fetch Data 📦
uses: JamesIves/fetch-api-data-action@v2
with:
endpoint: https://jsonplaceholder.typicode.com/todos/1
save-location: fetch-api-data-custom-encoding
save-name: todo2
encoding: hex
format: txt
retry: true

- name: Build and Deploy Repo 🚀
uses: JamesIves/[email protected]
with:
branch: gh-pages
folder: fetch-api-data-custom-encoding
target-folder: encoding
ssh-key: ${{ secrets.DEPLOY_KEY }}
2 changes: 1 addition & 1 deletion .github/workflows/version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
name: Update Major Version Tag
runs-on: ubuntu-latest
steps:
- uses: nowactions/update-majorver@v1
- uses: nowactions/update-majorver@v1.1.0
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ The following configuration options should be set.
| `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** |
| `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** |
| `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** |
| `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** |
| `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** |
| `debug` | If set to `true` the action will log the API responses it receives in the terminal. | `with` | **No** |

---
Expand Down
21 changes: 18 additions & 3 deletions __tests__/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('fetch', () => {
})
} catch (error) {
expect(error instanceof Error && error.message).toBe(
"There was an error fetching from the API: TypeError: Cannot read property 'cat' of null"
"There was an error fetching from the API: TypeError: Cannot read property 'cat' of null"
)
}
})
Expand All @@ -77,7 +77,7 @@ describe('fetch', () => {
})
} catch (error) {
expect(error instanceof Error && error.message).toBe(
'There was an error fetching from the API: Error: {"a":1}'
'There was an error fetching from the API: Error: {"a":1}'
)
}
})
Expand All @@ -101,7 +101,7 @@ describe('fetch', () => {
})
} catch (error) {
expect(error instanceof Error && error.message).toBe(
'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'
'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'
)
}
})
Expand Down Expand Up @@ -143,5 +143,20 @@ describe('fetch', () => {
})
expect(process.env['fetch-api-data']).toBe('68656C6C6F21')
})

it('should fail if invalid encoding is used', async () => {
try {
await generateExport({
data: '68656C6C6F21',
encoding: 'hexxxxx' as BufferEncoding,
format: 'txt',
saveName: 'hex-data'
})
} catch (error) {
expect(error instanceof Error && error.message).toBe(
`There was an error generating the export file: TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "hexxxxx" is invalid for option "encoding" ❌`
)
}
})
})
})
19 changes: 13 additions & 6 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function retrieveData({
}
)
} catch (error) {
throw new Error(`There was an error fetching from the API: ${error}`)
throw new Error(`There was an error fetching from the API: ${error}`)
}
}

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

info(`Saved ${file} 💾`)
try {
await mkdirP(`${saveLocation ? saveLocation : 'fetch-api-data-action'}`)
await fs.writeFile(file, data, dataEncoding)

info(`Saved ${file} 💾`)

exportVariable('fetch-api-data', data)
exportVariable('fetch-api-data', data)

return Status.SUCCESS
return Status.SUCCESS
} catch (error) {
throw new Error(
`There was an error generating the export file: ${error} ❌`
)
}
}

0 comments on commit efa1597

Please sign in to comment.