UNPKG

3.07 kBMarkdownView Raw
1![Build Passing Image](https://travis-ci.org/slooker/json-activity-streamish.svg?branch=master)
2
3# JSON Activity Stream-ish
4
5## History ##
6
7We started writing an activity user stream that used [JSON Activity Stream](http://activitystrea.ms) but was bit overly strict for us. We needed things verbs like 'Download' which aren't available in the stream. I wrote this to simplify our actions. It's a *VERY* simple implementation that produces JSON Activity Stream style JSON output.
8
9## To Begin ##
10
111. Install it:
12```
13 npm install json-activity-streamish --save
14```
15
162. Require it and use:
17```
18 const Activity = require('json-activity-streamish')
19 let activity = new Activity();
20 // <Set the actor, object and type fields as below>
21 // Get JSON object
22 console.log(activity.toJSON());
23 // Get string representation of JSON object
24 console.log(activity.toString());
25```
26
27### Notes ###
28You can pass in all data when you create the activity like so:
29
30```
31let activity = new Activity({
32 "name": "John Smith has accepted JPG.jpg",
33 "actor": {
34 "id": "98765",
35 "type": "Person",
36 "attributedTo": "john.smith@myfakedomain.com",
37 "name": "John Smith"
38 },
39 "object": {
40 "id": "123456",
41 "type": "Link",
42 "href": "http://www.fakedomain.com/image/123456",
43 "mediaType": "mime/jpeg",
44 "name": "JPG.jpg"
45 },
46 "type": "Accept",
47});
48```
49
50You can also set fields manually:
51
52```
53let activity = new Activity();
54activity.type('Accept')
55 .actor({
56 "id": "98765",
57 "type": "Person",
58 "attributedTo": "john.smith@myfakedomain.com",
59 "name": "John Smith"
60 })
61 .object({
62 "id": "123456",
63 "type": "Link",
64 "href": "http://www.fakedomain.com/image/123456",
65 "mediaType": "mime/jpeg",
66 "name": "JPG.jpg"
67 });
68```
69
70Note that `name` will be generated for you from your `type`, `actor` and `object` if you don't set it explicitly. `id` will also be auto-populated using the [guid](https://www.npmjs.com/package/guid) module
71
72
73We've added a `meta` field so that you can add any extraneous data that doesn't fit into JSON Activity Stream spec as well. It works the same way as all of the other fields you can set.
74
75```
76let activity = new Activity();
77activity.meta({
78 recipients: ["john.smith@myfakedomain.com", "jane.smith@myfakedomain.com"],
79 someOtherField: "someOtherData"
80});
81```
82
83You can return a json object or a json representation of a string.
84
85```
86// Get a JSON object
87activity.toJSON();
88
89// Output to a JSON string representation
90activity.toString();
91```
92
93## Supported Functions ##
94
95### Getters and Setters ###
96
97All getters and setters can be called with an argument to set the argument, or without to get the property.
98
99Here's a list of getters and setters:
100
101* `* activity.actor()`
102* `* activity.object()`
103* `activity.target()`
104* `* activity.type()`
105* `activity.content()`
106* `activity.meta()`
107* `activity.name()`
108
109* is a required field.
110
111`.name()` will be auto generated for you if you do not supply it.
112
113### Output ###
114
115`toJSON()` will return a JSON object
116`toString()` will return a string representation of the JSON object