5.1.4 • Published 1 year ago

@snipkode/stack v5.1.4

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

ENVIRONMENT REQUIREMENT

NODE VERSIONNPM VERSION
14.19.36.x.x

PROJECT STRUCTURE APPLICATION

    / stackproject
    └── public
        └── assets
        └── certs
    └── repository
        └── admin
            └── ejs-app-1
            └── ejs-app-2
        └── clients
            └── react-app-1
            └── react-app-2
    └── servers
        └── controller
            └── middleware
            └── ModuleControl.js
        └── model
            └── connection
            └── ModuleModel.js
        └── routes
            └── ApiRoutes.js
            └── WebRoutes.js

COPY THE CODE BELOW TO GET TESTING INTO NPM RUN KIT (node 14.x.x) OR VISIT THIS LINK https://admhabits.github.io/snipkode-stack/

    
require("@google-cloud/firestore");
const Stack = require("@snipkode/stack");
const Routes = Stack.router();
const Jwt = Stack.jwt;

Routes.get('/', (req, res) => {
    console.log(Jwt.sign('a', 'b')); // return eyJhbGciOiJIUzI1NiJ9.YQ.PrSVmEzsOLiTgzWfslar1L11W5nJXm8Vj0OFoGYUIjs
    const token = "eyJhbGciOiJIUzI1NiJ9.YQ.PrSVmEzsOLiTgzWfslar1L11W5nJXm8Vj0OFoGYUIjs";
    Jwt.verify(token, 'b', function(err, decoded) {
        console.log(decoded) // return a
    });
   return res.status(Stack.status.OK).send({
        message: 'hello world!'
    });
});

const stackType = 'default';
const stackData =  [
    {
        AppType: 'apis',
        AppPort: 8234,
        AppRoutes: Routes,
        AppPath: false,
 
    },
]
 
Stack.initializeApp({
    type: stackType, secure: false,
    servers: stackData
});
   

ALL STACK METHOD UTILITIES

METHODRETURN TYPESARGUMENT
initializeAppvoidObject config
createConnectionvoidObject connection
routervoidString route, callback(req, res)
removeFilevoidString filePath, String fileName
renameFilevoidString oldPath, String newPath, String fileName
uploadvoidObject { dest: './storages/' }
arrayToObjectobjectArrays
isEmptybooleanObject request body
getDirectoryStringString path

SETUP INITIALIZE APPLICATION

import Stack from '@snipkode/stack';

import ApiRoutes from 'path/to/routes/ApiRoutes';
import WebRoutes from 'path/to/routes/WebRoutes';

const stackType = process.env.NODE_ENV == 'production' ? 'extends' : 'default';
const stackData =  [
    {
        AppType: 'apis',
        AppPort: 8234,
        AppRoutes: ApiRoutes,
        AppPath: false,

    },
    {
        AppType: 'admin',
        AppPort: 9443,
        AppRoutes: WebRoutes,
        AppPath: '/admin',
    },
    {
        AppType: 'client',
        AppPort: 8000,
        AppRoutes: false,
        AppPath: '/clients',
    }
]

Stack.initializeApp({
    type: stackType, secure: true,
    servers: stackData
});

CREATE CONNECTION MODULE

import Stack from "@snipkode/stack";

const dataConnection = {
    admin: {
        name: 'stackadmin',
        user: 'root',
        pass: '',
        options: {
            host: 'localhost',
            dialect: 'mysql',
            timezone: '+07:00'
        }
    },
    client: {
        name: 'stackclient',
        user: 'root',
        pass: '',
        options: {
            host: 'localhost',
            dialect: 'mysql',
            timezone: '+07:00'
        }
    }
}
const connect = Stack.createConnection(dataConnection);

export default connect;

CREATE MODEL PATTERN MODULE

import connect from 'path/to/connection';
import Stack from "@snipkode/stack";

export const Users = connect.admin.define('users', {
    email: {
        type: Stack.schema.STRING()
    },
    password: {
        type: Stack.schema.CHAR(12)
    }
})

CREATE CONTROLLER CLASS MODULE

import { Users } from "path/to/UserModel";

export default class UsersController {
    static getUsers = async (req, res) => {
        try {
            const getUsers = await Users.findAll();
            if(!getUsers){
                res.status(404).send({
                    message: 'Tidak ditemukan data!'
                })
            }
            res.status(200).send({
                message: 'Data ditemukan!',
                data: getUsers
            })
        } catch (error) {
            res.status(500).send({
                message: error.message
            })
        }
    }
}

CREATE ROUTING STACK MODULE

import Stack from '@snipkode/stack'; 

import UsersController from 'path/to/controller/UsersController';

const ApiRouter = Stack.AppRouter();

ApiRouter.get('/', UsersController.getUsers);
ApiRouter.get('/users/:id', UsersController.updateUsers);

export default ApiRouter;
import { router } from '@snipkode/stack'; 

import UsersController from 'path/to/controller/UsersController';

const ApiRouter = router();

ApiRouter.get('/', UsersController.getUsers);
ApiRouter.get('/users/:id', UsersController.updateUsers);

export default ApiRouter;

FILE SYSTEM METHOD

    const ApiRouter = router();
    ApiRouter.get('/', upload({ dest: './storages/books/'}).single('thumb'), (req, res) => {
        console.log(req.file);
        const new_path_file = `${req.file.destination}/${req.file.filename}.${req.file.mimetype.split('/')[1]}`;
        const path_file_removed = `${req.file.destination}/a36094022797a4a1512133fcc1d57173.jpeg`;
        renameFile(req.file.path, new_file_path);
        removeFile(path_file_removed);
        res.send({
            message: 'hi'
        })
    });

HANDLE STATUS CODE RESPONSE

    import Stack, { status } from '@snipkode/stack';

    Stack.AppRouter.get('/', (req, res) => {
       return res.status(status.OK).send({
            message: 'hi'
        });
    });

STATUS CODE REFERENCE

    CONTINUE: "100",
    SWITCHING_PROTOCOLS: "101",
    PROCESSING: "102",
    OK: "200",
    CREATED: "201",
    ACCEPTED: "202",
    NON_AUTHORITATIVE_INFORMATION: "203",
    NO_CONTENT: "204",
    RESET_CONTENT: "205",
    PARTIAL_CONTENT: "206",
    MULTI_STATUS: "207",
    MULTIPLE_CHOICES: "300",
    MOVED_PERMANENTLY: "301",
    MOVED_TEMPORARILY: "302",
    SEE_OTHER: "303",
    NOT_MODIFIED: "304",
    USE_PROXY: "305",
    TEMPORARY_REDIRECT: "307",
    BAD_REQUEST: "400",
    UNAUTHORIZED: "401",
    PAYMENT_REQUIRED: "402",
    FORBIDDEN: "403",
    NOT_FOUND: "404",
    METHOD_NOT_ALLOWED: "405",
    NOT_ACCEPTABLE: "406",
    PROXY_AUTHENTICATION_REQUIRED: "407",
    REQUEST_TIME_OUT: "408",
    CONFLICT: "409",
    GONE: "410",
    LENGTH_REQUIRED: "411",
    PRECONDITION_FAILED: "412",
    REQUEST_ENTITY_TOO_LARGE: "413",
    REQUEST_URI_TOO_LARGE: "414",
    UNSUPPORTED_MEDIA_TYPE: "415",
    REQUESTED_RANGE_NOT_SATISFIABLE: "416",
    EXPECTATION_FAILED: "417",
    UNPROCESSABLE_ENTITY: "422",
    LOCKED: "423",
    FAILED_DEPENDENCY: "424",
    UNORDERED_COLLECTION: "425",
    UPGRADE_REQUIRED: "426",
    PRECONDITION_REQUIRED: "428",
    TOO_MANY_REQUESTS: "429",
    REQUEST_HEADER_FIELDS_TOO_LARGE: "431",
    INTERNAL_SERVER_ERROR: "500",
    NOT_IMPLEMENTED: "501",
    BAD_GATEWAY: "502",
    SERVICE_UNAVAILABLE: "503",
    GATEWAY_TIME_OUT: "504",
    STATUS_VERSION_NOT_SUPPORTED: "505",
    VARIANT_ALSO_NEGOTIATES: "506",
    INSUFFICIENT_STORAGE: "507",
    BANDWIDTH_LIMIT_EXCEEDED: "509",
    NOT_EXTENDED: "510",
    NETWORK_AUTHENTICATION_REQUIRED: "511"
5.1.4

1 year ago

4.9.6

2 years ago

4.9.5

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

4.9.3

2 years ago

4.9.0

2 years ago

4.9.2

2 years ago

4.9.1

2 years ago

4.8.9

2 years ago

4.8.8

2 years ago

4.8.5

2 years ago

4.8.4

2 years ago

4.8.7

2 years ago

4.8.6

2 years ago

4.8.1

2 years ago

4.8.3

2 years ago

4.4.8

2 years ago

4.4.5

2 years ago

4.4.4

2 years ago

4.4.7

2 years ago

4.4.6

2 years ago

4.4.3

2 years ago

4.4.2

2 years ago

4.4.1

2 years ago

4.4.0

2 years ago

4.3.9

2 years ago

4.3.8

2 years ago

4.3.7

2 years ago

4.3.6

2 years ago

4.3.2

2 years ago

4.3.1

2 years ago

4.3.0

2 years ago

4.2.9

2 years ago

4.2.8

2 years ago

4.2.7

2 years ago

4.2.6

2 years ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.5

2 years ago

2.0.1

2 years ago

2.0.4

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago