0.0.8 • Published 6 months ago

onedrive-picker-file v0.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

onedrive-picker-file

This repository is a fork of SDK to build and interface with OneDrive v8 Picker to make it a dependency of npm

Example SDK

This package demonstrates a way to wrap and interact with the picker control. There is no requirement to use this wrapper, but it shows setting up and using the picker in a reusable way across solutions.

It relies on the @pnp/core Timeline to orchestrate the messaging. There is also a sample sdk with no dependencies.

Overview

This package provides a wrapper for the picker and exposes a way to register various behaviors to control how the interaction occurs. These behaviors are described below, and you are encouraged to create your own behaviors as needed.

The picker provides eight subscribable events, listed below in the table. Generally you will use behaviors, but directly registering is always an option.

namedesacription
authenticateemits any time a token is required by the picker
pickemits when a user picks items and selects the Select button
closeemits when the user selects the Cancel button
notificationemits when the picker sends a notification
logemits when there is a log message
erroremits when there is an error within the picker
initemits as the first event when the picker is being established
disposeemits as the final event in the picker lifecycle

All events are registered through the "on" property:

picker.on.close(...);

picker.on.authenticate(...);

Initialize

The picker is initialized by specifying the window into which the picker should be rendered, and optionally an app and scenario.

import {
    Picker,
    MSALAuthenticate,
    LogNotifications,
    IFilePickerOptions,
    IPicker,
    Popup,
} from "onedrive-picker-file";

const msalParams = {
    auth: {
        authority: "{client id authority}",
        clientId: "{client id}",
        redirectUri: "http://localhost:3000",
    },
}

const pickerInitParams = {
    // represents the set of init params as discussed in the main docs article
}

// setup the picker with the desired behaviors
// here we use the built-in behaviors "Popup" and "MSALAuthenticate" described below
const picker = Picker(window.open("", "Picker", "width=800,height=600")).using(
    Popup(),
    LogNotifications(),
    MSALAuthenticate(msalParams),
);

// optionially log any logging to the console (or any log sink)
picker.on.log(function (this: IPicker, message, level) {
    console.log(`log: [${level}] ${message}`);
});


// activate the picker with our baseUrl and options object
// because we used the "Popup" behavior the activate promise will resolve once the user picks
// items or cancels.
const results = await picker.activate({
    baseUrl: "https://tenant.sharepoint.com/sites/dev",
    options: pickerInitParams,
});

Behaviors

The picker wrapper comes with a set of behaviors included to illustrate various ways to interact with the picker.

Some behaviors compose multiple behaviors to make registration easier. These are tagged below with (composed)

Embed (composed)

This behavior is used when establishing the picker within an iframe. It is composed of the Setup behavior and the supplied pick handler. When running in embed mode the activate promise never resolves by default.

Note that you must provide an authentication behavior in addition to these defaults.

import {
    Embed,
    IPickData,
} from "onedrive-picker-file";

picker.using(Embed((pickedItems: IPickData) => {

    console.log(`picked: ${JSON.stringify(pickedItems)}`);
}));

RejectOnErrors

Used as part of the "Popup" composed behavior. Causes the activate promise to reject on any errors within the picker.

LamdaAuthenticate

Provides a behavior to use when you already have an authentication system and do not want to make use of the MSALAuthenticate behavior. This behavior allows you to inject your funcitonality into the wrapper through a function which asyncronously returns a string, representing a valid token.

import {
    IAuthenticateCommand,
} from "onedrive-picker-file";

function async getToken(command: IAuthenticateCommand): Promise<string> {

    const { resource } = command;

    const token = await {your code to get token};

    return token;
}

picker.using(LamdaAuthenticate(getToken));

LogNotifications

Allows you to automatically log any notifications to the picker's log event. You can optionally register an observer on the notification event yourself.

This is included in both the Embed and Popup composed behaviors.

import {
    LogNotifications,
} from "onedrive-picker-file";

picker.using(LogNotifications());

MSALAuthenticate

This behavior sets up an MSAL instance to provide tokens to the picker. It requires the standard MSAL init configuration, and is a thin wrapper around the @azure/msal-browser library.

If you are already creating tokens for your application, consider using the LamdaAuthenticate behavior and wrapping your existing functionality.

import {
    MSALAuthenticate,
} from "onedrive-picker-file";

const msalParams = {
    auth: {
        authority: "{client id authority}",
        clientId: "{client id}",
        redirectUri: "http://localhost:3000",
    },
}

picker.using(MSALAuthenticate(msalParams));

Close

This behavior is used as part of the Popup composed behavior to close the window when a user selects the Cancel button.

import {
    Close,
} from "onedrive-picker-file";

picker.using(Close());

CloseOnPick

This behavior is used as part of the Popup composed behavior to close the window when a user selects items.

import {
    CloseOnPick,
} from "onedrive-picker-file";

picker.using(CloseOnPick());

Popup (composed)

This behavior is used to easily configure a scenario where a popup is used for the picker. It is composed of the Setup, Close, LogNotifications, ResolveWithPicks, and CloseOnPick behaviors.

Note that you must provide an authentication behavior in addition to these defaults.

import {
    Popup,
} from "onedrive-picker-file";

picker.using(Popup());

ResolveWithPicks

This behavior resolves the picker's activate promise with the picks made by the user. Once the promise is resolved the picker is done and no further interaction is possible.

import {
    ResolveWithPicks,
} from "onedrive-picker-file";

picker.using(ResolveWithPicks());
0.0.8

6 months ago

0.0.7

6 months ago

0.0.6

6 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago