Fetch wrapper for making HTTP(s) requests easier
- Package uses JSDOC commenting to assist via Intellisense - optimized for VSCode
import { WebbRequest } from '@webbspark/web-request/index.js';
- Note: for web use - Sever must be able to serve the node_modules/@webbspark folder or it should be copied into a folder the server can serve.
- Electron apps can access the node_modules folder directly, copying is not necessary
- post
- get
- put
- patch
- delete
- beforeRun
{
headers?: {[key: string]: any},
params?: {[key: string]: any},
mode?: ('cors' | 'no-cors' | 'same-origin'),
credentials?: ('omit' | 'same-origin' | 'include'),
cache?: ('default' | 'no-store' | 'reload' | 'no-cache' | 'force-cache' | 'only-if-cached'),
redirect?: ('follow' | 'error' | 'manual'),
referrerPolicy?: ('no-referrer' | 'no-referrer-when-downgrade' | 'same-origin' | 'origin' | 'strict-origin' | 'origin-when-cross-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url'),
integrity?: string,
keepalive?: boolean,
signal?: AbortController,
returnType?: ('arrayBuffer' | 'blob' | 'formData' | 'json' | 'redirect' | 'text'),
body?: (Blob | ArrayBuffer | DataView | FormData | URLSearchParams | ReadableStream | {[key: string]: any}),
rawBody?: boolean,
form?: {[key: string]: any},
}
{
response: ('arrayBuffer' | 'blob' | 'formData' | 'json' | 'redirect' | 'text');
request: (Request Options && { url?: string, method?: Methods, body?: any});
statusCode: number;
statusMessage: string;
}
import { WebbRequest } from '@webbspark/web-request/index.js';
const wreq = new WebRequest((opts) => {
opts.url = `https://someurl.com/${opts.url}`;
if (!opts.headers) opts.headers = {};
opts.headers['Accept'] = '*/*';
opts.headers['User-Agent'] = 'My Application (https://www.my-url.com)';
opts.headers['Content-Type'] = 'application/json';
opts.headers['Authorization'] = `Bearer ${opts.authToken}`;
delete opts.authToken; //make sure to delete user defined items, in the options object, before leaving this function
});
const saveNote = (noteText) => {
return new Promise(async (res, rej) => {
try {
const rtn = await wreq.post('http://someurl.com/api/notes', {
authToken: '<token>',
body: { note: noteText }
});
(rtn.statusCode === 200 ? res : rej)(rtn.response);
} catch (ex) {
console.log(err);
}
});
}
Bug Reporting
Feature Requests
Copyright (C) 2019 Christopher J. Webb - WebbSpark.com
Permission to use, and/or distribute this software for any legal purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
BY USING THIS SOFTWARE YOU AGREE TO ALL THE TERMS AND CONDITIONS HEREIN.
THE SOFTWARE IS PROVIDED "AS IS" AND THE COPYRIGHT HOLDER/AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
COPYRIGHT HOLDER/AUTHOR RESERVES THE RIGHT TO CHANGE THESE TERMS AND CONDITIONS AT ANY TIME, FOR ANY REASON, WITHOUT PRIOR NOTICE.