From 9f9a56afcb773a57003816c1a05ab8f3d8f7bd52 Mon Sep 17 00:00:00 2001 From: Derek Kershner Date: Sat, 22 May 2021 10:40:10 -0700 Subject: [PATCH] Update README, add test for parsing --- .prettierignore | 3 ++- README.md | 4 ++-- dist/index.js | 40 ++++++++++++++++++++++++++----------- package.json | 2 +- src/main.ts | 16 +++++---------- src/parseJsonSafely.test.ts | 11 ++++++++++ src/parseJsonSafely.ts | 10 ++++++++++ 7 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 src/parseJsonSafely.test.ts create mode 100644 src/parseJsonSafely.ts diff --git a/.prettierignore b/.prettierignore index 2186947..80cd62b 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,4 @@ dist/ lib/ -node_modules/ \ No newline at end of file +node_modules/ +parseJsonSafely.test.ts \ No newline at end of file diff --git a/README.md b/README.md index ce7535a..e8655ba 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,8 @@ Optional: JSON string of headers to pass into request. Default `"{}"`. uses: dkershner6/post-api-call-action@v1 with: url: ${{ secrets.API_URL }} - data: "{'command': 'publish'}" - headers: "{'Authorization': '${{ secrets.API_KEY }}'}" + data: "{\"command\": \"publish\"}" + headers: "{\"Authorization\": \"Bearer ${{ secrets.API_KEY }}\"}" ``` diff --git a/dist/index.js b/dist/index.js index 3c86988..1abcd9f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -21,33 +21,49 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", ({ value: true })); const core_1 = __nccwpck_require__(2186); const axios_1 = __importDefault(__nccwpck_require__(6545)); -const parseJsonSafely = (jsonString) => { - try { - return JSON.parse(jsonString); - } - catch (error) { - return {}; - } -}; +const parseJsonSafely_1 = __nccwpck_require__(8521); function run() { return __awaiter(this, void 0, void 0, function* () { try { const url = core_1.getInput('url'); core_1.info(`Sending POST request to ${url}`); - const data = parseJsonSafely(core_1.getInput('data')); - const headers = parseJsonSafely(core_1.getInput('headers')); + const data = parseJsonSafely_1.parseJsonSafely(core_1.getInput('data')); + const headers = parseJsonSafely_1.parseJsonSafely(core_1.getInput('headers')); yield axios_1.default.post(url, data, { headers, }); } - catch (error) { - core_1.setFailed(error.message); + catch (err) { + core_1.error(err.message); + core_1.setFailed(err.message); } }); } run(); +/***/ }), + +/***/ 8521: +/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { + +"use strict"; + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.parseJsonSafely = void 0; +const core_1 = __nccwpck_require__(2186); +const parseJsonSafely = (jsonString) => { + try { + return JSON.parse(jsonString); + } + catch (err) { + core_1.error(`Parsing error: ${jsonString} - ${err.message}`); + return {}; + } +}; +exports.parseJsonSafely = parseJsonSafely; + + /***/ }), /***/ 7351: diff --git a/package.json b/package.json index 9491345..7e01840 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "post-api-call-action", - "version": "1.0.2", + "version": "1.0.3", "private": true, "description": "Send a POST request action", "main": "lib/main.js", diff --git a/src/main.ts b/src/main.ts index a7eb4f9..d0ac969 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,6 @@ -import { getInput, info, setFailed } from '@actions/core'; +import { getInput, info, error, setFailed } from '@actions/core'; import axios from 'axios'; - -const parseJsonSafely = (jsonString: string): any => { - try { - return JSON.parse(jsonString); - } catch (error) { - return {}; - } -}; +import { parseJsonSafely } from './parseJsonSafely'; async function run(): Promise { try { @@ -18,8 +11,9 @@ async function run(): Promise { await axios.post(url, data, { headers, }); - } catch (error) { - setFailed(error.message); + } catch (err) { + error(err.message); + setFailed(err.message); } } diff --git a/src/parseJsonSafely.test.ts b/src/parseJsonSafely.test.ts new file mode 100644 index 0000000..14406f0 --- /dev/null +++ b/src/parseJsonSafely.test.ts @@ -0,0 +1,11 @@ +import { parseJsonSafely } from './parseJsonSafely'; + +it('Should parse example strings correctly', () => { + const testString = "{\"Authorization\": \"Bearer testsdtestdgsdsfgs\"}"; + + const result = parseJsonSafely(testString); + + expect(result).toEqual({ + Authorization: 'Bearer testsdtestdgsdsfgs', + }); +}); diff --git a/src/parseJsonSafely.ts b/src/parseJsonSafely.ts new file mode 100644 index 0000000..7c79d63 --- /dev/null +++ b/src/parseJsonSafely.ts @@ -0,0 +1,10 @@ +import { error } from '@actions/core'; + +export const parseJsonSafely = (jsonString: string): unknown => { + try { + return JSON.parse(jsonString); + } catch (err) { + error(`Parsing error: ${jsonString} - ${err.message}`); + return {}; + } +};