Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.
hasha2982 edited this page May 28, 2020 · 1 revision

Py93 has a SDK. It is stored in $py93.sdk.

Function $py93.sdk.checkLink

Sends a XMLHttpRequest to an URL given in link parameter.

Parameters

Function parameters

  • link - String, the function will send a XMLHttpRequest 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 is true

Callback parameters

  • succeed - Boolean, determines if request succeed (we got any response from server)
  • details - Object, contains details about the request.
details object

If request succeed, then details will have these properties:

  • contentType - Value of response header Content-Type, example: application/json; charset=utf-8
  • status - HTTP status code, for example 404, 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 are error and timeout
  • 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 {}
}

Use example

$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

Function $py93.sdk.validatePackageJSON

Validates JSON string or object.

Parameters

  • json - A JSON string or object to validate

Returns

If no errors happened, then this function will return an object that has these properties:

  • valid - Boolean, true if all properties in checks is true, otherwise false.
  • checks - Object that contains 8 booleans, if string/object is valid, then all booleans is true.

If an error happened, then this function will return undefined and log the error to JavaScript console using console.error.

checks

Object checks has these properties:

  • versionExist - True if version exist
  • versionIsNum - True if version is a number
  • versionSupp - True if package JSON file syntax (defined in version) is supported by this Py93 version
  • metaExist - True if meta exist and it's typeof is object
  • meta_titleExist - True if title in meta exist
  • meta_compVerExist - True if compVer in meta exist
  • installExist - True if install exist and typeof is object
  • install_packageExist - True if package in "install" exist and typeof is string

Use examples

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