Skip to content

Commit c6efb9c

Browse files
committed
Better typing for storage-related modules
1 parent 7299f41 commit c6efb9c

8 files changed

Lines changed: 57 additions & 46 deletions

File tree

desktop/src/app/components/data/shared/file-group-picker/file-group-picker.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
import {
55
ControlValueAccessor, FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR,
66
} from "@angular/forms";
7-
import { MatOptionSelectionChange } from "@angular/material/core";
87
import { FilterBuilder, ListView } from "@batch-flask/core";
98
import { Activity, DialogService } from "@batch-flask/ui";
109
import { FileGroupCreateFormComponent } from "app/components/data/action";

desktop/src/app/models/blob-container.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface BlobContainerAttributes {
88
name: string;
99
publicAccessLevel: string;
1010
metadata?: any;
11-
lastModified: Date;
11+
lastModified?: Date;
1212
lease?: Partial<ContainerLeaseAttributes>;
1313
}
1414

@@ -25,7 +25,7 @@ export class BlobContainer extends Record<BlobContainerAttributes> implements Na
2525

2626
@Prop() public publicAccessLevel: string;
2727
@Prop() public metadata: any;
28-
@Prop() public lastModified: Date;
28+
@Prop() public lastModified?: Date;
2929
@Prop() public lease: ContainerLease;
3030
@Prop() public storageAccountId: string;
3131

desktop/src/app/services/core/data/storage-list-getter.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Type } from "@angular/core";
22
import { ContinuationToken, ListGetter, ListGetterConfig, Record, ServerError } from "@batch-flask/core";
3+
import { BlobStorageClientProxy } from "app/services/storage";
34
import { StorageBlobResult } from "app/services/storage/models";
45
import { StorageClientService } from "app/services/storage/storage-client.service";
56
import { Observable, from, throwError } from "rxjs";
@@ -12,13 +13,15 @@ export interface StorageBaseParams {
1213
export interface StorageListConfig<TEntity extends Record<any>, TParams extends StorageBaseParams>
1314
extends ListGetterConfig<TEntity, TParams> {
1415

15-
getData: (client: any, params: TParams, options: any, token: any) => Promise<StorageBlobResult<TEntity[]>>;
16+
getData: (client: BlobStorageClientProxy, params: TParams, options: any, token: any) =>
17+
Promise<StorageBlobResult<TEntity[]>>;
1618
}
1719

1820
export class StorageListGetter<TEntity extends Record<any>, TParams extends StorageBaseParams>
1921
extends ListGetter<TEntity, TParams> {
2022

21-
private _getData: (client: any, params: TParams, options: any, token: any) => Promise<StorageBlobResult<TEntity[]>>;
23+
private _getData: (client: BlobStorageClientProxy, params: TParams, options: any, token: any) =>
24+
Promise<StorageBlobResult<TEntity[]>>;
2225

2326
constructor(
2427
type: Type<TEntity>,

desktop/src/app/services/storage/blob-storage-client-proxy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { IpcEvent } from "common/constants";
44
import { SharedAccessPolicy } from "./models";
55
import * as blob from "./models/storage-blob";
66
import { BlobContentResult } from "./storage-blob.service";
7+
import { StorageClient } from "./storage-client.service";
78

89
const storageIpc = IpcEvent.storageBlob;
910

@@ -18,7 +19,7 @@ export interface ListBlobResponse {
1819
};
1920
}
2021

21-
export class BlobStorageClientProxy {
22+
export class BlobStorageClientProxy implements StorageClient {
2223

2324
private storageInfo: { url: string; account: string, key: string };
2425

desktop/src/app/services/storage/models/storage-blob.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
1-
import { ContainerItem, BlobUploadCommonResponse, ContainerGetPropertiesResponse, CommonOptions } from "@azure/storage-blob";
1+
import { BlobUploadCommonResponse, ContainerGetPropertiesResponse, CommonOptions, ContainerItem } from "@azure/storage-blob";
22
import { Model, Prop, Record } from "@batch-flask/core";
33
import { SharedAccessPolicy } from "./shared-access-policy";
4+
import { BlobContainer } from "app/models";
45

56
// Placeholder; we don't use any options to storage-blob API requests
67
export type RequestOptions = Partial<CommonOptions>;
78

9+
export interface BlobProperties {
10+
name: string;
11+
url: string;
12+
isDirectory: boolean;
13+
properties: {
14+
contentLength: number;
15+
contentType: string;
16+
creationTime: Date | string;
17+
lastModified?: Date | string;
18+
};
19+
}
20+
21+
export type ContainerProperties = ContainerGetPropertiesResponse;
22+
23+
@Model()
24+
export class BlobItem extends Record<BlobProperties> {
25+
@Prop() name: string;
26+
@Prop() url: string;
27+
@Prop() isDirectory: boolean;
28+
@Prop() properties: {
29+
contentLength: number;
30+
contentType: string;
31+
creationTime: Date | string;
32+
lastModified?: Date | string;
33+
}
34+
}
35+
836
export interface BaseParams {
937
url: string;
1038
account: string;
@@ -94,32 +122,6 @@ export interface GetBlobContentResult {
94122
content: string;
95123
}
96124

97-
export interface BlobProperties {
98-
name: string;
99-
url: string;
100-
isDirectory: boolean;
101-
properties: {
102-
contentLength: number;
103-
contentType: string;
104-
creationTime: Date | string;
105-
lastModified: Date | string;
106-
};
107-
}
108-
109-
@Model()
110-
export class BlobItem extends Record<BlobProperties> {
111-
@Prop() name: string;
112-
@Prop() url: string;
113-
@Prop() isDirectory: boolean;
114-
@Prop() properties: {
115-
contentLength: number;
116-
contentType: string;
117-
creationTime: Date | string;
118-
lastModified: Date | string;
119-
}
120-
}
121-
122-
export type ContainerProperties = ContainerGetPropertiesResponse;
123125

124126
export interface ListBlobOptions {
125127
/**

desktop/src/app/services/storage/storage-blob.service.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Constants } from "common";
1919
import { AsyncSubject, Observable, from, of, throwError } from "rxjs";
2020
import { catchError, concat, concatMap, map, share, switchMap, take } from "rxjs/operators";
2121
import { BlobStorageClientProxy } from "./blob-storage-client-proxy";
22-
import { StorageClientService } from "./storage-client.service";
22+
import { StorageClient, StorageClientService } from "./storage-client.service";
2323

2424
export interface ListBlobParams {
2525
storageAccountId: string;
@@ -42,7 +42,10 @@ export interface BlobContentResult {
4242
content: string;
4343
}
4444

45-
export type StorageContainerProperties = ContainerProperties;
45+
export interface StorageContainerProperties extends Omit<ContainerProperties, "lastModified" | "etag"> {
46+
lastModified?: Date;
47+
etag?: string;
48+
}
4649

4750
export interface NavigateBlobsOptions {
4851
/**
@@ -117,21 +120,16 @@ export class StorageBlobService {
117120

118121
this._blobListGetter = new StorageListGetter(FileRecord, this.storageClient, {
119122
cache: (params) => this.getBlobFileCache(params),
120-
getData: (client: BlobStorageClientProxy,
121-
params, options, continuationToken) => {
123+
getData: async (client: StorageClient, params, options, continuationToken) => {
122124
const blobOptions: ListBlobOptions = {
123125
folder: options.original.folder,
124126
recursive: options.original.recursive,
125127
maxPages: options.original.limit,
126128
maxPageSize: this.maxBlobPageSize
127129
};
128130

129-
// N.B. `BlobItem` and `FileRecord` are nearly identical
130-
return client.listBlobs(
131-
params.container,
132-
blobOptions,
133-
continuationToken,
134-
) as Promise<StorageBlobResult<FileRecord[]>>;
131+
const blobs = await client.listBlobs(params.container, blobOptions, continuationToken);
132+
return { data: blobs.data.map(blob => new FileRecord(blob)) };
135133
},
136134
logIgnoreError: storageIgnoredErrors,
137135
});

desktop/src/app/services/storage/storage-client.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable } from "@angular/core";
22
import { ServerError } from "@batch-flask/core";
33
import { ElectronRemote } from "@batch-flask/electron";
4-
import { StorageKeys } from "app/models";
4+
import { BlobContainer, StorageKeys } from "app/models";
55
import { BatchExplorerService } from "app/services/batch-explorer.service";
66
import { ArmResourceUtils } from "app/utils";
77
import { Observable, throwError } from "rxjs";
@@ -10,6 +10,7 @@ import { BatchAccountService } from "../batch-account";
1010
import { BlobStorageClientProxy } from "./blob-storage-client-proxy";
1111
import { StorageAccountKeysService } from "./storage-account-keys.service";
1212
import { StorageClientProxyFactory } from "./storage-client-proxy-factory";
13+
import { ListBlobsResult, ListContainersResult, StorageBlobResult } from "./models";
1314

1415
export interface AutoStorageSettings {
1516
lastKeySync: Date;
@@ -23,6 +24,13 @@ export interface StorageKeyCachedItem {
2324
keys: StorageKeys;
2425
}
2526

27+
export interface StorageClient {
28+
listContainersWithPrefix(prefix: string, continuationToken?: string, options?: any):
29+
Promise<ListContainersResult>;
30+
listBlobs(containerName: string, options: any, continuationToken?: string):
31+
Promise<ListBlobsResult>;
32+
}
33+
2634
@Injectable({ providedIn: "root" })
2735
export class StorageClientService {
2836
public hasAutoStorage: Observable<boolean>;

desktop/src/app/services/storage/storage-container.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { SharedAccessPolicy } from "app/services/storage/models";
1414
import { Observable, Subject, from, throwError } from "rxjs";
1515
import { catchError, flatMap, share } from "rxjs/operators";
1616
import { BlobStorageClientProxy } from "./blob-storage-client-proxy";
17-
import { StorageClientService } from "./storage-client.service";
17+
import { StorageClient, StorageClientService } from "./storage-client.service";
1818

1919
export interface GetContainerParams {
2020
storageAccountId: string;
@@ -62,7 +62,7 @@ export class StorageContainerService {
6262
});
6363
this._containerListGetter = new StorageListGetter(BlobContainer, this.storageClient, {
6464
cache: params => this._containerCache.getCache(params),
65-
getData: async (client, params, options, continuationToken) => {
65+
getData: async (client: StorageClient, params, options, continuationToken) => {
6666
let prefix = null;
6767
if (options && options.filter) {
6868
prefix = options.filter.value;

0 commit comments

Comments
 (0)