UNPKG

@officesdk/web

Version:

Web JS SDK for the Office SDK

59 lines (58 loc) 2.08 kB
import { EditorModeType, EditorStandardRole } from '../shared'; import { UrlParamKey } from '../shared'; /** * 获取支持的语言,过滤掉不支持的语言,返回支持的语言。 * @param lang * @returns */ function getLang(lang) { var supportedLangs = ['en-US', 'zh-CN']; if (!lang) { return null; } if (supportedLangs.includes(lang)) { return lang; } // TODO: warn 传入的语言不支持 return null; } /** * 生成 Office SDK 的 URL * @param options * @returns */ export function generateUrl(options) { var defaultPath = '/v1/api/file/page'; var defaultMode = EditorModeType.Standard; var defaultRole = EditorStandardRole.Viewer; var endpoint = options.endpoint, token = options.token, fileId = options.fileId, fileType = options.fileType, userQuery = options.userQuery, _a = options.path, path = _a === void 0 ? defaultPath : _a, _b = options.mode, mode = _b === void 0 ? defaultMode : _b, _c = options.role, role = _c === void 0 ? defaultRole : _c; var url; try { // Office SDK 的 URL 格式 url = new URL(endpoint); // Check if the URL is valid if (url.protocol !== 'https:' && url.protocol !== 'http:') { // TODO: 抛出自定义错误 throw new Error('Invalid URL'); } // TODO: path、params 支持配置 url.pathname = path; url.searchParams.set(UrlParamKey.FileId, fileId); url.searchParams.set(UrlParamKey.Token, token); url.searchParams.set(UrlParamKey.FileType, fileType); url.searchParams.set(UrlParamKey.ModeType, mode); url.searchParams.set(UrlParamKey.ModeRole, role); var lang = getLang(options.lang); if (lang) { url.searchParams.set(UrlParamKey.Language, lang); } if (userQuery) { url.searchParams.set(UrlParamKey.UserQuery, encodeURIComponent(JSON.stringify(userQuery))); } } catch (error) { // TODO: 抛出自定义错误 throw error; } return url; }