0.2.8 • Published 10 months ago

picos-util v0.2.8

Weekly downloads
95
License
MIT
Repository
github
Last release
10 months ago

picos-util

This is a JavaScript module that provides utility functions for making HTTP requests, handling data compression, and managing environment variables. It utilizes the zlib, http, https, fs, querystring, and pico-common libraries.

Installation

To use this module, you need to have Node.js installed. You can install the required dependencies by running the following command:

npm i picos-util

Usage

To use this module in your JavaScript code, require it using require('http-utils'). The module exports the following functions:

zip(str, cb)

This function compresses a string using zlib's deflateRaw method and returns the compressed data as a Base64-encoded string.

  • str (string): The string to be compressed.
  • cb (function): The callback function that will be called with the compressed data or an error.

unzip(str, cb)

This function decompresses a Base64-encoded string using zlib's inflateRaw method and returns the decompressed data as a string.

  • str (string): The Base64-encoded string to be decompressed.
  • cb (function): The callback function that will be called with the decompressed data or an error.

ajax(method, href, params, opt, cb, userData)

This function sends an HTTP request to the specified URL with the given parameters and options.

  • method (string): The HTTP method to be used (get, post, put, delete, patch).
  • href (string): The URL path.
  • params (object or array): The parameters to be sent with the request. If it is an array, the objects will be merged, and overlapping keys will be overridden by the later object.
  • opt (object, optional): Additional options for the HTTP request. See the Node.js documentation for available options.
  • cb (function): The callback function that will be called with the response or an error.
  • userData (object, optional): Optional user data that will be passed to the callback function.

env(obj)

This function sets environment variables based on the provided object.

  • obj (object): An object containing key-value pairs representing environment variable names and their corresponding values.

Example

Here's an example of how to use this module:

const pu = require('picos-utils')

pu.zip('Hello, World!', (err, compressedData) => {
  if (err) {
    console.error('Compression error:', err)
    return
  }

  console.log('Compressed data:', compressedData)

  pu.unzip(compressedData, (err, decompressedData) => {
    if (err) {
      console.error('Decompression error:', err)
      return
    }

    console.log('Decompressed data:', decompressedData)
  })
})

pu.ajax('get', 'https://api.example.com/users', null, {}, (err, state, resBody, res, userData) => {
  if (4 === state) return // support response streaming
  if (err) {
    console.error('Request error:', err)
    return
  }

  console.log('Response body:', resBody)
})

Note: Make sure to replace 'https://api.example.com/users' with the actual API endpoint you want to request.

License

This module is licensed under the MIT License. See the LICENSE file for more information.

0.2.8

10 months ago

0.2.7

1 year ago

0.2.6

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.4

5 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.0

7 years ago