Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"packages": [
"packages/*"
],
"version": "independent",
"npmClient": "yarn",
"useWorkspaces": true
"npmClient": "pnpm"
}
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
"name": "userlist-javascript",
"private": true,
"devDependencies": {
"lerna": "^3.20.2"
"lerna": "^8.0.0"
},
"scripts": {
"build": "lerna run build --stream --concurrency 1",
"test": "lerna run test --stream --concurrency 1",
"lint": "lerna run lint --stream --concurrency 1"
},
"workspaces": [
"packages/*"
],
"resolutions": {
"minimist": "^1.2.2"
}
Expand Down
40 changes: 29 additions & 11 deletions packages/push/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ USERLIST_PUSH_KEY=401e5c498be718c0a38b7da7f1ce5b409c56132a49246c435ee296e07bf2be
Configuration via an constructor:

```javascript
var Userlist = require('@userlist/push');
var Userlist = require("@userlist/push");

var userlist = new Userlist({ pushKey: '401e5c498be718c0a38b7da7f1ce5b409c56132a49246c435ee296e07bf2be39' });
var userlist = new Userlist({
pushKey: "401e5c498be718c0a38b7da7f1ce5b409c56132a49246c435ee296e07bf2be39",
});
```

## Usage

### Tracking Users

To manually send user data into Userlist, use the `userlist.users.create` method.
To manually send user data into Userlist, use the `userlist.users.create` or `userlist.users.push` method.

```javascript
var userlist = new Userlist();
Expand All @@ -54,15 +56,32 @@ userlist.users.create({
email: user.email,
properties: {
first_name: user.first_name,
last_name: user.last_name
}
last_name: user.last_name,
},
});
```

To manually update a user data into Userlist, use the `userlist.users.update` method. Properties that aren't present in the payload are ignored and remain unchanged.

```javascript
var userlist = new Userlist();

userlist.users.update({
identifier: user.id,
properties: {
last_name: user.last_name,
},
});
```

It's also possible to delete a user from Userlist, using the `userlist.users.delete` method.

```javascript
userlist.users.delete(user.id)
// With an identifier
userlist.users.delete(user.id);

// With an object
userlist.users.delete({ identifier: user.id });
```

### Tracking Events
Expand All @@ -73,15 +92,14 @@ To track custom events use the `userlist.events.create` method.
var userlist = new Userlist();

userlist.events.create({
name: 'project_created',
name: "project_created",
user: user.id,
properties: {
project_name: project.name
}
})
project_name: project.name,
},
});
```


## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/userlist/userlist-javascript. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
Expand Down
4 changes: 2 additions & 2 deletions packages/push/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"lint": "eslint src test",
"build": "rollup -c",
"test": "mocha --require @babel/register --recursive --colors",
"test:watch": "yarn test --watch",
"prepublish": "yarn lint && yarn test && yarn build"
"test:watch": "pnpm test --watch",
"prepublish": "pnpm lint && pnpm test && pnpm build"
},
"bugs": {
"url": "https://github.com/userlistio/userlist-javascript/issues"
Expand Down
24 changes: 19 additions & 5 deletions packages/push/src/relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ export default class Relation {
this.resource = resource;
}

create(attributes) {
let resource = new this.resource(attributes);
push(attributes) {
return this.client.post(this.endpoint, this.normalize(attributes));
}

create() {
return this.push(...arguments);
}

return this.client.post(this.endpoint, resource);
update() {
return this.push(...arguments);
}

delete(identifier) {
return this.client.delete(`${this.endpoint}/${identifier}`);
delete(attributes) {
return this.client.delete(this.endpoint, this.normalize(attributes));
}

normalize(attributes) {
Comment thread
benedikt marked this conversation as resolved.
Outdated
if (typeof attributes === "string") {
attributes = { identifier: attributes };
}

return new this.resource(attributes);
}

get client() {
Expand Down
119 changes: 99 additions & 20 deletions packages/push/test/relation.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,131 @@
import { expect, use } from 'chai';
import { createStubInstance } from 'sinon';
import sinonChai from 'sinon-chai';
import { expect, use } from "chai";
import { createStubInstance } from "sinon";
import sinonChai from "sinon-chai";

use(sinonChai);

import Relation from '../src/relation';
import User from '../src/user';
import Client from '../src/client';
import Relation from "../src/relation";
import User from "../src/user";
import Client from "../src/client";

describe('Relation', function() {
describe("Relation", function () {
let resource = User;
let scope = {};

let relation;
let client;

beforeEach(function() {
beforeEach(function () {
client = createStubInstance(Client);
scope.client = client;

relation = new Relation(scope, resource);
});

it('should throw the resource\'s validation errors', function() {
expect(function() {
it("should throw the resource's validation errors", function () {
expect(function () {
relation.create();
}).to.throw('Missing required attributes object')
}).to.throw("Missing required attributes object");
});

describe('#create', function() {
describe("#create", function () {
let client = createStubInstance(Client);

beforeEach(function() {
beforeEach(function () {
scope.client = client;
});

it('should post the resource using the scope\'s client', function () {
relation.create({ identifier: '1' });
it("should post the resource using the scope's client", function () {
relation.create({
identifier: "1",
name: "John",
email: "john@userlist.com",
properties: {
foo: "bar",
bar: "baz",
},
});

expect(client.post).to.be.calledWithExactly('/users', new User({ identifier: '1' }));
expect(client.post).to.be.calledWithExactly(
"/users",
new User({
identifier: "1",
name: "John",
email: "john@userlist.com",
properties: {
foo: "bar",
bar: "baz",
},
})
);
});
});

describe('#delete', function() {
it('should delete the resource using the scope\'s client', function () {
relation.delete('1');
describe("#push", function () {
let client = createStubInstance(Client);

beforeEach(function () {
scope.client = client;
});

it("should post the resource using the scope's client", function () {
relation.push({ identifier: "1" });

expect(client.post).to.be.calledWithExactly(
"/users",
new User({ identifier: "1" })
);
});
});

describe("#update", function () {
let client = createStubInstance(Client);

beforeEach(function () {
scope.client = client;
});

it("should post the resource using the scope's client", function () {
relation.update({ identifier: "1" });

expect(client.post).to.be.calledWithExactly(
"/users",
new User({ identifier: "1" })
);
});
});

describe("#delete", function () {
it("should delete the resource using the scope's client", function () {
relation.delete("1");

expect(client.delete).to.be.calledWithExactly(
"/users",
new User({ identifier: "1" })
);
});

it("should delete the resource using the scope's client", function () {
relation.delete({ identifier: "1", name: "John" });

expect(client.delete).to.be.calledWithExactly(
"/users",
new User({ identifier: "1", name: "John" })
);
});
});

describe("#normalize", function () {
it("should normalize a string into a new user object with an identifier", function () {
let normalized = relation.normalize("1");

expect(normalized).to.be.eql(new User({ identifier: "1" }));
});

it("should normalize an object into a new user with it's properties", function () {
let normalized = relation.normalize({ identifier: "1", name: "John" });

expect(client.delete).to.be.calledWithExactly('/users/1');
expect(normalized).to.be.eql(new User({ identifier: "1", name: "John" }));
});
});
});
4 changes: 2 additions & 2 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"lint": "eslint src test",
"build": "rollup -c",
"test": "mocha --require test/helper --require @babel/register --recursive --colors",
"test:watch": "yarn test --watch",
"prepublish": "yarn lint && yarn test && yarn build"
"test:watch": "pnpm test --watch",
"prepublish": "pnpm lint && pnpm test && pnpm build"
},
"bugs": {
"url": "https://github.com/userlistio/userlist-javascript/issues"
Expand Down
4 changes: 2 additions & 2 deletions packages/widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"build": "rollup -c",
"serve": "rollup -c rollup.serve.js",
"test": "mocha --require ignore-styles --require test/helper --require @babel/register --recursive --colors",
"test:watch": "yarn test --watch",
"prepublish": "yarn lint && yarn test && yarn build"
"test:watch": "pnpm test --watch",
"prepublish": "pnpm lint && pnpm test && pnpm build"
},
"bugs": {
"url": "https://github.com/userlistio/userlist-javascript/issues"
Expand Down
Loading