Skip to content

Commit

Permalink
feat: allow creating provider-specific instances
Browse files Browse the repository at this point in the history
  • Loading branch information
mfkrause committed Apr 25, 2024
1 parent 6efc252 commit 435a46f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 45 deletions.
18 changes: 18 additions & 0 deletions docs/docs/api/CloudStorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ Gets the cloud storage provider currently in use.

**Returns**: The currently used [`CloudStorageProvider`](./enums/CloudStorageProvider).

### `getProviderInstance(provider)`

Gets a new instance of the `CloudStorage` API, forcing the use of the given provider. This is useful when you want to use multiple providers in the same app.

For more info, [see here](../guides/using-multiple-providers).

**Parameters**:

- `provider` ([`CloudStorageProvider`](./enums/CloudStorageProvider)): Required. The provider to use.

**Returns**: A new instance of the `CloudStorage` API. This instance will only include the [cloud operation methods](#cloud-operations) and not the configuration methods.

### `getProviderOptions(provider)`

Gets the currently set options of the given provider.
Expand All @@ -46,6 +58,12 @@ Gets the currently set options of the given provider.

**Returns**: The options of the given provider.

### `getSupportedProviders()`

Gets the list of supported cloud storage providers on the current platform.

**Returns**: An array of [`CloudStorageProvider`](./enums/CloudStorageProvider) values.

### `setProvider(provider)`

Sets the cloud storage provider to use.
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/guides/google-drive-files-same-name.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 1
sidebar_position: 2
---

# Handling multiple files with the same name in Google Drive
Expand Down
32 changes: 32 additions & 0 deletions docs/docs/guides/using-multiple-providers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
sidebar_position: 1
---

# Using multiple Cloud Storage Providers

By default, the [`CloudStorage`](../api/CloudStorage) API will use the provider set via [`setProvider()`](../api/CloudStorage#setproviderprovider) (or the default provider based on the platform).

If you want to use multiple providers in your app, you can however get a new instance of the `CloudStorage` API which uses a specific provider. For example, this can be useful if you want to provide multiple cloud backup options to the user at the same time, allowing him to backup his files to iCloud and Google Drive simultaneously.

Those new instances only contain the [cloud operation methods](../api/CloudStorage#cloud-operations) and do not contain the configuration methods such as [`setProviderOptions()`](../api/CloudStorage#setprovideroptionsprovider-options). The provider-specific configuration must be done via the main `CloudStorage` instance.

## Example

```ts
import { CloudStorageProvider, CloudStorageScope, CloudStorage } from 'react-native-cloud-storage';

const iCloudStorage = CloudStorage.getProviderInstance(CloudStorageProvider.ICloud);
const googleDriveStorage = CloudStorage.getProviderInstance(CloudStorageProvider.GoogleDrive);

// Configuration methods are still only available on the main CloudStorage instance – set provider-specific options there, they are shared across all instances
CloudStorage.setProviderOptions(CloudStorageProvider.GoogleDrive, { accessToken: 'some_access_token' });

// Then, use the provider-specific instances
const handleSaveFileToICloud = async () => {
await iCloudStorage.writeFile('/test.txt', 'Hello, iCloud!');
};

const handleSaveFileToGoogleDrive = async () => {
await googleDriveStorage.writeFile('/test.txt', 'Hello, Google Drive!');
};
```
95 changes: 51 additions & 44 deletions src/RNCloudStorage.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,9 @@
import createRNCloudStorage from './createRNCloudStorage';
import createNativeCloudStorage from './createRNCloudStorage';
import { CloudStorageProvider, CloudStorageScope, type CloudStorageFileStat } from './types/main';
import { providerService } from './ProviderService';
import type NativeRNCloudStorage from './types/native';

let nativeInstance = createRNCloudStorage(providerService.getProvider());

const RNCloudStorage = {
//#region Provider Options
/**
* Gets the list of supported CloudStorageProviders on the current platform.
* @returns An array of supported CloudStorageProviders.
*/
getSupportedProviders: providerService.getSupportedProviders,

/**
* Gets the current CloudStorageProvider.
* @returns The current CloudStorageProvider.
*/
getProvider: providerService.getProvider,

/**
* Sets the current CloudStorageProvider.
* @param newProvider The provider to set as the current provider.
* @throws An error if the provider is not supported on the current platform.
*/
setProvider: (newProvider: CloudStorageProvider) => {
providerService.setProvider(newProvider);
nativeInstance = createRNCloudStorage(newProvider);
},

/**
* Gets the options for the given provider.
* @param provider The provider to get the options for. To get the options for the current provider, use `CloudStorage.getProvider()`.
* @returns The options for the given provider.
*/
getProviderOptions: providerService.getProviderOptions,

/**
* Sets the options for the given provider.
* @param provider The provider to set the options for. To set the options for the current provider, use `CloudStorage.getProvider()`.
* @param options The options to set for the provider.
*/
setProviderOptions: providerService.setProviderOptions,
//#endregion

//#region Native Methods
const createCloudStorage = (nativeInstance: NativeRNCloudStorage) => ({
/**
* Tests whether or not the cloud storage is available. Always returns true for Google Drive. iCloud may be
* unavailable right after app launch or if the user is not logged in.
Expand Down Expand Up @@ -197,7 +157,54 @@ const RNCloudStorage = {
isFile: () => native.isFile,
};
},
//#endregion
});

let defaultNativeCloudStorage = createCloudStorage(createNativeCloudStorage(providerService.getProvider()));

let RNCloudStorage = {
...defaultNativeCloudStorage,

/**
* Gets a new instance of the CloudStorage API for the given provider.
* @param provider The provider to get an instance for.
* @returns A new instance of the CloudStorage API, without the provider configuration methods.
*/
getProviderInstance: (provider: CloudStorageProvider) => createCloudStorage(createNativeCloudStorage(provider)),

/**
* Gets the list of supported CloudStorageProviders on the current platform.
* @returns An array of supported CloudStorageProviders.
*/ getSupportedProviders: providerService.getSupportedProviders,

/**
* Gets the current CloudStorageProvider.
* @returns The current CloudStorageProvider.
*/
getProvider: providerService.getProvider,

/**
* Sets the current CloudStorageProvider.
* @param newProvider The provider to set as the current provider.
* @throws An error if the provider is not supported on the current platform.
*/
setProvider: (newProvider: CloudStorageProvider) => {
providerService.setProvider(newProvider);
defaultNativeCloudStorage = createCloudStorage(createNativeCloudStorage(newProvider));
},

/**
* Gets the options for the given provider.
* @param provider The provider to get the options for. To get the options for the current provider, use `CloudStorage.getProvider()`.
* @returns The options for the given provider.
*/
getProviderOptions: providerService.getProviderOptions,

/**
* Sets the options for the given provider.
* @param provider The provider to set the options for. To set the options for the current provider, use `CloudStorage.getProvider()`.
* @param options The options to set for the provider.
*/
setProviderOptions: providerService.setProviderOptions,

//#region Deprecated v1 Methods
/**
Expand Down

0 comments on commit 435a46f

Please sign in to comment.