Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

guardian-js

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

guardian-js - npm Package Compare versions

Comparing version 4.0.1 to 5.0.0

26

dist/Endpoint.d.ts

@@ -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 {};

68

dist/Endpoint.js
"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"
]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc