pnp-auth icon indicating copy to clipboard operation
pnp-auth copied to clipboard

Error with sp.createIsolated()

Open dannyfoncke opened this issue 4 years ago • 6 comments

Hello, I'm using pnp-auth a lot :) thank you for providing this lib

I'm trying to use isolated sp objects with pnp-auth but I get the following error :

TypeError: safe_global_js_1.safeGlobal.fetch is not a function (node:13276) UnhandledPromiseRejectionWarning: TypeError: safe_global_js_1.safeGlobal.fetch is not a function at FetchClient.fetch (C:\Projects\sptoolkit-projects\packages\common\net.ts:66:27) at SPHttpClient.retry (C:\Projects\sptoolkit-projects\packages\sp\sphttpclient.ts:122:24) at C:\Projects\sptoolkit-projects\packages\sp\sphttpclient.ts:144:19 at new Promise () at SPHttpClient.fetchRaw (C:\Projects\sptoolkit-projects\packages\sp\sphttpclient.ts:142:16) at SPHttpClient. (C:\Projects\sptoolkit-projects\packages\sp\sphttpclient.ts:82:21)

packages used :

 "@pnp/nodejs-commonjs": "^2.6.0",
  "@pnp/sp-commonjs": "^2.6.0",
   "pnp-auth": "^2.0.0",

Code that produces the error:

   import { bootstrap, } from 'pnp-auth';
   import { sp } from '@pnp/sp-commonjs';
   import { AuthConfig, } from 'node-sp-auth-config'
   
   const isolatedSP = await sp.createIsolated()
   const authConfig = new AuthConfig({
		authOptions: thecredsobject
	});
   bootstrap(isolatedSP, authConfig)

The same code works with the standard sp object (from the import)

     .....
      bootstrap(sp, authConfig)

I hope you can point me to my error or fix the problem ?

thanks again gr

dannyfoncke avatar Jul 08 '21 07:07 dannyfoncke

Thanks @dannyfoncke,

We'll take a look. Isolated environments are relatively new and something might be at edge of PnPjs & the auth library. However, messages out of the sphttpclient don't look so good to me.

koltyakov avatar Jul 08 '21 16:07 koltyakov

Hi @dannyfoncke,

I was not able to repro. Bootrapping with isolated sites works for me.

koltyakov avatar Jul 12 '21 10:07 koltyakov

Thanks for investigating @koltyakov

Can the fact that I'm using add-in creds have something to do with it ? Also I'm targeting SharePoint 2019 on-prem (I probably should have included that in my question - sorry)

dannyfoncke avatar Jul 12 '21 10:07 dannyfoncke

It might be. I've tested user creds to two different SPO tenants. Have no On-Prem with Add-ins model enabled to check if it's the case.

Can you try if a different auth strategy fails the same for you? I also used PnPjs v2.7 not 2.6.

koltyakov avatar Jul 12 '21 11:07 koltyakov

Sadly upgrading to v2.7 has not resolved the issue

Trying a different auth strategy will take a bit more doing (Also it will need to be done targeting SP2013)

dannyfoncke avatar Jul 13 '21 09:07 dannyfoncke

Hello

Update : it seems to be working in version 2.8 with some issues down the line.

Straightforward creating an isolated sp object and using it works in like "isolatedsp.web...." expressions (I'm on SharePoint 2019 onprem, use add-in creds and the pnp-auth/node-sp-auth-config combo)

Starting from a default created isolatedsp object (no configuration options), this scenario, taken from the pnp documentation, however does not :

const file = await isolatedsp.web.getFolderByServerRelativeUrl("/sites/dev/Shared%20Documents/test/").files.add("file.name", "file", true);
const item = await file.file.getItem();
await item.update({
  Title: "A Title",
  OtherField: "My Other Value"
});

The statement : isolatedsp.web.getFolder...... works The statement : file.file.getItem() works The statement item.fileupdate... fails with a TypeError: safe_global_js_1.safeGlobal.fetch is not a function

Thanks again for the work provided on pnp and related libs.

dannyfoncke avatar Aug 31 '21 05:08 dannyfoncke