2.0.1597 • Published 15 days ago

@nice-digital/search-client v2.0.1597

Weekly downloads
-
License
MIT
Repository
github
Last release
15 days ago

@nice-digital/search-client

Client for using NICE search in Node and browsers

Stack

Local development

Install Node

Use the version of Node that's pinned for Volta in package.json (see volta.node). The easiest way is using Volta, a JavaScript tool manager: cd into the cloned repo and Volta will automatically use the correct version of Node. If you're not using Volta then download and install the correct Node LTS version.

Build TypeScript

Run npm run ts to compile the TypeScript src files into the lib, es and types folders - these are what project import when the package is installed from npm.

Watch mode

Run npm run ts:es:watch to compile the TypeScript src files into the es folder in watch mode. This watches for changes and recompiles on save. This is useful for both real time type-checking and linking into a search project for real-world testing:

Linking

Linking search client into an actual project is great for development. It allows testing of integration with a real project, rather than just running unit tests.

First run npm link in this repository. Then, in your project that's consuming the search client (e.g. Next-Web), run npm link @nice-digital/search-client. Under the hood this sets up a symlink to this repo. Once the projects are linked, run npm run ts:es:watch as above to watch for changes.

Tests

Run npm test to run Jest tests. Or run npm run test:coverage to generate coverage into the coverage folder. Or run npm run test:watch to run tests and watch for changes.

Usage

Install Node

Install Node 14+ LTS in your project that's going to consume the search client.

Install TypeScript 4.5+

If you're using TypeScript, make sure it's version 4.5 or above.

Version 2 of @nice-digital/search-client uses import { type SomeName } from ... style imports (part of TypeScript 4.5), which requires the consuming project to also be using TypeScript 4.5+.

Version 1 had regular import type { SomeName } from ... style imports so works with TypeScript <4.5.

Install search client

Install the @nice-digital/search-client from npm:

npm i @nice-digital/search-client -s

Note the -s switch which installs the package as a dependency rather than a dev dependency.

Import and initialise

Import initialise:

import { initialise, search } from "@nice-digital/search-client";

Then call initialise with the Single Search API base URL and the index. For example to use the local search endpoint and the nice index:

import { initialise } from "@nice-digital/search-client";

// Note: you need to `initialise` before you can `search`
initialise({ baseURL: "https://localhost:44328/api", index: "nice" });

Find the dev, test, alpha, beta and live Single Search Endpoint base url from ops.

Main search methods

Now that the search client is initialised with a baseURL and index, you can call methods to retrieve data from ElasticSearch via the Single Search Endpoint. Import and call one of search, download, typeAhead or getDocument (see the example below).

Note: the main search methods each return a Promise that resolves with the data returned from the Single Search Endpoint. This means you'll need to await the methods inside an async function (or use then e.g. search("/search?q=test").then((data) => { /* do something with data */ })). Note: it has to be inside an async function becase Top-Level Await is still a draft proposal.

As an example, with NextJS you'd call search from within getServerSideProps:

import { initialise, search } from "@nice-digital/search-client";

export async function getServerSideProps(context) {
    initialise({ baseURL: "https://localhost:44328/api", index: "nice" });
    
    const searchResults = await search(context.resolvedUrl);

    return {
        props: {
            searchResults
        },
    }
}

TypeScript

The @nice-digital/search-client package is written in TypeScript and ships with TypeScript definitions. This means you can import the type defs for advanced use-cases:

import { SearchResults, Document } from "@nice-digital/search-client";

Browser usage

The @nice-digital/search-client package:

  • uses axios under the hood, with supports both the browser and node.js
  • and is transpiled to ES5 before publishing to npm.

These 2 things mean the library will work both server-side (e.g. via getServerSideProps in NextJS) or on the client for direct Single Search Endpoint communication. It will also work with webpack (or other bundlers) without extra config because it's published as ES5.

Finally, we also publish both:

  • a CommonJS module output to the lib folder (referenced via main in package.json)
  • an ES6 module output to the es folder (referenced via module in package.json).

Webpack supports the module property (see this guide), which will load the ES6 modules, which in turn supports tree-shaking which in turn results in smaller bundles including only code that is actually used.

Error handling

The @nice-digital/search-client package handles any errors e.g. network problems, server errrors or timeouts. This means methods like search shouldn't throw, instead they'll return an object like:

{
    "errorMessage": "Error: timeout of 3000ms exceeded",
    "failed": true,
}

There are unit tests for error states.

2.0.1589

5 months ago

2.0.1597

3 months ago

2.0.1595

3 months ago

2.0.1583

9 months ago

2.0.1582

10 months ago

2.0.1579

10 months ago

2.0.1578

11 months ago

2.0.1566

1 year ago

2.0.1562

1 year ago

2.0.1573

12 months ago

2.0.1571

1 year ago

2.0.1568-master

1 year ago

2.0.1554

1 year ago

2.0.1550

1 year ago

2.0.1534

2 years ago

2.0.1532

2 years ago

2.0.1530

2 years ago

2.0.1528

2 years ago

2.0.1537

2 years ago

2.0.1526

2 years ago

2.0.1527

2 years ago

2.0.1525

2 years ago

2.0.1521

2 years ago

2.0.1507-master

2 years ago

2.0.1503

2 years ago

2.0.1492

2 years ago

2.0.1499-master

2 years ago

2.0.1476

2 years ago

2.0.1473

2 years ago

2.0.1458

2 years ago

2.0.1470

2 years ago

2.0.1459-master

2 years ago

2.0.1451

3 years ago

2.0.1447

3 years ago

2.0.1442

3 years ago

2.0.1441

3 years ago

1.0.1419

3 years ago

2.0.1438

3 years ago

2.0.1435-master

3 years ago

2.0.1434-master

3 years ago

1.1.1422

3 years ago

2.0.1432

3 years ago

2.0.1426

3 years ago

1.0.1408

3 years ago

1.0.1404

3 years ago

1.0.1402

3 years ago

0.2.1399

3 years ago

0.2.1398

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago