node-ninjacat
Advanced tools
Comparing version 0.1.2 to 0.1.3
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _request = _interopRequireDefault(require("request")); | ||
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken")); | ||
var _JSONStream = _interopRequireDefault(require("JSONStream")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const request_1 = __importDefault(require("request")); | ||
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); | ||
// @ts-ignore | ||
const JSONStream_1 = __importDefault(require("JSONStream")); | ||
class NinjacatClient { | ||
constructor(opts) { | ||
_initialiseProps.call(this); | ||
this.client_id = opts.client_id; | ||
this.client_secret = opts.client_secret; | ||
this.agency_id = opts.agency_id; | ||
this.agency_identifier = opts.agency_identifier; | ||
this.report_secret = opts.report_secret; | ||
this.request = _request.default.defaults({ | ||
json: true, | ||
baseUrl: NinjacatClient.base_url, | ||
headers: { | ||
'Client-Id': this.client_id, | ||
Accept: 'application/json', | ||
'x-api-key': this.agency_identifier | ||
} | ||
}); | ||
this.report = { | ||
retrieve: request_id => new Promise(async resolve => { | ||
let res = { | ||
status: 0 | ||
}; | ||
while (res.status !== 2) { | ||
console.log(res); | ||
res = await this.report.get(request_id); | ||
} | ||
resolve(res); | ||
}), | ||
get: request_id => { | ||
const path = ['open_api/report', this.agency_id, request_id].join('/'); | ||
const payload = { | ||
agency_id: this.agency_id, | ||
request_id | ||
}; | ||
const token = _jsonwebtoken.default.sign(payload, this.report_secret); | ||
const config = { | ||
json: true, | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}` | ||
} | ||
}; | ||
return new Promise((resolve, reject) => { | ||
_request.default.get(`https://app.ninjacat.io/${path}`, config, function processReportGet(err, _res, body) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(body); | ||
}); | ||
constructor(opts) { | ||
this.ready = null; | ||
this.client_id = opts.client_id; | ||
this.client_secret = opts.client_secret; | ||
this.agency_id = opts.agency_id; | ||
this.agency_identifier = opts.agency_identifier; | ||
this.report_secret = opts.report_secret; | ||
this.request = request_1.default.defaults({ | ||
json: true, | ||
baseUrl: NinjacatClient.base_url, | ||
headers: { | ||
'Client-Id': this.client_id, | ||
Accept: 'application/json', | ||
'x-api-key': this.agency_identifier, | ||
}, | ||
}); | ||
}, | ||
post: (template_id, advertiser_id, opts) => { | ||
const path = ['open_api/report', this.agency_id, template_id, advertiser_id].join('/'); | ||
const payload = { | ||
agency_id: this.agency_id, | ||
template_id, | ||
advertiser_id | ||
this.report = { | ||
retrieve: (request_id) => new Promise(async (resolve) => { | ||
let res = { status: 0 }; | ||
while (res.status !== 2) { | ||
console.log(res); | ||
res = await this.report.get(request_id); | ||
} | ||
resolve(res); | ||
}), | ||
get: (request_id) => { | ||
const path = ['open_api/report', this.agency_id, request_id].join('/'); | ||
const payload = { | ||
agency_id: this.agency_id, | ||
request_id, | ||
}; | ||
const token = jsonwebtoken_1.default.sign(payload, this.report_secret); | ||
const config = { | ||
json: true, | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}; | ||
return new Promise((resolve, reject) => { | ||
request_1.default.get(`https://app.ninjacat.io/${path}`, config, function processReportGet(err, _res, body) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(body); | ||
}); | ||
}); | ||
}, | ||
post: (template_id, advertiser_id, opts) => { | ||
const path = [ | ||
'open_api/report', | ||
this.agency_id, | ||
template_id, | ||
advertiser_id, | ||
].join('/'); | ||
const payload = { | ||
agency_id: this.agency_id, | ||
template_id, | ||
advertiser_id, | ||
}; | ||
const token = jsonwebtoken_1.default.sign(payload, this.report_secret); | ||
const config = { | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}; | ||
if (opts) { | ||
config.form = opts; | ||
} | ||
return new Promise((resolve, reject) => { | ||
request_1.default.post(`https://app.ninjacat.io/${path}`, config, function processReportPost(err, _res, body) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(body); | ||
}); | ||
}); | ||
}, | ||
}; | ||
const token = _jsonwebtoken.default.sign(payload, this.report_secret); | ||
const config = { | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}` | ||
} | ||
}; | ||
if (opts) { | ||
config.form = opts; | ||
} | ||
auth() { | ||
if (!this.ready) { | ||
const basic = Buffer.from(this.client_id + ':' + this.client_secret).toString('base64'); | ||
this.ready = new Promise((resolve, reject) => { | ||
this.request.post('/oauth2/token', { | ||
headers: { | ||
'Cache-Control': 'no-cache', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Authorization: `Basic ${basic}`, | ||
}, | ||
}, (err, _res, body) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
this.request = this.request.defaults({ | ||
headers: { | ||
Authorization: `Bearer ${body.access_token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
return this.ready; | ||
} | ||
async advertisers() { | ||
await this.auth(); | ||
return new Promise((resolve, reject) => { | ||
_request.default.post(`https://app.ninjacat.io/${path}`, config, function processReportPost(err, _res, body) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(body); | ||
}); | ||
this.request | ||
.get('/management_open_api/advertisers') | ||
.on('error', (err) => reject(err)) | ||
// @ts-ignore | ||
.pipe(JSONStream_1.default.parse()) | ||
.on('data', (d) => resolve(d)); | ||
}); | ||
} | ||
}; | ||
} | ||
auth() { | ||
if (!this.ready) { | ||
const basic = Buffer.from(this.client_id + ':' + this.client_secret).toString('base64'); | ||
this.ready = new Promise((resolve, reject) => { | ||
this.request.post('/oauth2/token', { | ||
headers: { | ||
'Cache-Control': 'no-cache', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Authorization: `Basic ${basic}` | ||
} | ||
}, (err, _res, body) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
this.request = this.request.defaults({ | ||
headers: { | ||
Authorization: `Bearer ${body.access_token}`, | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
return this.ready; | ||
} | ||
async advertisers() { | ||
await this.auth(); | ||
return new Promise((resolve, reject) => { | ||
this.request.get('/management_open_api/advertisers').on('error', err => reject(err)) // @ts-ignore | ||
.pipe(_JSONStream.default.parse()).on('data', d => resolve(d)); | ||
}); | ||
} | ||
} | ||
exports.default = NinjacatClient; | ||
NinjacatClient.base_url = 'https://api.ninjacat.io'; | ||
var _initialiseProps = function () { | ||
this.ready = null; | ||
}; | ||
//# sourceMappingURL=Client.js.map |
{ | ||
"name": "node-ninjacat", | ||
"description": "Reporting & Management API Client for https://www.ninjacat.io/", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"main": "lib/Client.js", | ||
@@ -25,3 +25,3 @@ "license": "MIT", | ||
"scripts": { | ||
"prepare": "gosub build", | ||
"prepare": "gosub tsc", | ||
"watch": "babel --watch src --out-dir lib --extensions '.ts,.tsx'", | ||
@@ -28,0 +28,0 @@ "build": "babel src --out-dir lib --extensions '.ts,.tsx'", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11934
391