Skip to content

Commit 1b01189

Browse files
Add encoding option
1 parent 1ff2fad commit 1b01189

7 files changed

Lines changed: 28 additions & 11 deletions

File tree

.github/workflows/version.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ name: Update Major Version Tag
33
on:
44
push:
55
tags:
6-
- "v*"
6+
- 'v*'
77

88
jobs:
99
update-majorver:
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

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,10 @@ The following configuration options should be set.
183183
| `token-endpoint` | If the `endpoint` API requires you to make a request to get an access token prior to fetching data you can perform this task by specifying a token endpoint. Any data returned from the token end can be referenced in the `configuration` variable using the triple bracket syntax: `{{{ access_token }}}`. For more information refer to the [Token Request part of the readme](https://github.com/JamesIves/fetch-api-data-action#token-request-%EF%B8%8F); | `with` | **No** |
184184
| `token-configuration` | Any applicable configuration settings that should be set such as authentication tokens. You can reference secrets using the `${{ secrets.secret_name }}` syntax. For more information refer to the [Fetch API documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch). | `secrets / with` | **No** |
185185
| `retry` | If you're working with an intermittent API you can toggle this option to `true`. Doing so will make the action try the request 3 times at random invervals before failing. | `with` | **No** |
186-
| `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** |
186+
| `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** |
189190
| `debug` | If set to `true` the action will log the API responses it receives in the terminal. | `with` | **No** |
190191

191192
---

__tests__/fetch.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,15 @@ describe('fetch', () => {
133133
})
134134
expect(process.env['fetch-api-data']).toBe('{"bestCat":"montezuma"}')
135135
})
136+
137+
it('should save file with custom encoding', async () => {
138+
await generateExport({
139+
data: '68656C6C6F21',
140+
encoding: 'hex',
141+
format: 'txt',
142+
saveName: 'hex-data'
143+
})
144+
expect(process.env['fetch-api-data']).toBe('68656C6C6F21')
145+
})
136146
})
137147
})

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ inputs:
4242

4343
format:
4444
description: 'Allows you to modify the format of the saved file, for example you can use txt here to save the file as a txt file. This field defaults to json.'
45+
required: false
46+
47+
encoding:
48+
description: 'Allows you to specify the encoding the saved file, can be of type BufferEncoding: "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex".'
49+
required: false
4550

4651
outputs:
4752
fetch-api-data:

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {isNullOrUndefined} from './util'
44
export interface ActionInterface {
55
/** Allows you to log the retrieved data to the terminal. */
66
debug?: boolean
7+
/** The encoding of the data to be finally stored */
8+
encoding?: BufferEncoding
79
/** The primary endpoint to fetch data from. */
810
endpoint: string
911
/** The configuration for the primary endpoint. Must be a stringified JSON object. */
@@ -40,6 +42,8 @@ export interface DataInterface {
4042
export interface ExportInterface {
4143
/** The data to save. */
4244
data: string
45+
/** The encoding of the data to be finally stored */
46+
encoding?: BufferEncoding
4347
/** The save location. */
4448
saveLocation?: string
4549
/** The name of the file to save. */
@@ -53,6 +57,7 @@ export const action = {
5357
debug: !isNullOrUndefined(getInput('debug'))
5458
? getInput('debug').toLowerCase() === 'true'
5559
: false,
60+
encoding: <BufferEncoding>getInput('encoding'),
5661
endpoint: getInput('endpoint'),
5762
configuration: getInput('configuration'),
5863
tokenEndpoint: getInput('token-endpoint'),

src/fetch.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export async function retrieveData({
6565
/* Saves the data to the local file system and exports an environment variable containing the retrieved data. */
6666
export async function generateExport({
6767
data,
68+
encoding,
6869
format,
6970
saveLocation,
7071
saveName
@@ -73,17 +74,11 @@ export async function generateExport({
7374
const file = `${saveLocation ? saveLocation : 'fetch-api-data-action'}/${
7475
saveName ? saveName : 'data'
7576
}.${format ? format : 'json'}`
77+
const dataEncoding = encoding ? encoding : 'utf8'
7678
await mkdirP(`${saveLocation ? saveLocation : 'fetch-api-data-action'}`)
77-
await fs.writeFile(file, data, 'utf8')
79+
await fs.writeFile(file, data, dataEncoding)
7880

7981
info(`Saved ${file} 💾`)
80-
await fs.writeFile(
81-
`${saveLocation ? saveLocation : 'fetch-api-data-action'}/${
82-
saveName ? saveName : 'data'
83-
}.json`,
84-
data,
85-
'utf8'
86-
)
8782

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

src/lib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default async function run(
5252

5353
status = await generateExport({
5454
data,
55+
encoding: settings.encoding,
5556
saveLocation: settings.saveLocation,
5657
saveName: settings.saveName,
5758
format: settings.format

0 commit comments

Comments
 (0)