mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-03-17 17:10:03 +01:00
Add AutocompleteClient
wrapper over the HTTP API
This commit is contained in:
parent
3c2a3e956a
commit
6948aa5d1c
1 changed files with 73 additions and 0 deletions
73
assets/js/autocomplete/client.ts
Normal file
73
assets/js/autocomplete/client.ts
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
import { HttpClient } from '../utils/http-client.ts';
|
||||||
|
|
||||||
|
export interface TagSuggestion {
|
||||||
|
/**
|
||||||
|
* If present, then this suggestion is for a tag alias.
|
||||||
|
* If absent, then this suggestion is for the `canonical` tag name.
|
||||||
|
*/
|
||||||
|
alias?: null | string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The canonical name of the tag (non-alias).
|
||||||
|
*/
|
||||||
|
canonical: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Number of images tagged with this tag.
|
||||||
|
*/
|
||||||
|
images: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GetTagSuggestionsResponse {
|
||||||
|
suggestions: TagSuggestion[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GetTagSuggestionsRequest {
|
||||||
|
/**
|
||||||
|
* Term to complete.
|
||||||
|
*/
|
||||||
|
term: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum number of suggestions to return.
|
||||||
|
*/
|
||||||
|
limit: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Autocomplete API client for Philomena backend.
|
||||||
|
*/
|
||||||
|
export class AutocompleteClient {
|
||||||
|
private http: HttpClient = new HttpClient();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches server-side tag suggestions for the given term. The provided incomplete
|
||||||
|
* term is expected to be normalized by the caller (i.e. lowercased and trimmed).
|
||||||
|
* This is because the caller is responsible for caching the normalized term.
|
||||||
|
*/
|
||||||
|
async getTagSuggestions(request: GetTagSuggestionsRequest): Promise<GetTagSuggestionsResponse> {
|
||||||
|
return this.http.fetchJson('/autocomplete/tags', {
|
||||||
|
query: {
|
||||||
|
vsn: '2',
|
||||||
|
term: request.term,
|
||||||
|
limit: request.limit.toString(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Issues a GET request to fetch the compiled autocomplete index.
|
||||||
|
*/
|
||||||
|
async getCompiledAutocomplete(): Promise<ArrayBuffer> {
|
||||||
|
const now = new Date();
|
||||||
|
const key = `${now.getUTCFullYear()}-${now.getUTCMonth()}-${now.getUTCDate()}`;
|
||||||
|
|
||||||
|
const response = await this.http.fetch(`/autocomplete/compiled`, {
|
||||||
|
query: { vsn: '2', key },
|
||||||
|
credentials: 'omit',
|
||||||
|
cache: 'force-cache',
|
||||||
|
});
|
||||||
|
|
||||||
|
return response.arrayBuffer();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue