snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
4
examples/angular/rxjs/.devcontainer/devcontainer.json
Normal file
4
examples/angular/rxjs/.devcontainer/devcontainer.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "Node.js",
|
||||
"image": "mcr.microsoft.com/devcontainers/javascript-node:22"
|
||||
}
|
||||
6
examples/angular/rxjs/.eslintrc.cjs
Normal file
6
examples/angular/rxjs/.eslintrc.cjs
Normal file
@@ -0,0 +1,6 @@
|
||||
// @ts-check
|
||||
|
||||
/** @type {import('eslint').Linter.Config} */
|
||||
const config = {}
|
||||
|
||||
module.exports = config
|
||||
6
examples/angular/rxjs/README.md
Normal file
6
examples/angular/rxjs/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# TanStack Query Angular RxJS Example
|
||||
|
||||
To run this example:
|
||||
|
||||
- `pnpm install`
|
||||
- `pnpm start`
|
||||
130
examples/angular/rxjs/angular.json
Normal file
130
examples/angular/rxjs/angular.json
Normal file
@@ -0,0 +1,130 @@
|
||||
{
|
||||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
||||
"version": 1,
|
||||
"cli": {
|
||||
"packageManager": "pnpm",
|
||||
"analytics": false,
|
||||
"cache": {
|
||||
"enabled": false
|
||||
}
|
||||
},
|
||||
"newProjectRoot": "projects",
|
||||
"projects": {
|
||||
"rxjs": {
|
||||
"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/build:application",
|
||||
"options": {
|
||||
"outputPath": "dist/rxjs",
|
||||
"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/build:dev-server",
|
||||
"configurations": {
|
||||
"production": {
|
||||
"buildTarget": "rxjs:build:production"
|
||||
},
|
||||
"development": {
|
||||
"buildTarget": "rxjs:build:development"
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "development"
|
||||
},
|
||||
"extract-i18n": {
|
||||
"builder": "@angular/build:extract-i18n",
|
||||
"options": {
|
||||
"buildTarget": "rxjs:build"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"schematics": {
|
||||
"@schematics/angular:component": {
|
||||
"type": "component"
|
||||
},
|
||||
"@schematics/angular:directive": {
|
||||
"type": "directive"
|
||||
},
|
||||
"@schematics/angular:service": {
|
||||
"type": "service"
|
||||
},
|
||||
"@schematics/angular:guard": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:interceptor": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:module": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:pipe": {
|
||||
"typeSeparator": "."
|
||||
},
|
||||
"@schematics/angular:resolver": {
|
||||
"typeSeparator": "."
|
||||
}
|
||||
}
|
||||
}
|
||||
28
examples/angular/rxjs/package.json
Normal file
28
examples/angular/rxjs/package.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"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/common": "^20.0.0",
|
||||
"@angular/compiler": "^20.0.0",
|
||||
"@angular/core": "^20.0.0",
|
||||
"@angular/forms": "^20.0.0",
|
||||
"@angular/platform-browser": "^20.0.0",
|
||||
"@tanstack/angular-query-experimental": "^5.102.8",
|
||||
"rxjs": "^7.8.2",
|
||||
"tslib": "^2.8.1",
|
||||
"zone.js": "0.15.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@angular/build": "^20.0.0",
|
||||
"@angular/cli": "^20.0.0",
|
||||
"@angular/compiler-cli": "^20.0.0",
|
||||
"typescript": "5.8.3"
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
10
examples/angular/rxjs/src/app/app.component.ts
Normal file
10
examples/angular/rxjs/src/app/app.component.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { ChangeDetectionStrategy, Component } from '@angular/core'
|
||||
import { ExampleComponent } from './components/example.component'
|
||||
|
||||
@Component({
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
selector: 'app-root',
|
||||
template: `<example />`,
|
||||
imports: [ExampleComponent],
|
||||
})
|
||||
export class AppComponent {}
|
||||
31
examples/angular/rxjs/src/app/app.config.ts
Normal file
31
examples/angular/rxjs/src/app/app.config.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import {
|
||||
provideHttpClient,
|
||||
withFetch,
|
||||
withInterceptors,
|
||||
} from '@angular/common/http'
|
||||
import {
|
||||
QueryClient,
|
||||
provideTanStackQuery,
|
||||
} from '@tanstack/angular-query-experimental'
|
||||
import { withDevtools } from '@tanstack/angular-query-experimental/devtools'
|
||||
import { autocompleteMockInterceptor } from './api/autocomplete-mock.interceptor'
|
||||
import type { ApplicationConfig } from '@angular/core'
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideHttpClient(
|
||||
withFetch(),
|
||||
withInterceptors([autocompleteMockInterceptor]),
|
||||
),
|
||||
provideTanStackQuery(
|
||||
new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
gcTime: 1000 * 60 * 60 * 24, // 24 hours
|
||||
},
|
||||
},
|
||||
}),
|
||||
withDevtools(),
|
||||
),
|
||||
],
|
||||
}
|
||||
@@ -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>
|
||||
@@ -0,0 +1,43 @@
|
||||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
|
||||
import { toSignal } from '@angular/core/rxjs-interop'
|
||||
import { NonNullableFormBuilder, ReactiveFormsModule } from '@angular/forms'
|
||||
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',
|
||||
templateUrl: './example.component.html',
|
||||
imports: [ReactiveFormsModule],
|
||||
})
|
||||
export class ExampleComponent {
|
||||
readonly #autocompleteService = inject(AutocompleteService)
|
||||
readonly #fb = inject(NonNullableFormBuilder)
|
||||
|
||||
readonly form = this.#fb.group({
|
||||
term: '',
|
||||
})
|
||||
|
||||
readonly term = toSignal(
|
||||
this.form.controls.term.valueChanges.pipe(
|
||||
debounceTime(300),
|
||||
distinctUntilChanged(),
|
||||
),
|
||||
{ initialValue: '' },
|
||||
)
|
||||
|
||||
readonly query = injectQuery(() => ({
|
||||
queryKey: ['suggestions', this.term()],
|
||||
queryFn: () => {
|
||||
return lastValueFrom(
|
||||
this.#autocompleteService.getSuggestions(this.term()),
|
||||
)
|
||||
},
|
||||
placeholderData: keepPreviousData,
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
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 {
|
||||
readonly #http = inject(HttpClient)
|
||||
|
||||
getSuggestions = (term: string = '') =>
|
||||
term.trim() === ''
|
||||
? of({ suggestions: [] })
|
||||
: this.#http.get<Response>(
|
||||
`/api/autocomplete?term=${encodeURIComponent(term)}`,
|
||||
)
|
||||
}
|
||||
BIN
examples/angular/rxjs/src/favicon.ico
Normal file
BIN
examples/angular/rxjs/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
examples/angular/rxjs/src/index.html
Normal file
13
examples/angular/rxjs/src/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>TanStack Query Angular RxJS example</title>
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
5
examples/angular/rxjs/src/main.ts
Normal file
5
examples/angular/rxjs/src/main.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser'
|
||||
import { appConfig } from './app/app.config'
|
||||
import { AppComponent } from './app/app.component'
|
||||
|
||||
bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err))
|
||||
9
examples/angular/rxjs/tsconfig.app.json
Normal file
9
examples/angular/rxjs/tsconfig.app.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./out-tsc/app",
|
||||
"types": []
|
||||
},
|
||||
"files": ["src/main.ts"],
|
||||
"include": ["src/**/*.d.ts"]
|
||||
}
|
||||
31
examples/angular/rxjs/tsconfig.json
Normal file
31
examples/angular/rxjs/tsconfig.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"compileOnSave": false,
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist/out-tsc",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"noImplicitOverride": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"skipLibCheck": true,
|
||||
"isolatedModules": true,
|
||||
"esModuleInterop": true,
|
||||
"sourceMap": true,
|
||||
"declaration": false,
|
||||
"experimentalDecorators": true,
|
||||
"moduleResolution": "Bundler",
|
||||
"importHelpers": true,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"useDefineForClassFields": false,
|
||||
"lib": ["ES2022", "dom"]
|
||||
},
|
||||
"angularCompilerOptions": {
|
||||
"enableI18nLegacyMessageIdFormat": false,
|
||||
"strictInjectionParameters": true,
|
||||
"strictInputAccessModifiers": true,
|
||||
"strictStandalone": true,
|
||||
"strictTemplates": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user