@foxpage/foxpage-node-sdk
Version:
foxpage node sdk
168 lines (167 loc) • 6.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMockConditions = exports.getPreviewVersion = exports.getPreviewLocale = exports.getPageId = exports.getTicket = exports.getFoxpagePreviewTime = exports.isProd = exports.initEnv = exports.initCanary = exports.isTest = exports.isCanary = exports.isMock = exports.isDebug = exports.isModuleView = exports.isPreview = void 0;
const PREFIX = '_foxpage_';
const PREVIEW = 'preview';
const PREVIEW_TIME = `${PREVIEW}_time`;
const PREVIEW_LOCALE = `${PREVIEW}_locale`;
const PREVIEW_VERSION = `${PREVIEW}_version`;
const DEBUG = 'debug';
const MOCK = 'mock';
const CANARY = 'canary';
const TEST = 'auto_test';
const MODULE_VIEW = 'module_view';
const TICKET = 'ticket';
const PAGE_ID = 'pageid';
const MOCK_CONDITION = 'mockCondition';
const parseParams = ({ ctxKey, queryKey }) => (ctx) => {
var _a, _b, _c;
if (ctx) {
const state = (_c = (_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.URL) === null || _a === void 0 ? void 0 : _a.searchParams) === null || _b === void 0 ? void 0 : _b.has(queryKey)) !== null && _c !== void 0 ? _c : false;
ctx[ctxKey] = state;
return !!state;
}
return false;
};
/**
* check is preview mode
*/
const isPreview = (ctx) => {
return (parseParams({ ctxKey: 'isPreviewMode', queryKey: PREVIEW })(ctx) ||
parseParams({ ctxKey: 'isPreviewMode', queryKey: `${PREFIX}${PREVIEW}` })(ctx));
};
exports.isPreview = isPreview;
/**
* check is module view
*/
const isModuleView = (ctx) => {
return (parseParams({ ctxKey: 'isModuleViewMode', queryKey: MODULE_VIEW })(ctx) ||
parseParams({ ctxKey: 'isModuleViewMode', queryKey: `${PREFIX}${MODULE_VIEW}` })(ctx));
};
exports.isModuleView = isModuleView;
/**
* check is debug mode
*/
const isDebug = (ctx) => {
return (parseParams({ ctxKey: 'isDebugMode', queryKey: DEBUG })(ctx) ||
parseParams({ ctxKey: 'isDebugMode', queryKey: `${PREFIX}${DEBUG}` })(ctx));
};
exports.isDebug = isDebug;
/**
* check is debug mode
*/
const isMock = (ctx) => {
return (parseParams({ ctxKey: 'isMock', queryKey: MOCK })(ctx) ||
parseParams({ ctxKey: 'isMock', queryKey: `${PREFIX}${MOCK}` })(ctx));
};
exports.isMock = isMock;
/**
* check is canary mode
*/
const isCanary = (ctx) => {
return (parseParams({ ctxKey: 'isCanary', queryKey: CANARY })(ctx) ||
parseParams({ ctxKey: 'isCanary', queryKey: `${PREFIX}${CANARY}` })(ctx));
};
exports.isCanary = isCanary;
const isTest = (ctx) => {
return (parseParams({ ctxKey: 'isTest', queryKey: TEST })(ctx) ||
parseParams({ ctxKey: 'isTest', queryKey: `${PREFIX}${TEST}` })(ctx));
};
exports.isTest = isTest;
/**
* init canary
* @param ctx
* @returns
*/
const initCanary = (ctx) => {
var _a, _b;
// headers
// had x-ctx-fox-canaryReq = 1;
const _isCanary = (_b = (_a = ctx.request) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b['x-ctx-fox-canaryreq'];
return (0, exports.isCanary)(ctx) || !!_isCanary;
};
exports.initCanary = initCanary;
/**
* init mode
* @param ctx context
*/
const initEnv = (ctx) => {
ctx.isPreviewMode = ctx.isPreviewMode || (0, exports.isPreview)(ctx);
ctx.isModuleViewMode = ctx.isModuleViewMode || (0, exports.isModuleView)(ctx);
ctx.isDebugMode = ctx.isDebugMode || (0, exports.isDebug)(ctx);
ctx.isMock = ctx.isMock || (0, exports.isMock)(ctx);
ctx.isCanary = ctx.isCanary || (0, exports.initCanary)(ctx);
};
exports.initEnv = initEnv;
/**
* is prod
* @param ctx
* @returns
*/
const isProd = (ctx) => {
return !(ctx.isPreviewMode || ctx.isDebugMode || ctx.isMock || ctx.isCanary || ctx.isTest);
};
exports.isProd = isProd;
/**
* get foxpage preview time
* @param ctx context
* @returns preview time
*/
function getFoxpagePreviewTime(ctx) {
var _a, _b;
return ((_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.URL) === null || _a === void 0 ? void 0 : _a.searchParams) === null || _b === void 0 ? void 0 : _b.get(`${PREFIX}${PREVIEW_TIME}`)) || undefined;
}
exports.getFoxpagePreviewTime = getFoxpagePreviewTime;
/**
* get foxpage access control ticket
* @param ctx context
* @returns ticket
*/
function getTicket(ctx) {
var _a, _b;
return ((_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.URL) === null || _a === void 0 ? void 0 : _a.searchParams) === null || _b === void 0 ? void 0 : _b.get(`${PREFIX}${TICKET}`)) || undefined;
}
exports.getTicket = getTicket;
/**
* get pageId
* @param ctx
* @returns
*/
function getPageId(ctx) {
var _a, _b;
return ((_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.URL) === null || _a === void 0 ? void 0 : _a.searchParams) === null || _b === void 0 ? void 0 : _b.get(PAGE_ID)) || undefined;
}
exports.getPageId = getPageId;
/**
* get preview locale
* @param ctx context
* @returns preview locale
*/
function getPreviewLocale(ctx) {
var _a, _b;
return ((_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.URL) === null || _a === void 0 ? void 0 : _a.searchParams) === null || _b === void 0 ? void 0 : _b.get(`${PREFIX}${PREVIEW_LOCALE}`)) || undefined;
}
exports.getPreviewLocale = getPreviewLocale;
/**
* get preview version
* @param ctx context
* @returns preview version
*/
function getPreviewVersion(ctx) {
var _a, _b;
const version = (_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.URL) === null || _a === void 0 ? void 0 : _a.searchParams) === null || _b === void 0 ? void 0 : _b.get(`${PREFIX}${PREVIEW_VERSION}`);
return version ? Number(version) || undefined : undefined;
}
exports.getPreviewVersion = getPreviewVersion;
/**
* get mock condition
* @param ctx context
* @returns mock conditions
*/
function getMockConditions(ctx) {
var _a, _b;
const idStr = (_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.URL) === null || _a === void 0 ? void 0 : _a.searchParams) === null || _b === void 0 ? void 0 : _b.get(`${PREFIX}${MOCK_CONDITION}`);
const ids = idStr === null || idStr === void 0 ? void 0 : idStr.split(',').map(item => item.trim());
return ids;
}
exports.getMockConditions = getMockConditions;