-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added typescript support, defaulted secure to false
- Loading branch information
Showing
19 changed files
with
2,767 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Endpoint } from './Endpoint'; | ||
interface IClient { | ||
content: Endpoint; | ||
tags: Endpoint; | ||
sections: Endpoint; | ||
editions: Endpoint; | ||
item: Endpoint; | ||
} | ||
export declare class Client implements IClient { | ||
content: Endpoint; | ||
tags: Endpoint; | ||
sections: Endpoint; | ||
editions: Endpoint; | ||
item: Endpoint; | ||
constructor(key: string, secure?: boolean); | ||
} | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Client = void 0; | ||
const Endpoint_1 = require("./Endpoint"); | ||
class Client { | ||
constructor(key, secure = false) { | ||
this.content = new Endpoint_1.Endpoint('search', key, secure); | ||
this.tags = new Endpoint_1.Endpoint('tags', key, secure); | ||
this.sections = new Endpoint_1.Endpoint('sections', key, secure); | ||
this.editions = new Endpoint_1.Endpoint('editions', key, secure); | ||
this.item = new Endpoint_1.Endpoint(null, key, secure, ['getById']); | ||
} | ||
} | ||
exports.Client = Client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
interface IEndpoint { | ||
endpoint: string; | ||
key: string; | ||
http: string; | ||
base: string; | ||
availableFunctions: Array<string>; | ||
request: (params: Object) => Promise<Object>; | ||
search: (query: string, filters: object) => Function; | ||
getById: (id: string) => Function; | ||
} | ||
export declare class Endpoint implements IEndpoint { | ||
endpoint: string; | ||
key: string; | ||
http: string; | ||
base: string; | ||
availableFunctions: Array<string>; | ||
request: (params: Object) => Promise<any>; | ||
search: (query: string, filters: object) => Function; | ||
getById: (id: string) => Function; | ||
constructor(endpoint: string, key: string, useSSL: boolean, availableFunctions?: Array<string>); | ||
} | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Endpoint = void 0; | ||
const request_1 = require("request"); | ||
class GuardianJSInvalidMethodException { | ||
constructor(message) { | ||
this.message = message; | ||
this.name = 'GuardianJSInvalidMethodException'; | ||
} | ||
} | ||
class Endpoint { | ||
constructor(endpoint, key, useSSL, availableFunctions = ['search']) { | ||
this.endpoint = endpoint; | ||
this.key = key; | ||
this.http = useSSL ? 'https://' : 'http://'; | ||
this.base = `${this.http}content.guardianapis.com`; | ||
this.availableFunctions = availableFunctions; | ||
this.request = function (params) { | ||
return new Promise((resolve, reject) => { | ||
request_1.get(params, (err, res, body) => { | ||
if (err) | ||
reject(err); | ||
resolve({ response: res, body }); | ||
}); | ||
}); | ||
}; | ||
this.search = function (query = '', filters = {}) { | ||
if (!this.availableFunctions.includes('search')) { | ||
throw new GuardianJSInvalidMethodException('search is an invalid method'); | ||
} | ||
let filter = ''; | ||
Object.entries(filters).forEach((entry) => { | ||
const key = entry[0].replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase(); | ||
filter = `${filter}&${key}=${entry[1]}`; | ||
}); | ||
return this.request(`${this.base}/${this.endpoint}?api-key=${this.key}&q=${query}${filter}`); | ||
}; | ||
this.getById = function (id) { | ||
if (!this.availableFunctions.includes('getById')) { | ||
throw new GuardianJSInvalidMethodException('getById is an invalid method'); | ||
} | ||
return this.request(`${this.base}/${id}?api-key=${this.key}`); | ||
}; | ||
} | ||
} | ||
exports.Endpoint = Endpoint; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { Client } from './Client'; | ||
export default Client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Client_1 = require("./Client"); | ||
exports.default = Client_1.Client; |
Oops, something went wrong.