1.0.0 • Published 1 year ago

vartrick v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Node.js Rest APIs with Express & MySQL MongDb and sqlite

Installation

`npm install backend`

Run

nodemon index.js

Response

    { "success": true, "message": "data"}

    { "success": false, "message": "error response "}

Routes Available

NONAMEMETHODROUTE
1CreatePOST/create
2Bulk CreatePOST/bulk-create
3ReadPOST/read
4Bulk ReadPOST/bulk-read
5UpdatePOST/update
6Bulk UpdatePOST/bulk-update
7Deletec/delete
8Bulk DeletePOST/bulk-delete
9ListPOST/list
10List AllPOST/list-all
11Create EncryptedPOST/create-encryption
12authenticatePOST/authenticate
13SearchPOST/search
14Download FilePOST/download
15Upload FilePOST/upload-file
16Upload FilePOST/upload-files
17Delete FilePOST/delete-file
18Delete FilesPOST/delete-files
19Send SMSPOST/send-sms
20Send EmailPOST/send-email
21Generate OTPGET/generate-otp
22Send OTPPOST/send-otp
24CountGET/count

URL

http://localhost:PORT/api

OR

https://domain.com:PORT/api

Routes Explanation

1. /create

Requirements

    1. path => /create
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    }

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify({
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    })
    // api post request
    let response = await fetch(`URL/create`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": {
        "id": 5,
        "password": "vsvgdjdjdgdndeueoeddbd66h",
        "name": "nodejs"
    }
}

2. /bulk-create

Requirements

    1. path => /bulk-create
    2. method => POST
    3. data => array of object

JSON Request:

    [
        {
            "table": "test",
            "data": {
                 "name": "java",
                "password": "gjgjggitntrjrnrjrjr555r"
            }
        },
        {
            "tsble": "test",
            "data": {
                "name": "mysql",
                "password": "vsvgdjdjdgdndeueoeddbd66h"
            }
        }
    ]

Fetch Request:

async function bulkCreate() {

    // stringifying body
    const body = JSON.stringify([
         {
            "table": "test",
            "data": {
                 "name": "java",
                "password": "gjgjggitntrjrnrjrjr555r"
            }
        },
        {
            "tsble": "test",
            "data": {
                "name": "mysql",
                "password": "vsvgdjdjdgdndeueoeddbd66h"
            }
        }
    ])

    // api post request
    let response = await fetch(`URL/bulk-create`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    // printing response
    response = await response.json()

}

JSON Response:

    {
    "success": true,
    "message": [
        {
            "success": true,
            "message": {
                "id": 7,
                "name": "java",
                "password": "gjgjggitntrjrnrjrjr555r"
            }
        },
        {
            "success": true,
            "message": {
                "id": 8,
                "name": "mysql",
                "password": "vsvgdjdjdgdndeueoeddbd66h"
            }
        }
    ]
}

3. /read

Requirements

    1. path => /read
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "condition": {
            "name": "nodejs"
        }
    }

Fetch Request

async function read(){
    // stringifying body
    const body = JSON.stringify({
        "table": "test",
        "condition": {
            "name": "nodejs"
        }
    })
    // api post request
    let response = await fetch(`URL/read`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": {
        "id": 5,
        "name": "nodejs",
        "password": "vsvgdjdjdgdndeueoeddbd66h"
    }
}

4. /bulk-read

Requirements

    1. path => /bulk-read
    2. method => POST
    3. data => object or stringified object

JSON Request:

[
    {
        "table": "test",
        "condition": {
            "name": "nodejs"
        }
    },
    {
        "table": "test",
        "condition": {
            "name": "mysql"
        }
    }
]

Fetch Request

async function readBulk(){
    // stringifying body
    const body = JSON.stringify([
    {
        "table": "test",
        "condition": {
            "name": "nodejs"
        }
    },
    {
        "table": "test",
        "condition": {
            "name": "mysql"
        }
    }
]
    )
    // api post request
    let response = await fetch(`URL/bulk-read`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

[
    {
        "success": true,
        "message": {
            "id": 5,
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    },
    {
        "success": true,
        "message": {
            "id": 8,
            "name": "mysql",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    }
]

5. /update

Requirements

    1. path => /update
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "condition": {
            "name": "nodejs"
        },
        "data":{
            "name":"typescript"
        }
    }

Fetch Request

async function update(){
    // stringifying body
    const body = JSON.stringify(
    {
        "table": "test",
        "condition": {
            "name": "nodejs"
        },
        "data":{
            "name":"typescript"
        }
    })
    // api post request
    let response = await fetch(`URL/update`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": "data updated successfull"
}

6. /bulk-update

Requirements

    1. path => /bulk-update
    2. method => POST
    3. data => object or stringified object

JSON Request:

[
    {
        "table": "test",
        "condition": {
            "name": "java8"
        },
        "data":{
            "name":"javascript"
        }
    },
    {
        "table": "test",
        "condition": {
            "id": "8"
        },
        "data":{
            "name":"python"
        }
    }
]

Fetch Request

async function bulkUpdate(){
    // stringifying body
    const body = JSON.stringify([
    {
        "table": "test",
        "condition": {
            "name": "java8"
        },
        "data":{
            "name":"javascript"
        }
    },
    {
        "table": "test",
        "condition": {
            "id": "8"
        },
        "data":{
            "name":"python"
        }
    }
])
    // api post request
    let response = await fetch(`URL/bulk-update`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

[
    {
        "success": true,
        "message": "data updated successfull"
    },
    {
        "success": true,
        "message": "data updated successfull"
    }
]

7. /delete

Requirements

    1. path => /delete
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "condtion": {
            "name": "nodejs",
        }
    }

Fetch Request

async function delete(){
    // stringifying body
    const body = JSON.stringify(
    {
        "table": "test",
        "condtion": {
            "name": "nodejs"
        }
    })
    // api post request
    let response = await fetch(`URL/delete`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": "data succsesful deleted in the database"
}

8. /bulk-delete

Requirements

    1. path => /bulk-delete
    2. method => POST
    3. data => object or stringified object

JSON Request:

[
    {
        "table": "test",
        "condition": {
            "name": "javascript"
        }
    },
    {
        "table": "test",
        "condition": {
            "name": "nodejs"
        }
    }
]

Fetch Request

async function bulkDelete(){
    // stringifying body
    const body = JSON.stringify([
    {
        "table": "test",
        "condition": {
            "name": "javascript"
        }
    },
    {
        "table": "test",
        "condition": {
            "name": "nodejs"
        }
    }
])
    // api post request
    let response = await fetch(`URL/bulk-delete`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

[
    {
    "success": true,
    "message": "data succsesful deleted in the database"
    },
    {
    "success": true,
    "message": "data succsesful deleted in the database"
    }
]

9. /list

Requirements

    1. path => /list
    2. method => POST
    3. data => object or stringified object

JSON Request:

{
    "table": "test",
    "limit":2,
    "page":2,
    "sort":{
        "id":"DESC"
    }
}

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify({
    "table": "test",
    "limit":2,
    "page":2,
    "sort":{
        "id":"DESC"
    }
})
    // api post request
    let response = await fetch(`URL/create`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}
`+``
JSON Response

```JSON
{
    "success": true,
    "message": {
        "limit": 2,
        "totalPages": 3,
        "previousPage": 1,
        "currentPage": 2,
        "nextPage": 3,
        "pages": [
            1,
            2,
            3
        ],
        "data": [
            {
                "id": 6,
                "name": "java",
                "password": "gjgjggitntrjrnrjrjr555r"
            },
            {
                "id": 4,
                "name": "patrick2",
                "password": "vsvgdjdjdgdndeueoeddbd66h"
            }
        ]
    }
}

10. /list-all

Requirements

    1. path => /list-all
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test"
    }

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify({
        "table": "test"
    })
    // api post request
    let response = await fetch(`URL/list-all`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": [
        {
            "id": 1,
            "name": "patrick2",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        },
        {
            "id": 4,
            "name": "patrick2",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        },
        {
            "id": 6,
            "name": "java",
            "password": "gjgjggitntrjrnrjrjr555r"
        },
        {
            "id": 7,
            "name": "java",
            "password": "gjgjggitntrjrnrjrjr555r"
        },
        {
            "id": 8,
            "name": "python",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    ]
}

11. /create-encryption

Requirements

    1. path => /create-encryption
    2. method => POST
    3. data => object or stringified object

JSON Request:

{
    "table": "test",
    "data":{
        "name":"trick"
    },
    "encript": {
        "password":"trick"
    }

}

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify(
{
    "table": "test",
    "data":{
        "name":"trick"
      },
    "encript": {
        "password":"trick"
    }
})
    // api post request
    let response = await fetch(`URL/create-encryption`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": {
        "id": 12,
        "name": "possss",
        "password": "$2b$10$JwuYQqppcjwxomHoGWoLCujQkhWaITgihtTh4FW/DC931.lREi63y"
    }
}

12. /search

Requirements

    1. path => /search
    2. method => POST
    3. data => object or stringified object

JSON Request:

{
    "table": "test",
    "condition":{
        "name":"java"
    }
}

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify(
        {
            "table": "test",
            "condition":{
                "name":"java"
                }
        })
    // api post request
    let response = await fetch(`URL/search`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": [
        {
            "id": 6,
            "name": "java",
            "password": "gjgjggitntrjrnrjrjr555r"
        },
        {
            "id": 7,
            "name": "java",
            "password": "gjgjggitntrjrnrjrjr555r"
        }
    ]
}

13. /count

Requirements

    1. path => /count
    2. method => POST
    3. data => object or stringified object

JSON Request:

{
    "table": "test",
    "condition":{
        "name":"java"
    }
}

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify(
        {
            "table": "test",
            "condition":{
                "name":""
                }
        })
    // api post request
    let response = await fetch(`URL/count`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": 9
}

14. /xxxxxx

15. /authenticate

Requirements

    1. path => /create
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    }

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify({
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    })
    // api post request
    let response = await fetch(`URL/create`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": {
        "id": 5,
        "password": "vsvgdjdjdgdndeueoeddbd66h",
        "name": "nodejs"
    }
}

16. /download

Requirements

    1. path => /download
    2. method => GET
    3. data => object or stringified object

JSON Request:

    {
        "parameter": {
            "folder": "",
            "file": "index.js"
        }
    }

Fetch Request

async function download(){
    // api get request
    let response = await fetch(`URL/download?${parameters}`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

## 17. /delete-file

Requirements
1. path => /delete-file
2. method => POST
3. data => object or stringified object
JSON Request:

```JSON 
{
    "file" : "index.js",
    "folder" : ""
}

Fetch Request

async function deleteFile(){
    // stringifying body
    const body = JSON.stringify({
            "file": "index.js",
            "folder": ""
    })
    // api post request
    let response = await fetch(`URL/delete-file`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": "file has been deleted"
}

18. /delete-files

Requirements

    1. path => /delete-files
    2. method => POST
    3. data => object or stringified object

JSON Request:

[
    {
        "file": "index.js",
        "folder":""
    },
    {
        "file": "node.js",
        "folder":""
    } 
]

Fetch Request

async function deleteFiles(){
    // stringifying body
    const body = JSON.stringify(
        [
            {
                "file": "index.js",
                "folder":""
            },
            {
                "file": "node.js",
                "folder":""
            }
        ])
    // api post request
    let response = await fetch(`URL/delete-files`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

[
    {
        "success": true,
        "message": "file have been deleted "
    },
    {
        "success": true,
        "message": "file have been deleted "
    }
]

19. /upload-file

Requirements

    1. path => /create
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    }

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify({
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    })
    // api post request
    let response = await fetch(`URL/create`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": {
        "id": 5,
        "password": "vsvgdjdjdgdndeueoeddbd66h",
        "name": "nodejs"
    }
}

20. /upload-files

Requirements

    1. path => /create
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    }

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify({
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    })
    // api post request
    let response = await fetch(`URL/create`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": {
        "id": 5,
        "password": "vsvgdjdjdgdndeueoeddbd66h",
        "name": "nodejs"
    }
}

21. /send-sms

Requirements

    1. path => /send-sms
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "message": "test api",
        "to": [
            "+25525449295","+25555995270"
        ]
    }

Fetch Request

async function sendSms(){
    // stringifying body
    const body = JSON.stringify({
        "message": "test api",
        "to": [
            "+25525449295","+25555995270"
        ]
    })
    // api post request
    let response = await fetch(`URL/send-sms`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "Message": "Sent to 1/1 Total Cost: TZS 22.0000",
    "Recipients": [
        {
            "cost": "TZS 22.0000",
            "messageId": "ATXid_6tttttttttttttttttttttt5",
            "messageParts": 1,
            "number": "+255625449295",
            "status": "Success",
            "statusCode": 101
        },
               {
            "cost": "TZS 22.0000",
            "messageId": "ATXid_6tttttttttttttttttttttt5",
            "messageParts": 1,
            "number": "+255755995270",
            "status": "Success",
            "statusCode": 101
        }
    ]
}

22. /send-email

Requirements

    1. path => /send-email
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    }

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify({
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    })
    // api post request
    let response = await fetch(`URL/create`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": false,
    "message": {
        "accepted": [
            "patrickmunuo98@gmail.com"
        ],
        "rejected": [],
        "envelopeTime": 992,
        "messageTime": 886,
        "messageSize": 523,
        "response": "250 2.0.0 OK  1673730887 cs3-20020a0564020c4300btgrvrgrvr0c2a08esm9686936edb.36 - gsmtp",
        "envelope": {
            "from": "",
            "to": [
                "patrickmunuo98@gmail.com"
            ]
        },
        "messageId": "<840d31e1-8419-e792-3f1b-d00bf5c110d6@localhost>"
    }
}

23. /generate-otp

Requirements

    1. path => /generate-otp
    2. method => GET
    3. data => object or stringified object

Fetch Request

async function generateOtp(){
    // api get request
    let response = await fetch(`URL/generate-otp`, {
        method: 'GET',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": 546454
}

24. /send-otp

Requirements

    1. path => /create
    2. method => POST
    3. data => object or stringified object

JSON Request:

    {
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    }

Fetch Request

async function create(){
    // stringifying body
    const body = JSON.stringify({
        "table": "test",
        "data": {
            "name": "nodejs",
            "password": "vsvgdjdjdgdndeueoeddbd66h"
        }
    })
    // api post request
    let response = await fetch(`URL/create`, {
        method: 'POST',
        mode: 'cors',
        body,
        headers: {
            'Content-Type': 'application/json'
        }
    })

    response = await response.json()

    // printing response
    console.log(response)

}

JSON Response

{
    "success": true,
    "message": {
        "id": 5,
        "password": "vsvgdjdjdgdndeueoeddbd66h",
        "name": "nodejs"
    }
}