npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@empathyco/x-adapter-platform

v1.1.0-alpha.8

Published

A search client for the Empathy Platform API

Downloads

5,586

Readme

x-adapter-platform

Empathy Platform Adapter is a library to ease the communication with empathy.co Empathy Platform API. Built upon the x-adapter library, it contains a sample configuration for setup, global configurations, mappers and models.

It is used as the default adapter configuration in x-archetype, a standardised implementation of the x-components library, which imports it as a plugin.

Tech Stack

TypeScript Jest

Installation

# or pnpm or yarn
npm install @empathyco/x-adapter-platform

Configuration & Usage

The PlatformAdapter is an object containing several endpoint adapters.

Each EndpointAdapter contains the configuration of an endpoint, including the URL, the mapper to adapt the responses and the requests, the request options ...

export const platformAdapter: PlatformAdapter = {
  search: searchEndpointAdapter,
  popularSearches: popularSearchesEndpointAdapter,
  recommendations: recommendationsEndpointAdapter,
  nextQueries: nextQueriesEndpointAdapter,
  querySuggestions: querySuggestionsEndpointAdapter,
  relatedTags: relatedTagsEndpointAdapter,
  identifierResults: identifierResultsEndpointAdapter,
  tagging: taggingEndpointAdapter
};

Platform Endpoint Adapters & usage

The Empathy Platform API has the particularity of needing an env, instance and a language to be passed in each endpoint call (except the tagging).

In an x-archetype project context, which would be the recommended scenario to use this package, these parameters are configured through the snippetConfig.

If you are not using the x-platform-adapter inside an x-archetype based project, but you want to use the Empathy Platform API, you can use the extraParams field to specify the required parameters to make it work, as shown in the examples below.

Search endpoint adapter

The search endpoint will include an array of results in its response.

import { platformAdapter } from '@empathyco/x-adapter-platform';

const { results } = await platformAdapter.search({
  query: 'trousers',
  extraParams: {
    lang: 'en',
    instance: 'empathy',
    env: 'staging'
  }
});
Popular searches endpoint adapter

The PopularSearches endpoint will return top searched queries.

import { platformAdapter } from '@empathyco/x-adapter-platform';

const { suggestions } = await platformAdapter.popularSearches({
  extraParams: {
    lang: 'en',
    instance: 'empathy',
    env: 'staging'
  }
});
Recommendations endpoint adapter

These recommendations are top clicked products based on user click interactions (note: no personal user data is collected).

import { platformAdapter } from '@empathyco/x-adapter-platform';

const { results } = await platformAdapter.recommendations({
  extraParams: {
    lang: 'en',
    instance: 'empathy',
    env: 'staging'
  }
});
Next Queries endpoint adapter

The NextQueries endpoint returns recurring searches that users tend to do after searching for a specific item. The aim is to suggest a new term that the user may be interested in.

The NextQueriesRequest is usually done after a SearchRequest has been made.

import { platformAdapter } from '@empathyco/x-adapter-platform';

const { nextQueries } = await platformAdapter.nextQueries({
  query: 'trousers',
  extraParams: {
    lang: 'en',
    instance: 'empathy',
    env: 'staging'
  }
});
Query suggestions endpoint adapter

The QuerySuggestions endpoint returns suggestions based on a query. For example, for the query "trousers" we could have "denim trousers, cargo trousers, chino trousers, etc..." as query suggestions.

import { platformAdapter } from '@empathyco/x-adapter-platform';

const { suggestions } = await platformAdapter.querySuggestions({
  query: 'trousers',
  extraParams: {
    lang: 'en',
    instance: 'empathy',
    env: 'staging'
  }
});
Related tags endpoint adapter

The RelatedTags endpoint will return terms used to help refining a search query by adding more specificity (e.g, adjectives: long, short, gluten-free; categories: kids, summer...).

import { platformAdapter } from '@empathyco/x-adapter-platform';

const { relatedTags } = await platformAdapter.relatedTags({
  query: 'trousers',
  extraParams: {
    lang: 'en',
    instance: 'empathy',
    env: 'staging'
  }
});
Identifier results endpoint adapter

The IdentifierResults endpoint will return the results whose identifier matches a given query.

import { platformAdapter } from '@empathyco/x-adapter-platform';

const { results } = await platformAdapter.identifierResults({
  query: '1234',
  extraParams: {
    lang: 'en',
    instance: 'empathy',
    env: 'staging'
  }
});
Tagging endpoint adapter

The Tagging endpoint allows sending events to a server to collect metrics about how the search is performing (this won't collect user data, just the use of tools per session).

You can configure your tagging object following the Taggable interface, which contains several user actions and the capability to include your own.

import { platformAdapter } from '@empathyco/x-adapter-platform';

const tagging = await platformAdapter.tagging({
  // The tagging request will be sent to this URL
  url: 'https://api.staging.empathy.co/tagging/v1/track/empathy/query',
  // Info that will be sent along with the query event
  params: {
    filtered: 'false',
    lang: 'en',
    origin: 'customer:no_query',
    page: '1',
    q: 'trousers',
    scope: 'desktop',
    spellcheck: 'false',
    totalHits: 700
  }
});

Modifying the x-platform-adapter

Each request and response schemas are created as MutableSchemas, so they can be modified using the $extends, $override, $replace methods accordingly. You can check the x-adapter's package documentation for further details.

Example of overriding a response by adding a new parameter
import { PlatformResult, resultSchema } from '@empathyco/x-adapter-platform';
import { Result } from '@empathyco/x-types';

interface EmpathyDemoPlatformResult extends PlatformResult {
  season: string;
}

declare module '@empathyco/x-types' {
  export interface Result {
    season: string;
  }
}

resultSchema.$override<EmpathyDemoPlatformResult, Partial<Result>>({
  season: 'season'
});

Test

Empathy Adapter Platform features are tested using Jest. You will find a __tests__ folder inside the src folder with tests for each of the endpoint calls, and also inside each of the project's sections functionalities (endpoint-adapters, mappers and schemas).

npm run test

Changelog

Changelog summary

Contributing

To start contributing to the project, please take a look at our Contributing Guide. Take in account that x-adapter-platform is developed using Typescript, so we recommend you to check it out.

License

empathyco/x License