Socket
Socket
Sign inDemoInstall

govuk

Package Overview
Dependencies
13
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    govuk

JavaScript API client for GOV.UK Content and Search APIs.


Version published
Maintainers
1
Install size
9.03 MB
Created

Readme

Source

GOV.UK API Client

JavaScript API client for GOV.UK Content and Search APIs.

GOVUK API latest npm version

Contents

Getting started

Node

npm init mjs -y # initialise module-ready package.json
npm install govuk
// index.js;
import { SearchAPI, ContentAPI } from "govuk";

const searchApi = new SearchAPI();
const contentApi = new ContentAPI();

const results = await searchApi.get("Keeping a pet pig");
// Find the first result that is closest...
const searchItem = results.find((item) => item.title.includes("micropig"));
const contentItem = await contentApi.get(searchItem.link);

console.log(contentItem);
node index.js

Browser

<!-- index.html -->
<script type="module">
  import { SearchAPI, ContentAPI } from "https://unpkg.com/govuk";

  const searchApi = new SearchAPI();
  const contentApi = new ContentAPI();

  const results = await searchApi.get("Keeping a pet pig");
  // Find the first result that is closest...
  const searchItem = results.find((item) => item.title.includes("micropig"));
  const contentItem = await contentApi.get(searchItem.link);

  console.log(contentItem);
</script>

Examples

Content API

Implements the GOV.UK Content API.

get(path)

Get a content item.

ParameterTypeRequiredDescription
pathstringtrueThe path to the content on GOV.UK e.g for https://www.gov.uk/register-to-vote you’d use register-to-vote

Returns a content item from a promise or emitted by the 'data' event.

Getting data from resolved promise
import { ContentAPI } from "govuk";
const api = new ContentAPI();
const contentItem = await api.get("Register-to-vote");
console.log(contentItem);
Getting data from event
import { ContentAPI } from "govuk";
const api = new ContentAPI();
api.on("data", (contentItem) => {
  console.log(contentItem);
});
api.get("Register-to-vote");

Search API

Implements the GOV.UK Search API.

Use the search API to get useful information about GOV.UK content

constructor(queryOrOptions, [options])

Set the default query and options for all other calls to get, getAll and total methods

ParameterTypeRequired
queryOrOptionsstring | Optionstrue
[options]Optionsfalse
Getting data from resolved promise
import { SearchAPI } from "govuk";
const api = new SearchAPI("Micro pig", { count: 10 });
const searchResults = await api.get();
console.log(searchResults);

get(queryOrOptions, [options])

Get first page of search items for a query

ParameterTypeRequired
queryOrOptionsstring | Optionstrue
[options]Optionsfalse
Getting data from resolved promise
import { SearchAPI } from "govuk";
const api = new SearchAPI();
const searchResults = await api.get("Micro pig");
console.log(searchResults);
Getting data from event
import { SearchAPI } from "govuk";
const api = new SearchAPI();
api.on("data", (searchResults) => {
  console.log(searchResults);
});
api.get("Micro pig");

getAll(queryOrOptions, [options])

Get all pages of search items for a query.

ParameterTypeRequiredDescription
queryOrOptionsstring | Optionstrue
[options]Optionsfalse
options.totalnumberfalsemaximum amount of results
Getting data from resolved promise
import { SearchAPI } from "govuk";
const api = new SearchAPI();
const searchResults = await api.getAll("Micro pig");
console.log(searchResults);
Getting data from event
import { SearchAPI } from "govuk";
const api = new SearchAPI();
api.on("data", (searchResults) => {
  console.log(searchResults);
});
api.getAll("Micro pig");

info(path)

Get metadata for a content item.

ParameterTypeRequiredDescription
pathstringtrueThe path to the content on GOV.UK e.g for https://www.gov.uk/register-to-vote you’d use register-to-vote
Getting info from resolved promise
import { SearchAPI } from "govuk";
const api = new SearchAPI();
const contentInfo = await api.info("register-to-vote");
console.log(contentInfo);

total(queryOrOptions, [options])

Get total amount of search items for a query.

ParameterTypeRequired
queryOrOptionsstring | Optionstrue
[options]Optionsfalse
Getting total from resolved promise
import { SearchAPI } from "govuk";
const api = new SearchAPI();
const totalResults = await api.total("Micro pig");
console.log(totalResults);

facets(field)

Get facets for a field.

ParameterTypeRequired
fieldstringtrue
Getting facets from resolved promise
import { SearchAPI } from "govuk";
const api = new SearchAPI();
const facets = await api.facets("formats");
console.log(facets);

Options

You can use any options available in the Search API.

NameTypeDescription
qstringsearch query
Pagination options
NameTypeDescription
startnumberposition to start
countnumbernumber of results to return
orderstringsort order
Field options
NameTypeDescription
fieldsArrayproperties to return in search item
Faceted options
NameTypeDescription
filter.[field]stringfield to filter by
aggregate.[field]stringfield to aggregate by
reject.[field]stringfield to reject by
facet.[field]stringgroup by field

FAQs

Last updated on 12 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc