-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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): add rxjs example (#8108)
- Loading branch information
Showing
24 changed files
with
509 additions
and
28 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
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
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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "Node.js", | ||
"image": "mcr.microsoft.com/devcontainers/javascript-node:18" | ||
} |
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,6 @@ | ||
// @ts-check | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
const config = {} | ||
|
||
module.exports = config |
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,6 @@ | ||
# TanStack Query Angular RxJS Example | ||
|
||
To run this example: | ||
|
||
- `npm install` or `yarn` or `pnpm i` or `bun i` | ||
- `npm run start` or `yarn start` or `pnpm start` or `bun start` |
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,104 @@ | ||
{ | ||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json", | ||
"version": 1, | ||
"cli": { | ||
"packageManager": "pnpm", | ||
"analytics": false, | ||
"cache": { | ||
"enabled": false | ||
} | ||
}, | ||
"newProjectRoot": "projects", | ||
"projects": { | ||
"basic": { | ||
"projectType": "application", | ||
"schematics": { | ||
"@schematics/angular:component": { | ||
"inlineTemplate": true, | ||
"inlineStyle": true, | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:class": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:directive": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:guard": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:interceptor": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:pipe": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:resolver": { | ||
"skipTests": true | ||
}, | ||
"@schematics/angular:service": { | ||
"skipTests": true | ||
} | ||
}, | ||
"root": "", | ||
"sourceRoot": "src", | ||
"prefix": "app", | ||
"architect": { | ||
"build": { | ||
"builder": "@angular-devkit/build-angular:application", | ||
"options": { | ||
"outputPath": "dist/basic", | ||
"index": "src/index.html", | ||
"browser": "src/main.ts", | ||
"polyfills": ["zone.js"], | ||
"tsConfig": "tsconfig.app.json", | ||
"assets": ["src/favicon.ico", "src/mockServiceWorker.js"], | ||
"styles": [], | ||
"scripts": [] | ||
}, | ||
"configurations": { | ||
"production": { | ||
"budgets": [ | ||
{ | ||
"type": "initial", | ||
"maximumWarning": "500kb", | ||
"maximumError": "1mb" | ||
}, | ||
{ | ||
"type": "anyComponentStyle", | ||
"maximumWarning": "2kb", | ||
"maximumError": "4kb" | ||
} | ||
], | ||
"outputHashing": "all" | ||
}, | ||
"development": { | ||
"optimization": false, | ||
"extractLicenses": false, | ||
"sourceMap": true | ||
} | ||
}, | ||
"defaultConfiguration": "production" | ||
}, | ||
"serve": { | ||
"builder": "@angular-devkit/build-angular:dev-server", | ||
"configurations": { | ||
"production": { | ||
"buildTarget": "basic:build:production" | ||
}, | ||
"development": { | ||
"buildTarget": "basic:build:development" | ||
} | ||
}, | ||
"defaultConfiguration": "development" | ||
}, | ||
"extract-i18n": { | ||
"builder": "@angular-devkit/build-angular:extract-i18n", | ||
"options": { | ||
"buildTarget": "basic:build" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,31 @@ | ||
{ | ||
"name": "@tanstack/query-example-angular-rxjs", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"ng": "ng", | ||
"start": "ng serve", | ||
"build": "ng build", | ||
"watch": "ng build --watch --configuration development" | ||
}, | ||
"dependencies": { | ||
"@angular/cdk": "17.3.10", | ||
"@angular/common": "^17.3.12", | ||
"@angular/compiler": "^17.3.12", | ||
"@angular/core": "^17.3.12", | ||
"@angular/forms": "17.3.12", | ||
"@angular/platform-browser": "^17.3.12", | ||
"@angular/platform-browser-dynamic": "^17.3.12", | ||
"@tanstack/angular-query-experimental": "^5.56.2", | ||
"rxjs": "^7.8.1", | ||
"tslib": "^2.6.3", | ||
"zone.js": "^0.14.8" | ||
}, | ||
"devDependencies": { | ||
"@angular-devkit/build-angular": "^17.3.8", | ||
"@angular/cli": "^17.3.8", | ||
"@angular/compiler-cli": "^17.3.12", | ||
"@tanstack/angular-query-devtools-experimental": "^5.58.0", | ||
"typescript": "5.3.3" | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
examples/angular/rxjs/src/app/api/autocomplete-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,51 @@ | ||
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) | ||
} |
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,12 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core' | ||
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental' | ||
import { ExampleComponent } from './components/example.component' | ||
|
||
@Component({ | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
selector: 'app-root', | ||
standalone: true, | ||
template: `<example /><angular-query-devtools initialIsOpen />`, | ||
imports: [AngularQueryDevtools, ExampleComponent], | ||
}) | ||
export class AppComponent {} |
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,29 @@ | ||
import { | ||
provideHttpClient, | ||
withFetch, | ||
withInterceptors, | ||
} from '@angular/common/http' | ||
import { | ||
QueryClient, | ||
provideAngularQuery, | ||
} from '@tanstack/angular-query-experimental' | ||
import { autocompleteMockInterceptor } from './api/autocomplete-mock.interceptor' | ||
import type { ApplicationConfig } from '@angular/core' | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideAngularQuery( | ||
new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
gcTime: 1000 * 60 * 60 * 24, // 24 hours | ||
}, | ||
}, | ||
}), | ||
), | ||
provideHttpClient( | ||
withFetch(), | ||
withInterceptors([autocompleteMockInterceptor]), | ||
), | ||
], | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/angular/rxjs/src/app/components/example.component.html
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,13 @@ | ||
<h1>Search for a programming language</h1> | ||
|
||
<form [formGroup]="form"> | ||
<input type="text" formControlName="term" /> | ||
|
||
@if (query.isSuccess() && query.data().suggestions.length) { | ||
<ul> | ||
@for (suggestion of query.data().suggestions; track suggestion) { | ||
<li>{{ suggestion }}</li> | ||
} | ||
</ul> | ||
} | ||
</form> |
45 changes: 45 additions & 0 deletions
45
examples/angular/rxjs/src/app/components/example.component.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 { ChangeDetectionStrategy, Component, inject } from '@angular/core' | ||
import { toSignal } from '@angular/core/rxjs-interop' | ||
import { NonNullableFormBuilder, ReactiveFormsModule } from '@angular/forms' | ||
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental' | ||
import { | ||
injectQuery, | ||
keepPreviousData, | ||
} from '@tanstack/angular-query-experimental' | ||
import { debounceTime, distinctUntilChanged, lastValueFrom } from 'rxjs' | ||
import { AutocompleteService } from '../services/autocomplete-service' | ||
|
||
@Component({ | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
selector: 'example', | ||
standalone: true, | ||
templateUrl: './example.component.html', | ||
imports: [AngularQueryDevtools, ReactiveFormsModule], | ||
}) | ||
export class ExampleComponent { | ||
#autocompleteService = inject(AutocompleteService) | ||
#fb = inject(NonNullableFormBuilder) | ||
|
||
form = this.#fb.group({ | ||
term: '', | ||
}) | ||
|
||
term = toSignal( | ||
this.form.controls.term.valueChanges.pipe( | ||
debounceTime(300), | ||
distinctUntilChanged(), | ||
), | ||
{ initialValue: '' }, | ||
) | ||
|
||
query = injectQuery(() => ({ | ||
queryKey: ['suggestions', this.term()], | ||
queryFn: () => { | ||
return lastValueFrom( | ||
this.#autocompleteService.getSuggestions(this.term()), | ||
) | ||
}, | ||
placeholderData: keepPreviousData, | ||
staleTime: 1000 * 60 * 5, // 5 minutes | ||
})) | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/angular/rxjs/src/app/services/autocomplete-service.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,18 @@ | ||
import { HttpClient } from '@angular/common/http' | ||
import { Injectable, inject } from '@angular/core' | ||
import { of } from 'rxjs' | ||
|
||
interface Response { | ||
suggestions: Array<string> | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AutocompleteService { | ||
#http = inject(HttpClient) | ||
getSuggestions = (term: string) => | ||
term.trim() === '' | ||
? of({ suggestions: [] }) | ||
: this.#http.get<Response>(`/api/autocomplete?term=${term}`) | ||
} |
Binary file not shown.
Oops, something went wrong.