mirror of
https://github.com/dkershner6/post-api-call-action.git
synced 2024-12-27 19:33:55 +08:00
Update README, add test for parsing
This commit is contained in:
parent
a11a644105
commit
9f9a56afcb
@ -1,3 +1,4 @@
|
||||
dist/
|
||||
lib/
|
||||
node_modules/
|
||||
parseJsonSafely.test.ts
|
@ -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 }}\"}"
|
||||
```
|
||||
|
||||
|
||||
|
40
dist/index.js
vendored
40
dist/index.js
vendored
@ -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:
|
||||
|
@ -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",
|
||||
|
16
src/main.ts
16
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<void> {
|
||||
try {
|
||||
@ -18,8 +11,9 @@ async function run(): Promise<void> {
|
||||
await axios.post(url, data, {
|
||||
headers,
|
||||
});
|
||||
} catch (error) {
|
||||
setFailed(error.message);
|
||||
} catch (err) {
|
||||
error(err.message);
|
||||
setFailed(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
11
src/parseJsonSafely.test.ts
Normal file
11
src/parseJsonSafely.test.ts
Normal 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
10
src/parseJsonSafely.ts
Normal 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 {};
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user