Skip to content

Commit

Permalink
api report updates and changests for StorageApi refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
  • Loading branch information
Rugvip committed Dec 21, 2021
1 parent 281bac6 commit a195284
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 12 deletions.
11 changes: 11 additions & 0 deletions .changeset/cyan-seahorses-film.md
@@ -0,0 +1,11 @@
---
'@backstage/core-plugin-api': minor
---

**BREAKING CHANGE** The `StorageApi` has received several updates that fills in gaps for some use-cases and makes it easier to avoid mistakes:

- The `StorageValueChange` type has been renamed to `StorageValueSnapshot`, the `newValue` property has been renamed to `value`, the stored value type has been narrowed to `JsonValue`, and it has received a new `presence` property that is `'unknown'`, `'absent'`, or `'present'`.
- The `get` method has been deprecated in favor of a new `snapshot` method, which returns a `StorageValueSnapshot`.
- The `observe$` method has had its contract changed. It should now emit values when the `presence` of a key changes, this may for example happen when remotely stored values are requested on page load and the presence switches from `'unknown'` to either `'absent'` or `'present'`.

The above changes have been made with deprecations in place to maintain much of the backwards compatibility for consumers of the `StorageApi`. The only breaking change is the narrowing of the stored value type, which may in some cases require the addition of an explicit type parameter to the `get` and `observe$` methods.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-beers-teach.md
@@ -0,0 +1,5 @@
---
'@backstage/core-app-api': minor
---

Updated `WebStorageApi` to reflect the `StorageApi` changes in `@backstage/core-plugin-api`.
5 changes: 5 additions & 0 deletions .changeset/green-timers-film.md
@@ -0,0 +1,5 @@
---
'@backstage/test-utils': minor
---

Updated `MockStorageApi` to reflect the `StorageApi` changes in `@backstage/core-plugin-api`.
7 changes: 5 additions & 2 deletions packages/core-app-api/api-report.md
Expand Up @@ -39,6 +39,7 @@ import { gitlabAuthApiRef } from '@backstage/core-plugin-api';
import { googleAuthApiRef } from '@backstage/core-plugin-api';
import { IconComponent } from '@backstage/core-plugin-api';
import { IdentityApi } from '@backstage/core-plugin-api';
import { JsonValue } from '@backstage/types';
import { microsoftAuthApiRef } from '@backstage/core-plugin-api';
import { OAuthApi } from '@backstage/core-plugin-api';
import { OAuthRequestApi } from '@backstage/core-plugin-api';
Expand All @@ -59,7 +60,7 @@ import { RouteRef } from '@backstage/core-plugin-api';
import { SessionApi } from '@backstage/core-plugin-api';
import { SessionState } from '@backstage/core-plugin-api';
import { StorageApi } from '@backstage/core-plugin-api';
import { StorageValueChange } from '@backstage/core-plugin-api';
import { StorageValueSnapshot } from '@backstage/core-plugin-api';
import { SubRouteRef } from '@backstage/core-plugin-api';

// @public
Expand Down Expand Up @@ -606,10 +607,12 @@ export class WebStorage implements StorageApi {
// (undocumented)
get<T>(key: string): T | undefined;
// (undocumented)
observe$<T>(key: string): Observable<StorageValueChange<T>>;
observe$<T>(key: string): Observable<StorageValueSnapshot<T>>;
// (undocumented)
remove(key: string): Promise<void>;
// (undocumented)
set<T>(key: string, data: T): Promise<void>;
// (undocumented)
snapshot<T extends JsonValue>(key: string): StorageValueSnapshot<T>;
}
```
32 changes: 25 additions & 7 deletions packages/core-plugin-api/api-report.md
Expand Up @@ -10,6 +10,7 @@ import { ComponentType } from 'react';
import { Config } from '@backstage/config';
import { IconComponent as IconComponent_2 } from '@backstage/core-plugin-api';
import { IdentityApi as IdentityApi_2 } from '@backstage/core-plugin-api';
import { JsonValue } from '@backstage/types';
import { Observable } from '@backstage/types';
import { ProfileInfo as ProfileInfo_2 } from '@backstage/core-plugin-api';
import { default as React_2 } from 'react';
Expand Down Expand Up @@ -762,20 +763,37 @@ export type SignInResult = {
// @public
export interface StorageApi {
forBucket(name: string): StorageApi;
get<T>(key: string): T | undefined;
observe$<T>(key: string): Observable<StorageValueChange<T>>;
// @deprecated
get<T extends JsonValue>(key: string): T | undefined;
observe$<T extends JsonValue>(
key: string,
): Observable<StorageValueSnapshot<T>>;
remove(key: string): Promise<void>;
set(key: string, data: any): Promise<void>;
set<T extends JsonValue>(key: string, data: T): Promise<void>;
snapshot<T extends JsonValue>(key: string): StorageValueSnapshot<T>;
}

// @public
export const storageApiRef: ApiRef<StorageApi>;

// @public @deprecated (undocumented)
export type StorageValueChange<TValue extends JsonValue> =
StorageValueSnapshot<TValue>;

// @public
export type StorageValueChange<T = any> = {
key: string;
newValue?: T;
};
export type StorageValueSnapshot<TValue extends JsonValue> =
| {
key: string;
presence: 'unknown' | 'absent';
value?: undefined;
newValue?: undefined;
}
| {
key: string;
presence: 'present';
value: TValue;
newValue?: TValue;
};

// @public
export type SubRouteRef<Params extends AnyParams = any> = {
Expand Down
5 changes: 4 additions & 1 deletion packages/core-plugin-api/src/apis/definitions/StorageApi.ts
Expand Up @@ -38,7 +38,10 @@ export type StorageValueSnapshot<TValue extends JsonValue> =
newValue?: TValue;
};

/** @deprecated Use StorageValueSnapshot instead */
/**
* @public
* @deprecated Use StorageValueSnapshot instead
*/
export type StorageValueChange<TValue extends JsonValue> =
StorageValueSnapshot<TValue>;

Expand Down
7 changes: 5 additions & 2 deletions packages/test-utils/api-report.md
Expand Up @@ -12,13 +12,14 @@ import { ErrorApi } from '@backstage/core-plugin-api';
import { ErrorApiError } from '@backstage/core-plugin-api';
import { ErrorApiErrorContext } from '@backstage/core-plugin-api';
import { ExternalRouteRef } from '@backstage/core-plugin-api';
import { JsonValue } from '@backstage/types';
import { Observable } from '@backstage/types';
import { ReactElement } from 'react';
import { ReactNode } from 'react';
import { RenderResult } from '@testing-library/react';
import { RouteRef } from '@backstage/core-plugin-api';
import { StorageApi } from '@backstage/core-plugin-api';
import { StorageValueChange } from '@backstage/core-plugin-api';
import { StorageValueSnapshot } from '@backstage/core-plugin-api';

// @public
export type AsyncLogCollector = () => Promise<void>;
Expand Down Expand Up @@ -81,11 +82,13 @@ export class MockStorageApi implements StorageApi {
// (undocumented)
get<T>(key: string): T | undefined;
// (undocumented)
observe$<T>(key: string): Observable<StorageValueChange<T>>;
observe$<T>(key: string): Observable<StorageValueSnapshot<T>>;
// (undocumented)
remove(key: string): Promise<void>;
// (undocumented)
set<T>(key: string, data: T): Promise<void>;
// (undocumented)
snapshot<T extends JsonValue>(key: string): StorageValueSnapshot<T>;
}

// @public
Expand Down

0 comments on commit a195284

Please sign in to comment.