Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search 1 helper and tests #19

Merged
merged 3 commits into from
May 26, 2024
Merged

Search 1 helper and tests #19

merged 3 commits into from
May 26, 2024

Conversation

stephenwf
Copy link
Member

@stephenwf stephenwf commented May 25, 2024

Adds new new helpers:

  • Search 1 Autocomplete
  • Search 1

These can be used with Search1 compatible services. Likely need some additional helpers in the future - for example, creating a query FROM an autocomplete result - so they work better together.

Both have fetch cancellation, so you can do:

autocomplete.search('w');
autocomplete.search('wu');
autocomplete.search('wun');
autocomplete.search('wund');
autocomplete.search('wunde');
autocomplete.search('wunder'); // <- last one called only.

And the first 5 will be cancelled, and the last will resolve. This should make it easy to hook up to UIs.

Autocomplete search1 store:

import { createSearch1AutocompleteStore } from '@iiif/helpers/search1';

const example = {
  '@id': 'https://iiif.wellcomecollection.org/search/v1/b18035723',
  '@type': 'SearchService1',
  profile: 'http://iiif.io/api/search/1/search',
  label: 'Search within this manifest',
  service: {
    '@id': 'https://iiif.wellcomecollection.org/search/autocomplete/v1/b18035723',
    '@type': 'AutoCompleteService1',
    profile: 'http://iiif.io/api/search/1/autocomplete',
    label: 'Autocomplete words in this manifest',
  },
};

const autocomplete = createSearch1AutocompleteStore(example);

const store = autocomplete.getState();

await store.search('wunder');

console.log(autocomplete.getState().results); //  [{ match: 'wunder', search: '...', ... }]

Search 1 store

import { createSearch1Store } from '@iiif/helpers/search1';

const example = {
  '@id': 'https://iiif.wellcomecollection.org/search/v1/b18035723',
  '@type': 'SearchService1',
  profile: 'http://iiif.io/api/search/1/search',
  label: 'Search within this manifest',
  service: {
    '@id': 'https://iiif.wellcomecollection.org/search/autocomplete/v1/b18035723',
    '@type': 'AutoCompleteService1',
    profile: 'http://iiif.io/api/search/1/autocomplete',
    label: 'Autocomplete words in this manifest',
  },
};

const autocomplete = createSearch1Store(example);

const store = autocomplete.getState();

await store.search({ q: 'wunder' });

const state = autocomplete.getState();

state.resources; // 

The full shape of the store:

export interface Search1Store {
  endpoint: string | undefined;
  service: Search1Service | undefined;
  lastQuery: SearchServiceQueryParams;
  resources: SearchServiceSearchResponse['resources'];
  loading: boolean;
  error: boolean;
  hasAutocomplete: boolean;
  hasSearch: boolean;
  errorMessage: string;
  highlight: SearchServiceSearchResponse['resources'][number] | null;
  search: (query: SearchServiceQueryParams, options?: { headers?: HeadersInit }) => void | Promise<void>;
  clearSearch: () => void;
  highlightResult: (id: string) => void;
  nextResult: () => void;
}

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@stephenwf stephenwf merged commit 5f51395 into main May 26, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant