Skip to content

Commit

Permalink
Merge branch 'dev' into releases/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesIves committed Jan 12, 2022
2 parents 0f1ff08 + efa1597 commit b91d0cc
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 79 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/sponsors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: actions/[email protected]

- name: Generate Sponsors 💖
uses: JamesIves/github-sponsors-readme-action@1.0.7
uses: JamesIves/github-sponsors-readme-action@v1.0.8
with:
token: ${{ secrets.PAT }}
file: 'README.md'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: Update Major Version Tag
on:
push:
tags:
- "v*"
- 'v*'

jobs:
update-majorver:
name: Update Major Version Tag
runs-on: ubuntu-latest
steps:
- uses: nowactions/update-majorver@v1
- uses: nowactions/update-majorver@v1.1.0
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ import run, {
retrieveData,
generateExport,
ActionInterface
} from 'fetch-api-data-action'
} from '@jamesives/fetch-api-data-action'
```

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

```javascript
import run from 'fetch-api-data-action'
import run from '@jamesives/fetch-api-data-action'

run({
endpoint: 'https://example.com',
Expand Down Expand Up @@ -183,9 +183,10 @@ The following configuration options should be set.
| `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** |
| `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** |
| `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** |
| `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-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 `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
31 changes: 28 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 @@ -133,5 +133,30 @@ describe('fetch', () => {
})
expect(process.env['fetch-api-data']).toBe('{"bestCat":"montezuma"}')
})

it('should save file with custom encoding', async () => {
await generateExport({
data: '68656C6C6F21',
encoding: 'hex',
format: 'txt',
saveName: 'hex-data'
})
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" ❌`
)
}
})
})
})
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ inputs:

format:
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.'
required: false

encoding:
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".'
required: false

outputs:
fetch-api-data:
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@jamesives/fetch-api-data-action",
"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.",
"author": "James Ives <[email protected]> (https://jamesiv.es)",
"version": "2.0.1",
"version": "2.1.0",
"license": "MIT",
"main": "lib/lib.js",
"types": "lib/lib.d.ts",
Expand Down Expand Up @@ -44,15 +44,15 @@
"@types/jest": "27.4.0",
"@types/mustache": "4.1.2",
"@types/node": "17.0.8",
"@typescript-eslint/eslint-plugin": "5.9.0",
"@typescript-eslint/parser": "5.9.0",
"@typescript-eslint/eslint-plugin": "5.9.1",
"@typescript-eslint/parser": "5.9.1",
"eslint": "8.6.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-jest": "25.3.4",
"eslint-plugin-prettier": "4.0.0",
"jest": "27.4.7",
"jest-circus": "27.4.6",
"nock": "13.2.1",
"nock": "13.2.2",
"prettier": "2.5.1",
"ts-jest": "27.1.2",
"typescript": "4.5.4"
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {isNullOrUndefined} from './util'
export interface ActionInterface {
/** Allows you to log the retrieved data to the terminal. */
debug?: boolean
/** The encoding of the data to be finally stored */
encoding?: BufferEncoding
/** The primary endpoint to fetch data from. */
endpoint: string
/** The configuration for the primary endpoint. Must be a stringified JSON object. */
Expand Down Expand Up @@ -40,6 +42,8 @@ export interface DataInterface {
export interface ExportInterface {
/** The data to save. */
data: string
/** The encoding of the data to be finally stored */
encoding?: BufferEncoding
/** The save location. */
saveLocation?: string
/** The name of the file to save. */
Expand All @@ -53,6 +57,7 @@ export const action = {
debug: !isNullOrUndefined(getInput('debug'))
? getInput('debug').toLowerCase() === 'true'
: false,
encoding: <BufferEncoding>getInput('encoding'),
endpoint: getInput('endpoint'),
configuration: getInput('configuration'),
tokenEndpoint: getInput('token-endpoint'),
Expand Down
28 changes: 15 additions & 13 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ 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}`)
}
}

/* Saves the data to the local file system and exports an environment variable containing the retrieved data. */
export async function generateExport({
data,
encoding,
format,
saveLocation,
saveName
Expand All @@ -73,19 +74,20 @@ export async function generateExport({
const file = `${saveLocation ? saveLocation : 'fetch-api-data-action'}/${
saveName ? saveName : 'data'
}.${format ? format : 'json'}`
await mkdirP(`${saveLocation ? saveLocation : 'fetch-api-data-action'}`)
await fs.writeFile(file, data, 'utf8')
const dataEncoding = encoding ? encoding : 'utf8'

info(`Saved ${file} 💾`)
await fs.writeFile(
`${saveLocation ? saveLocation : 'fetch-api-data-action'}/${
saveName ? saveName : 'data'
}.json`,
data,
'utf8'
)
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} ❌`
)
}
}
1 change: 1 addition & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default async function run(

status = await generateExport({
data,
encoding: settings.encoding,
saveLocation: settings.saveLocation,
saveName: settings.saveName,
format: settings.format
Expand Down
Loading

0 comments on commit b91d0cc

Please sign in to comment.