Skip to content

Commit

Permalink
refactor(scpper): conversion of SiteInitial to enum
Browse files Browse the repository at this point in the history
BREAKING CHANGE: If you pass an incorrect site initial in the "site"property or a branch initial not supported by Scpper, an error will be throw.
  • Loading branch information
HelloEdit committed Feb 12, 2018
1 parent a8fd74d commit 73b6df5
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/scpper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import { FindPagesOptions, FindUsersOptions, FindWithTag } from './typings/FindO
export class Scpper {
public readonly url: string = 'http://scpper.com/api/'
public readonly api: apisauce.ApisauceInstance
public readonly site: SiteInitial
public readonly limit: number
protected _site: SiteInitial

constructor (options: ScpperOptions, config?: AxiosRequestConfig) {
if (!options) {
throw new Error('Configuration must be present')
}

if (options.url) {
this.url = options.url
}

if (!options.site) {
throw new Error('You need to pass "site" property')
}

this.site = options.site
this.limit = options.limit

Expand Down Expand Up @@ -44,7 +52,7 @@ export class Scpper {
*/
public async getPage (id: number) {
const response = await this.api.get('page', { id }) as apisauce.ApiResponse<PageItem>

if (!response.ok) {
throw new Error(response.problem)
}
Expand Down Expand Up @@ -118,4 +126,20 @@ export class Scpper {

return response
}

// region setter and getter
set site (initial: string) {
initial = initial.toLowerCase()

if (!(initial in SiteInitial)) {
throw new Error(`Branch "${initial}" is not supported yet.`)
}

this._site = initial as SiteInitial
}

get site () {
return this._site
}
// endregion
}

0 comments on commit 73b6df5

Please sign in to comment.