-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Storage.ts
215 lines (190 loc) · 5.92 KB
/
Storage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Storage instance options
*/
import { ICredentials } from '@aws-amplify/core';
import {
StorageProvider,
StorageProviderApi,
AWSS3Provider,
StorageProviderWithCopy,
S3ProviderGetOuput,
S3ProviderRemoveOutput,
S3ProviderListOutput,
S3ProviderCopyOutput,
S3ProviderPutOutput,
S3ProviderGetPropertiesOutput,
StorageProviderWithGetProperties,
} from '../';
type Tail<T extends any[]> = ((...t: T) => void) extends (
h: any,
...r: infer R
) => void
? R
: never;
type Last<T extends any[]> = T[Exclude<keyof T, keyof Tail<T>>];
// Utility type to extract the last parameter type of a function
type LastParameter<F extends (...args: any) => any> = Last<Parameters<F>>;
export interface StorageOptions {
credentials?: ICredentials;
region?: string;
level?: StorageAccessLevel;
bucket?: string;
provider?: string;
/**
* Custom mapping of your prefixes.
* For example, customPrefix: { public: 'myPublicPrefix' } will make public level operations access 'myPublicPrefix/'
* instead of the default 'public/'.
*/
customPrefix?: CustomPrefix;
/**
* if set to true, automatically sends Storage Events to Amazon Pinpoint
**/
track?: boolean;
dangerouslyConnectToHttpEndpointForTesting?: boolean;
}
export type StorageAccessLevel = 'public' | 'protected' | 'private';
export type CustomPrefix = {
[key in StorageAccessLevel]?: string;
};
export type StorageCopyTarget = {
key: string;
level?: string;
identityId?: string;
};
export type StorageCopySource = StorageCopyTarget;
export type StorageCopyDestination = Omit<StorageCopyTarget, 'identityId'>;
/**
* If provider is AWSS3, provider doesn't have to be specified since it's the default, else it has to be passed into
* config.
*/
type StorageOperationConfig<
T extends
| StorageProvider
| StorageProviderWithCopy
| StorageProviderWithGetProperties,
U extends StorageProviderApi
> = ReturnType<T['getProviderName']> extends 'AWSS3'
? LastParameter<AWSS3Provider[U]> // check if it has 'copy' function because 'copy' is optional
: T extends StorageProviderWithGetProperties & StorageProviderWithCopy
? LastParameter<T[U]> & {
provider: ReturnType<T['getProviderName']>;
}
: T extends StorageProviderWithCopy
? LastParameter<T[Exclude<U, 'getProperties'>]> & {
provider: ReturnType<T['getProviderName']>;
}
: T extends StorageProviderWithGetProperties
? LastParameter<T[Exclude<U, 'copy'>]> & {
provider: ReturnType<T['getProviderName']>;
}
: LastParameter<T[Exclude<U, 'copy' | 'getProperties'>]> & {
provider: ReturnType<T['getProviderName']>;
};
export type StorageGetConfig<T extends Record<string, any>> =
T extends StorageProvider
? StorageOperationConfig<T, 'get'>
: StorageOperationConfigMap<
StorageOperationConfig<AWSS3Provider, 'get'>,
T
>;
export type StorageGetPropertiesConfig<T extends Record<string, any>> =
T extends StorageProviderWithGetProperties
? StorageOperationConfig<T, 'getProperties'>
: StorageOperationConfigMap<
StorageOperationConfig<AWSS3Provider, 'getProperties'>,
T
>;
export type StoragePutConfig<T extends Record<string, any>> =
T extends StorageProvider
? StorageOperationConfig<T, 'put'>
: StorageOperationConfigMap<
StorageOperationConfig<AWSS3Provider, 'put'>,
T
>;
export type StorageRemoveConfig<T extends Record<string, any>> =
T extends StorageProvider
? StorageOperationConfig<T, 'remove'>
: StorageOperationConfigMap<
StorageOperationConfig<AWSS3Provider, 'remove'>,
T
>;
export type StorageListConfig<T extends Record<string, any>> =
T extends StorageProvider
? StorageOperationConfig<T, 'list'>
: StorageOperationConfigMap<
StorageOperationConfig<AWSS3Provider, 'list'>,
T
>;
export type StorageCopyConfig<T extends Record<string, any>> =
T extends StorageProviderWithCopy
? StorageOperationConfig<T, 'copy'>
: StorageOperationConfigMap<
StorageOperationConfig<AWSS3Provider, 'copy'>,
T
>;
/**
* Utility type for checking if the generic type is a provider or a Record that has the key 'provider'.
* If it's a provider, check if it's the S3 Provider, use the default type else use the generic's 'get' method
* return type.
* If it's a Record, check if provider is 'AWSS3', use the default type else use any.
*/
type PickProviderOutput<
DefaultOutput,
T,
api extends StorageProviderApi
> = T extends StorageProvider
? T['getProviderName'] extends 'AWSS3'
? DefaultOutput
: T extends StorageProviderWithCopy & StorageProviderWithGetProperties
? ReturnType<T[api]>
: T extends StorageProviderWithCopy
? ReturnType<T[Exclude<api, 'getProperties'>]>
: T extends StorageProviderWithGetProperties
? ReturnType<T[Exclude<api, 'copy'>]>
: ReturnType<T[Exclude<api, 'copy' | 'getProperties'>]>
: T extends { provider: string }
? T extends { provider: 'AWSS3' }
? DefaultOutput
: Promise<any>
: DefaultOutput;
export type StorageGetOutput<T extends StorageProvider | Record<string, any>> =
PickProviderOutput<Promise<S3ProviderGetOuput<T>>, T, 'get'>;
export type StoragePutOutput<T> = PickProviderOutput<
S3ProviderPutOutput<T>,
T,
'put'
>;
export type StorageRemoveOutput<T> = PickProviderOutput<
Promise<S3ProviderRemoveOutput>,
T,
'remove'
>;
export type StorageListOutput<T> = PickProviderOutput<
Promise<S3ProviderListOutput>,
T,
'list'
>;
export type StorageCopyOutput<T> = PickProviderOutput<
Promise<S3ProviderCopyOutput>,
T,
'copy'
>;
export type StorageGetPropertiesOutput<T> = PickProviderOutput<
Promise<S3ProviderGetPropertiesOutput>,
T,
'getProperties'
>;
/**
* Utility type to allow custom provider to use any config keys, if provider is set to AWSS3 then it should use
* AWSS3Provider's config.
*/
export type StorageOperationConfigMap<
Default,
T extends Record<string, any>
> = T extends { provider: string }
? T extends { provider: 'AWSS3' }
? Default
: T & { provider: string }
: Default;