Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use strict query params #331

Merged
merged 1 commit into from
Jan 30, 2025
Merged

Conversation

LayZeeDK
Copy link
Member

@LayZeeDK LayZeeDK commented Jan 30, 2025

Features

  • Use StrictQueryParams for query parameters instead of StrictRouteParams

Array query parameters like ?size=m&size=l&size=xl are now correctly resolved to readonly string[] instead of string.

BREAKING CHANGES

RouterStore#queryParams$ and MinimalActivatedRouteSnapshot#queryParams use StrictQueryParams instead of StrictRouteParams. Members are of type string | readonly string[] | undefined instead of string | undefined.

The TypeScript compiler will fail to compile code that does not handle the string array type.

BEFORE:

// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";

@Component({
  // (...)
})
export class ShirtsComponent {
  #routerStore = inject(RouterStore);

  size$: Observable<string> = this.#routerStore.queryParams$.pipe(
    map((params) => params["size"]),
  );
}

AFTER:

// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";

@Component({
  // (...)
})
export class ShirtsComponent {
  #routerStore = inject(RouterStore);

  size$: Observable<readonly string[]> = this.#routerStore.queryParams$.pipe(
    map((params) => params["size"]),
    map((size) => (Array.isArray(size) ? size : [size]))
  );
}

RouterStore#selectQueryParam use StrictQueryParams instead of StrictRouteParams. The returned value is of type string | readonly string[] | undefined instead of string | undefined.

The TypeScript compiler will fail to compile code that does not handle the string array type.

BEFORE:

// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";

@Component({
  // (...)
})
export class ShirtsComponent {
  #routerStore = inject(RouterStore);

  size$: Observable<string> = this.#routerStore.selectQueryParam('size');
}

AFTER:

// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";

@Component({
  // (...)
})
export class ShirtsComponent {
  #routerStore = inject(RouterStore);

  size$: Observable<readonly string[]> = this.#routerStore.selectQueryParam('size').pipe(
    map((size) => (Array.isArray(size) ? size : [size]))
  );
}

Array query parameters like `?size=m&size=l&size=xl` are now correctly resolved to `readonly string[]` instead of `string`.

**BREAKING CHANGES**

`RouterStore#queryParams$` and `MinimalActivatedRouteSnapshot#queryParams` use `StrictQueryParams` instead of `StrictRouteParams`. Members are of type `string | readonly string[] | undefined` instead of `string | undefined`.

The TypeScript compiler will fail to compile code that does not handle the string array type.

BEFORE:

```typescript
// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";

@component({
  // (...)
})
export class ShirtsComponent {
  #routerStore = inject(RouterStore);

  size$: Observable<string> = this.#routerStore.queryParams$.pipe(
    map((params) => params["size"]),
  );
}
```

AFTER:

```typescript
// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";

@component({
  // (...)
})
export class ShirtsComponent {
  #routerStore = inject(RouterStore);

  size$: Observable<readonly string[]> = this.#routerStore.queryParams$.pipe(
    map((params) => params["size"]),
    map((size) => (Array.isArray(size) ? size : [size]))
  );
}
```

`RouterStore#selectQueryParam` use `StrictQueryParams` instead of `StrictRouteParams`. The returned value is of type `string | readonly string[] | undefined` instead of `string | undefined`.

The TypeScript compiler will fail to compile code that does not handle the string array type.

BEFORE:

```typescript
// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";

@component({
  // (...)
})
export class ShirtsComponent {
  #routerStore = inject(RouterStore);

  size$: Observable<string> = this.#routerStore.selectQueryParam('size');
}
```

AFTER:

```typescript
// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";

@component({
  // (...)
})
export class ShirtsComponent {
  #routerStore = inject(RouterStore);

  size$: Observable<readonly string[]> = this.#routerStore.selectQueryParam('size').pipe(
    map((size) => (Array.isArray(size) ? size : [size]))
  );
}
```
@LayZeeDK LayZeeDK force-pushed the feat/add-strict-query-params.ts branch from 5c37c02 to d8f093d Compare January 30, 2025 10:00
@LayZeeDK LayZeeDK merged commit a95ede8 into main Jan 30, 2025
7 checks passed
@LayZeeDK LayZeeDK deleted the feat/add-strict-query-params.ts branch January 30, 2025 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant