npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sp-preset

v2.0.21

Published

Convenient preset that make @pnp/sp version 3 usage easier

Downloads

26

Readme

sp-preset

Convenient preset that make @pnp/sp version 3 usage more convenient

It is a slightly modified version of the preset method provided by @pnp/sp authors, just packaged in a node module and made reusable so i don't have to write it every time.

Setup

In order to work, the module needs to receive the context from your web-part. This is usually done in the onInit method:

import { setupSP } from 'sp-preset';

export default class MyWebPart extends BaseClientSideWebPart<ITasksWebPartProps> {
// ...
    protected async onInit(): Promise<void> {
        super.onInit();

        setupSP({
            // webpart context
            context: this.context,
            // additional tennants
            tennants: {
                Data: this.properties.dataSourceRoot,
                Users: userWebUrl,
            },
            // whether to use request frequency controller
            useRPM: true,
            // how many requests per minute are allowed by this controller
            rpmTreshold: 200,
            // save rpm trace (which urls were called and how many times) to localstorage
            rpmTracing: true,
            // alert user if number of request is exceeded and application is blocked
            rpmAlerting: true,
            // any additional Timelines to add to spfi
            additionalTimelinePipes: [
                InjectHeaders({
                    "Accept": "application/json;odata=nometadata"
                }),
            ],
        });
    }
// ...
}

Usage

Later in the application, when you need a specific instance of spfi:

import { getSP } from 'sp-preset';

// ...
const sp = getSP(); // get an instance of spfi
const spData = getSP('Data'); // get an instance of spfi that points to another tennant (it should be included in setup, see above)
// ...

Sometimes it's needed to receive an spfi object and use caching on it just in certain methods or classes. Just using .getSP() will not work as it would affect all methods using it, and as a result, everything will start getting cached. In this case we i can use .getNewSP() method, that will create and return a new instance of spfi so i can do whatever i want with it.

import { getNewSP } from 'sp-preset';
// ...

const spCached = getNewSP().using(Caching());
const spUsersCached = getNewSP('Users').using(Caching());
// ...

RPM (request per minute control) Control

The Request Frequency controller or RPMController is just a custom Timeline that controlls how many api calls per minute the webpart is doing. Each webpart can have different treshold of api calls.

If the treshold is reached, the application is blocked (there is a property saved to localstorage that indicates this).

This is done in order to avoid misuse or throttling of the application.

Worst case would be if because of a mistake or some misconfiguration, the webpart starts calling some api uncontrollingly and user just ignores or doesn't notice it. Then the application/user or tennant can be just blocked by Microsoft.

In order to turn on request frequency control, just pass the appropriate properties into setup.

Dependencies

The library does not include version, and requires at least version 3.1.0 to be available in the project where it's imported.