Skip to content

Commit

Permalink
Added typescript support, defaulted secure to false
Browse files Browse the repository at this point in the history
  • Loading branch information
PorterK committed May 10, 2021
1 parent 8ffb716 commit 0a7374b
Show file tree
Hide file tree
Showing 19 changed files with 2,767 additions and 273 deletions.
17 changes: 17 additions & 0 deletions dist/Client.d.ts
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 {};
14 changes: 14 additions & 0 deletions dist/Client.js
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;
22 changes: 22 additions & 0 deletions dist/Endpoint.d.ts
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 {};
46 changes: 46 additions & 0 deletions dist/Endpoint.js
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;
2 changes: 2 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Client } from './Client';
export default Client;
4 changes: 4 additions & 0 deletions dist/index.js
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;
Loading

0 comments on commit 0a7374b

Please sign in to comment.