Comparing version 1.0.0 to 1.1.0
export declare class SportmonksApi { | ||
baseUrl: any; | ||
token: any; | ||
versionId: any; | ||
constructor(tokenId: string, versionId?: string); | ||
get(endpoint: any, params: any): void; | ||
constructor(tokenId: string, sport?: string); | ||
get(endpoint: any, params: any): Promise<{}>; | ||
_composeUrl(endpoint: any, params: any): any; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const https = require("https"); | ||
class SportmonksApi { | ||
constructor(tokenId, versionId = 'v2') { | ||
constructor(tokenId, sport = 'soccer') { | ||
this.token = tokenId; | ||
this.versionId = versionId; | ||
this.baseUrl = `https://${sport}.sportmonks.com/api/`; | ||
} | ||
get(endpoint, params) { | ||
let url = this._composeUrl(endpoint, params); | ||
return new Promise((resolve, reject) => { | ||
const request = https.get(url, (response) => { | ||
if (response && response.statusCode && (response.statusCode < 200 || response.statusCode > 299)) { | ||
reject(new Error('Failed to load page, status code: ' + response.statusCode)); | ||
} | ||
const body = []; | ||
response.on('data', (chunk) => body.push(chunk)); | ||
response.on('end', () => resolve(JSON.parse(body.join('')))); | ||
}); | ||
request.on('error', (err) => reject(err)); | ||
}); | ||
} | ||
_composeUrl(endpoint, params) { | ||
let page; | ||
let newEndpoint = this.baseUrl + endpoint; | ||
let wrapped = endpoint.match(/\{(.*?)\}/g); | ||
if (wrapped) { | ||
let unwrapped = (wrapped) => wrapped.replace('{', '').replace('}', ''); | ||
for (let w in wrapped) { | ||
let k = unwrapped(wrapped[w]); | ||
newEndpoint = newEndpoint.replace(wrapped[w], params[k]); | ||
delete params[k]; | ||
} | ||
} | ||
newEndpoint += "?api_token=" + this.token; | ||
if (params && Object.keys(params).length > 0) { | ||
let plist = []; | ||
let pkeys = Object.keys(params); | ||
for (let p in pkeys) { | ||
if (pkeys[p] != 'page' && params[pkeys[p]]) | ||
plist.push(pkeys[p]); | ||
else { | ||
if (pkeys[p] == 'page') | ||
page = params[pkeys[p]]; | ||
} | ||
} | ||
if (page) | ||
newEndpoint += "&page=" + page; | ||
if (plist.length > 0) | ||
newEndpoint += "&include=" + plist.join(','); | ||
} | ||
return newEndpoint; | ||
} | ||
} | ||
exports.SportmonksApi = SportmonksApi; | ||
//# sourceMappingURL=sportmonks.class.js.map |
{ | ||
"name": "sportmonks", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Sportmonks Soccer API", | ||
@@ -17,3 +17,3 @@ "main": "dist/index.js", | ||
"commit": "git-cz", | ||
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 40 --lines 100", | ||
"check-coverage": "nyc check-coverage --statements 70 --branches 60 --functions 30 --lines 70", | ||
"report-coverage": "cat ./coverage/lcov.info | coveralls", | ||
@@ -20,0 +20,0 @@ "watch:test": "npm t -- -w", |
@@ -1,1 +0,48 @@ | ||
# SportMonks Soccer API | ||
# SportMonks Soccer API | ||
# installation | ||
```js | ||
npm install sportmonks | ||
``` | ||
# usage | ||
```js | ||
var sportmonks = new Sportmonks(__YOUR_API_TOKEN__); | ||
sportmonks.get(endpoint,params).then( function(resp){ | ||
//resp.data will contain your data | ||
//resp.meta will contain the meta informations | ||
console.log(resp); | ||
}); | ||
``` | ||
## endpoint | ||
you can get the endpoint from the [official sportmonks documentation](https://www.sportmonks.com/sports/soccer#documentation) | ||
omitting the base url and the parameters (that are set with the params field) | ||
```js | ||
sportmonks.get('v2.0/countries').then( function(resp){ | ||
console.log(resp) | ||
}); | ||
``` | ||
## params | ||
if you need to specify parameters you can set the params field as follow | ||
```js | ||
sportmonks.get('v2.0/countries/{id}', { id: 13, competitions: true }).then( function(resp){ | ||
//id in the params field will replace {id} in the endpoint | ||
//competitions: true, will add include=competitions in query string | ||
}); | ||
``` | ||
## pagination | ||
page number can be added using the page param | ||
```js | ||
sportmonks.get('v2.0/fixtures/between/{from}/{to}', { from: '1998-01-01', to: '2017-12-01', page: 2, lineup: true}).then( function(resp) { | ||
//pagination info can be found in | ||
console.log(resp.meta.pagination) | ||
}); | ||
``` | ||
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
8961
62
49
6