DEV Community

Nicolaas Nel
Nicolaas Nel

Posted on

Node-Essentials V2 πŸ“¦

Intro πŸ±β€πŸ’»

So first off, I just want to thank everyone that used my first and only NPM package I've ever created.

I pretty much abandoned the project after getting more experienced these past few years but today I decided I wanted to update this package and make it a little bit more useful.

How the original project came to be:

Late last night I decided to finally publish my very own npm package, it's something I've wanted to do for a while now and I could never decide what to package.

While brainstorming I was busy working on a node.js app to process some data, the same code I've used in several projects.

This was it, the perfect package. I don't like searching for stuff I've already used and will use again in the future so I decided to package my most used node functions into a lightweight npm package.

Link to the old version and legacy post: Node-Essentials

About Node-Essentials πŸ±β€πŸ

This is a nodejs toolkit for doing asynchronous REST API calls, writing files to the system, starting up express-js servers and more coming soon.

Setup 🚧

Run:

npm install node-essentials
Enter fullscreen mode Exit fullscreen mode

And include it in your app:

const node = require('node-essentials');
Enter fullscreen mode Exit fullscreen mode

or

import * as node from 'node-essentials';
Enter fullscreen mode Exit fullscreen mode

Current Methods βš™

  • http - REST Requests.

    • get - Simple REST "GET" request.
    • post - Simple REST "POST" request.
    • put - Simple REST "PUT" request.
  • helpers - Helper functions.

  • server - Express server.

    • start - Set-up a express-js server on the specified port and directory.
  • fileManager - Manage files.

    • writeToFile - Writes anything passed through to storage as any file.

Quickstart

Simple Get

Want to quickly and easily retrieve data from an API?

executeGet(url: string, options?: any): Promise<any>
Enter fullscreen mode Exit fullscreen mode

Implementation:

import http from 'node-essentials';

async function getData() {
    const data = await http.executeGet("https://my-json-server.typicode.com/typicode/demo/db").then();
    console.log(data); 
}
Enter fullscreen mode Exit fullscreen mode

Response:

{
  posts: [
    { id: 1, title: 'Post 1' },
    { id: 2, title: 'Post 2' },
    { id: 3, title: 'Post 3' }
  ],
  comments: [
    { id: 1, body: 'some comment', postId: 1 },
    { id: 2, body: 'some comment', postId: 1 }
  ],
  profile: { name: 'typicode' }
}
Enter fullscreen mode Exit fullscreen mode

Distinct

Want to easily return distinct values from an array?

distinct(array: Array[any]);
Enter fullscreen mode Exit fullscreen mode

Implementation:

import helpers from 'node-essentials';

const nonDistinct = [1,1,1,2,3];

function getDistinct(){
    const distinct = helpers.distinct(nonDistinct);
    console.log(distinct);
}
Enter fullscreen mode Exit fullscreen mode

Response:

[1,2,3]
Enter fullscreen mode Exit fullscreen mode

Write To File

Want to write files to storage with data?

writeToFile(folder: string, fileName: string, extension: string, data: any);
Enter fullscreen mode Exit fullscreen mode

Implementation:

import fileManager from 'node-essentials';

const data = {
    sample: "Sample Data"
}

fileManager.writeToFile("./", "fileName", "json", JSON.stringify(data));
Enter fullscreen mode Exit fullscreen mode

Conclusion 🏁

The package contains functions I commonly use, I will be adding more simplified essential tools/functions as time goes on!

It's been an interesting journey making my first package and any feedback/suggestions of functions to add would be welcomed.

Where to get the package

Author

Node Essentials is Developed and Maintained by Nicolaas Nel

Facebook Twitter LinkedIn YouTube DEV


Made with πŸ’™ and β˜• by Nicolaas Nel.

Top comments (0)