/* * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is located at * * http://aws.amazon.com/apache2.0/ * * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ /** * Storage instance options */ import { ICredentials } from 'nono-aws-amplify/core'; import { StorageProvider, StorageProviderApi, AWSS3Provider, StorageProviderWithCopy, S3ProviderGetOuput, S3ProviderRemoveOutput, S3ProviderListOutput, S3ProviderCopyOutput, S3ProviderPutOutput, } from '../'; type Tail = ((...t: T) => void) extends ( h: any, ...r: infer R ) => void ? R : never; type Last = T[Exclude>]; // Utility type to extract the last parameter type of a function type LastParameter any> = Last>; 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; /** * 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, U extends StorageProviderApi > = ReturnType extends 'AWSS3' ? LastParameter // check if it has 'copy' function because 'copy' is optional : T extends StorageProviderWithCopy ? LastParameter & { provider: ReturnType } : LastParameter]> & { provider: ReturnType; }; export type StorageGetConfig = T extends StorageProvider ? StorageOperationConfig : StorageOperationConfigMap, T>; export type StoragePutConfig = T extends StorageProvider ? StorageOperationConfig : StorageOperationConfigMap, T>; export type StorageRemoveConfig = T extends StorageProvider ? StorageOperationConfig : StorageOperationConfigMap< StorageOperationConfig, T >; export type StorageListConfig = T extends StorageProvider ? StorageOperationConfig : StorageOperationConfigMap, T>; export type StorageCopyConfig = T extends StorageProviderWithCopy ? StorageOperationConfig : StorageOperationConfigMap, 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 ? ReturnType : ReturnType]> : T extends { provider: string } ? T extends { provider: 'AWSS3' } ? DefaultOutput : Promise : DefaultOutput; export type StorageGetOutput< T extends StorageProvider | Record > = PickProviderOutput>, T, 'get'>; export type StoragePutOutput = PickProviderOutput< S3ProviderPutOutput, T, 'put' >; export type StorageRemoveOutput = PickProviderOutput< Promise, T, 'remove' >; export type StorageListOutput = PickProviderOutput< Promise, T, 'list' >; export type StorageCopyOutput = PickProviderOutput< Promise, T, 'copy' >; /** * 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 > = T extends { provider: string } ? T extends { provider: 'AWSS3' } ? Default : T & { provider: string } : Default;