Socket
Socket
Sign inDemoInstall

oxford-dictionary

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

oxford-dictionary - npm Package Compare versions

Comparing version 1.4.1 to 1.5.0

186

index.js
var https = require('https');
var OxfordDictionary = function(obj) {
var OxfordDictionary = function (obj) {
this.config = {
app_id : obj.app_id,
app_key : obj.app_key,
source_lang: obj.source_lang || 'en'
app_id: obj.app_id,
app_key: obj.app_key,
source_lang: obj.source_lang || 'en-us'
};

@@ -12,6 +12,6 @@ };

// GET /entries/{source_lang}/{word_id}
// GET /entries/{source_lang}/{word_id}/regions={region}
// GET /entries/{source_lang}/{word_id}/{filters}
OxfordDictionary.prototype.find = function(props) {
var path = validate('/api/v1/entries/', props, this, null);
// GET /entries/{source_lang}/{word_id}?fields={filters}
// filters should be comma-separated string (e.g. "filter1,filter2,filter3")
OxfordDictionary.prototype.find = function (props) {
var path = validate('entries', props, this, null);
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -21,5 +21,5 @@ return buildRequest(options);

// GET /entries/{source_lang}/{word_id}/definitions
OxfordDictionary.prototype.definitions = function(props) {
var path = validate('/api/v1/entries/', props, this, 'definitions');
// GET /entries/{source_lang}/{word_id}?fields=definitions
OxfordDictionary.prototype.definitions = function (props) {
var path = validate('entries', props, this, 'definitions');
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -29,5 +29,5 @@ return buildRequest(options);

// GET /entries/{source_lang}/{word_id}/examples
OxfordDictionary.prototype.examples = function(props) {
var path = validate('/api/v1/entries/', props, this, 'examples');
// GET /entries/{source_lang}/{word_id}?fields=examples
OxfordDictionary.prototype.examples = function (props) {
var path = validate('entries', props, this, 'examples');
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -37,5 +37,5 @@ return buildRequest(options);

// GET /entries/{source_lang}/{word_id}/pronunciations
OxfordDictionary.prototype.pronunciations = function(props) {
var path = validate('/api/v1/entries/', props, this, 'pronunciations');
// GET /entries/{source_lang}/{word_id}?fields=pronunciations
OxfordDictionary.prototype.pronunciations = function (props) {
var path = validate('entries', props, this, 'pronunciations');
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -45,6 +45,6 @@ return buildRequest(options);

// GET /inflections/{source_lang}/{word_id}
// GET /inflections/{source_lang}/{word_id}/{filters}
OxfordDictionary.prototype.inflections = function(props) {
var path = validate('/api/v1/inflections/', props, this, null);
// GET /lemmas/{source_lang}/{word_id}
// GET /lemmas/{source_lang}/{word_id}?{filters}
OxfordDictionary.prototype.inflections = function (props) {
var path = validate('lemmas', props, this, null);
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -54,5 +54,5 @@ return buildRequest(options);

//GET /entries/{source_lang}/{word_id}/synonyms
OxfordDictionary.prototype.synonyms = function(props) {
var path = validate('/api/v1/entries/', props, this, 'synonyms');
//GET /thesaurus/{source_lang}/{word_id}?fields=synonyms
OxfordDictionary.prototype.synonyms = function (props) {
var path = validate('thesaurus', props, this, 'synonyms');
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -62,5 +62,5 @@ return buildRequest(options);

//GET /entries/{source_lang}/{word_id}/antonyms
OxfordDictionary.prototype.antonyms = function(props) {
var path = validate('/api/v1/entries/', props, this, 'antonyms');
//GET /thesaurus/{source_lang}/{word_id}?fields=synonyms
OxfordDictionary.prototype.antonyms = function (props) {
var path = validate('thesaurus', props, this, 'antonyms');
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -70,5 +70,5 @@ return buildRequest(options);

// GET /entries/{source_lang}/{word_id}/synonyms;antonyms
OxfordDictionary.prototype.thesaurus = function(props) {
var path = validate('/api/v1/entries/', props, this, 'synonyms;antonyms');
//GET /thesaurus/{source_lang}/{word_id}
OxfordDictionary.prototype.thesaurus = function (props) {
var path = validate('thesaurus', props, this, null);
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -78,5 +78,5 @@ return buildRequest(options);

// GET /entries/{source_language}/{word_id}/sentences
OxfordDictionary.prototype.sentences = function(props) {
var path = validate('/api/v1/entries/', props, this, 'sentences');
// GET /sentences/{source_language}/{word_id}
OxfordDictionary.prototype.sentences = function (props) {
var path = validate('sentences', props, this, null);
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -86,5 +86,5 @@ return buildRequest(options);

// GET entries/{source_translation_language}/{word_id}/translations={target_translation_language}
OxfordDictionary.prototype.translate = function(props) {
var path = validate('/api/v1/entries/', props, this, null);
// GET translations/{source_translation_language}/{target_translation_language}/{word_id}
OxfordDictionary.prototype.translate = function (props) {
var path = validate('translations', props, this, null);
var options = new OptionObj(path, this.config.app_id, this.config.app_key);

@@ -96,63 +96,65 @@ return buildRequest(options);

// Validation function
var validate = function(path, props, $this, dtype) {
if ( !($this.config.app_id) || !($this.config.app_key) ) {
throw Error('API_ID or API_KEY is undefined or NULL.');
}
var validate = function (endpoint, props, $this, dtype) {
let path = `/api/v2/${endpoint}`;
if (typeof props != 'object' && typeof props != 'string') {
throw Error('Argument is not of proper type');
if (typeof props === 'string') {
props = { word: props.toLowerCase() };
}
if (!($this.config.app_id) || !($this.config.app_key)) {
throw Error('API_ID or API_KEY is undefined or NULL.');
}
if (typeof props != 'object' && typeof props != 'string') {
throw Error('Argument is not of proper type');
}
if (typeof props != 'undefined' && typeof props === 'object') {
if (!props.hasOwnProperty('word')) {
throw Error('A word must be passed as a prop.');
}
// translate endpoint
if (endpoint === 'translations' && props.hasOwnProperty('target_language') && (typeof props.target_language === 'string')) {
path += `/${encodeURIComponent(props.target_language.toLowerCase())}`;
}
if (typeof props != 'undefined' && typeof props === 'object') {
if (props.hasOwnProperty('word') && (typeof props.word === 'string')) {
path += `/${$this.config.source_lang}/${props.word.toLowerCase()}`;
} else {
throw Error('Word argument not found');
}
let fields;
if ( props.hasOwnProperty('word') && (typeof props.word === 'string') ) {
path += $this.config.source_lang + '/' + props.word.toLowerCase();
if (!(dtype === null) && (typeof dtype === 'string') && !(dtype === 'entries')) {
if (fields == null) {
fields = `?fields=${encodeURIComponent(dtype)}`;
} else {
throw Error('Word argument not found');
fields += `,${encodeURIComponent(dtype)}`;
}
}
if ( !(dtype === null) && (typeof dtype === 'string') && !(dtype === 'entries') ) {
path += '/' + dtype;
if (props.hasOwnProperty('filters') && (typeof props.filters === 'string')) {
if (fields == null) {
fields = `?fields=${encodeURIComponent(props.filters)}`;
} else {
fields += `,${encodeURIComponent(props.filters)}`;
}
if ( props.hasOwnProperty('region') && (typeof props.region === 'string') ) {
if (dtype === 'entries') {
path += '/regions=' + encodeURIComponent(props.region.toString());
} else {
path += ';regions=' + encodeURIComponent(props.region.toString());
}
}
else if (props.hasOwnProperty('filters') && (typeof props.filters === 'string') ) {
if (dtype === 'entries') {
path += '/' + encodeURIComponent(props.filters.toString());
} else {
path += ';' + encodeURIComponent(props.filters.toString());
}
}
else if (props.hasOwnProperty('target_language') && (typeof props.target_language === 'string') ) {
path += '/translations=' + encodeURIComponent(props.target_language.toLowerCase());
}
}
if (typeof props === 'string' && props.length > 0) {
path += $this.config.source_lang + '/' + props.toLowerCase();
if ( dtype != null && typeof dtype === 'string') {
path += '/' + dtype;
}
if (fields) {
path += fields;
}
return path;
}
return path;
}; // end validateProp
// HTTPS Request Promise Builder
var buildRequest = function(options) {
return new Promise(function(resolve, reject) {
https.get(options, function(res) {
var buildRequest = function (options) {
return new Promise(function (resolve, reject) {
https.get(options, function (res) {
if (res.statusCode == 404) {
return reject("No such entry found.");
return reject("No such entry found.");
}
var data = "";
res.on('data', function (chunk) {

@@ -162,3 +164,3 @@ data += chunk;

res.on('end', function() {
res.on('end', function () {
var result;

@@ -174,3 +176,3 @@ try {

}
resolve(result);
resolve(result);
});

@@ -180,3 +182,3 @@

reject(err);
});
});
}); // end https.get

@@ -190,15 +192,15 @@ }); // end promise

var options = {
host : 'od-api.oxforddictionaries.com',
port : 443,
path : path,
method : 'GET',
headers : {
host: 'od-api.oxforddictionaries.com',
port: 443,
path: path,
method: 'GET',
headers: {
"Accept": "application/json",
"app_id": app_id,
"app_key": app_key
}
};
}
};
return options;
} // end OptionObj
module.exports = OxfordDictionary;
module.exports = OxfordDictionary;
{
"name": "oxford-dictionary",
"version": "1.4.1",
"version": "1.5.0",
"description": "A nodeJS wrapper for using the oxforddictionary.com REST API.",

@@ -19,2 +19,2 @@ "main": "index.js",

"homepage": "https://github.com/SleepyBandit/oxford-dictionary#readme"
}
}
# oxford-dictionary
A NodeJS wrapper for using the oxforddictionary.com REST API.
A NodeJS wrapper for using the oxforddictionary.com V2 REST API.
Special thanks to ajivoin for making the updates to V2!
# Install:
```
```shell
npm install oxford-dictionary

@@ -11,3 +12,3 @@ ```

Require the module then pass in a config object with your APP_ID, APP_KEY, and preferred supported language.
```
```javascript
var Dictionary = require("oxford-dictionary");

@@ -26,7 +27,8 @@

To use you can pass in a word directly...
```
```javascript
var lookup = dict.find("awesome");
lookup.then(function(res) {
console.log(res);
// stringify JSON object to see full structure in console log
console.log(JSON.stringify(res, null, 4));
},

@@ -39,3 +41,3 @@ function(err) {

```
```javascript
var props = {

@@ -51,3 +53,3 @@ word: "stupendous",

lookup.then(function(res) {
console.log(res);
console.log(JSON.stringify(res, null, 4));
},

@@ -59,6 +61,6 @@ function(err) {

A promise is returned which you can handle as desired.
```
```javascript
lookup.then(function(res) {
// res contains the json response
console.log(res);
console.log(JSON.stringify(res, null, 4));
},

@@ -77,3 +79,3 @@ function(err) {

A region OR filter can optionally be passed in an object with the word.
```
```javascript
var lookup = dict.find("awesome");

@@ -83,3 +85,3 @@ ```

`.definitions` retrieves available dictionary entries for given word and language and returns the definitions.
```
```javascript
var lookup = dict.definitions("awesome");

@@ -90,3 +92,3 @@ ```

A filter can optionally be passed in an object with the word.
```
```javascript
var lookup = dict.inflections("awesome");

@@ -96,3 +98,3 @@ ```

`.pronunciations` retrieves available dictionary entries for given word and language and returns the pronunciation.
```
```javascript
var lookup = dict.pronunciations("awesome");

@@ -102,3 +104,3 @@ ```

`.examples` retrieves available dictionary entries for given word and language and returns only examples.
```
```javascript
var lookup = dict.examples("awesome");

@@ -108,3 +110,3 @@ ```

`.synonyms` retrieves available synonyms for a given word and language.
```
```javascript
var lookup = dict.synonyms("awesome");

@@ -114,3 +116,3 @@ ```

`.antonyms` retrieves available antonyms for a given word and language.
```
```javascript
var lookup = dict.antonyms("awesome");

@@ -120,3 +122,3 @@ ```

`.thesaurus` retrieves available synonyms AND antonyms for a given word and language.
```
```javascript
var lookup = dict.thesaurus("awesome");

@@ -126,3 +128,3 @@ ```

`.sentences` retrieves list of sentences and list of senses (English language only).
```
```javascript
var lookup = dict.sentences("awesome");

@@ -133,3 +135,3 @@ ```

You must pass in a word and target language.
```
```javascript
var lookup = dict.translate({

@@ -143,3 +145,3 @@ word: "awesome",

Response is a json object from which you can access the required details such as definition, type, audio files, example statements.
```
```javascript
{

@@ -423,2 +425,2 @@ "metadata": {

}
```
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc