mirror of
https://github.com/dkershner6/post-api-call-action.git
synced 2024-11-15 12:13:51 +08:00
Merge pull request #2 from dkershner6/DAK/params
Added params as an optional parameter (query parameters)
This commit is contained in:
commit
30ecd56ecf
@ -1,6 +1,6 @@
|
||||
# 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
|
||||
|
||||
@ -16,6 +16,10 @@ Optional: JSON string of data 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
|
||||
|
||||
@ -25,6 +29,7 @@ with:
|
||||
url: ${{ secrets.API_URL }}
|
||||
data: "{\"command\": \"publish\"}"
|
||||
headers: "{\"Authorization\": \"Bearer ${{ secrets.API_KEY }}\"}"
|
||||
params: "{\"content\": \"Test Content\"}"
|
||||
```
|
||||
|
||||
|
||||
|
@ -16,6 +16,10 @@ inputs:
|
||||
description: 'JSON string of headers to pass into request'
|
||||
default: '{}'
|
||||
required: false
|
||||
params:
|
||||
description: 'JSON string of query parameters to pass into request'
|
||||
default: '{}'
|
||||
required: false
|
||||
runs:
|
||||
using: 'node12'
|
||||
using: 'node16'
|
||||
main: 'dist/index.js'
|
17
dist/index.js
vendored
17
dist/index.js
vendored
@ -26,16 +26,22 @@ 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_1.parseJsonSafely(core_1.getInput('data'));
|
||||
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, {
|
||||
headers,
|
||||
params,
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
core_1.error(err.message);
|
||||
core_1.setFailed(err.message);
|
||||
if (err instanceof Error) {
|
||||
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;
|
||||
const core_1 = __nccwpck_require__(2186);
|
||||
const parseJsonSafely = (jsonString) => {
|
||||
var _a, _b;
|
||||
try {
|
||||
return JSON.parse(jsonString);
|
||||
}
|
||||
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 {};
|
||||
}
|
||||
};
|
||||
@ -4311,7 +4318,7 @@ module.exports = {
|
||||
/***/ ((module) => {
|
||||
|
||||
"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"}]}');
|
||||
|
||||
/***/ }),
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
module.exports = {
|
||||
clearMocks: true,
|
||||
moduleFileExtensions: ['js', 'ts'],
|
||||
testEnvironment: 'node',
|
||||
testMatch: ['**/*.test.ts'],
|
||||
testRunner: 'jest-circus/runner',
|
||||
transform: {
|
||||
'^.+\\.ts$': 'ts-jest'
|
||||
},
|
||||
verbose: true
|
||||
}
|
||||
clearMocks: true,
|
||||
moduleFileExtensions: ['js', 'ts'],
|
||||
testEnvironment: 'node',
|
||||
testMatch: ['**/*.test.ts'],
|
||||
testRunner: 'jest-circus/runner',
|
||||
transform: {
|
||||
'^.+\\.ts$': 'ts-jest',
|
||||
},
|
||||
verbose: true,
|
||||
};
|
||||
|
8299
package-lock.json
generated
8299
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "post-api-call-action",
|
||||
"version": "1.0.3",
|
||||
"version": "1.1.0",
|
||||
"private": true,
|
||||
"description": "Send a POST request action",
|
||||
"main": "lib/main.js",
|
||||
|
14
src/main.ts
14
src/main.ts
@ -5,15 +5,23 @@ import { parseJsonSafely } from './parseJsonSafely';
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
const url: string = getInput('url');
|
||||
info(`Sending POST request to ${url}`);
|
||||
const data = parseJsonSafely(getInput('data'));
|
||||
const headers = parseJsonSafely(getInput('headers'));
|
||||
const params = parseJsonSafely(getInput('params'));
|
||||
|
||||
info(`Sending POST request to ${url}`);
|
||||
await axios.post(url, data, {
|
||||
headers,
|
||||
params,
|
||||
});
|
||||
} catch (err) {
|
||||
error(err.message);
|
||||
setFailed(err.message);
|
||||
if (err instanceof Error) {
|
||||
error(err.message);
|
||||
setFailed(err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
setFailed('An unknown error occured');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,11 @@ export const parseJsonSafely = (jsonString: string): unknown => {
|
||||
try {
|
||||
return JSON.parse(jsonString);
|
||||
} catch (err) {
|
||||
error(`Parsing error: ${jsonString} - ${err.message}`);
|
||||
error(
|
||||
`Parsing error: ${jsonString} - ${
|
||||
(err as Error)?.message ?? 'Unknown error while parsing JSON'
|
||||
}`
|
||||
);
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user