-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(angular-query): replace msw with http interceptor (#6870)
interceptor works better in sandbox environments
- Loading branch information
Showing
8 changed files
with
84 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
examples/angular/infinite-query-with-max-pages/src/app/api/projects-mock.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 projectsMockInterceptor: HttpInterceptorFn = ( | ||
req, | ||
next, | ||
): Observable<HttpEvent<any>> => { | ||
const { url } = req | ||
|
||
if (url.includes('/api/projects')) { | ||
const cursor = parseInt( | ||
new URLSearchParams(req.url.split('?')[1]).get('cursor') || '0', | ||
10, | ||
) | ||
const pageSize = 4 | ||
|
||
const data = Array(pageSize) | ||
.fill(0) | ||
.map((_, i) => { | ||
return { | ||
name: 'Project ' + (i + cursor) + ` (server time: ${Date.now()})`, | ||
id: i + cursor, | ||
} | ||
}) | ||
|
||
const nextId = cursor < 20 ? data[data.length - 1].id + 1 : null | ||
const previousId = cursor > -20 ? data[0].id - pageSize : null | ||
|
||
// 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: { | ||
data, | ||
nextId, | ||
previousId, | ||
}, | ||
}), | ||
).pipe(delayWhen(() => timer(delayDuration))) | ||
} | ||
return next(req) | ||
} |
11 changes: 8 additions & 3 deletions
11
examples/angular/infinite-query-with-max-pages/src/app/app.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,5 @@ | ||
import { bootstrapApplication } from '@angular/platform-browser' | ||
import { appConfig } from './app/app.config' | ||
import { isDevMode } from '@angular/core' | ||
import { AppComponent } from './app/app.component' | ||
|
||
async function prepareApp() { | ||
if (isDevMode()) { | ||
const { worker } = await import('./mocks/browser') | ||
return worker.start({ | ||
quiet: true, | ||
}) | ||
} | ||
|
||
return Promise.resolve() | ||
} | ||
|
||
prepareApp().then(() => { | ||
bootstrapApplication(AppComponent, appConfig).catch((err) => | ||
console.error(err), | ||
) | ||
}) | ||
bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err)) |
287 changes: 0 additions & 287 deletions
287
examples/angular/infinite-query-with-max-pages/src/mockServiceWorker.js
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
examples/angular/infinite-query-with-max-pages/src/mocks/browser.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.