1.1.3 • Published 10 months ago

sms-href v1.1.3

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







Installation

ManagerCommand
npm install sms-href
yarn add sms-href
pnpm add sms-href

DEV runners

runneroutputdescription
npm run build./lib/**/*Transpile from Typescript to Javascript
npm run build:pack./sms-href-{package-json-version}.tgzCreate NPM package file (*.tgz)
npm run demo./demo/dist/demo.jsBuild demo and measure minimal build size

Basic usage (without catching outputs)

import {SmsHref} from "sms-href";

const smsHref: SmsHref = new SmsHref();

// As promise
smsHref.fixAll().catch();

// OR sync
try {
    await smsHref.fixAll();
} catch (e) {
}

Tested platforms

AndroidiOSiPadOS
10 (Quince Tart)66
12 (Snow Cone)88
13 (Tiramisu)1212
1515
1616
17 (beta)17 (beta)

API

Instance

Syntax:

new SmsHref( [ options: Options ] )

Instance of SmsHref

Example:

const smsHref: SmsHref = new SmsHref();

Constructor parameters:

typedefaultdescription
optionsOptionsConfiguration of custom separator and allowed devices - Show detail
options.allowDevicesList of enable/disable devices - Show detail
options.allow.mobile = true booleantrueEnable/disable href update for mobile devices
options.allow.tablet = true booleantrueEnable/disable href update for tablet devices
options.allow.facebook = true booleantrueEnable/disable href update for Facebook app web view
options.separator = null string nullnullUser defined SMS body (body=) separator. Read more NOTE: Internal platform detection and allowed devices list will be disabled.
options.encode = true booleantrueEnable/disable message text encoding globally ( e.g., encodeURIComponent )NOTE: Methods fixValue() and create() have their own parameter for enable/disable encoding and this global parameter can be overridden.

Example (disable tablets and facebook app web view):

const smsHref: SmsHref = new SmsHref({
    allow: {
        facebook: false,
        tablet: false
    }
});

Custom separator definition

If you want to define your own separator, you can use existed separator constants

constant namevaluetypedescription
ANDROID_SEPARATOR?stringBody separator ?body= for Android platform
IOS_7_AND_LOWER_SEPARATOR;stringBody separator ;body= for IOS <= 7 platform
IOS_8_AND_HIGHER_SEPARATOR&stringBody separator &body= for IOS >= 8 platform
MIN_IOS_VERSION7numberValue is 7 - because iOS7 allegedly doesn't support sms: href links.

NOTE: IOS 7 allegedly does not support sms: protocol.

Example:

import {
    SmsHref,
    MIN_IOS_VERSION,
    ANDROID_SEPARATOR,
    IOS_7_AND_LOWER_SEPARATOR,
    IOS_8_AND_HIGHER_SEPARATOR
} from "sms-href";

// Custom
function customSeparatorMechanism(): string | null {
    const platform: CustomPlatformDetection = new CustomPlatformDetection();

    if (platform.name === 'Android')
        return ANDROID_SEPARATOR;

    if (platform.name === 'IOS') {
        if (platform.version.major <= MIN_IOS_VERSION)
            return IOS_7_AND_LOWER_SEPARATOR
        return IOS_8_AND_HIGHER_SEPARATOR;
    }

    return null;
}

// Instance
const smsHref: SmsHref = new SmsHref({
    separator: customSeparatorMechanism()
});

fixAll

Syntax:

async fixAll( [ context: Element | HTMLElement | Document ] ) : Promise<ResultCode>

Finds and update all anchor links with sms: protocol value by current platform in set DOM context.

typedefaultdescription
context = document Element HTMLElement DocumentdocumentDefines parent DOM node for search
returnsPromise<ResultCode>
throwsPromise.catch<ResultCode>

Example:

await smsHref.fixAll();

Example with custom defined context:

const context: HTMLElement = document.querySelector('.context-element');

await smsHref.fixAll(context);

Example with using resultCode in Promise<ResultCode>.

import {
    SmsHref,
    ResultCode,
    CODE_SUCCESS,
    CODE_NOT_FOUND,
    CODE_UNSUPPORTED_OS
} from "sms-href";

const smsHref: SmsHref = new SmsHref();

smsHref.fixAll()
    .then((resultCode: ResultCode): void => {
        if (resultCode === CODE_SUCCESS)
            console.log(`All sms: href values in anchors was updated`);
    })
    .catch((resultCode: any): void => {
        if (resultCode === CODE_NOT_FOUND)
            console.log(`'Anchors with sms: href value doesn't exist'`);
        else if (resultCode === CODE_UNSUPPORTED_OS)
            console.log(`Current platform doesn't support sms: href protocol`);
    });

ResultCode constants list:

constant namevaluetypestatusdescription
CODE_SUCCESS200numberOkAll sms: href values in anchors was updated
CODE_NOT_FOUND404numberNot foundAnchors with sms: href value doesn't exist
CODE_UNSUPPORTED_OS501numberNot ImplementedCurrent platform doesn't support sms: href protocol

fixValue

Syntax:

async fixValue( smsValue: string [, encode: boolean ] ) : Promise<string>

Update input string value by current platform.

typedefaultdescription
smsValuestringInput string for update
encodebooleanConstructor options.encode valueEnable/disable message text encoding ( e.g., encodeURIComponent )
returnsPromise<string>Valid SMS Href sms:anchor string

Example:

await smsHref.fixValue('1234@body=Your message');
// Android  --> sms:1234?body=Your%20message
// IOS <= 7 --> sms:1234;body=Your%20message
// IOS >= 8 --> sms:1234&body=Your%20message

Example (with disabled message encoding):

await smsHref.fixValue('1234@body=Your message', false);
// Android  --> sms:1234?body=Your message
// IOS <= 7 --> sms:1234;body=Your message
// IOS >= 8 --> sms:1234&body=Your message

create

Syntax:

async create( smsConfiguration: SmsConfiguration [, encode: boolean] ) : Promise<string>

Creates sms: href string from phone number and sms message text.

typedefaultdescription
smsConfigurationSmsConfiguration
smsConfiguration[.phone]string numberSMS Phone number
smsConfiguration[.message]stringSMS message text
encode booleanConstructor options.encode valueEnable/disable message text encoding ( e.g., encodeURIComponent )
returnsPromise<string> - Valid SMS Href sms:anchor string
throwsPromise.reject<TypeError>If phone and message are both not provided

NOTES:

  • Phone and message are both optional, but one of them must be provided.
  • Phone number format validation is not implemented. You should use own validation.
  • SMS message format validation is not implemented. You should use own validation.

Example:

await smsHref.create({
    phone: 1234,
    message: 'Your message'
});
// Android  --> sms:1234?body=Your%20message
// IOS <= 7 --> sms:1234;body=Your%20message
// IOS >= 8 --> sms:1234&body=Your%20message

Example (with disabled message encoding):

await smsHref.create({
    phone: 1234,
    message: 'Your message'
}, false);
// Android  --> sms:1234?body=Your message
// IOS <= 7 --> sms:1234;body=Your message
// IOS >= 8 --> sms:1234&body=Your message



Types

ResultCode

type ResultCode = number;

Options

type Options = {
    allow?: Devices;
    separator?: string | null;
    encode?: boolean;
}

Devices

type Devices = {
    mobile?: boolean;
    tablet?: boolean;
    facebook?: boolean;
};

SmsConfiguration

type SmsConfiguration = {
    phone?: string | number;
    message?: string;
}



License

MIT © Dárius Bokor


References