@contentstack/utils
Advanced tools
| /// <reference types="node" /> | ||
| import * as path from 'path'; | ||
| import * as fs from 'fs'; | ||
| // Load regions.json at runtime from the dist/lib directory | ||
| function loadRegions() { | ||
| // Only look for regions.json in dist/lib directory | ||
| var regionsPath = path.join(process.cwd(), 'dist', 'lib', 'regions.json'); | ||
| if (fs.existsSync(regionsPath)) { | ||
| try { | ||
| var regionsData = fs.readFileSync(regionsPath, 'utf-8'); | ||
| return JSON.parse(regionsData); | ||
| } | ||
| catch (error) { | ||
| throw new Error("Failed to parse regions.json: ".concat(error instanceof Error ? error.message : String(error))); | ||
| } | ||
| } | ||
| // If not found, throw clear error | ||
| throw new Error('regions.json file not found at dist/lib/regions.json. Please ensure the package is properly installed and postinstall script has run.'); | ||
| } | ||
| // Cache the loaded regions data | ||
| var cachedRegions = null; | ||
| function getRegions() { | ||
| if (!cachedRegions) { | ||
| cachedRegions = loadRegions(); | ||
| } | ||
| return cachedRegions; | ||
| } | ||
| export function getContentstackEndpoint(region, service, omitHttps) { | ||
| if (region === void 0) { region = 'us'; } | ||
| if (service === void 0) { service = ''; } | ||
| if (omitHttps === void 0) { omitHttps = false; } | ||
| // Validate empty region before any processing | ||
| if (region === '') { | ||
| console.warn('Invalid region: empty or invalid region provided'); | ||
| throw new Error('Unable to set the host. Please put valid host'); | ||
| } | ||
| try { | ||
| var regionsData = getRegions(); | ||
| // Normalize the region input | ||
| var normalizedRegion = region.toLowerCase().trim() || 'us'; | ||
| // Check if regions data is malformed | ||
| if (!Array.isArray(regionsData.regions)) { | ||
| throw new Error('Invalid Regions file. Please install the SDK again to fix this issue.'); | ||
| } | ||
| // Find the region by ID or alias | ||
| var regionData = findRegionByIDOrAlias(regionsData.regions, normalizedRegion); | ||
| if (!regionData) { | ||
| // Check if this looks like a legacy format that should throw an error | ||
| if (region.includes('_') || region.includes('-')) { | ||
| var parts = region.split(/[-_]/); | ||
| if (parts.length >= 2) { | ||
| console.warn("Invalid region combination."); | ||
| throw new Error('Region Invalid. Please use a valid region identifier.'); | ||
| } | ||
| } | ||
| console.warn('Invalid region:', region, '(normalized:', normalizedRegion + ')'); | ||
| console.warn('Failed to fetch endpoints:', new Error("Invalid region: ".concat(region))); | ||
| return getDefaultEndpoint(service, omitHttps); | ||
| } | ||
| // Get the endpoint(s) | ||
| var endpoint = void 0; | ||
| if (service) { | ||
| // Return specific service endpoint | ||
| endpoint = regionData.endpoints[service]; | ||
| if (!endpoint) { | ||
| // For invalid services, return undefined (as expected by some tests) | ||
| return undefined; | ||
| } | ||
| } | ||
| else { | ||
| return omitHttps ? stripHttps(regionData.endpoints) : regionData.endpoints; | ||
| } | ||
| return omitHttps ? stripHttps(endpoint) : endpoint; | ||
| } | ||
| catch (error) { | ||
| console.warn('Failed to fetch endpoints:', error); | ||
| return getDefaultEndpoint(service, omitHttps); | ||
| } | ||
| } | ||
| function getDefaultEndpoint(service, omitHttps) { | ||
| var _a; | ||
| var regions = getRegions(); | ||
| var defaultEndpoints = ((_a = regions.regions.find(function (r) { return r.isDefault; })) === null || _a === void 0 ? void 0 : _a.endpoints) || {}; | ||
| var value = defaultEndpoints[service]; | ||
| var endpoint = typeof value === 'string' ? value : 'https://cdn.contentstack.io'; | ||
| return omitHttps ? endpoint.replace(/^https?:\/\//, '') : endpoint; | ||
| } | ||
| function findRegionByIDOrAlias(regions, regionInput) { | ||
| // First try to find by exact ID match | ||
| var region = regions.find(function (r) { return r.id === regionInput; }); | ||
| if (region) { | ||
| return region; | ||
| } | ||
| // Then try to find by alias | ||
| region = regions.find(function (r) { | ||
| return r.alias.some(function (alias) { return alias.toLowerCase() === regionInput.toLowerCase(); }); | ||
| }); | ||
| return region || null; | ||
| } | ||
| function stripHttps(endpoint) { | ||
| if (typeof endpoint === 'string') { | ||
| return endpoint.replace(/^https?:\/\//, ''); | ||
| } | ||
| else { | ||
| var result = {}; | ||
| for (var key in endpoint) { | ||
| result[key] = stripHttps(endpoint[key]); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| //# sourceM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AC,CAAC;IACrD,IAAI,MAAM,EAAE;QACV,OAAO,MAAM,CAAC;KACf;IAED,4BAA4B;IAC5B,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,UAAA,CAAC;QACrB,OAAA,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,EAAjD,CAAiD,CAAC;IAAxE,CAAwE,CACzE,CAAC;IAEF,OAAO,MAAM,IAAI,IAAI,CAAC;AACxB,CAAC;AAED,SAAS,UAAU,CAAC,QAAwC;IAC1D,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;QAChC,OAAO,QAAQ,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;KAC7C;SAAM;QACL,IAAM,MAAM,GAA0B,EAAE,CAAC;QACzC,KAAK,IAAM,GAAG,IAAI,QAAQ,EAAE;YAC1B,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;SACzC;QACD,OAAO,MAAM,CAAC;KACf;AACH,CAAC"} |
| { | ||
| "regions": [ | ||
| { | ||
| "id": "na", | ||
| "name": "AWS North America", | ||
| "cloudProvider": "AWS", | ||
| "location": "North America", | ||
| "alias": [ | ||
| "na", | ||
| "us", | ||
| "aws-na", | ||
| "aws_na" | ||
| ], | ||
| "isDefault": true, | ||
| "endpoints": { | ||
| "application": "https://app.contentstack.com", | ||
| "contentDelivery": "https://cdn.contentstack.io", | ||
| "contentManagement": "https://api.contentstack.io", | ||
| "auth": "https://auth-api.contentstack.com", | ||
| "graphqlDelivery": "https://graphql.contentstack.com", | ||
| "preview": "https://rest-preview.contentstack.com", | ||
| "graphqlPreview": "https://graphql-preview.contentstack.com", | ||
| "images": "https://images.contentstack.io", | ||
| "assets": "https://assets.contentstack.io", | ||
| "automate": "https://automations-api.contentstack.com", | ||
| "launch": "https://launch-api.contentstack.com", | ||
| "developerHub": "https://developerhub-api.contentstack.com", | ||
| "brandKit": "https://brand-kits-api.contentstack.com", | ||
| "genAI": "https://ai.contentstack.com", | ||
| "personalize": "https://personalize-api.contentstack.com", | ||
| "personalizeEdge": "https://personalize-edge.contentstack.com" | ||
| } | ||
| }, | ||
| { | ||
| "id": "eu", | ||
| "name": "AWS Europe", | ||
| "cloudProvider": "AWS", | ||
| "location": "Europe", | ||
| "alias": [ | ||
| "eu", | ||
| "aws-eu", | ||
| "aws_eu" | ||
| ], | ||
| "isDefault": false, | ||
| "endpoints": { | ||
| "application": "https://eu-app.contentstack.com", | ||
| "contentDelivery": "https://eu-cdn.contentstack.com", | ||
| "contentManagement": "https://eu-api.contentstack.com", | ||
| "auth": "https://eu-auth-api.contentstack.com", | ||
| "graphqlDelivery": "https://eu-graphql.contentstack.com", | ||
| "preview": "https://eu-rest-preview.contentstack.com", | ||
| "graphqlPreview": "https://eu-graphql-preview.contentstack.com", | ||
| "images": "https://eu-images.contentstack.com", | ||
| "assets": "https://eu-assets.contentstack.com", | ||
| "automate": "https://eu-prod-automations-api.contentstack.com", | ||
| "launch": "https://eu-launch-api.contentstack.com", | ||
| "developerHub": "https://eu-developerhub-api.contentstack.com", | ||
| "brandKit": "https://eu-brand-kits-api.contentstack.com", | ||
| "genAI": "https://eu-ai.contentstack.com", | ||
| "personalize": "https://eu-personalize-api.contentstack.com", | ||
| "personalizeEdge": "https://eu-personalize-edge.contentstack.com" | ||
| } | ||
| }, | ||
| { | ||
| "id": "au", | ||
| "name": "AWS Australia", | ||
| "cloudProvider": "AWS", | ||
| "location": "Australia", | ||
| "alias": [ | ||
| "au", | ||
| "aws-au", | ||
| "aws_au" | ||
| ], | ||
| "isDefault": false, | ||
| "endpoints": { | ||
| "application": "https://au-app.contentstack.com", | ||
| "contentDelivery": "https://au-cdn.contentstack.com", | ||
| "contentManagement": "https://au-api.contentstack.com", | ||
| "auth": "https://au-auth-api.contentstack.com", | ||
| "graphqlDelivery": "https://au-graphql.contentstack.com", | ||
| "preview": "https://au-rest-preview.contentstack.com", | ||
| "graphqlPreview": "https://au-graphql-preview.contentstack.com", | ||
| "images": "https://au-images.contentstack.com", | ||
| "assets": "https://au-assets.contentstack.com", | ||
| "automate": "https://au-prod-automations-api.contentstack.com", | ||
| "launch": "https://au-launch-api.contentstack.com", | ||
| "developerHub": "https://au-developerhub-api.contentstack.com", | ||
| "brandKit": "https://au-brand-kits-api.contentstack.com", | ||
| "genAI": "https://au-ai.contentstack.com", | ||
| "personalize": "https://au-personalize-api.contentstack.com", | ||
| "personalizeEdge": "https://au-personalize-edge.contentstack.com" | ||
| } | ||
| }, | ||
| { | ||
| "id": "azure-na", | ||
| "name": "Azure North America", | ||
| "cloudProvider": "Azure", | ||
| "location": "North America", | ||
| "alias": [ | ||
| "azure-na", | ||
| "azure_na" | ||
| ], | ||
| "isDefault": false, | ||
| "endpoints": { | ||
| "application": "https://azure-na-app.contentstack.com", | ||
| "contentDelivery": "https://azure-na-cdn.contentstack.com", | ||
| "contentManagement": "https://azure-na-api.contentstack.com", | ||
| "auth": "https://azure-na-auth-api.contentstack.com", | ||
| "graphqlDelivery": "https://azure-na-graphql.contentstack.com", | ||
| "preview": "https://azure-na-rest-preview.contentstack.com", | ||
| "graphqlPreview": "https://azure-na-graphql-preview.contentstack.com", | ||
| "images": "https://azure-na-images.contentstack.com", | ||
| "assets": "https://azure-na-assets.contentstack.com", | ||
| "automate": "https://azure-na-automations-api.contentstack.com", | ||
| "launch": "https://azure-na-launch-api.contentstack.com", | ||
| "developerHub": "https://azure-na-developerhub-api.contentstack.com", | ||
| "brandKit": "https://azure-na-brand-kits-api.contentstack.com", | ||
| "genAI": "https://azure-na-ai.contentstack.com", | ||
| "personalize": "https://azure-na-personalize-api.contentstack.com", | ||
| "personalizeEdge": "https://azure-na-personalize-edge.contentstack.com" | ||
| } | ||
| }, | ||
| { | ||
| "id": "azure-eu", | ||
| "name": "Azure Europe", | ||
| "cloudProvider": "Azure", | ||
| "location": "Europe", | ||
| "alias": [ | ||
| "azure-eu", | ||
| "azure_eu" | ||
| ], | ||
| "isDefault": false, | ||
| "endpoints": { | ||
| "application": "https://azure-eu-app.contentstack.com", | ||
| "contentDelivery": "https://azure-eu-cdn.contentstack.com", | ||
| "contentManagement": "https://azure-eu-api.contentstack.com", | ||
| "auth": "https://azure-eu-auth-api.contentstack.com", | ||
| "graphqlDelivery": "https://azure-eu-graphql.contentstack.com", | ||
| "preview": "https://azure-eu-rest-preview.contentstack.com", | ||
| "graphqlPreview": "https://azure-eu-graphql-preview.contentstack.com", | ||
| "images": "https://azure-eu-images.contentstack.com", | ||
| "assets": "https://azure-eu-assets.contentstack.com", | ||
| "automate": "https://azure-eu-automations-api.contentstack.com", | ||
| "launch": "https://azure-eu-launch-api.contentstack.com", | ||
| "developerHub": "https://azure-eu-developerhub-api.contentstack.com", | ||
| "brandKit": "https://azure-eu-brand-kits-api.contentstack.com", | ||
| "genAI": "https://azure-eu-ai.contentstack.com", | ||
| "personalize": "https://azure-eu-personalize-api.contentstack.com", | ||
| "personalizeEdge": "https://azure-eu-personalize-edge.contentstack.com" | ||
| } | ||
| }, | ||
| { | ||
| "id": "gcp-na", | ||
| "name": "GCP North America", | ||
| "cloudProvider": "GCP", | ||
| "location": "North America", | ||
| "alias": [ | ||
| "gcp-na", | ||
| "gcp_na" | ||
| ], | ||
| "isDefault": false, | ||
| "endpoints": { | ||
| "application": "https://gcp-na-app.contentstack.com", | ||
| "contentDelivery": "https://gcp-na-cdn.contentstack.com", | ||
| "contentManagement": "https://gcp-na-api.contentstack.com", | ||
| "auth": "https://gcp-na-auth-api.contentstack.com", | ||
| "graphqlDelivery": "https://gcp-na-graphql.contentstack.com", | ||
| "preview": "https://gcp-na-rest-preview.contentstack.com", | ||
| "graphqlPreview": "https://gcp-na-graphql-preview.contentstack.com", | ||
| "images": "https://gcp-na-images.contentstack.com", | ||
| "assets": "https://gcp-na-assets.contentstack.com", | ||
| "automate": "https://gcp-na-automations-api.contentstack.com", | ||
| "launch": "https://gcp-na-launch-api.contentstack.com", | ||
| "developerHub": "https://gcp-na-developerhub-api.contentstack.com", | ||
| "brandKit": "https://gcp-na-brand-kits-api.contentstack.com", | ||
| "genAI": "https://gcp-na-brand-kits-api.contentstack.com", | ||
| "personalize": "https://gcp-na-personalize-api.contentstack.com", | ||
| "personalizeEdge": "https://gcp-na-personalize-edge.contentstack.com" | ||
| } | ||
| }, | ||
| { | ||
| "id": "gcp-eu", | ||
| "name": "GCP Europe", | ||
| "cloudProvider": "GCP", | ||
| "location": "Europe", | ||
| "alias": [ | ||
| "gcp-eu", | ||
| "gcp_eu" | ||
| ], | ||
| "isDefault": false, | ||
| "endpoints": { | ||
| "application": "https://gcp-eu-app.contentstack.com", | ||
| "contentDelivery": "https://gcp-eu-cdn.contentstack.com", | ||
| "contentManagement": "https://gcp-eu-api.contentstack.com", | ||
| "auth": "https://gcp-eu-auth-api.contentstack.com", | ||
| "graphqlDelivery": "https://gcp-eu-graphql.contentstack.com", | ||
| "preview": "https://gcp-eu-rest-preview.contentstack.com", | ||
| "graphqlPreview": "https://gcp-eu-graphql-preview.contentstack.com", | ||
| "images": "https://gcp-eu-images.contentstack.com", | ||
| "assets": "https://gcp-eu-assets.contentstack.com", | ||
| "automate": "https://gcp-eu-automations-api.contentstack.com", | ||
| "launch": "https://gcp-eu-launch-api.contentstack.com", | ||
| "developerHub": "https://gcp-eu-developerhub-api.contentstack.com", | ||
| "brandKit": "https://gcp-eu-brand-kits-api.contentstack.com", | ||
| "genAI": "https://gcp-eu-brand-kits-api.contentstack.com", | ||
| "personalize": "https://gcp-eu-personalize-api.contentstack.com", | ||
| "personalizeEdge": "https://gcp-eu-personalize-edge.contentstack.com" | ||
| } | ||
| } | ||
| ] | ||
| } |
| export interface ContentstackEndpoints { | ||
| [key: string]: string | ContentstackEndpoints; | ||
| } | ||
| export interface RegionData { | ||
| id: string; | ||
| name: string; | ||
| cloudProvider: string; | ||
| location: string; | ||
| alias: string[]; | ||
| isDefault: boolean; | ||
| endpoints: ContentstackEndpoints; | ||
| } | ||
| export interface RegionsResponse { | ||
| regions: RegionData[]; | ||
| } | ||
| export declare function getContentstackEndpoint(region?: string, service?: string, omitHttps?: boolean): string | ContentstackEndpoints; |
+134
-0
| 'use strict'; | ||
| var path = require('path'); | ||
| var fs = require('fs'); | ||
| function _interopNamespaceDefault(e) { | ||
| var n = Object.create(null); | ||
| if (e) { | ||
| Object.keys(e).forEach(function (k) { | ||
| if (k !== 'default') { | ||
| var d = Object.getOwnPropertyDescriptor(e, k); | ||
| Object.defineProperty(n, k, d.get ? d : { | ||
| enumerable: true, | ||
| get: function () { return e[k]; } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| n.default = e; | ||
| return Object.freeze(n); | ||
| } | ||
| var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path); | ||
| var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs); | ||
| function replaceHtmlEntities(text) { | ||
@@ -1076,2 +1099,112 @@ return text | ||
| /// <reference types="node" /> | ||
| // Load regions.json at runtime from the dist/lib directory | ||
| function loadRegions() { | ||
| // Only look for regions.json in dist/lib directory | ||
| var regionsPath = path__namespace.join(process.cwd(), 'dist', 'lib', 'regions.json'); | ||
| if (fs__namespace.existsSync(regionsPath)) { | ||
| try { | ||
| var regionsData = fs__namespace.readFileSync(regionsPath, 'utf-8'); | ||
| return JSON.parse(regionsData); | ||
| } | ||
| catch (error) { | ||
| throw new Error("Failed to parse regions.json: ".concat(error instanceof Error ? error.message : String(error))); | ||
| } | ||
| } | ||
| // If not found, throw clear error | ||
| throw new Error('regions.json file not found at dist/lib/regions.json. Please ensure the package is properly installed and postinstall script has run.'); | ||
| } | ||
| // Cache the loaded regions data | ||
| var cachedRegions = null; | ||
| function getRegions() { | ||
| if (!cachedRegions) { | ||
| cachedRegions = loadRegions(); | ||
| } | ||
| return cachedRegions; | ||
| } | ||
| function getContentstackEndpoint(region, service, omitHttps) { | ||
| if (region === void 0) { region = 'us'; } | ||
| if (service === void 0) { service = ''; } | ||
| if (omitHttps === void 0) { omitHttps = false; } | ||
| // Validate empty region before any processing | ||
| if (region === '') { | ||
| console.warn('Invalid region: empty or invalid region provided'); | ||
| throw new Error('Unable to set the host. Please put valid host'); | ||
| } | ||
| try { | ||
| var regionsData = getRegions(); | ||
| // Normalize the region input | ||
| var normalizedRegion = region.toLowerCase().trim() || 'us'; | ||
| // Check if regions data is malformed | ||
| if (!Array.isArray(regionsData.regions)) { | ||
| throw new Error('Invalid Regions file. Please install the SDK again to fix this issue.'); | ||
| } | ||
| // Find the region by ID or alias | ||
| var regionData = findRegionByIDOrAlias(regionsData.regions, normalizedRegion); | ||
| if (!regionData) { | ||
| // Check if this looks like a legacy format that should throw an error | ||
| if (region.includes('_') || region.includes('-')) { | ||
| var parts = region.split(/[-_]/); | ||
| if (parts.length >= 2) { | ||
| console.warn("Invalid region combination."); | ||
| throw new Error('Region Invalid. Please use a valid region identifier.'); | ||
| } | ||
| } | ||
| console.warn('Invalid region:', region, '(normalized:', normalizedRegion + ')'); | ||
| console.warn('Failed to fetch endpoints:', new Error("Invalid region: ".concat(region))); | ||
| return getDefaultEndpoint(service, omitHttps); | ||
| } | ||
| // Get the endpoint(s) | ||
| var endpoint = void 0; | ||
| if (service) { | ||
| // Return specific service endpoint | ||
| endpoint = regionData.endpoints[service]; | ||
| if (!endpoint) { | ||
| // For invalid services, return undefined (as expected by some tests) | ||
| return undefined; | ||
| } | ||
| } | ||
| else { | ||
| return omitHttps ? stripHttps(regionData.endpoints) : regionData.endpoints; | ||
| } | ||
| return omitHttps ? stripHttps(endpoint) : endpoint; | ||
| } | ||
| catch (error) { | ||
| console.warn('Failed to fetch endpoints:', error); | ||
| return getDefaultEndpoint(service, omitHttps); | ||
| } | ||
| } | ||
| function getDefaultEndpoint(service, omitHttps) { | ||
| var _a; | ||
| var regions = getRegions(); | ||
| var defaultEndpoints = ((_a = regions.regions.find(function (r) { return r.isDefault; })) === null || _a === void 0 ? void 0 : _a.endpoints) || {}; | ||
| var value = defaultEndpoints[service]; | ||
| var endpoint = typeof value === 'string' ? value : 'https://cdn.contentstack.io'; | ||
| return omitHttps ? endpoint.replace(/^https?:\/\//, '') : endpoint; | ||
| } | ||
| function findRegionByIDOrAlias(regions, regionInput) { | ||
| // First try to find by exact ID match | ||
| var region = regions.find(function (r) { return r.id === regionInput; }); | ||
| if (region) { | ||
| return region; | ||
| } | ||
| // Then try to find by alias | ||
| region = regions.find(function (r) { | ||
| return r.alias.some(function (alias) { return alias.toLowerCase() === regionInput.toLowerCase(); }); | ||
| }); | ||
| return region || null; | ||
| } | ||
| function stripHttps(endpoint) { | ||
| if (typeof endpoint === 'string') { | ||
| return endpoint.replace(/^https?:\/\//, ''); | ||
| } | ||
| else { | ||
| var result = {}; | ||
| for (var key in endpoint) { | ||
| result[key] = stripHttps(endpoint[key]); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| exports.Document = Document; | ||
@@ -1086,2 +1219,3 @@ exports.GQL = GQL; | ||
| exports.attributeToString = attributeToString; | ||
| exports.getContentstackEndpoint = getContentstackEndpoint; | ||
| exports.jsonToHTML = jsonToHTML$1; | ||
@@ -1088,0 +1222,0 @@ exports.render = render; |
@@ -13,2 +13,3 @@ export { attributeToString } from './Models/metadata-model'; | ||
| export { updateAssetURLForGQL } from './updateAssetURLForGQL'; | ||
| export { getContentstackEndpoint } from './endpoints'; | ||
| //# sourceMappingURL=index.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAwB,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAClF,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAGlE,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO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,CAAA;AAC7D,OAAO,EAAE,uBAAuB,EAAyB,MAAM,aAAa,CAAA"} |
@@ -16,1 +16,2 @@ export { Next, Option, RenderNode, RenderMark, RenderItem, RenderContentType, RenderOption } from './options/index'; | ||
| export { updateAssetURLForGQL } from './updateAssetURLForGQL'; | ||
| export { getContentstackEndpoint, ContentstackEndpoints } from './endpoints'; |
+9
-5
| { | ||
| "name": "@contentstack/utils", | ||
| "version": "1.5.0", | ||
| "version": "1.6.0", | ||
| "description": "Contentstack utilities for Javascript", | ||
| "main": "dist/index.es.js", | ||
| "types": "dist/types/index.d.ts", | ||
| "types": "dist/types/src/index.d.ts", | ||
| "files": [ | ||
@@ -23,6 +23,8 @@ "dist" | ||
| "clear:badges": "rm -rf badges", | ||
| "test": "npm run clear:reports && jest --ci --json --coverage --testLocationInResults --outputFile=./reports/report.json", | ||
| "pretest": "npm run build", | ||
| "test": "npm run clear:reports && pwd && jest --ci --json --coverage --testLocationInResults --outputFile=./reports/report.json", | ||
| "test:badges": "npm run clear:badges && npm run test && jest-coverage-badges --input ./reports/coverage/coverage-summary.json --output ./badges", | ||
| "test:debug": "jest --watchAll --runInBand", | ||
| "prebuild": "rimraf dist", | ||
| "prebuild": "rimraf dist && mkdir -p dist/lib && npm run download-regions", | ||
| "download-regions": "curl -v --max-time 30 --fail https://artifacts.contentstack.com/regions.json -o dist/lib/regions.json || echo 'Warning: Failed to download regions.json'", | ||
| "build": "tsc && rollup -c", | ||
@@ -34,3 +36,5 @@ "format": "prettier --write \"src/**/*.ts\"", | ||
| "version": "npm run format && git add -A src", | ||
| "postversion": "git push && git push --tags" | ||
| "postversion": "git push && git push --tags", | ||
| "postinstall": "npm run download-regions", | ||
| "postupdate": "npm run download-regions" | ||
| }, | ||
@@ -37,0 +41,0 @@ "author": "Contentstack", |
Sorry, the diff of this file is too big to display
Install scripts
Supply chain riskInstall scripts are run when the package is installed or built. Malicious packages often use scripts that run automatically to execute payloads or fetch additional code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 2 instances in 1 package
323072
11.29%87
4.82%2967
18.82%1
Infinity%4
Infinity%