Skip to content

janis-commerce/apps-helpers

Repository files navigation

Apps helpers

Build Status Coverage Status npm version

This package provides utilities for use in Janis apps.

📦 Installation

npm install @janiscommerce/apps-helpers

Utils

Members

isEqualObjboolean

check if two objects are exactly the same

isValidIsoStringboolean

validate if a ISO string is valid or not

Functions

debounce(fn, [wait])

The function debounce is a function that takes two arguments, a function and a number. The function debounce returns a function that will execute the function passed to it after the number of milliseconds passed to it

formatIsoToDate(params)string

it is a function that receives an ISO string and formats it

generateRandomId()string

returns a random combination of letters and/or numbers

getHeaders([params], [deviceDataHeaders], [customHeaders])object

get correct headers for janis api

isArray(arr)bool

return true or false if arg is a valid array

isBoolean(fn)bool

return true or false if arg is a valid boolean

isEmptyArray(arr)bool

return true or false if arg is a valid array and has length

isEmptyObject(obj)bool

return true or false if arg is a valid object and has or not keys

isFunction(fn)bool

return true or false if arg is a valid function

isJSON(param)bool

return true or false if arg is a valid JSON

isObject(obj)bool

return true or false if arg is a valid object

isRequired(param)Error

throw error with required param

isString(str)bool

If the type of the argument is a string, return true, otherwise return false.

isValidFormatPosition(position)boolean

If the position has the valid format required it will return true, otherwise return false. For a position to be valid, you need a positionKey: string or a positionId: string

isValidUrl(str)boolean

return true if the argument is a valid url

promiseWrapper(fn)array.<data, error>

wrapper to execute promise and return tuple with data and error

isEqualObj ⇒ boolean

check if two objects are exactly the same

Kind: global variable
Returns: boolean -

Returns true if the objects are equivalent, else false.

Param Type Description
object Object

The object to compare.

other Object

The other object to compare.

Example

// eslint-disable-next-line no-restricted-syntax
import { isEqualObj } from '@janiscommerce/apps-helpers';
isEqualObj({ items: [{ id: 123 }] }, { items: [{ id: 123, ean: 456 }] }); // false

isValidIsoString ⇒ boolean

validate if a ISO string is valid or not

Kind: global variable
Returns: boolean -

returns true if the date passed as an argument is valid

Param Type Description
date Date

date in ISO format

debounce(fn, [wait]) ⇒

The function debounce is a function that takes two arguments, a function and a number. The function debounce returns a function that will execute the function passed to it after the number of milliseconds passed to it

Kind: global function
Returns:

A function that will be called after the specified wait time.

Param Default Description
fn

The function to be debounced.

[wait] 300

The number of milliseconds to wait before executing the function.

formatIsoToDate(params) ⇒ string

it is a function that receives an ISO string and formats it

Kind: global function
Returns: string -

  • returns an formated date. Example: 18/05/23
Param Type Description
params Object

param

params.date Date

iso format. Example: '2022-02-03T17:29:28.637Z'

params.locale String

iso format. Example: 'es-Ar'

params.customFormat String

iso format. Example: 'pp'

generateRandomId() ⇒ string

returns a random combination of letters and/or numbers

Kind: global function
Example

import { generateRandomId } from '@janiscommerce/apps-helpers';
generateRandomId(); // '6kj4nk9c5so'

getHeaders([params], [deviceDataHeaders], [customHeaders]) ⇒ object

get correct headers for janis api

Kind: global function

Param Type Default Description
[params] object {}

object with params

[deviceDataHeaders] object {}

headers with the device info

[customHeaders] object {}

extra custom headers

params.client string

client name for janis api

params.accessToken string

access token for janis api

params.page number

number of page

params.pageSize number

quantity per page

params.getTotals boolean

request api totals

params.getOnlyTotals boolean

request api totals without body response

Example

const params = {
	client: 'my-client',
	accessToken: 'my-access-token',
	page: 1,
	pageSize: 10,
	getTotals: true,
	getOnlyTotals: false
};
const deviceDataHeaders = {
	'janis-app-name': 'MyApp',
	'janis-app-version': '1.0.0',
	'janis-app-device-os-name': 'iOS',
	'janis-app-device-os-version': '14.5',
	'janis-app-device-name': 'iPhone 12',
	'janis-app-device-id': '123456789'
};
const customHeaders = {
	'custom-header': 'custom-value'
};
const headers = getHeaders(params, deviceDataHeaders, customHeaders);
// {
//   'content-Type': 'application/json',
//   'janis-api-key': 'Bearer',
//   'janis-client': 'my-client',
//   'janis-api-secret': 'my-access-token',
//   'x-janis-page': 1,
//   'x-janis-page-size': 10,
//   'x-janis-totals': true,
//   'x-janis-only-totals': false,
//   'user-agent': 'MyApp/1.0.0 (iOS 14.5; iPhone 12; 123456789)',
//   'custom-header': 'custom-value'
// }

isArray(arr) ⇒ bool

return true or false if arg is a valid array

Kind: global function

Param Type
arr array

Example

import { isArray } from '@janiscommerce/apps-helpers';
isArray(['Janis']); // true

isBoolean(fn) ⇒ bool

return true or false if arg is a valid boolean

Kind: global function

Param Type
fn boolean

Example

import {isBoolean} from '@janiscommerce/apps-helpers'
isBoolean((true) // true

isEmptyArray(arr) ⇒ bool

return true or false if arg is a valid array and has length

Kind: global function

Param Type
arr array

Example

import { isEmptyArray } from '@janiscommerce/apps-helpers';
isEmptyArray(['Janis']); // false

isEmptyObject(obj) ⇒ bool

return true or false if arg is a valid object and has or not keys

Kind: global function

Param Type
obj object

Example

import { isEmptyObject } from '@janiscommerce/apps-helpers';
isEmptyObject('Janis'); // false

isFunction(fn) ⇒ bool

return true or false if arg is a valid function

Kind: global function

Param Type
fn function

Example

import { isFunction } from '@janiscommerce/apps-helpers';
isFunction(() => true); // true

isJSON(param) ⇒ bool

return true or false if arg is a valid JSON

Kind: global function

Param Type
param string

Example

import { isJSON } from '@janiscommerce/apps-helpers';
isJSON('{"key": "value"}'); // true

isObject(obj) ⇒ bool

return true or false if arg is a valid object

Kind: global function

Param Type
obj object

Example

import { isObject } from '@janiscommerce/apps-helpers';
isObject('Janis'); // false

isRequired(param) ⇒ Error

throw error with required param

Kind: global function

Param Type Description
param string

name of the param that is required

Example

import { isRequired } from '@janiscommerce/apps-helpers';
const promise = async (arg = isRequired('arg')) => {
	return arg;
};

promise(); // Error: 'arg is required'

isString(str) ⇒ bool

If the type of the argument is a string, return true, otherwise return false.

Kind: global function

Param Type Description
str string

String to validate.

Example

import { isString } from '@janiscommerce/apps-helpers';
isString('Janis'); // true

isValidFormatPosition(position) ⇒ boolean

If the position has the valid format required it will return true, otherwise return false. For a position to be valid, you need a positionKey: string or a positionId: string

Kind: global function

Param Type Description
position object

position object

position.positionKey string

positionKey

position.positionId string

positionId

Example

import { isValidFormatPosition } from '@janiscommerce/apps-helpers';
isValidFormatPosition({ positionKey: '001-D-01-1', positionId: '632b40c90adf68f197caa91f' }); // true
isValidFormatPosition({ positionKey: '001-D-01-1' }); // true
isValidFormatPosition({ positionId: '632b40c90adf68f197caa91f' }); // true
isValidFormatPosition({ positionKey: 123 }); // false
isValidFormatPosition({}); // false

isValidUrl(str) ⇒ boolean

return true if the argument is a valid url

Kind: global function
Returns: boolean -

true or false

Param Type
str string

Example

isValidUrl('www.valid-url.com'); // true

Example

isValidUrl('invalid-url.com'); // false

Example

isValidUrl(); // false

promiseWrapper(fn) ⇒ array.<data, error>

wrapper to execute promise and return tuple with data and error

Kind: global function

Param Type
fn function

Example

import { promiseWrapper } from '@janiscommerce/apps-helpers';
const [data, error] = await promiseWrapper(promise());

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors