-
Notifications
You must be signed in to change notification settings - Fork 0
SDK
Py93 has a SDK.
It is stored in $py93.sdk
.
Sends a XMLHttpRequest
to an URL given in link
parameter.
-
link
- String, the function will send aXMLHttpRequest
to URL given in this parameter -
callback
- Function, takes 2 parameters, will be called when got a response or an error occured -
async
(optional) - Boolean, determines should request be asynchronous, default istrue
-
succeed
- Boolean, determines if request succeed (we got any response from server) -
details
- Object, contains details about the request.
If request succeed, then details
will have these properties:
-
contentType
- Value of response headerContent-Type
, example:application/json; charset=utf-8
-
status
- HTTP status code, for example404
,200
-
statusText
- HTTP status text, example:Not Found
-
resp
- A string with response from server. -
xhr
-XMLHttpRequest
copy that used to configure and send the request, can be used for getting more data about the request.
If request failed, then details
will have these attributes:
-
errorType
- Type of an error, possible values areerror
andtimeout
-
xhr
-XMLHttpRequest
copy that used to configure and send the request, can be used for getting more data about the request.
errorType
will be equal to error
if request failed (event error
happened), for example: it was blocked by CORS.
If request failed, you can see the error message in the JavaScript console.
errorType
will be equal to timeout
if the request timed out (event timeout
happened)
Example of details
object, request succeed:
{
contentType: "application/json; charset=utf-8",
status: 200,
statusText: "",
resp: '{"foo": "bar"}',
xhr: XMLHttpRequest {}
}
Example of details
object, request failed:
{
errorType: "error",
xhr: XMLHttpRequest {}
}
$py93.sdk.checkLink('https://example.com/', function(succeed, details) {
if (succeed) { // code in this if statement will not run
console.log('Request succeed!')
} else { // but code in else will run
console.log(`Request failed! Error type: ${details.errorType}`) // => Request failed! Error type: error
}
}, true) // Note: this request will be blocked by CORS
Validates JSON string or object.
-
json
- A JSON string or object to validate
If no errors happened, then this function will return an object that has these properties:
-
valid
- Boolean,true
if all properties inchecks
istrue
, otherwisefalse
. -
checks
- Object that contains 8 booleans, if string/object is valid, then all booleans istrue
.
If an error happened, then this function will return undefined
and log the error to JavaScript console using console.error
.
Object checks
has these properties:
-
versionExist
- True ifversion
exist -
versionIsNum
- True ifversion
is a number -
versionSupp
- True if package JSON file syntax (defined inversion
) is supported by this Py93 version -
metaExist
- True ifmeta
exist and it'stypeof
isobject
-
meta_titleExist
- True iftitle
inmeta
exist -
meta_compVerExist
- True ifcompVer
inmeta
exist -
installExist
- True ifinstall
exist andtypeof
isobject
-
install_packageExist
- True ifpackage
in "install" exist and typeof isstring
Example 1:
var returned = $py93.sdk.validatePackageJSON({
version: 1,
meta: {
title: "hello-wiki",
compVer: 1
},
install: {
package: "https://example.com"
}
})
console.log(`Valid: ${returned.valid}`) // => Valid: true
Example 2:
var returned = $py93.sdk.validatePackageJSON('{"version":1,"meta":{"title":"hello-wiki","compVer":1},"install":{"package":"https://example.com"}}')
console.log(`Valid: ${returned.valid}`) // => Valid: true
Example 3:
var returned = $py93.sdk.validatePackageJSON({
meta: {
title: "hello-wiki",
compVer: 1
},
install: {
package: "https://example.com"
}
})
console.log(`Valid: ${returned.valid}`) // => Valid: false
Example 4:
var returned = $py93.sdk.validatePackageJSON('{"meta":{"title":"hello-wiki","compVer":1},"install":{"package":"https://example.com"}}')
console.log(`Valid: ${returned.valid}`) // => Valid: false
Example 5:
var returned = $py93.sdk.validatePackageJSON('{version:1,"meta":{"title":"hello-wiki","compVer":1},"install":{"package":"https://example.com"}}')
console.log(returned) // => undefined