Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

harvey-sdk-pnptimeline

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

harvey-sdk-pnptimeline

Provides an SDK to construct and interact with the OneDrive v8 Picker

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5K
decreased by-41.29%
Maintainers
0
Weekly downloads
 
Created
Source

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(...);

Build Example SDK

To reference this library from another sample solution:

  1. In this repository run npm run watch to start up tsc in watch mode or npm run build to transpile the source
  2. In the solution where you want to reference this package, use npm install file://path/to/picker-ts-api --save

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 "@pnp/picker-api";

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 "@pnp/picker-api";

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 "@pnp/picker-api";

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 "@pnp/picker-api";

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 "@pnp/picker-api";

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 "@pnp/picker-api";

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 "@pnp/picker-api";

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 "@pnp/picker-api";

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 "@pnp/picker-api";

picker.using(ResolveWithPicks());

FAQs

Package last updated on 06 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc