Update README, add test for parsing

This commit is contained in:
Derek Kershner 2021-05-22 10:40:10 -07:00
parent a11a644105
commit 9f9a56afcb
7 changed files with 59 additions and 27 deletions

View File

@ -1,3 +1,4 @@
dist/ dist/
lib/ lib/
node_modules/ node_modules/
parseJsonSafely.test.ts

View File

@ -23,8 +23,8 @@ Optional: JSON string of headers to pass into request. Default `"{}"`.
uses: dkershner6/post-api-call-action@v1 uses: dkershner6/post-api-call-action@v1
with: with:
url: ${{ secrets.API_URL }} url: ${{ secrets.API_URL }}
data: "{'command': 'publish'}" data: "{\"command\": \"publish\"}"
headers: "{'Authorization': '${{ secrets.API_KEY }}'}" headers: "{\"Authorization\": \"Bearer ${{ secrets.API_KEY }}\"}"
``` ```

40
dist/index.js vendored
View File

@ -21,33 +21,49 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
const core_1 = __nccwpck_require__(2186); const core_1 = __nccwpck_require__(2186);
const axios_1 = __importDefault(__nccwpck_require__(6545)); const axios_1 = __importDefault(__nccwpck_require__(6545));
const parseJsonSafely = (jsonString) => { const parseJsonSafely_1 = __nccwpck_require__(8521);
try {
return JSON.parse(jsonString);
}
catch (error) {
return {};
}
};
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const url = core_1.getInput('url'); const url = core_1.getInput('url');
core_1.info(`Sending POST request to ${url}`); core_1.info(`Sending POST request to ${url}`);
const data = parseJsonSafely(core_1.getInput('data')); const data = parseJsonSafely_1.parseJsonSafely(core_1.getInput('data'));
const headers = parseJsonSafely(core_1.getInput('headers')); const headers = parseJsonSafely_1.parseJsonSafely(core_1.getInput('headers'));
yield axios_1.default.post(url, data, { yield axios_1.default.post(url, data, {
headers, headers,
}); });
} }
catch (error) { catch (err) {
core_1.setFailed(error.message); core_1.error(err.message);
core_1.setFailed(err.message);
} }
}); });
} }
run(); 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: /***/ 7351:

View File

@ -1,6 +1,6 @@
{ {
"name": "post-api-call-action", "name": "post-api-call-action",
"version": "1.0.2", "version": "1.0.3",
"private": true, "private": true,
"description": "Send a POST request action", "description": "Send a POST request action",
"main": "lib/main.js", "main": "lib/main.js",

View File

@ -1,13 +1,6 @@
import { getInput, info, setFailed } from '@actions/core'; import { getInput, info, error, setFailed } from '@actions/core';
import axios from 'axios'; import axios from 'axios';
import { parseJsonSafely } from './parseJsonSafely';
const parseJsonSafely = (jsonString: string): any => {
try {
return JSON.parse(jsonString);
} catch (error) {
return {};
}
};
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
@ -18,8 +11,9 @@ async function run(): Promise<void> {
await axios.post(url, data, { await axios.post(url, data, {
headers, headers,
}); });
} catch (error) { } catch (err) {
setFailed(error.message); error(err.message);
setFailed(err.message);
} }
} }

View File

@ -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',
});
});

10
src/parseJsonSafely.ts Normal file
View File

@ -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 {};
}
};