@paypal/connect-loader-component
Advanced tools
Comparing version 0.2.0 to 0.3.0
export declare const constants: { | ||
readonly AXO_URL: Record<string, string>; | ||
readonly LOCALE_PATH: "connect-v0/locales/"; | ||
readonly CDNX_PROD: "https://www.paypalobjects.com"; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.constants = void 0; | ||
var ASSETS_URLS = { | ||
minified: "https://www.paypalobjects.com/connect-v0/axo.min.js", | ||
unminified: "https://www.paypalobjects.com/connect-v0/axo.js", | ||
var CDNX_PROD = "https://www.paypalobjects.com"; | ||
var ASSET_PATH = { | ||
minified: "connect-v0/axo.min.js", | ||
unminified: "connect-v0/axo.js", | ||
}; | ||
var LOCALE_PATH = "connect-v0/locales/"; | ||
exports.constants = { | ||
AXO_URL: ASSETS_URLS, | ||
AXO_URL: ASSET_PATH, | ||
LOCALE_PATH: LOCALE_PATH, | ||
CDNX_PROD: CDNX_PROD, | ||
}; |
@@ -7,3 +7,7 @@ import { LoadAxoOptions } from "./types"; | ||
*/ | ||
declare function loadAxo(options?: LoadAxoOptions): Promise<HTMLScriptElement>; | ||
declare function loadAxo(options?: LoadAxoOptions): Promise<{ | ||
metadata: { | ||
localeUrl: string; | ||
}; | ||
}>; | ||
export = loadAxo; |
@@ -47,16 +47,22 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var assetUrl; | ||
var assetUrl, localeUrl, err_1; | ||
return __generator(this, function (_a) { | ||
try { | ||
assetUrl = getAssetsUrl(options); | ||
return [2 /*return*/, (0, asset_loader_1.loadScript)({ | ||
id: "axo-id", | ||
src: assetUrl, | ||
forceScriptReload: true, | ||
})]; | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
assetUrl = getAssetsUrl(options); | ||
localeUrl = getLocaleUrl(options); | ||
return [4 /*yield*/, (0, asset_loader_1.loadScript)({ | ||
id: "axo-id", | ||
src: assetUrl, | ||
forceScriptReload: true, | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, { metadata: { localeUrl: localeUrl } }]; | ||
case 2: | ||
err_1 = _a.sent(); | ||
return [2 /*return*/, Promise.reject(err_1)]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
catch (err) { | ||
return [2 /*return*/, Promise.reject(err)]; | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
@@ -66,12 +72,39 @@ }); | ||
/** | ||
* Prepends the domain to the asset url | ||
* @param options object with assetUrl and bundleid parameters to determine which URL to return | ||
* @returns full domain and assets URL as string | ||
*/ | ||
function generateAssetUrl(_a) { | ||
var assetUrl = _a.assetUrl, bundleId = _a.bundleId; | ||
return bundleId | ||
? "https://cdn-".concat(bundleId, ".static.engineering.dev.paypalinc.com/").concat(assetUrl) | ||
: "".concat(constants_1.constants.CDNX_PROD, "/").concat(assetUrl); | ||
} | ||
/** | ||
* Retrieves either the minified or unminified assets URL as specified | ||
* @param options (Optional) object with a minified parameter to determine which URL to return | ||
* @param options (Optional) object with a minified and metadata with bundleIdOverride parameters to determine which URL to return | ||
* @returns assets URL as string | ||
*/ | ||
function getAssetsUrl(options) { | ||
var axoURL = !options || options.minified | ||
var _a; | ||
var assetUrl = (options === null || options === void 0 ? void 0 : options.minified) !== false | ||
? constants_1.constants.AXO_URL.minified | ||
: constants_1.constants.AXO_URL.unminified; | ||
return axoURL; | ||
return generateAssetUrl({ | ||
assetUrl: assetUrl, | ||
bundleId: (_a = options === null || options === void 0 ? void 0 : options.metadata) === null || _a === void 0 ? void 0 : _a.bundleIdOveride, | ||
}); | ||
} | ||
/** | ||
* Retrieves the Locales URL, the path to our language files | ||
* @param options (Optional) object with a minified and metadata with bundleIdOverride parameters to determine which URL to return | ||
* @returns locale URL as string | ||
*/ | ||
function getLocaleUrl(options) { | ||
var _a; | ||
return generateAssetUrl({ | ||
assetUrl: constants_1.constants.LOCALE_PATH, | ||
bundleId: (_a = options === null || options === void 0 ? void 0 : options.metadata) === null || _a === void 0 ? void 0 : _a.bundleIdOveride, | ||
}); | ||
} | ||
module.exports = loadAxo; |
@@ -0,3 +1,12 @@ | ||
type LoadAxoOptionsMetaData = { | ||
bundleIdOveride?: string; | ||
}; | ||
export type GenerateAssetUrl = { | ||
assetUrl: string; | ||
bundleId?: string; | ||
}; | ||
export type LoadAxoOptions = { | ||
minified?: boolean; | ||
metadata?: LoadAxoOptionsMetaData; | ||
}; | ||
export {}; |
{ | ||
"name": "@paypal/connect-loader-component", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Loads AXO script", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -17,11 +17,7 @@ jest.mock("@braintree/asset-loader", () => { | ||
testContext = {}; | ||
testContext.fakeScriptTag = document.createElement("script"); | ||
testContext.fakeScriptLoader = () => { | ||
return Promise.resolve(testContext.fakeScriptTag); | ||
testContext.mockLoadResponse = { | ||
metadata: { | ||
localeUrl: "https://www.paypalobjects.com/connect-v0/locales/", | ||
}, | ||
}; | ||
jest | ||
.spyOn(assetLoader, "loadScript") | ||
.mockImplementation(testContext.fakeScriptLoader); | ||
}); | ||
@@ -31,7 +27,7 @@ | ||
const promise = loadAxo({ minified: true }); | ||
expect(promise).resolves.toBe(testContext.fakeScriptTag); | ||
expect(promise).resolves.toStrictEqual(testContext.mockLoadResponse); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: constants.AXO_URL.minified, | ||
src: `${constants.CDNX_PROD}/${constants.AXO_URL.minified}`, | ||
}) | ||
@@ -44,7 +40,7 @@ ); | ||
expect(promise).resolves.toBe(testContext.fakeScriptTag); | ||
expect(promise).resolves.toStrictEqual(testContext.mockLoadResponse); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: constants.AXO_URL.unminified, | ||
src: `${constants.CDNX_PROD}/${constants.AXO_URL.unminified}`, | ||
}) | ||
@@ -54,12 +50,48 @@ ); | ||
it("default to the unminified script when no environment is specified", () => { | ||
it("default to the minified script when no environment is specified", () => { | ||
const promise = loadAxo(); | ||
expect(promise).resolves.toBe(testContext.fakeScriptTag); | ||
expect(promise).resolves.toStrictEqual(testContext.mockLoadResponse); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: constants.AXO_URL.minified, | ||
src: `${constants.CDNX_PROD}/${constants.AXO_URL.minified}`, | ||
}) | ||
); | ||
}); | ||
it("loads the bundle override URL when specified", () => { | ||
const bundleId = "2bc4343c92655"; | ||
const promise = loadAxo({ metadata: { bundleIdOveride: bundleId } }); | ||
expect(promise).resolves.toStrictEqual({ | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_URL.minified}`, | ||
}) | ||
); | ||
}); | ||
it("calls loadScript with the minified URL and bundle override URL when specified", async () => { | ||
const bundleId = "9df9369c92655"; | ||
const promise = loadAxo({ | ||
minified: true, | ||
metadata: { bundleIdOveride: bundleId }, | ||
}); | ||
expect(promise).resolves.toStrictEqual({ | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_URL.minified}`, | ||
}) | ||
); | ||
}); | ||
}); |
@@ -1,8 +0,14 @@ | ||
const ASSETS_URLS: Record<string, string> = { | ||
minified: "https://www.paypalobjects.com/connect-v0/axo.min.js", | ||
unminified: "https://www.paypalobjects.com/connect-v0/axo.js", | ||
const CDNX_PROD = "https://www.paypalobjects.com"; | ||
const ASSET_PATH: Record<string, string> = { | ||
minified: "connect-v0/axo.min.js", | ||
unminified: "connect-v0/axo.js", | ||
}; | ||
const LOCALE_PATH = "connect-v0/locales/"; | ||
export const constants = { | ||
AXO_URL: ASSETS_URLS, | ||
AXO_URL: ASSET_PATH, | ||
LOCALE_PATH, | ||
CDNX_PROD, | ||
} as const; |
import { loadScript } from "@braintree/asset-loader"; | ||
import { constants } from "./constants"; | ||
import { LoadAxoOptions } from "./types"; | ||
import { LoadAxoOptions, GenerateAssetUrl } from "./types"; | ||
@@ -13,3 +13,4 @@ /** | ||
const assetUrl = getAssetsUrl(options); | ||
return loadScript({ | ||
const localeUrl = getLocaleUrl(options); | ||
await loadScript({ | ||
id: "axo-id", | ||
@@ -19,2 +20,3 @@ src: assetUrl, | ||
}); | ||
return { metadata: { localeUrl } }; | ||
} catch (err) { | ||
@@ -26,15 +28,41 @@ return Promise.reject(err); | ||
/** | ||
* Prepends the domain to the asset url | ||
* @param options object with assetUrl and bundleid parameters to determine which URL to return | ||
* @returns full domain and assets URL as string | ||
*/ | ||
function generateAssetUrl({ assetUrl, bundleId }: GenerateAssetUrl): string { | ||
return bundleId | ||
? `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${assetUrl}` | ||
: `${constants.CDNX_PROD}/${assetUrl}`; | ||
} | ||
/** | ||
* Retrieves either the minified or unminified assets URL as specified | ||
* @param options (Optional) object with a minified parameter to determine which URL to return | ||
* @param options (Optional) object with a minified and metadata with bundleIdOverride parameters to determine which URL to return | ||
* @returns assets URL as string | ||
*/ | ||
function getAssetsUrl(options?: LoadAxoOptions): string { | ||
const axoURL = | ||
!options || options.minified | ||
const assetUrl = | ||
options?.minified !== false | ||
? constants.AXO_URL.minified | ||
: constants.AXO_URL.unminified; | ||
return axoURL; | ||
return generateAssetUrl({ | ||
assetUrl, | ||
bundleId: options?.metadata?.bundleIdOveride, | ||
}); | ||
} | ||
/** | ||
* Retrieves the Locales URL, the path to our language files | ||
* @param options (Optional) object with a minified and metadata with bundleIdOverride parameters to determine which URL to return | ||
* @returns locale URL as string | ||
*/ | ||
function getLocaleUrl(options?: LoadAxoOptions): string { | ||
return generateAssetUrl({ | ||
assetUrl: constants.LOCALE_PATH, | ||
bundleId: options?.metadata?.bundleIdOveride, | ||
}); | ||
} | ||
export = loadAxo; |
@@ -0,3 +1,13 @@ | ||
type LoadAxoOptionsMetaData = { | ||
bundleIdOveride?: string; | ||
}; | ||
export type GenerateAssetUrl = { | ||
assetUrl: string; | ||
bundleId?: string; | ||
}; | ||
export type LoadAxoOptions = { | ||
minified?: boolean; | ||
metadata?: LoadAxoOptionsMetaData; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16294
340