UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

138 lines 6.87 kB
import { jsonParse } from "../../helpers/json"; import { isNullOrEmptyString, isNullOrUndefined, isString, isValidGuid } from "../../helpers/typecheckers"; import { makeServerRelativeUrl, normalizeUrl } from "../../helpers/url"; import { ConsoleLogger } from "../consolelogger"; import { getCacheItem, setCacheItem } from "../localstoragecache"; import { mediumLocalCache } from "../rest"; import { GetWebIdSync, GetWebInfoSync } from "./web"; const logger = ConsoleLogger.get("sharepoint.rest/common"); export const LIST_SELECT = `ListExperienceOptions,EffectiveBasePermissions,Description,Title,EnableAttachments,EnableModeration,BaseTemplate,BaseType,Id,Hidden,IsApplicationList,IsPrivate,IsCatalog,ImageUrl,ItemCount,ParentWebUrl,EntityTypeName,DefaultViewUrl,ParentWeb/Id,ParentWeb/Title`; export const LIST_EXPAND = `ParentWeb/Id,ParentWeb/Title`; export const WEB_SELECT = "Title,ServerRelativeUrl,Id,WebTemplate,Description,SiteLogoUrl"; export const CONTENT_TYPES_SELECT = "Name,Description,StringId,Group,Hidden,ReadOnly,NewFormUrl,DisplayFormUrl,EditFormUrl,Sealed,MobileDisplayFormUrl,MobileNewFormUrl,MobileEditFormUrl,NewFormTemplateName,DisplayFormTemplateName,EditFormTemplateName"; export const CONTENT_TYPES_SELECT_WITH_FIELDS = `${CONTENT_TYPES_SELECT},Fields`; export function hasGlobalContext() { //_spPageContextInfo.webServerRelativeUrl can be empty string return typeof (_spPageContextInfo) !== "undefined" && isString(_spPageContextInfo.webServerRelativeUrl); } export 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 = getCacheItem(key); if (isNullOrUndefined(siteUrl)) { while (!isValidGuid(GetWebIdSync(urlParts.join('/')))) urlParts.pop(); siteUrl = normalizeUrl(urlParts.join('/')); setCacheItem(key, siteUrl, mediumLocalCache.localStorageExpiration); //keep for 15 minutes } //must end with / otherwise root sites will return "" and we will think there is no site url. return makeServerRelativeUrl(normalizeUrl(siteUrl, true)); } /** 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 */ export function GetSiteUrl(siteUrlOrId) { let siteUrl; if (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 (isValidGuid(siteUrlOrId)) { //GetWebInfoSync calls GetSiteUrl recursively, but with null should not get in here let webInfo = 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 makeServerRelativeUrl(normalizeUrl(siteUrl, true)); } /** gets a site url, returns its REST _api url */ export function GetRestBaseUrl(siteUrl) { siteUrl = GetSiteUrl(siteUrl); return siteUrl + '_api'; } /** Get the field internal name as you can find it in item.FieldValuesAsText[name] (Or FieldValuesForEdit) */ export function DecodeFieldValuesAsTextKey(key) { return key.replace(/_x005f_/g, "_").replace('OData__', '_'); } /** Replaces _ with _x005f_, except OData_ at the start */ export function EncodeFieldValuesAsTextKey(key) { return key.replace('OData_', '~').replace(/_/g, "_x005f_").replace('~', 'OData_'); } /** Gets REST FieldValuesAsText or FieldValuesForEdit and fix their column names so that you can get a field value by its internal name */ export function DecodeFieldValuesAsText(FieldValuesAsText) { return DecodeFieldValuesForEdit(FieldValuesAsText); } /** Gets REST FieldValuesAsText or FieldValuesForEdit and fix their column names so that you can get a field value by its internal name */ export function DecodeFieldValuesForEdit(FieldValuesForEdit) { let result = {}; Object.keys(FieldValuesForEdit).forEach(key => { result[DecodeFieldValuesAsTextKey(key)] = FieldValuesForEdit[key]; }); return result; } /** Get the field internal name as you can find it in the item[name] to get raw values */ export 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; } /** Get the field name to set on the item update REST request */ export 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); } export function __isIRestError(e) { let x = e; return !isNullOrUndefined(x) && !isNullOrUndefined(x.xhr) && isString(x.message); } /** extract the error message from a SharePoint REST failed request */ export function __getSPRestErrorData(restError) { let code = "Unknown"; let errorMessage = "Unspecified error"; if (restError && restError.message) errorMessage = restError.message; if (restError && restError.xhr && !isNullOrEmptyString(restError.xhr.responseText)) { let errorData = 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 }; } //# sourceMappingURL=common.js.map