sn-utilities

SN Utilities

Usage no npm install needed!

<script type="module">
  import snUtilities from 'https://cdn.skypack.dev/sn-utilities';
</script>

README

Utilities

function getParams(req, func)

Parameters:

 Parameter          | Type        | Description            
--------------------+-------------+------------------------------------------
 req                | JSON        | HTTP request object containing HTTP parameters
 func               | function    | JavaScript function (example below)

Example of func parameter:

function getParams(source, target) {
    if (source) {
        if (source.id && !target.id) {
            target.id = source.id;
        }
        if (source.name && !target.name) {
            target.name = source.name;
        }
    }
    return target;
}

function handleError(error, res)

Parameters:

 Parameter          | Type        | Description            
--------------------+-------------+------------------------------------------
 error              | JSON        | JSON error object (format below)
 res                | JSON        | HTTP response object

Format of JSON error parameter:

{
  "error": [{}],
  "status": [123]
}
Above JSON explained:
 Parameter          | Type        | Description            
--------------------+-------------+------------------------------------------
 error              | JSON        | The JSON object of the error thrown
 status             | INT         | (Optional) HTTP status code to be used if the status in error is not specified
Example of above JSON error:
{
  "error": {
    "errors": [
      {
        "error": "invalid input syntax for uuid: \"3334c105-f842-98e7-603a\""
      }
    ],
    "name": "error",
    "length": 118,
    "severity": "ERROR",
    "code": "22P02",
    "detail": "undefined",
    "hint": "undefined",
    "position": "undefined",
    "internalPosition": "undefined",
    "internalQuery": "undefined",
    "where": "undefined",
    "schema": "undefined",
    "table": "undefined",
    "column": "undefined",
    "dataType": "undefined",
    "constraint": "undefined",
    "file": "uuid.c",
    "line": "129",
    "routine": "string_to_uuid"
  }
}

function handleSuccess(success, res)

Parameters:

 Parameter          | Type        | Description            
--------------------+-------------+------------------------------------------
 success            | JSON        | JSON success object (format below)
 res                | JSON        | HTTP response object

Format of JSON success parameter:

{
  "success": {
    "status": [123],
    "message": ["abc"],
    "data": {}
  },
  "status": [123]
}
Above JSON explained:
 Parameter          | Type        | Description            
--------------------+-------------+-----------------------------------------------------------------------------------
 success->status    | INT         | (Optional) HTTP status code
 success->message   | TEXT        | Description of status
 success->data      | JSON        | (Optional) Metadata
 status             | INT         | (Optional) HTTP status code to be used if the status in success is not specified

function handleContentTypeJSON(param, res)

Parameters:

 Parameter          | Type        | Description            
--------------------+-------------+------------------------------------------
 param              | JSON        | JSON param object (format below)
 res                | JSON        | HTTP response object

Format of JSON param parameter:

{
  "contentType": ["abc"]
}
Above JSON explained:
 Parameter           | Type        | Description            
---------------------+-------------+-------------------------------------------------
 contentType         | TEXT        | The content type of the request to be validated

function createUUID(data)

Parameters:

 Parameter          | Type        | Description            
--------------------+-------------+------------------------------------------
 data               | ANYTHING    | (Optional)

function getLocalTimestampString()