@janiscommerce/apps-helpers
v2.2.0
Published
Helpers for janis apps
Downloads
861
Maintainers
Readme
Apps helpers
This package provides utilities for use in Janis apps.
📦 Installation
npm install @janiscommerce/apps-helpersUtils
Members
Functions
isEqualObj ⇒ boolean
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 }] }); // falseisValidIsoString ⇒ boolean
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]) ⇒
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
Kind: global function
Returns: string -
| 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
Kind: global function
Example
import { generateRandomId } from '@janiscommerce/apps-helpers';
generateRandomId(); // '6kj4nk9c5so'getHeaders([params], [deviceDataHeaders], [customHeaders]) ⇒ object
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
Kind: global function
| Param | Type | | ----- | ------------------ | | arr | array |
Example
import { isArray } from '@janiscommerce/apps-helpers';
isArray(['Janis']); // trueisBoolean(fn) ⇒ bool
Kind: global function
| Param | Type | | ----- | -------------------- | | fn | boolean |
Example
import {isBoolean} from '@janiscommerce/apps-helpers'
isBoolean((true) // trueisEmptyArray(arr) ⇒ bool
Kind: global function
| Param | Type | | ----- | ------------------ | | arr | array |
Example
import { isEmptyArray } from '@janiscommerce/apps-helpers';
isEmptyArray(['Janis']); // falseisEmptyObject(obj) ⇒ bool
Kind: global function
| Param | Type | | ----- | ------------------- | | obj | object |
Example
import { isEmptyObject } from '@janiscommerce/apps-helpers';
isEmptyObject('Janis'); // falseisFunction(fn) ⇒ bool
Kind: global function
| Param | Type | | ----- | --------------------- | | fn | function |
Example
import { isFunction } from '@janiscommerce/apps-helpers';
isFunction(() => true); // trueisJSON(param) ⇒ bool
Kind: global function
| Param | Type | | ----- | ------------------- | | param | string |
Example
import { isJSON } from '@janiscommerce/apps-helpers';
isJSON('{"key": "value"}'); // trueisObject(obj) ⇒ bool
Kind: global function
| Param | Type | | ----- | ------------------- | | obj | object |
Example
import { isObject } from '@janiscommerce/apps-helpers';
isObject('Janis'); // falseisRequired(param) ⇒ Error
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
Kind: global function
| Param | Type | Description | | ----- | ------------------- | -------------------------- | | str | string | String to validate. |
Example
import { isString } from '@janiscommerce/apps-helpers';
isString('Janis'); // trueisValidFormatPosition(position) ⇒ boolean
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({}); // falseisValidUrl(str) ⇒ boolean
Kind: global function
Returns: boolean - true or false
| Param | Type | | ----- | ------------------- | | str | string |
Example
isValidUrl('www.valid-url.com'); // trueExample
isValidUrl('invalid-url.com'); // falseExample
isValidUrl(); // falsepromiseWrapper(fn) ⇒ array.<data, error>
Kind: global function
| Param | Type | | ----- | --------------------- | | fn | function |
Example
import { promiseWrapper } from '@janiscommerce/apps-helpers';
const [data, error] = await promiseWrapper(promise());