@rsweeten/dropbox-sync
Version:
Reusable Dropbox synchronization module with framework adapters
38 lines (37 loc) • 1.23 kB
JavaScript
import { Dropbox } from 'dropbox';
import { createAuthMethods } from './auth';
import { createSyncMethods } from './sync';
import { createSocketMethods } from './socket';
/**
* Creates a Dropbox sync client with auth, sync, and socket methods
*/
export function createDropboxSyncClient(credentials) {
let dropboxClient = null;
function getClient() {
if (!dropboxClient) {
if (credentials.accessToken) {
dropboxClient = new Dropbox({
accessToken: credentials.accessToken,
});
}
else if (credentials.clientId) {
dropboxClient = new Dropbox({ clientId: credentials.clientId });
}
else {
throw new Error('Either clientId or accessToken must be provided');
}
}
return dropboxClient;
}
function setAccessToken(token) {
dropboxClient = new Dropbox({ accessToken: token });
}
const auth = createAuthMethods(getClient, credentials, setAccessToken);
const socket = createSocketMethods();
const sync = createSyncMethods(getClient, credentials, socket);
return {
auth,
sync,
socket,
};
}