Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

autocomplete-service.ts

21 lines523 Bytes

1import { HttpClient } from '@angular/common/http'
2import { Injectable, inject } from '@angular/core'
3import { of } from 'rxjs'
4​
5interface SuggestionsResponse {
6 suggestions: Array<string>
7}
8​
9@Injectable({
10 providedIn: 'root',
11})
12export class AutocompleteService {
13 readonly #http = inject(HttpClient)
14​
15 getSuggestions = (term: string = '') =>
16 term.trim() === ''
17 ? of({ suggestions: [] })
18 : this.#http.get<SuggestionsResponse>(
19 `/api/autocomplete?term=${encodeURIComponent(term)}`,
20 )