npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

onesignal-api-client-core

v1.2.2

Published

Typescript client for OneSignal Server Rest API

Downloads

3,247

Readme

OneSignal API Client

npm version

Features

  • Written in Typescript.
  • The code is covered by tests.
  • The library uses Axios.
  • Two clients for different method calls: OneSignalAppClient, OneSignalUserClient. OneSignalAppClient is created for notifications, devices, tracks or sessions. OneSignalUserClient is created for apps.
  • Support create a notification builder. You may target users in one of three ways using this builders: by NotificationBySegmentBuilder, by NotificationByFilterBuilder, or by NotificationByDeviceBuilder.
  • Support methods for different contents: Notification or Email. Support methods is available in the builders.

Getting Started

Npm

npm install onesignal-api-client-core

Yarn

yarn add onesignal-api-client-core

Using

Create notification

The Create Notification method is used when you want your server to programmatically send notifications or emails to a segment or individual users. You may target users in one of three ways using this method: by Segment, by Filter, or by Device. At least one targeting parameter must be specified.

Create notification through the Segment

API Reference Segments are the most common way developers send notifications via OneSignal. Sending to segments is easy: you simply specify which segments you want to send to, and, optionally, which ones you don't.

import { OneSignalAppClient, NotificationBySegmentBuilder } from 'onesignal-api-client-core';

const client = new OneSignalAppClient('appId', 'restApiKey');

const input = new NotificationBySegmentBuilder()
    .setIncludedSegments(['Active Users', 'Inactive Users'])
    .notification() // .email()
    .setContents({ en: 'My Message' })
    .build();

const result = await client.createNotification(input);

Create notification through the Filter

API Reference Filters are a powerful way to target users, allowing you to use both data that OneSignal has about a user and any Tags your app may send OneSignal. Filters can be combined together to form advanced, highly precise user targeting. OneSignal customers use all sorts of filters to send notifications, including language, location, user activity, and more.

import { OneSignalAppClient, NotificationByFilterBuilder } from 'onesignal-api-client-core';

const client = new OneSignalAppClient('appId', 'restApiKey');

const filters = [
    { 'field': 'tag', 'key': 'level', 'relation': '>', 'value': '10' },
    { 'field': 'amount_spent', 'relation': '>', 'value': '0' },
];
const input = new NotificationByFilterBuilder()
    .setFilters(filters)
    .notification() // .email()
    .setContents({ en: 'My Message' })
    .build();

const result = await client.createNotification(input);

Create notification through the Device

API Reference You may also target specific devices with the create notification method. Targeting devices is typically used in two ways:

  • For notifications that target individual users, such as if they've received a message from someone.
  • For apps that wish to manage their own segments, such as tracking a user's followers and sending notifications to them when that user posts.
import { OneSignalAppClient, NotificationByDeviceBuilder } from 'onesignal-api-client-core';

const client = new OneSignalAppClient('appId', 'restApiKey');

const input = new NotificationByDeviceBuilder()
    .setIncludeExternalUserIds(['externalUserId1', 'externalUserId2'])
    .notification() // .email()
    .setContents({ en: 'My Message' })
    .build();

const result = await client.createNotification(input);

Cancel notification

API Reference Stop a scheduled or currently outgoing notification.

import { OneSignalAppClient, ICancelNotificationInput } from 'onesignal-api-client-core';

const client = new OneSignalAppClient('appId', 'restApiKey');

const input = { id: notification.id } as ICancelNotificationInput;
const result = await client.cancelNotification(input);

Create, View or Update a Device

API Reference

import { OneSignalAppClient, ICreateDeviceInput, IViewDeviceInput, IUpdateDeviceInput, DeviceType } from 'onesignal-api-client-core';

const client = new OneSignalAppClient('appId', 'restApiKey');

const createInput = { device_type: DeviceType.ChromeWebPush } as ICreateDeviceInput;
const createResult = await client.createDevice(input);

const viewInput = { id: device.id } as IViewDeviceInput;
const viewResult = await client.viewDevice(viewInput);
const viewsResult = await client.viewDevices();

const updateInput = { id: device.id, device_type: DeviceType.Android } as IUpdateDeviceInput;
const updateResult = await client.updateDevice(input);

Create, View or Update an OneSignal App

API Reference

import { OneSignalUserClient, ICreateAppInput, IViewAppInput, IUpdateAppInput } from 'onesignal-api-client-core';

const client = new OneSignalUserClient('userAuthKey');

const createInput = { name } as ICreateAppInput;
const createResult = await client.createApp(input);

const viewInput = { id: app.id } as IViewAppInput;
const viewResult = await client.viewApp(viewInput);
const viewsResult = await client.viewApps();

const updateInput = { id: app.id, name: 'Updated name' } as IUpdateAppInput;
const updateResult = await client.updateApp(input);

Track open

API Reference

import { OneSignalAppClient, ITrackOpenInput } from 'onesignal-api-client-core';

const client = new OneSignalAppClient('appId', 'restApiKey');

const trackOpenInput = { notificationId: notification.id, opened: true } as ITrackOpenInput;
const trackOpenResult = await client.trackOpen(input);

New Session

API Reference

import { OneSignalAppClient, INewSessionInput } from 'onesignal-api-client-core';

const client = new OneSignalAppClient('appId', 'restApiKey');

const newSessionInput = { deviceId: device.id } as INewSessionInput;
const newSessionResult = await client.newSession(input);

OneSignal server implementation methods

List of server api methods.

Support

If any bugs are found in the API wrapper, please open an issue on GitHub, or a Pull Request if you want to fix it yourself! Please be as explicit as possible and provide a minimum reproducing repository if at all possible, as it helps track down what went wrong.

Documentation

All documentation for this wrapper comes from the Server Rest API, if there are any typos, please let me know or open a PR to fix it.

TODO

  • Implements all methods

Licence

MIT