File size: 907 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
import { HttpResponse } from '@angular/common/http'
import { delay, of } from 'rxjs'
import type { Observable } from 'rxjs'
import type { HttpEvent, HttpInterceptorFn } from '@angular/common/http'

export const projectsMockInterceptor: HttpInterceptorFn = (
  req,
  next,
): Observable<HttpEvent<any>> => {
  const { url } = req

  if (url.includes('/api/projects')) {
    const page = parseInt(
      new URLSearchParams(req.url.split('?')[1]).get('page') || '0',
      10,
    )
    const pageSize = 10

    const projects = Array(pageSize)
      .fill(0)
      .map((_, i) => {
        const id = page * pageSize + (i + 1)
        return {
          name: 'Project ' + id,
          id,
        }
      })

    return of(
      new HttpResponse({
        status: 200,
        body: {
          projects,
          hasMore: page < 9,
        },
      }),
    ).pipe(delay(1000))
  }
  return next(req)
}