UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

153 lines 8.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.__getSPRestErrorData = exports.__isIRestError = exports.getFieldNameForUpdate = exports.GetFieldNameFromRawValues = exports.DecodeFieldValuesForEdit = exports.DecodeFieldValuesAsText = exports.EncodeFieldValuesAsTextKey = exports.DecodeFieldValuesAsTextKey = exports.GetRestBaseUrl = exports.GetSiteUrl = exports.GetFileSiteUrl = exports.hasGlobalContext = exports.CONTENT_TYPES_SELECT_WITH_FIELDS = exports.CONTENT_TYPES_SELECT = exports.WEB_SELECT = exports.LIST_EXPAND = exports.LIST_SELECT = void 0; const json_1 = require("../../helpers/json"); const typecheckers_1 = require("../../helpers/typecheckers"); const url_1 = require("../../helpers/url"); const consolelogger_1 = require("../consolelogger"); const localstoragecache_1 = require("../localstoragecache"); const rest_1 = require("../rest"); const web_1 = require("./web"); const logger = consolelogger_1.ConsoleLogger.get("sharepoint.rest/common"); exports.LIST_SELECT = `ListExperienceOptions,EffectiveBasePermissions,Description,Title,EnableAttachments,EnableModeration,BaseTemplate,BaseType,Id,Hidden,IsApplicationList,IsPrivate,IsCatalog,ImageUrl,ItemCount,ParentWebUrl,EntityTypeName,DefaultViewUrl,ParentWeb/Id,ParentWeb/Title`; exports.LIST_EXPAND = `ParentWeb/Id,ParentWeb/Title`; exports.WEB_SELECT = "Title,ServerRelativeUrl,Id,WebTemplate,Description,SiteLogoUrl"; exports.CONTENT_TYPES_SELECT = "Name,Description,StringId,Group,Hidden,ReadOnly,NewFormUrl,DisplayFormUrl,EditFormUrl,Sealed,MobileDisplayFormUrl,MobileNewFormUrl,MobileEditFormUrl,NewFormTemplateName,DisplayFormTemplateName,EditFormTemplateName"; exports.CONTENT_TYPES_SELECT_WITH_FIELDS = `${exports.CONTENT_TYPES_SELECT},Fields`; function hasGlobalContext() { //_spPageContextInfo.webServerRelativeUrl can be empty string return typeof (_spPageContextInfo) !== "undefined" && (0, typecheckers_1.isString)(_spPageContextInfo.webServerRelativeUrl); } exports.hasGlobalContext = hasGlobalContext; function GetFileSiteUrl(fileUrl) { let siteUrl; let urlParts = fileUrl.split('/'); if (urlParts[urlParts.length - 1].indexOf('.') > 0) //file name urlParts.pop(); //file name let key = "GetSiteUrl|" + urlParts.join("/").toLowerCase(); siteUrl = (0, localstoragecache_1.getCacheItem)(key); if ((0, typecheckers_1.isNullOrUndefined)(siteUrl)) { while (!(0, typecheckers_1.isValidGuid)((0, web_1.GetWebIdSync)(urlParts.join('/')))) urlParts.pop(); siteUrl = (0, url_1.normalizeUrl)(urlParts.join('/')); (0, localstoragecache_1.setCacheItem)(key, siteUrl, rest_1.mediumLocalCache.localStorageExpiration); //keep for 15 minutes } //must end with / otherwise root sites will return "" and we will think there is no site url. return (0, url_1.makeServerRelativeUrl)((0, url_1.normalizeUrl)(siteUrl, true)); } exports.GetFileSiteUrl = GetFileSiteUrl; /** gets a site URL or null, returns the current web URL or siteUrl as relative URL - end with / * If you send a guid - it will look for a site with that ID in the current context site collection */ function GetSiteUrl(siteUrlOrId) { let siteUrl; if ((0, typecheckers_1.isNullOrUndefined)(siteUrlOrId)) { if (hasGlobalContext()) { siteUrl = _spPageContextInfo.webServerRelativeUrl; if (_spPageContextInfo.isAppWeb) //#1300 if in a classic app sub-site siteUrl = siteUrl.substring(0, siteUrl.lastIndexOf("/")); } else { siteUrl = GetFileSiteUrl(window.location.pathname); } } else if ((0, typecheckers_1.isValidGuid)(siteUrlOrId)) { //GetWebInfoSync calls GetSiteUrl recursively, but with null should not get in here let webInfo = (0, web_1.GetWebInfoSync)(null, siteUrlOrId); siteUrl = webInfo.ServerRelativeUrl; } else siteUrl = siteUrlOrId; //must end with / otherwise root sites will return "" and we will think there is no site url. return (0, url_1.makeServerRelativeUrl)((0, url_1.normalizeUrl)(siteUrl, true)); } exports.GetSiteUrl = GetSiteUrl; /** gets a site url, returns its REST _api url */ function GetRestBaseUrl(siteUrl) { siteUrl = GetSiteUrl(siteUrl); return siteUrl + '_api'; } exports.GetRestBaseUrl = GetRestBaseUrl; /** Get the field internal name as you can find it in item.FieldValuesAsText[name] (Or FieldValuesForEdit) */ function DecodeFieldValuesAsTextKey(key) { return key.replace(/_x005f_/g, "_").replace('OData__', '_'); } exports.DecodeFieldValuesAsTextKey = DecodeFieldValuesAsTextKey; /** Replaces _ with _x005f_, except OData_ at the start */ function EncodeFieldValuesAsTextKey(key) { return key.replace('OData_', '~').replace(/_/g, "_x005f_").replace('~', 'OData_'); } exports.EncodeFieldValuesAsTextKey = EncodeFieldValuesAsTextKey; /** Gets REST FieldValuesAsText or FieldValuesForEdit and fix their column names so that you can get a field value by its internal name */ function DecodeFieldValuesAsText(FieldValuesAsText) { return DecodeFieldValuesForEdit(FieldValuesAsText); } exports.DecodeFieldValuesAsText = DecodeFieldValuesAsText; /** Gets REST FieldValuesAsText or FieldValuesForEdit and fix their column names so that you can get a field value by its internal name */ function DecodeFieldValuesForEdit(FieldValuesForEdit) { let result = {}; Object.keys(FieldValuesForEdit).forEach(key => { result[DecodeFieldValuesAsTextKey(key)] = FieldValuesForEdit[key]; }); return result; } exports.DecodeFieldValuesForEdit = DecodeFieldValuesForEdit; /** Get the field internal name as you can find it in the item[name] to get raw values */ function GetFieldNameFromRawValues(field, //ISSUE: 1250 options = { excludeIdFromName: false }) { let fieldName = field.InternalName; if (options.excludeIdFromName !== true && (field.TypeAsString === "User" || field.TypeAsString === "UserMulti" || field.TypeAsString === "Lookup" || field.TypeAsString === "LookupMulti" || field.InternalName === "ContentType")) { fieldName = fieldName += "Id"; } //issue 6698 fields that are too short will encode their first letter, and will start with _. this will add OData_ as a prefix in REST //Issue 336 _EndDate > OData__EndDate if (fieldName.startsWith('_')) { fieldName = "OData_" + fieldName; } return fieldName; } exports.GetFieldNameFromRawValues = GetFieldNameFromRawValues; /** Get the field name to set on the item update REST request */ function getFieldNameForUpdate(field) { if (field.TypeAsString === "TaxonomyFieldTypeMulti") { //Updating multi taxonomy value is allowed as string to the associated hidden text field return field.HiddenMultiValueFieldName; } return GetFieldNameFromRawValues(field); } exports.getFieldNameForUpdate = getFieldNameForUpdate; function __isIRestError(e) { let x = e; return !(0, typecheckers_1.isNullOrUndefined)(x) && !(0, typecheckers_1.isNullOrUndefined)(x.xhr) && (0, typecheckers_1.isString)(x.message); } exports.__isIRestError = __isIRestError; /** extract the error message from a SharePoint REST failed request */ function __getSPRestErrorData(restError) { let code = "Unknown"; let errorMessage = "Unspecified error"; if (restError && restError.message) errorMessage = restError.message; if (restError && restError.xhr && !(0, typecheckers_1.isNullOrEmptyString)(restError.xhr.responseText)) { let errorData = (0, json_1.jsonParse)(restError.xhr.responseText); let error = errorData && errorData.error; if (!error && errorData) //in minimal rest - error is in "odata.error" error = errorData && errorData["odata.error"]; if (error) { if (error && error.message && error.message.value) errorMessage = error.message.value; if (error && error.code) code = error.code; } } logger.error(errorMessage); return { code: code, message: errorMessage }; } exports.__getSPRestErrorData = __getSPRestErrorData; //# sourceMappingURL=common.js.map