guardian-js
Advanced tools
Comparing version 4.0.1 to 5.0.0
@@ -7,5 +7,5 @@ interface IEndpoint { | ||
availableFunctions: Array<string>; | ||
request: (params: Object) => Promise<Object>; | ||
_request: (params: Object) => Promise<Object>; | ||
search: (query: string, filters: object) => Promise<unknown>; | ||
getById: (id: string) => Function; | ||
getById: (id: string) => Promise<unknown>; | ||
} | ||
@@ -18,7 +18,23 @@ export declare class Endpoint implements IEndpoint { | ||
availableFunctions: Array<string>; | ||
request: (params: Object) => Promise<any>; | ||
search: (query: string, filters: object) => Promise<unknown>; | ||
getById: (id: string) => Function; | ||
constructor(endpoint: string, key: string, useSSL: boolean, availableFunctions?: Array<string>); | ||
/** | ||
* Internal request wrapper | ||
* @param url URL to send the request to | ||
* @returns Response data | ||
*/ | ||
_request(url: string): Promise<any>; | ||
/** | ||
* Search the Guardian api | ||
* @param query The string to search on | ||
* @param filters A list of filters | ||
* @returns Response data | ||
*/ | ||
search(query?: string, filters?: object): Promise<any>; | ||
/** | ||
* Get an item by ID | ||
* @param id ID of the item to get | ||
* @returns Response data | ||
*/ | ||
getById(id: string): Promise<any>; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Endpoint = void 0; | ||
const request_1 = require("request"); | ||
class GuardianJSInvalidMethodException { | ||
@@ -18,30 +17,45 @@ constructor(message) { | ||
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}`); | ||
}; | ||
} | ||
/** | ||
* Internal request wrapper | ||
* @param url URL to send the request to | ||
* @returns Response data | ||
*/ | ||
async _request(url) { | ||
const resp = await fetch(url); | ||
const body = await resp.json(); | ||
if (resp.status >= 400) { | ||
throw new Error(body.message); | ||
} | ||
return body.response; | ||
} | ||
/** | ||
* Search the Guardian api | ||
* @param query The string to search on | ||
* @param filters A list of filters | ||
* @returns Response data | ||
*/ | ||
async search(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}`); | ||
} | ||
/** | ||
* Get an item by ID | ||
* @param id ID of the item to get | ||
* @returns Response data | ||
*/ | ||
async getById(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; |
{ | ||
"name": "guardian-js", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "A JavaScript lib for the Guardian's api", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "mocha test" | ||
"build": "npx ts ./index.ts -o dist/", | ||
"test": "jest ./test/test.js" | ||
}, | ||
@@ -23,10 +24,9 @@ "repository": { | ||
"homepage": "https://github.com/PorterK/GuardianJSClient#readme", | ||
"dependencies": { | ||
"request": "^2.88.0" | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"devDependencies": { | ||
"expect": "^25.3.0", | ||
"mocha": "^7.1.1", | ||
"typescript": "^4.2.4" | ||
"jest": "^29.4.2", | ||
"typescript": "^4.9.5" | ||
} | ||
} |
@@ -1,3 +0,1 @@ | ||
import { get } from 'request'; | ||
interface IGuardianJSInvalidMethodException { | ||
@@ -24,5 +22,5 @@ message: string, | ||
availableFunctions: Array<string>, | ||
request: (params: Object) => Promise<Object>, | ||
_request: (params: Object) => Promise<Object>, | ||
search: (query: string, filters: object) => Promise<unknown>, | ||
getById: (id: string) => Function, | ||
getById: (id: string) => Promise<unknown>, | ||
} | ||
@@ -36,5 +34,2 @@ | ||
availableFunctions: Array<string>; | ||
request: (params: Object) => Promise<any>; | ||
search: (query: string, filters: object) => Promise<unknown>; | ||
getById: (id: string) => Function; | ||
@@ -47,36 +42,53 @@ constructor(endpoint: string, key: string, useSSL: boolean, availableFunctions:Array<string> = ['search']) { | ||
this.availableFunctions = availableFunctions; | ||
} | ||
this.request = function(params: Object) { | ||
return new Promise((resolve, reject) => { | ||
get(params, (err, res, body) => { | ||
if (err) reject(err); | ||
resolve({ response: res, body }); | ||
}); | ||
}); | ||
/** | ||
* Internal request wrapper | ||
* @param url URL to send the request to | ||
* @returns Response data | ||
*/ | ||
async _request(url: string) { | ||
const resp = await fetch(url); | ||
const body = await resp.json(); | ||
if (resp.status >= 400) { | ||
throw new Error(body.message); | ||
} | ||
this.search = function (query: string = '', filters: object = {}) { | ||
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}`); | ||
return body.response; | ||
} | ||
/** | ||
* Search the Guardian api | ||
* @param query The string to search on | ||
* @param filters A list of filters | ||
* @returns Response data | ||
*/ | ||
async search(query: string = '', filters: object = {}) { | ||
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: string) { | ||
if (!this.availableFunctions.includes('getById')) { | ||
throw new GuardianJSInvalidMethodException('getById is an invalid method'); | ||
} | ||
return this.request(`${this.base}/${id}?api-key=${this.key}`); | ||
/** | ||
* Get an item by ID | ||
* @param id ID of the item to get | ||
* @returns Response data | ||
*/ | ||
async getById(id: string) { | ||
if (!this.availableFunctions.includes('getById')) { | ||
throw new GuardianJSInvalidMethodException('getById is an invalid method'); | ||
} | ||
return this._request(`${this.base}/${id}?api-key=${this.key}`); | ||
} | ||
} |
'use strict'; | ||
const expect = require('expect'); | ||
const guardian = require('../dist').default; | ||
let api = new guardian('2a896a41-f4fb-4dcd-829d-2034c043bb0d', false); | ||
let api = new guardian('test_api_key', false); | ||
@@ -9,21 +8,23 @@ | ||
it('should return OK', async () => { | ||
const { response } = await api.content.search(); | ||
const response = await api.content.search(); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.status).toEqual('ok'); | ||
}) | ||
}); | ||
describe('Content', async () => { | ||
describe('Content', () => { | ||
it('has a search function that returns OK', async () => { | ||
const { response } = await api.content.search(); | ||
const response = await api.content.search(); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.status).toEqual('ok'); | ||
}); | ||
it('properly parses filters', async () => { | ||
const { response } = await api.content.search('football', { | ||
it('properly parses filters', async () => { | ||
const spy = jest.spyOn(api.content, '_request'); | ||
await api.content.search('football', { | ||
starRating: 3 | ||
}); | ||
expect(response.req._header.includes('star-rating')); | ||
expect(spy.mock.calls[0][0].includes('star-rating=3')).toEqual(true); | ||
}); | ||
@@ -34,3 +35,3 @@ | ||
expect(JSON.parse(response.body).response.results.length).toBeGreaterThan(0); | ||
expect(response.results.length).toBeGreaterThan(0); | ||
}); | ||
@@ -42,13 +43,15 @@ | ||
it('has a search function that returns OK', async () => { | ||
const { response } = await api.tags.search(); | ||
const response = await api.tags.search(); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.status).toEqual('ok'); | ||
}); | ||
it('properly parses filters', async () => { | ||
const { response } = await api.tags.search('football', { | ||
const spy = jest.spyOn(api.tags, '_request'); | ||
await api.tags.search('basketball', { | ||
pageSize: 3 | ||
}); | ||
expect(response.req._header.includes('page-size')); | ||
expect(spy.mock.calls[0][0].includes('page-size=3')).toEqual(true); | ||
}); | ||
@@ -59,3 +62,3 @@ | ||
expect(JSON.parse(response.body).response.results.length).toBeGreaterThan(0); | ||
expect(response.results.length).toBeGreaterThan(0); | ||
}); | ||
@@ -66,11 +69,11 @@ }); | ||
it('has a search function that returns OK', async () => { | ||
const { response } = await api.sections.search(); | ||
const response = await api.sections.search(); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.status).toEqual('ok'); | ||
}); | ||
it('actually returns content', async () => { | ||
const { body } = await api.tags.search('world'); | ||
const response = await api.tags.search('world'); | ||
expect(JSON.parse(body).response.results.length).toBeGreaterThan(0); | ||
expect(response.results.length).toBeGreaterThan(0); | ||
}); | ||
@@ -81,11 +84,11 @@ }); | ||
it('has a search function that returns OK', async () => { | ||
const { response } = await api.editions.search(); | ||
const response = await api.editions.search(); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.status).toEqual('ok'); | ||
}); | ||
it('actually returns content', async () => { | ||
const { body } = await api.editions.search('u'); | ||
const response = await api.editions.search('u'); | ||
expect(JSON.parse(body).response.results.length).toBeGreaterThan(0); | ||
expect(response.results.length).toBeGreaterThan(0); | ||
}); | ||
@@ -96,12 +99,12 @@ }); | ||
it('has a search function that returns OK', async () => { | ||
const { response } = await api.item.getById('business/2014/feb/18/uk-inflation-falls-below-bank-england-target'); | ||
const response = await api.item.getById('business/2014/feb/18/uk-inflation-falls-below-bank-england-target'); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.status).toEqual('ok'); | ||
}); | ||
it('actually returns content', async () => { | ||
const { body } = await api.item.getById('business/2014/feb/18/uk-inflation-falls-below-bank-england-target'); | ||
const response = await api.item.getById('business/2014/feb/18/uk-inflation-falls-below-bank-england-target'); | ||
expect(JSON.parse(body).response.total).toBeGreaterThan(0); | ||
expect(response.total).toBeGreaterThan(0); | ||
}); | ||
}); |
@@ -6,4 +6,7 @@ { | ||
"module": "commonjs", | ||
"target": "es2015", | ||
"lib": ["es2020"] | ||
"target": "es2020", | ||
"lib": ["es2020", "DOM"], | ||
"strict": false, | ||
"skipLibCheck": true, | ||
"types": [] | ||
}, | ||
@@ -14,5 +17,7 @@ "include": [ | ||
"exclude": [ | ||
"node_modules", | ||
"**/*.spec.ts" | ||
"node_modules", | ||
"./node_modules", | ||
"./node_modules/*", | ||
"./node_modules/@types/node/index.d.ts" | ||
] | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14358
0
2
338
1
- Removedrequest@^2.88.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.13.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)