var plugin = {}, common = require('../../../api/utils/common.js'), plugins = require('../../pluginManager.js');
(function(plugin) { //write api call plugins.register("/i", function(ob) { //get request parameters var params = ob.params;
//check if it has data we need if (params.qstring.user_details) { //if it is string, but we expect json, lets parse it if (typeof params.qstring.ourplugin == "string") { try { params.qstring.ourplugin = JSON.parse(params.qstring.ourplugin); } catch (SyntaxError) { console.log('Parse JSON failed'); //we are not doing anything with request return false; } //start doing something with request
//and tell core we are working on it, by returning true return true; }
//we did not have data we were interested in return false; } }); }(plugin));
module.exports = plugin;
Params 对象
在api端有一个通过许多方法传递的公共对象,它通常存储在一个名为“params”的变量中。此对象的 Contens 可能取决于 api 请求的类型以及处理此请求的阶段
属性名称
它包含什么
何时添加
href
完整的请求网址
From the start
qstring
随请求传递的查询字符串或正文的对象
From the start
res
Response 对象
From the start
req
请求对象
From the start
apiPath
两级路径字符串
From the start
fullPath
完整 api 端点路径的字符串
From the start
files
通过请求上传的文件
从 POST 请求开始
cancelRequest
如果包含 true,则应忽略且不处理请求
任何插件都可以随时设置,但 API 仅在 / 和 /sdk 事件之后开始检查它,因此插件应该在需要时设置它
bulk
如果从批量方法处理此 SDK 请求,则为 True
使用 /i/bulk 终结点时
promises
不同事件的承诺数组
当所有承诺都得到满足时,请求就结束了
ip_address
设备提交请求的 IP 地址
在所有 SDK 请求上
user
包含一些用户信息的数据,例如来自请求的国家/地区地理信息等
在所有 SDK 请求上
app_user_id
用户app_users文档的 ID
在所有 SDK 请求上
member
有关仪表板用户的所有数据
对于所有包含api_key的请求,在通过验证方法进行验证后
app
应用程序的文档
在所有 SDK 请求上和 validateUserForDataReadAPI 验证之后
app_user
app_user文件
在所有 SDK 请求上
time
包含以下时间对象:
在所有 SDK 请求上
1 2 3 4 5 6 7 8 9 10 11 12
now - 请求时间的 moment 对象 nowUTC - 以 UTC 为单位的请求时间的 moment 对象 nowWithoutTimestamp - 时刻对象或当前时间 timestamp - 请求时间戳 mstimestamp - 请求毫秒时间戳 yearly - moment.format("YYYY"), monthly - moment.format("YYYY.M"), daily - moment.format("YYYY.M.D"), hourly - moment.format("YYYY.M.D.H"), weekly - Math.ceil(moment.format("DDD") / 7), month - moment.format("M"), day - moment.format("D"), hour - moment.format("H")
可用的事件路径和参数
Path
Description
Usable
Properties
/master
从管理工作线程的主集群派发出来的
当启动后台任务,您想要与计数服务器工作线程并行执行时
没有传递给它的参数
/worker
worker的初始化,基本上在nodejs服务器启动时只执行一次
若要完成每次启动需要执行一次的任务,例如创建数据库的连接池(如果未使用 Countly 默认连接) common 常见
具有通用 js 作为获取 db 连接和其他通用实用程序的另一种方式
/
对 Countly API 的任何 HTTP 请求
在 Countly core 处理数据之前或处理特定 URL 的文件上传时需要修改数据时
params - params 对象,带有路径请求的 apiPath 字符串已发出,urlParts 字符串与 url parts
var plugin = {}, common = require('../../../api/utils/common.js'), {validateUserForWrite} = require('../../../api/utils/rights.js'); plugins = require('../../pluginManager.js');
(function(plugin) { //handling custom path plugins.register("/i/ourplugin", function(ob) { //get parameters var params = ob.params; //request params var paths = ob.paths; validateUserForWrite(params, function(params) { //user is validated process request switch (paths[3]) { case 'create': //create new object var data = params.qstring; //validate data if needed and write object to db common.db.collection('ourplugin').insert(data, function(err, app) { if (err) common.returnMessage(params, 200, err); else common.returnMessage(params, 200, "Success"); }); break; case 'update': //update existing object var id = params.qstring.id; var data = params.qstring; //validate data if needed and write object to db common.db.collection('ourplugin').update({ _id: id }, data, function(err, app) { if (err) common.returnMessage(params, 200, err); else common.returnMessage(params, 200, "Success"); }); break; case 'delete': //delete existing object var id = params.qstring.id; common.db.collection('ourplugin').remove({ _id: id }, function(err, app) { if (err) common.returnMessage(params, 200, err); else common.returnMessage(params, 200, "Success"); }); break; default: common.returnMessage(params, 400, 'Invalid path, must be one of /create, /update or /delete'); break; } }); //need to return true, so core does not repond that path does not exist return true; }); }(plugin));
var plugin = {}, common = require('../../../api/utils/common.js'), {validateUserForRead} = require('../../../api/utils/rights.js'); plugins = require('../../pluginManager.js'), fetch = require('../../../api/parts/data/fetch.js');
(function(plugin) {
//waiting for metrics to be received plugins.register("/session/metrics", function(ob) { var predefinedMetrics = ob.predefinedMetrics;
//tell countly to process our metric predefinedMetrics.push({ db: "mymetric", //collection name metrics: [{ name: "_mymetric", //what to wait for in query string set: "mymetric", // metric mymetric short_code: "mymetric" } //optionally can provide short name ] }); });
//waiting for read request plugins.register("/o", function(ob) { var params = ob.params;
//if user requested to read our metric if (params.qstring.method == "mymetric") {
//validate user and output data using fetchTimeObj method validateUserForRead(params, fetch.fetchTimeObj, 'mymetric');
//return true, we responded to this request return true; }
//else we are not interested in this request return false; });
//waiting for app delete event plugins.register("/i/apps/delete", function(ob) { var appId = ob.appId;
//delete all app data from our metric collection common.db.collection('mymetric').remove({ '_id': { $regex: appId + ".*" } }, function() {}); });
//waiting for app reset event plugins.register("/i/apps/reset", function(ob) { var appId = ob.appId;
//delete all app data from our metric collection common.db.collection('mymetric').remove({ '_id': { $regex: appId + ".*" } }, function() {});