Skip to content

Commit

Permalink
BREAKING CHANGE: Layout rework
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Layout rework
  • Loading branch information
ArkhamCookie authored Aug 22, 2023
2 parents 614d7a0 + 91d07a1 commit faafdb8
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
2 changes: 1 addition & 1 deletion jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"source": {
"includePattern": ".+\\.js(doc|x)?$",
"excludePattern": "(^|\\/|\\\\)_",
"include": ["src/", "README.md"],
"include": ["lib/", "README.md"],
"exclude": ["node_modules/", "out/", "assets/", ".github/"]
},
"sourceType": "module",
Expand Down
File renamed without changes.
84 changes: 84 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'dotenv'

/** Utils to setup your config */

/**
* Get API Key
* @returns {string} key
*/
export function getKey() {
if (Deno.env.has('WORDNIK_KEY')) {
const key = 'api_key=' + Deno.env.get('WORDNIK_KEY')
return key
} else {
console.error('No API key found!')
}
}

/**
* Get API Version
* @returns {string} version
*/
export function getVersion() {
if (Deno.env.has('API_VERSION')) {
const version = 'v' + Deno.env.get('API_VERSION') + '/'
return version
} else if (!Deno.env.has('API_VERSION')) {
const version = 'v4/' // update when new api version comes out
return version
}
}

export class Wordnik {
constructor() {
/**
* Generate base vars for Wordnik's API
* @constructs
* @param {string} base
* @param {string} key
* @param {string} version
*/
this.base = 'https://api.wordnik.com/'
this.key = getKey()
this.version = getVersion()
}

/**
* @param {string} word
* @param {string} target
* @param {array} options
* @returns {string} - url to fetch
*/
word(word, target, options) {
if (!word) word = 'hackable'
if (!target) target = 'scrabbleScore'
if (options) {
this.key = '&' + this.key
} else {
this.key = '?' + this.key
}

return this.base + this.version + 'word.json/' +
word + '/' +
target +
this.key
}

/**
* @param {string} target
* @param {*} options
* @returns {string} - url to fetch
*/
words(target, options) {
if (!target) target = 'wordOfTheDay'
if (options) {
this.key = '&' + this.key
} else {
this.key = '?' + this.key
}

return this.base + this.version + 'words.json/' +
target +
this.key
}
}
8 changes: 8 additions & 0 deletions lib/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Module for Worknik's API
* @module wordnik
*/

export * from './main.js'
export * from './word/mod.js'
export * from './words/mod.js'
File renamed without changes.
7 changes: 7 additions & 0 deletions lib/word/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* [word] section of wordnik module
* @module @wordnik/word
*
*/

export * from './scrabbleScore.js'
34 changes: 34 additions & 0 deletions lib/word/scrabbleScore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Wordnik } from '../main.js'

const wordnik = new Wordnik()

/**
* Get the Scrabble score for a word
* @param {string} word
* @returns {integer} - score for the word
*/
export async function scrabbleScore(word) {
let response
if (word) {
response = await fetch(wordnik.word(word, 'scrabbleScore'))
} else {
response = await fetch(wordnik.word('hackable', 'scrabbleScore'))
}

if (response.status !== 200) {
console.error('Error! Status:', response.statusText)
return
}

const json = JSON.stringify(await response.json())
const data = JSON.parse(json)
return data.value
}

/**
* @example
* ```js
* import { scrabbleScore } from 'https://deno.land/x/wordnik/mod.js'
* console.log(await scrabbleScore('cookie')) // returns cookie's score (12)
* ```
*/
File renamed without changes.
21 changes: 17 additions & 4 deletions src/lib/words/random.js → lib/words/random.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// deno-lint-ignore no-unused-vars
import { buildTarget } from '../../mod.js'
import { Wordnik } from './../mod.js'

const wordnik = new Wordnik()

/**
* Random Word - Return a single random word/WordObject
Expand All @@ -13,6 +14,18 @@ import { buildTarget } from '../../mod.js'
* @param {integer} [minLength] - Minimum word length
* @param {integer} [maxLength] - Maximum word length
*/
export function random() {
console.debug('random')
export async function randomWord(options) {
const response = await fetch(wordnik.words('randomWord', options))
const json = JSON.stringify(await response.json())
const data = JSON.parse(json)
return data
}

/**
* @example
* ```js
* import { randomWord } from 'https://deno.land/x/wordnik/mod.js'
* const response = await randomWord()
* console.log(response.word) // prints a random word
* ```
*/
8 changes: 5 additions & 3 deletions src/lib/words/wordOfTheDay.js → lib/words/wordOfTheDay.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { buildTarget } from '../../mod.js'
import { Wordnik } from './../mod.js'

const wordnik = new Wordnik()

/**
* Word of the Day
Expand All @@ -9,9 +11,9 @@ export async function wordOfTheDay(date) {
let response

if (date) {
response = await fetch(buildTarget('/words.json/wordOfTheDay' + '?date=' + date + '&'))
response = await fetch(wordnik.words('wordOfTheDay', date))
} else if (!date) {
response = await fetch(buildTarget('/words.json/wordOfTheDay?'))
response = await fetch(wordnik.words('wordOfTheDay'))
}

if (response.status !== 200) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"http:test": "./src/tests/vaildate.js",
"clean": "rm -r ./static/out",
"jsdoc": "npm run clean ; jsdoc ./src --configure ./jsdoc.json --destination ./static/out/"
"jsdoc": "npm run clean ; jsdoc --configure ./jsdoc.json --destination ./static/out/"
},
"keywords": [],
"author": "ArkhamCookie <[email protected]> (https://theuntitledproject.dev)",
Expand Down
48 changes: 0 additions & 48 deletions src/lib/config.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/mod.js

This file was deleted.

0 comments on commit faafdb8

Please sign in to comment.