Skip to content

Commit b91d0cc

Browse files
committed
Merge branch 'dev' into releases/v2
2 parents 0f1ff08 + efa1597 commit b91d0cc

File tree

11 files changed

+144
-79
lines changed

11 files changed

+144
-79
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/[email protected]
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/[email protected]
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/sponsors.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
uses: actions/[email protected]
1313

1414
- name: Generate Sponsors 💖
15-
uses: JamesIves/github-sponsors-readme-action@1.0.7
15+
uses: JamesIves/github-sponsors-readme-action@v1.0.8
1616
with:
1717
token: ${{ secrets.PAT }}
1818
file: 'README.md'

.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.1.0

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ import run, {
146146
retrieveData,
147147
generateExport,
148148
ActionInterface
149-
} from 'fetch-api-data-action'
149+
} from '@jamesives/fetch-api-data-action'
150150
```
151151

152152
Calling the functions directly will require you to pass in an object containing the variables found in the [configuration section](https://github.com/JamesIves/fetch-api-data-action#configuration-).
153153

154154
```javascript
155-
import run from 'fetch-api-data-action'
155+
import run from '@jamesives/fetch-api-data-action'
156156

157157
run({
158158
endpoint: 'https://example.com',
@@ -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 `utf8`. Choose from `ascii`, `utf8`, `utf-8`, `utf16le`, `ucs2`, `ucs-2`, `base64`, `latin1`, `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: 28 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
})
@@ -133,5 +133,30 @@ 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+
})
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+
})
136161
})
137162
})

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:

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@jamesives/fetch-api-data-action",
33
"description": "GitHub action for handling authenticated API requests, allowing you to save the data from the request into your workspace as an environment variable and a .json file.",
44
"author": "James Ives <[email protected]> (https://jamesiv.es)",
5-
"version": "2.0.1",
5+
"version": "2.1.0",
66
"license": "MIT",
77
"main": "lib/lib.js",
88
"types": "lib/lib.d.ts",
@@ -44,15 +44,15 @@
4444
"@types/jest": "27.4.0",
4545
"@types/mustache": "4.1.2",
4646
"@types/node": "17.0.8",
47-
"@typescript-eslint/eslint-plugin": "5.9.0",
48-
"@typescript-eslint/parser": "5.9.0",
47+
"@typescript-eslint/eslint-plugin": "5.9.1",
48+
"@typescript-eslint/parser": "5.9.1",
4949
"eslint": "8.6.0",
5050
"eslint-config-prettier": "8.3.0",
5151
"eslint-plugin-jest": "25.3.4",
5252
"eslint-plugin-prettier": "4.0.0",
5353
"jest": "27.4.7",
5454
"jest-circus": "27.4.6",
55-
"nock": "13.2.1",
55+
"nock": "13.2.2",
5656
"prettier": "2.5.1",
5757
"ts-jest": "27.1.2",
5858
"typescript": "4.5.4"

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: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ 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

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,19 +74,20 @@ export async function generateExport({
7374
const file = `${saveLocation ? saveLocation : 'fetch-api-data-action'}/${
7475
saveName ? saveName : 'data'
7576
}.${format ? format : 'json'}`
76-
await mkdirP(`${saveLocation ? saveLocation : 'fetch-api-data-action'}`)
77-
await fs.writeFile(file, data, 'utf8')
77+
const dataEncoding = encoding ? encoding : 'utf8'
7878

79-
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-
)
79+
try {
80+
await mkdirP(`${saveLocation ? saveLocation : 'fetch-api-data-action'}`)
81+
await fs.writeFile(file, data, dataEncoding)
82+
83+
info(`Saved ${file} 💾`)
8784

88-
exportVariable('fetch-api-data', data)
85+
exportVariable('fetch-api-data', data)
8986

90-
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+
}
9193
}

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)