Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(onesignal): use User Model endpoints and tag schema

Rudy Celeklib41c28d0afbe
diff --git a/tests/onesignal-users.test.cjs b/tests/onesignal-users.test.cjs
new file mode 100644
index 0000000..7c749d7
--- /dev/null
+++ b/tests/onesignal-users.test.cjs
@@ -0,0 +1,39 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../tools/clis/onesignal.js')
+function run(args, network = false) {
+ const code = `global.fetch = async (url, options) => {
+ if (!${network}) throw new Error('Unexpected network request');
+ if (!url.startsWith('https://api.onesignal.com/apps/app123/users')) throw new Error('Unknown user route');
+ const body = options.body ? JSON.parse(options.body) : null;
+ if (body && body.tags) throw new Error('Tags must be nested in properties');
+ return { status: 200, text: async () => JSON.stringify({url, method: options.method, body}) };
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], {encoding: 'utf8', env: {...process.env, ONESIGNAL_REST_API_KEY: 'test-only-key', ONESIGNAL_APP_ID: 'app123'}})
+}
+test('create user reaches the User Model route and preserves segment tags', () => {
+ const r = run(['users', 'create', '--external-id', 'customer', '--tags', '{"plan":"pro"}'], true)
+ assert.equal(r.status, 0, r.stderr)
+ const response = JSON.parse(r.stdout)
+ assert.deepEqual(response.body.identity, { external_id: 'customer' })
+ assert.deepEqual(response.body.properties, { tags: { plan: 'pro' } })
+})
+for (const sub of ['get', 'delete']) test(`${sub} encodes complete alias ID as one path segment`, () => {
+ const r = run(['users', sub, '--alias-id', 'tenant/[email protected]', '--dry-run'])
+ assert.equal(r.status, 0, r.stderr)
+ assert.equal(JSON.parse(r.stdout).url, 'https://api.onesignal.com/apps/app123/users/by/external_id/tenant%2Fcustomer%2Btest%40example.com')
+})
+test('create preview masks authorization and uses the same schema', () => {
+ const r = run(['users', 'create', '--external-id', 'customer', '--tags', '{"plan":"pro"}', '--dry-run'])
+ const body = JSON.parse(r.stdout)
+ assert.equal(body.headers.Authorization, '***')
+ assert.deepEqual(body.body.properties.tags, {plan: 'pro'})
+})
+test('invalid tags and missing identity fail locally', () => {
+ for (const args of [['users', 'create', '--external-id', 'customer', '--tags', '[]'], ['users', 'create', '--tags', '{}']]) {
+ const r = run(args)
+ assert.ok(JSON.parse(r.stdout).error)
+ }
+})
diff --git a/tools/clis/onesignal.js b/tools/clis/onesignal.js
index 63431e1..c214318 100755
--- a/tools/clis/onesignal.js
+++ b/tools/clis/onesignal.js
@@ -152,7 +152,7 @@ async function main() {
const aliasLabel = args['alias-label'] || 'external_id'
const aliasId = args['alias-id']
if (!aliasId) { result = { error: '--alias-id required' }; break }
- result = await api('GET', `/api/v1/apps/${APP_ID}/users/by/${aliasLabel}/${aliasId}`)
+ result = await api('GET', `/apps/${APP_ID}/users/by/${encodeURIComponent(aliasLabel)}/${encodeURIComponent(aliasId)}`)
break
}
case 'create': {
@@ -164,16 +164,25 @@ async function main() {
payload.subscriptions = [{ type: 'Email', token: args.email }]
}
if (args.tags) {
- try { payload.tags = JSON.parse(args.tags) } catch { result = { error: 'Invalid --tags JSON' }; break }
+ try {
+ const tags = JSON.parse(args.tags)
+ if (!tags || typeof tags !== 'object' || Array.isArray(tags)) {
+ result = { error: '--tags must be a JSON object' }; break
+ }
+ payload.properties = { tags }
+ } catch { result = { error: 'Invalid --tags JSON' }; break }
+ }
+ if (!payload.identity && !payload.subscriptions) {
+ result = { error: '--external-id or --email required' }; break
}
- result = await api('POST', `/api/v1/apps/${APP_ID}/users`, payload)
+ result = await api('POST', `/apps/${APP_ID}/users`, payload)
break
}
case 'delete': {
const aliasLabel = args['alias-label'] || 'external_id'
const aliasId = args['alias-id']
if (!aliasId) { result = { error: '--alias-id required' }; break }
- result = await api('DELETE', `/api/v1/apps/${APP_ID}/users/by/${aliasLabel}/${aliasId}`)
+ result = await api('DELETE', `/apps/${APP_ID}/users/by/${encodeURIComponent(aliasLabel)}/${encodeURIComponent(aliasId)}`)
break
}
default:
diff --git a/tools/integrations/onesignal.md b/tools/integrations/onesignal.md
index 3b881e1..42a96eb 100644
--- a/tools/integrations/onesignal.md
+++ b/tools/integrations/onesignal.md
@@ -129,7 +129,7 @@ Headers:
### Get user by external ID
```bash
-GET https://api.onesignal.com/api/v1/apps/{APP_ID}/users/by/external_id/{external_id}
+GET https://api.onesignal.com/apps/{APP_ID}/users/by/external_id/{external_id}
Headers:
Authorization: Basic {REST_API_KEY}
@@ -138,7 +138,7 @@ Headers:
### Create a user
```bash
-POST https://api.onesignal.com/api/v1/apps/{APP_ID}/users
+POST https://api.onesignal.com/apps/{APP_ID}/users
Headers:
Authorization: Basic {REST_API_KEY}
@@ -149,7 +149,7 @@ Headers:
"subscriptions": [
{ "type": "Email", "token": "[email protected]" }
],
- "tags": { "plan": "pro", "signup_source": "organic" }
+ "properties": { "tags": { "plan": "pro", "signup_source": "organic" } }
}
```