npm package diff

Package: @forge/api

Versions: 5.1.0-next.5-experimental-edac07a - 5.1.0-next.6

File: package/out/api/fetch.js

Index: package/out/api/fetch.js
===================================================================
--- package/out/api/fetch.js
+++ package/out/api/fetch.js
@@ -1,7 +1,9 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.getSandboxRuntimeAPI = exports.getNodeRuntimeAPI = exports.addMagicAgent = exports.handleProxyResponseErrors = exports.getForgeProxyError = exports.fetchRemote = exports.__fetchProduct = void 0;
+exports.getSandboxRuntimeAPI = exports.getNodeRuntimeAPI = exports.addMagicAgent = exports.handleProxyResponseErrors = exports.getForgeProxyError = exports.createRemoteUrlWithPath = exports.fetchRemote = exports.__fetchProduct = void 0;
+const tslib_1 = require("tslib");
+const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
 const _1 = require(".");
 const runtime_1 = require("./runtime");
 const polyfill_response_1 = require("./polyfill-response");
 const errors_1 = require("./errors");
@@ -15,33 +17,65 @@
     finally {
         timer.stop();
     }
 }
-function __fetchProduct(args) {
+function __fetchProduct(args, useUndici = false) {
     return async (path, init) => {
-        const response = await global.__forge_fetch__({
-            type: args.type,
-            provider: args.provider,
-            remote: args.remote
-        }, path, init);
+        let response;
+        if (useUndici && global.__forge_fetch__) {
+            response = await global.__forge_fetch__({
+                type: 'fpp',
+                provider: args.provider,
+                remote: args.remote
+            }, path, init);
+        }
+        else {
+            const url = productURL(args.remote, path);
+            init = (0, exports.addMagicAgent)(init);
+            const defaultHeaders = getDefaultHeaders(init.headers, args);
+            init.headers = { ...init.headers, ...defaultHeaders };
+            response = await (0, node_fetch_1.default)(url, init);
+        }
         (0, exports.handleProxyResponseErrors)(response);
         return response;
     };
 }
 exports.__fetchProduct = __fetchProduct;
 function fetchRemote(args) {
     return async (path, init) => {
-        const response = await global.__forge_fetch__({
-            type: 'tpp',
-            provider: args.provider,
-            remote: args.remote,
-            accountId: args.account
-        }, path, init);
+        let response;
+        if (global.__forge_fetch__) {
+            response = await global.__forge_fetch__({
+                type: 'tpp',
+                provider: args.provider,
+                remote: args.remote,
+                accountId: args.account
+            }, path, init);
+        }
+        else {
+            const remoteUrl = createRemoteUrlWithPath(`https://${args.remote}`, path);
+            init = (0, exports.addMagicAgent)(init, 'EXTERNAL_AUTH_REQUEST');
+            init.headers = {
+                ...init.headers,
+                authorization: `Forge user ${args.provider} ${args.account}`
+            };
+            response = await (0, node_fetch_1.default)(remoteUrl, init);
+        }
         (0, exports.handleProxyResponseErrors)(response);
         return response;
     };
 }
 exports.fetchRemote = fetchRemote;
+function createRemoteUrlWithPath(baseUrl, path) {
+    const remoteUrl = new URL(baseUrl);
+    const url = new URL(path, remoteUrl);
+    if (url.hostname !== remoteUrl.hostname) {
+        throw new Error(`Invalid path provided ${path}`);
+    }
+    remoteUrl.searchParams.append('path', path);
+    return remoteUrl;
+}
+exports.createRemoteUrlWithPath = createRemoteUrlWithPath;
 function getDefaultRemote(provider) {
     const externalAuthProvider = findExternalAuthProviderConfigOrThrow(provider);
     if (!externalAuthProvider.remotes.length) {
         throw new Error(`Missing remote config for provider ${provider}`);
@@ -70,8 +104,25 @@
         throw new errors_1.ProxyRequestError(response.status, errorReason);
     }
 };
 exports.handleProxyResponseErrors = handleProxyResponseErrors;
+const getDefaultHeaders = (headersInit, args) => {
+    let defaultHeaders = {};
+    const headers = new Headers(headersInit);
+    if (!headers.has('Content-Type')) {
+        defaultHeaders = { 'Content-Type': 'application/json' };
+    }
+    if (!headers.has('authorization')) {
+        defaultHeaders = { ...defaultHeaders, authorization: `Forge ${args.provider}` };
+    }
+    return defaultHeaders;
+};
+function productURL(remote, path) {
+    if (!path.startsWith('/')) {
+        path = '/' + path;
+    }
+    return `https://${remote}${path}`;
+}
 function lazyThrowNeedsAuthenticationError(serviceKey) {
     return async (scopes) => wrapInMetrics({ name: 'api.asUser.withProvider.requestCredentials', tags: { passingScopes: String(!!scopes) } }, async () => {
         throw new errors_1.NeedsAuthenticationError('Authentication Required', serviceKey, { scopes, isExpectedError: true });
     });
@@ -143,25 +194,25 @@
         };
     }
     return {
         fetch: (0, _1.wrapWithRouteUnwrapper)(fetch),
-        requestJira: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'none', remote: 'jira', type: 'fpp' })),
-        requestConfluence: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'none', remote: 'confluence', type: 'fpp' })),
-        requestBitbucket: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'none', remote: 'bitbucket', type: 'fpp' })),
+        requestJira: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'none', remote: 'jira' }, true)),
+        requestConfluence: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'none', remote: 'confluence' }, true)),
+        requestBitbucket: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'none', remote: 'bitbucket' }, true)),
         asUser: () => ({
-            requestJira: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'user', remote: 'jira', type: 'fpp' })),
-            requestConfluence: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'user', remote: 'confluence', type: 'fpp' })),
-            requestBitbucket: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'user', remote: 'bitbucket', type: 'fpp' })),
-            requestGraph: (0, _1.wrapRequestGraph)(__fetchProduct({ provider: 'user', remote: 'stargate', type: 'fpp' })),
-            requestConnectedData: (0, _1.wrapRequestConnectedData)(__fetchProduct({ provider: 'user', remote: 'stargate', type: 'fpp' })),
+            requestJira: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'user', remote: 'jira' }, true)),
+            requestConfluence: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'user', remote: 'confluence' }, true)),
+            requestBitbucket: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'user', remote: 'bitbucket' }, true)),
+            requestGraph: (0, _1.wrapRequestGraph)(__fetchProduct({ provider: 'user', remote: 'stargate' }, true)),
+            requestConnectedData: (0, _1.wrapRequestConnectedData)(__fetchProduct({ provider: 'user', remote: 'stargate' }, true)),
             withProvider
         }),
         asApp: () => ({
-            requestJira: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'app', remote: 'jira', type: 'fpp' })),
-            requestConfluence: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'app', remote: 'confluence', type: 'fpp' })),
-            requestBitbucket: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'app', remote: 'bitbucket', type: 'fpp' })),
-            requestGraph: (0, _1.wrapRequestGraph)(__fetchProduct({ provider: 'app', remote: 'stargate', type: 'fpp' })),
-            requestConnectedData: (0, _1.wrapRequestConnectedData)(__fetchProduct({ provider: 'app', remote: 'stargate', type: 'fpp' }))
+            requestJira: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'app', remote: 'jira' }, true)),
+            requestConfluence: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'app', remote: 'confluence' }, true)),
+            requestBitbucket: (0, _1.wrapRequestProduct)(__fetchProduct({ provider: 'app', remote: 'bitbucket' }, true)),
+            requestGraph: (0, _1.wrapRequestGraph)(__fetchProduct({ provider: 'app', remote: 'stargate' }, true)),
+            requestConnectedData: (0, _1.wrapRequestConnectedData)(__fetchProduct({ provider: 'app', remote: 'stargate' }, true))
         })
     };
 }
 exports.getNodeRuntimeAPI = getNodeRuntimeAPI;