Skip to content

Commit

Permalink
headers params
Browse files Browse the repository at this point in the history
  • Loading branch information
qertis committed Jun 10, 2024
1 parent d855a7f commit 3342eb9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 38 deletions.
26 changes: 21 additions & 5 deletions index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
*/
const VERSION = '2.0';
/**
* @param {string} obj - obj
* @param {string} obj - object
* @param {string} obj.url - url
* @param {object} obj.body - body
* @param {object} [obj.headers] - headers
* @param {object} [obj.auth] - basic auth
* @param {string} [obj.signature] - verification Ed25519 signatures
* @param {boolean} [obj.dev] - development
* @param {object} [app] - Express JS Application
* @returns {Promise<*>}
*/
const rpc = ({ url, body, headers = {}, auth, jwt, signature, dev = false, }, app) => {
const rpc = ({
url,
body,
headers = {},
auth,
jwt,
signature,
dev = 'production' !== process.env.NODE_ENV,
}, app) => {
if (dev) {
if (!app) {
return Promise.reject({
Expand Down Expand Up @@ -61,9 +70,16 @@ const rpc = ({ url, body, headers = {}, auth, jwt, signature, dev = false, }, ap
});
}

const fheaders = new Headers()
fheaders.append('Accept', 'application/json');
fheaders.append('Content-Type', 'application/json');
const fheaders = new Headers();
Object.keys(headers).forEach(((key) => {
fheaders.append(key, headers[key]);
}));
if (!fheaders.has('Accept')) {
fheaders.append('Accept', 'application/json');
}
if (!fheaders.has('Content-Type')) {
fheaders.append('Content-Type', 'application/json');
}
if (signature) {
fheaders.append('Signature', JSON.stringify(signature));
}
Expand Down
18 changes: 14 additions & 4 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const VERSION = '2.0';
* @param {string} obj - object
* @param {string} obj.url - url
* @param {object} obj.body - body
* @param {object} [obj.headers] - headers
* @param {object} [obj.auth] - basic auth
* @param {string} [obj.signature] - verification Ed25519 signatures
* @param {boolean} [obj.dev] - development
* @param {object} [app] - Express JS Application
* @returns {Promise<*>}
*/
Expand All @@ -19,8 +21,9 @@ export default async({
auth,
jwt,
signature,
dev = 'production' !== process.env.NODE_ENV,
}, app) => {
if ('production' !== process.env.NODE_ENV) {
if (dev) {
if (!app) {
return Promise.reject({
jsonrpc: VERSION,
Expand Down Expand Up @@ -67,9 +70,16 @@ export default async({
}))
}

const fheaders = new Headers()
fheaders.append('Accept', 'application/json');
fheaders.append('Content-Type', 'application/json');
const fheaders = new Headers();
Object.keys(headers).forEach(((key) => {
fheaders.append(key, headers[key]);
}));
if (!fheaders.has('Accept')) {
fheaders.append('Accept', 'application/json');
}
if (!fheaders.has('Content-Type')) {
fheaders.append('Content-Type', 'application/json');
}
if (signature) {
fheaders.append('Signature', JSON.stringify(signature));
}
Expand Down
44 changes: 21 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "request-json-rpc2",
"version": "2.1.2",
"description": "Simplified JSON-RPC2 request nodejs client",
"version": "2.1.3",
"description": "Simplified JSON-RPC2 request nodejs and browser client",
"main": "index.cjs",
"module": "dist/index.mjs",
"type": "module",
"source": "index.mjs",
"scripts": {
"test": "npm run test:esm && npm run test:cjs",
"test:esm": "node tests/index.test.mjs",
"test:cjs": "node tests/index.test.cjs",
"build": "NODE_ENV=production parcel build --no-autoinstall"
Expand All @@ -20,7 +21,7 @@
"dist/index.mjs"
],
"keywords": [
"request-json-rpc",
"request json rpc",
"request-json-rpc2",
"node request-json-rpc 2",
"json-rpc2",
Expand All @@ -41,6 +42,6 @@
"devDependencies": {
"express": "~4.19.1",
"parcel": "~2.12.0",
"supertest": "~6.3.4"
"supertest": "~7.0.0"
}
}
2 changes: 1 addition & 1 deletion tests/index.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const requestJsonRpc2 = require('../index.cjs');
const app = express();
app.use(express.json());

test('ExpressJS', async () => {
test('CJS', async () => {
app.post('/api', function(req, res) {
switch (req.body.body.method) {
case 'ping': {
Expand Down
2 changes: 1 addition & 1 deletion tests/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import requestJsonRpc2 from '../index.mjs';
const app = express();
app.use(express.json());

test('ExpressJS', async () => {
test('MJS', async () => {
app.post('/api', (req, res) => {
switch (req.body.body.method) {
case 'ping': {
Expand Down

0 comments on commit 3342eb9

Please sign in to comment.