Gathering detailed insights and metrics for @codexteam/ajax
Gathering detailed insights and metrics for @codexteam/ajax
Gathering detailed insights and metrics for @codexteam/ajax
Gathering detailed insights and metrics for @codexteam/ajax
npm install @codexteam/ajax
56
Supply Chain
92.7
Quality
80.7
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
33 Stars
73 Commits
11 Forks
8 Watching
8 Branches
6 Contributors
Updated on 16 Oct 2024
Minified
Minified + Gzipped
JavaScript (75.95%)
HTML (24.05%)
Cumulative downloads
Total Downloads
Last day
34.5%
639
Compared to previous day
Last week
24.9%
2,947
Compared to previous week
Last month
6.6%
11,752
Compared to previous month
Last year
72.8%
120,582
Compared to previous year
Module for async requests on a native JavaScript for a browser.
Package has been renamed from
codex.ajax
to@codexteam/ajax
transport
method: ask user for a file(s) and upload itobject
, FormData
or HTMLFormElement
data is being supportedYou can install this package via NPM or Yarn
1npm install @codexteam/ajax
1yarn add @codexteam/ajax
Require package on your script page.
1const ajax = require('@codexteam/ajax');
Also you can get this module from CDN or download a bundle file and use it locally.
There are a few public functions available to be used by user. All of them return Promise.
successCallback
and errorCallback
have the same input object response
as a param.
param | type | description |
---|---|---|
response.body | object or string | Response body parsed JSON or a string |
response.code | number | Response code |
response.headers | object | Response headers object |
1function successCallback(response) { 2 console.log('Response:', response.body); 3}
1function errorCallback(response) { 2 console.log(`Error code ${response.code}. Response:`, response.body); 3}
Wrapper for a GET request over an ajax.request()
function.
param | type | default value | description |
---|---|---|---|
url | string | '' | Request URL |
data | object | null | Data to be sent |
headers | object | null | Custom headers object |
progress | function | (percentage) => {} | Progress callback |
ratio | number | 90 | Max % of bar for uploading progress |
beforeSend | function | null | Fire callback before sending a request |
1ajax.get({ 2 url: '/getUserData', 3 data: { 4 user: 22 5 } 6}) 7 .then(successCallback) 8 .catch(errorCallback);
Wrapper for a POST request over an ajax.request()
function.
param | type | default value | description |
---|---|---|---|
url | string | '' | Request URL |
data | object , FormData or HTMLFormElement | null | Data to be sent |
type | string | ajax.contentType.JSON | Header from ajax.contentType object |
headers | object | null | Custom headers object |
progress | function | (percentage) => {} | Progress callback |
ratio | number | 90 | Max % of bar for uploading progress |
beforeSend | function | null | Fire callback before sending a request |
Simple POST request
1ajax.post({ 2 url: '/saveArticle', 3 data: { 4 title: 'Awesome article', 5 text: 'will be written later', 6 isPublished: false 7 }, 8 9 /** 10 * Choose the content type you need 11 */ 12 // type: ajax.contentType.JSON /* (default) */ 13 // type: ajax.contentType.URLENCODED 14 // type: ajax.contentType.FORM 15}) 16 .then(successCallback) 17 .catch(errorCallback);
To send any form you can pass HTMLFormElement as a data
to ajax.post()
.
1<form id="form-element"> 2 <input type="text" name="firstName" placeholder="First name"> 3 <input type="text" name="lastName" placeholder="Last name"> 4 <input type="file" name="profileImage" accept="image/*"> 5 <button onclick="event.preventDefault(); sendForm()">Send form</button> 6</form> 7 8<script> 9function sendForm() { 10 var form = document.getElementById('form-element'); 11 12 ajax.post({ 13 url:'/addUser', 14 data: form 15 }) 16 .then(successCallback) 17 .catch(errorCallback); 18} 19</script>
Main function for all requests.
param | type | default value | description |
---|---|---|---|
url | string | '' | Request URL |
method | string | 'GET' | Request method |
data | object | null | Data to be sent |
headers | object | null | Custom headers object |
progress | function | (percentage) => {} | Progress callback |
ratio | number | 90 | Max % of bar for uploading progress |
beforeSend | function | (files) => {} | Fire callback before sending a request |
1ajax.request({ 2 url: '/joinSurvey', 3 method: 'POST', 4 data: { 5 user: 22 6 } 7}) 8 .then(successCallback) 9 .catch(errorCallback);
This is a function for uploading files from client.
User will be asked to choose a file (or multiple) to be uploaded. Then FormData object will be sent to the server via ajax.post()
function.
param | type | default value | description |
---|---|---|---|
url | string | '' | Request URL |
data | object | null | Additional data to be sent |
accept | string | null | Mime-types of accepted files |
multiple | boolean | false | Let user choose more than one file |
fieldName | string | 'files' | Name of field in form with files |
headers | object | null | Custom headers object |
progress | function | (percentage) => {} | Progress callback |
ratio | number | 90 | Max % of bar for uploading progress |
beforeSend | function | (files) => {} | Fire callback with chosen files before sending |
1ajax.transport({ 2 url: '/uploadImage', 3 accept: 'image/*', 4 progress: function (percentage) { 5 document.title = `${percentage}%`; 6 }, 7 ratio: 95, 8 fieldName: 'image' 9}) 10 .then(successCallback) 11 .catch(errorCallback);
One simple button for uploading files.
1<button onclick='ajax.transport({url: "/uploadFiles"}).then(successCallback).catch(errorCallback)'>Upload file<button>
Ask user for a file (or multiple) and process it. FileList object will be returned in Promise.
param | type | default value | description |
---|---|---|---|
accept | string | null | Mime-types of accepted files |
multiple | boolean | false | Let user choose more than one file |
1ajax.selectFiles({ 2 accept: 'image/*' 3}) 4 .then(successCallback);
List of params, their types, descriptions and examples.
string
Target page URL. By default current page url will be used.
/user/22
, /getPage
, /saveArticle
string
Used in
ajax.request()
function only
Request method.
GET
, POST
Read more about available request methods methods on the page at developer.mozilla.org.
object|FormData|HTMLFormElement
You can pass data as object
, FormData
or HTMLFormElement
.
Data will be encoded automatically.
1ajax.request({ 2 url: '/joinSurvey', 3 method: 'POST', 4 data: {user: 22} 5}) 6 .then(successCallback) 7 .catch(errorCallback);
1ajax.request({ 2 url: '/sendForm', 3 method: 'POST', 4 data: new FormData(document.getElementById('my-form')) 5}) 6 .then(successCallback) 7 .catch(errorCallback);
For
ajax.get()
you can passobject
data
1ajax.get({ 2 url: '/getUserData', 3 data: { 4 user: 22 5 } 6}) 7 .then(successCallback) 8 .catch(errorCallback);
is the same as
1ajax.get({ 2 url: '/getUserData?user=22' 3}) 4 .then(successCallback) 5 .catch(errorCallback);
For
ajax.transport()
should passobject
data if it is necessary
You can send additional data with files.
1ajax.transport({ 2 url: '/uploadImage', 3 accept: 'image/*', 4 data: { 5 visible: true, 6 caption: 'Amazing pic' 7 }, 8 fieldName: 'image' 9}) 10 .then(successCallback) 11 .catch(errorCallback);
string
Specify the content type of data to be encoded (by ajax module) and sent.
You can get value for this param from ajax.contentType
object. Data will be encoded that way.
ajax.contentType | value |
---|---|
JSON | application/json; charset=utf-8 |
URLENCODED | application/x-www-form-urlencoded; charset=utf-8 |
FORM | multipart/form-data |
1const params = { 2 // ... 3 4 type: ajax.contentType.JSON 5 // type: ajax.contentType.URLENCODED 6 // type: ajax.contentType.FORM 7};
object
Object of custom headers which will be added to request.
1headers = { 2 'authorization': 'Bearer eyJhbGciJ9...TJVA95OrM7h7HgQ', 3 // ... 4}
function
Almost all requests have responses. To show a correct progress for a call we need to combine a request progress (uploading) and a response progress (downloading). This ajax module uses one progress
callback for it.
1/** 2 * @param {number} percentage - progress value from 0 to 100 3 */ 4var progressCallback = function progressCallback(percentage) { 5 document.title = `${percentage}%`; 6};
Check out ratio
param to show progress more accurate.
number
Used with
progress
param
Value should be in the 0
-100
interval.
If you know that some requests may take more time than their responses or vice versa, you can set up a ratio
param and define a boundary between them on the progress bar.
For example if you want to show progress for a file uploading process, you know that uploading will take a much more time than downloading response, then pass bigger ratio (~95). When you want to download big file — use smaller ratio (~5).
string
Used in
ajax.transport()
function only
String of available types of files to be chosen by user.
*/*
— any files (default)
image/*
— only images
image/png, image/jpg, image/bmp
— restrict accepted types
Read more about MIME-types on the page at developer.mozilla.org.
boolean
Used in
ajax.transport()
function only
false
by default. User can choose only one file.
If you want to allow user choose more than a one file to be uploaded, then pass a true
value.
string
Used in
ajax.transport()
function only
Name of data field with the file or array of files.
files
by default.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 4/7 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
43 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More