Added params as an optional parameter (query parameters)

This commit is contained in:
Derek Kershner 2022-08-06 07:38:37 -07:00
parent 9f9a56afcb
commit ce01c19ac6
8 changed files with 8342 additions and 29 deletions

View File

@ -1,6 +1,6 @@
# webhook-action # webhook-action
GitHub Action that sends a POST request of any kind. GitHub Action that sends a POST request of any kind. This is a thin wrapper around Axios.post.
## Inputs ## Inputs
@ -16,6 +16,10 @@ Optional: JSON string of data to pass into request. Default `"{}"`.
Optional: JSON string of headers to pass into request. Default `"{}"`. Optional: JSON string of headers to pass into request. Default `"{}"`.
### `params`
Optional: JSON string of query parameters to pass into request. Default `"{}"`.
## Example usage ## Example usage
@ -25,6 +29,7 @@ with:
url: ${{ secrets.API_URL }} url: ${{ secrets.API_URL }}
data: "{\"command\": \"publish\"}" data: "{\"command\": \"publish\"}"
headers: "{\"Authorization\": \"Bearer ${{ secrets.API_KEY }}\"}" headers: "{\"Authorization\": \"Bearer ${{ secrets.API_KEY }}\"}"
params: "{\"content\": \"Test Content\"}"
``` ```

View File

@ -16,6 +16,10 @@ inputs:
description: 'JSON string of headers to pass into request' description: 'JSON string of headers to pass into request'
default: '{}' default: '{}'
required: false required: false
params:
description: 'JSON string of query parameters to pass into request'
default: '{}'
required: false
runs: runs:
using: 'node12' using: 'node16'
main: 'dist/index.js' main: 'dist/index.js'

17
dist/index.js vendored
View File

@ -26,16 +26,22 @@ 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}`);
const data = parseJsonSafely_1.parseJsonSafely(core_1.getInput('data')); const data = parseJsonSafely_1.parseJsonSafely(core_1.getInput('data'));
const headers = parseJsonSafely_1.parseJsonSafely(core_1.getInput('headers')); const headers = parseJsonSafely_1.parseJsonSafely(core_1.getInput('headers'));
const params = parseJsonSafely_1.parseJsonSafely(core_1.getInput('params'));
core_1.info(`Sending POST request to ${url}`);
yield axios_1.default.post(url, data, { yield axios_1.default.post(url, data, {
headers, headers,
params,
}); });
} }
catch (err) { catch (err) {
core_1.error(err.message); if (err instanceof Error) {
core_1.setFailed(err.message); core_1.error(err.message);
core_1.setFailed(err.message);
return;
}
core_1.setFailed('An unknown error occured');
} }
}); });
} }
@ -53,11 +59,12 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseJsonSafely = void 0; exports.parseJsonSafely = void 0;
const core_1 = __nccwpck_require__(2186); const core_1 = __nccwpck_require__(2186);
const parseJsonSafely = (jsonString) => { const parseJsonSafely = (jsonString) => {
var _a, _b;
try { try {
return JSON.parse(jsonString); return JSON.parse(jsonString);
} }
catch (err) { catch (err) {
core_1.error(`Parsing error: ${jsonString} - ${err.message}`); core_1.error(`Parsing error: ${jsonString} - ${(_b = (_a = err) === null || _a === void 0 ? void 0 : _a.message) !== null && _b !== void 0 ? _b : 'Unknown error while parsing JSON'}`);
return {}; return {};
} }
}; };
@ -4311,7 +4318,7 @@ module.exports = {
/***/ ((module) => { /***/ ((module) => {
"use strict"; "use strict";
module.exports = JSON.parse('{"_from":"axios","_id":"axios@0.21.1","_inBundle":false,"_integrity":"sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==","_location":"/axios","_phantomChildren":{},"_requested":{"type":"tag","registry":true,"raw":"axios","name":"axios","escapedName":"axios","rawSpec":"","saveSpec":null,"fetchSpec":"latest"},"_requiredBy":["#USER","/"],"_resolved":"https://registry.npmjs.org/axios/-/axios-0.21.1.tgz","_shasum":"22563481962f4d6bde9a76d516ef0e5d3c09b2b8","_spec":"axios","_where":"/home/dkershner/repos/post-api-call-action","author":{"name":"Matt Zabriskie"},"browser":{"./lib/adapters/http.js":"./lib/adapters/xhr.js"},"bugs":{"url":"https://github.com/axios/axios/issues"},"bundleDependencies":false,"bundlesize":[{"path":"./dist/axios.min.js","threshold":"5kB"}],"dependencies":{"follow-redirects":"^1.10.0"},"deprecated":false,"description":"Promise based HTTP client for the browser and node.js","devDependencies":{"bundlesize":"^0.17.0","coveralls":"^3.0.0","es6-promise":"^4.2.4","grunt":"^1.0.2","grunt-banner":"^0.6.0","grunt-cli":"^1.2.0","grunt-contrib-clean":"^1.1.0","grunt-contrib-watch":"^1.0.0","grunt-eslint":"^20.1.0","grunt-karma":"^2.0.0","grunt-mocha-test":"^0.13.3","grunt-ts":"^6.0.0-beta.19","grunt-webpack":"^1.0.18","istanbul-instrumenter-loader":"^1.0.0","jasmine-core":"^2.4.1","karma":"^1.3.0","karma-chrome-launcher":"^2.2.0","karma-coverage":"^1.1.1","karma-firefox-launcher":"^1.1.0","karma-jasmine":"^1.1.1","karma-jasmine-ajax":"^0.1.13","karma-opera-launcher":"^1.0.0","karma-safari-launcher":"^1.0.0","karma-sauce-launcher":"^1.2.0","karma-sinon":"^1.0.5","karma-sourcemap-loader":"^0.3.7","karma-webpack":"^1.7.0","load-grunt-tasks":"^3.5.2","minimist":"^1.2.0","mocha":"^5.2.0","sinon":"^4.5.0","typescript":"^2.8.1","url-search-params":"^0.10.0","webpack":"^1.13.1","webpack-dev-server":"^1.14.1"},"homepage":"https://github.com/axios/axios","jsdelivr":"dist/axios.min.js","keywords":["xhr","http","ajax","promise","node"],"license":"MIT","main":"index.js","name":"axios","repository":{"type":"git","url":"git+https://github.com/axios/axios.git"},"scripts":{"build":"NODE_ENV=production grunt build","coveralls":"cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js","examples":"node ./examples/server.js","fix":"eslint --fix lib/**/*.js","postversion":"git push && git push --tags","preversion":"npm test","start":"node ./sandbox/server.js","test":"grunt test && bundlesize","version":"npm run build && grunt version && git add -A dist && git add CHANGELOG.md bower.json package.json"},"typings":"./index.d.ts","unpkg":"dist/axios.min.js","version":"0.21.1"}'); module.exports = JSON.parse('{"name":"axios","version":"0.21.1","description":"Promise based HTTP client for the browser and node.js","main":"index.js","scripts":{"test":"grunt test && bundlesize","start":"node ./sandbox/server.js","build":"NODE_ENV=production grunt build","preversion":"npm test","version":"npm run build && grunt version && git add -A dist && git add CHANGELOG.md bower.json package.json","postversion":"git push && git push --tags","examples":"node ./examples/server.js","coveralls":"cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js","fix":"eslint --fix lib/**/*.js"},"repository":{"type":"git","url":"https://github.com/axios/axios.git"},"keywords":["xhr","http","ajax","promise","node"],"author":"Matt Zabriskie","license":"MIT","bugs":{"url":"https://github.com/axios/axios/issues"},"homepage":"https://github.com/axios/axios","devDependencies":{"bundlesize":"^0.17.0","coveralls":"^3.0.0","es6-promise":"^4.2.4","grunt":"^1.0.2","grunt-banner":"^0.6.0","grunt-cli":"^1.2.0","grunt-contrib-clean":"^1.1.0","grunt-contrib-watch":"^1.0.0","grunt-eslint":"^20.1.0","grunt-karma":"^2.0.0","grunt-mocha-test":"^0.13.3","grunt-ts":"^6.0.0-beta.19","grunt-webpack":"^1.0.18","istanbul-instrumenter-loader":"^1.0.0","jasmine-core":"^2.4.1","karma":"^1.3.0","karma-chrome-launcher":"^2.2.0","karma-coverage":"^1.1.1","karma-firefox-launcher":"^1.1.0","karma-jasmine":"^1.1.1","karma-jasmine-ajax":"^0.1.13","karma-opera-launcher":"^1.0.0","karma-safari-launcher":"^1.0.0","karma-sauce-launcher":"^1.2.0","karma-sinon":"^1.0.5","karma-sourcemap-loader":"^0.3.7","karma-webpack":"^1.7.0","load-grunt-tasks":"^3.5.2","minimist":"^1.2.0","mocha":"^5.2.0","sinon":"^4.5.0","typescript":"^2.8.1","url-search-params":"^0.10.0","webpack":"^1.13.1","webpack-dev-server":"^1.14.1"},"browser":{"./lib/adapters/http.js":"./lib/adapters/xhr.js"},"jsdelivr":"dist/axios.min.js","unpkg":"dist/axios.min.js","typings":"./index.d.ts","dependencies":{"follow-redirects":"^1.10.0"},"bundlesize":[{"path":"./dist/axios.min.js","threshold":"5kB"}]}');
/***/ }), /***/ }),

View File

@ -1,11 +1,11 @@
module.exports = { module.exports = {
clearMocks: true, clearMocks: true,
moduleFileExtensions: ['js', 'ts'], moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node', testEnvironment: 'node',
testMatch: ['**/*.test.ts'], testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner', testRunner: 'jest-circus/runner',
transform: { transform: {
'^.+\\.ts$': 'ts-jest' '^.+\\.ts$': 'ts-jest',
}, },
verbose: true verbose: true,
} };

8299
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "post-api-call-action", "name": "post-api-call-action",
"version": "1.0.3", "version": "1.1.0",
"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

@ -5,15 +5,23 @@ import { parseJsonSafely } from './parseJsonSafely';
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
const url: string = getInput('url'); const url: string = getInput('url');
info(`Sending POST request to ${url}`);
const data = parseJsonSafely(getInput('data')); const data = parseJsonSafely(getInput('data'));
const headers = parseJsonSafely(getInput('headers')); const headers = parseJsonSafely(getInput('headers'));
const params = parseJsonSafely(getInput('params'));
info(`Sending POST request to ${url}`);
await axios.post(url, data, { await axios.post(url, data, {
headers, headers,
params,
}); });
} catch (err) { } catch (err) {
error(err.message); if (err instanceof Error) {
setFailed(err.message); error(err.message);
setFailed(err.message);
return;
}
setFailed('An unknown error occured');
} }
} }

View File

@ -4,7 +4,11 @@ export const parseJsonSafely = (jsonString: string): unknown => {
try { try {
return JSON.parse(jsonString); return JSON.parse(jsonString);
} catch (err) { } catch (err) {
error(`Parsing error: ${jsonString} - ${err.message}`); error(
`Parsing error: ${jsonString} - ${
(err as Error)?.message ?? 'Unknown error while parsing JSON'
}`
);
return {}; return {};
} }
}; };