File size: 1,206 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import { HttpResponse } from '@angular/common/http'
import { delayWhen, of, timer } from 'rxjs'
import type { Observable } from 'rxjs'
import type { HttpEvent, HttpInterceptorFn } from '@angular/common/http'
export const autocompleteMockInterceptor: HttpInterceptorFn = (
req,
next,
): Observable<HttpEvent<any>> => {
const { url } = req
if (url.includes('/api/autocomplete')) {
const term = new URLSearchParams(req.url.split('?')[1]).get('term') || ''
const data = [
'C#',
'C++',
'Go',
'Java',
'JavaScript',
'Kotlin',
'Lisp',
'Objective-C',
'PHP',
'Perl',
'Python',
'R',
'Ruby',
'Rust',
'SQL',
'Scala',
'Shell',
'Swift',
'TypeScript',
]
// Simulate network latency with a random delay between 100ms and 500ms
const delayDuration = Math.random() * (500 - 100) + 100
return of(
new HttpResponse({
status: 200,
body: {
suggestions: data.filter((item) =>
item.toLowerCase().startsWith(term.toLowerCase()),
),
},
}),
).pipe(delayWhen(() => timer(delayDuration)))
}
return next(req)
}
|