提交 ed67074d 编写于 作者: fxy060608's avatar fxy060608

feat(api): validate protocol

上级 24f81fd1
import {
validateProtocols,
API_TYPE_ON_PROTOCOLS,
} from '../../src/helpers/protocol'
import { Upx2pxProtocol } from '../../src/protocols/base/upx2px'
import { CreateCanvasContextProtocol } from '../../src/protocols/context/context'
describe('protocol', () => {
test('validate without protocol', () => {
expect(validateProtocols('upx2px', [])).toBeFalsy()
})
test('validate upx2px', () => {
expect(validateProtocols('upx2px', [1], Upx2pxProtocol)).toBeFalsy()
expect(validateProtocols('upx2px', [], Upx2pxProtocol)).toEqual({
errMsg: `upx2px:fail Missing required args: "upx"`,
})
expect(validateProtocols('upx2px', [true], Upx2pxProtocol)).toEqual({
errMsg: `upx2px:fail Invalid args: type check failed for args "upx". Expected Number, String, got Boolean with value true.`,
})
})
test('validate onLocationChange', () => {
expect(
validateProtocols('onLocationChange', [() => {}], API_TYPE_ON_PROTOCOLS)
).toBeFalsy()
expect(
validateProtocols('onLocationChange', [], API_TYPE_ON_PROTOCOLS)
).toEqual({
errMsg: `onLocationChange:fail Missing required args: "callback"`,
})
expect(
validateProtocols('onLocationChange', [1], API_TYPE_ON_PROTOCOLS)
).toEqual({
errMsg: `onLocationChange:fail Invalid args: type check failed for args "callback". Expected Function, got Number with value 1.`,
})
})
test('validate createCanvasContext', () => {
expect(
validateProtocols(
'createCanvasContext',
['123'],
CreateCanvasContextProtocol
)
).toBeFalsy()
expect(
validateProtocols('createCanvasContext', [], CreateCanvasContextProtocol)
).toEqual({
errMsg: `createCanvasContext:fail Missing required args: "canvasId"`,
})
expect(
validateProtocols('createCanvasContext', [1], CreateCanvasContextProtocol)
).toEqual({
errMsg: `createCanvasContext:fail Invalid args: type check failed for args "canvasId". Expected String with value "1", got Number with value 1.`,
})
})
})
import { ApiOptions, ApiProtocol, ProtocolOptions } from '../../protocols/type'
import { ApiOptions, ApiProtocols } from '../../protocols/type'
import { API_TYPE_ON_PROTOCOLS, validateProtocols } from '../protocol'
import {
invokeCallback,
createAsyncApiCallback,
createKeepAliveApiCallback,
invokeCallback,
} from './callback'
import { promisify } from './promise'
type ApiProtocols = ApiProtocol | ProtocolOptions[]
export const API_TYPE_ON = 0
export const API_TYPE_TASK = 1
export const API_TYPE_SYNC = 2
......@@ -19,11 +18,6 @@ type API_TYPES =
| typeof API_TYPE_SYNC
| typeof API_TYPE_ASYNC
function validateProtocol(name: string, args: any[], protocol: ApiProtocols) {
console.log('validateProtocol', name, args, protocol)
return true
}
function formatApiArgs(args: any[], options?: ApiOptions) {
return args
}
......@@ -59,23 +53,26 @@ function wrapperApi<T extends Function>(
options?: ApiOptions
) {
return (function (...args: any[]) {
if (!(__DEV__ && protocol && !validateProtocol(name!, args, protocol))) {
return fn.apply(null, formatApiArgs(args, options))
if (__DEV__) {
const errMsg = validateProtocols(name!, args, protocol)
if (errMsg) {
return errMsg
}
}
return fn.apply(null, formatApiArgs(args, options))
} as unknown) as T
}
export function createOnApi<T extends Function>(
name: string,
fn: T,
protocol?: ApiProtocols,
options?: ApiOptions
) {
return createApi(
API_TYPE_ON,
name,
fn,
__DEV__ ? protocol : undefined,
__DEV__ ? API_TYPE_ON_PROTOCOLS : undefined,
options
)
}
......
import { isArray } from '@vue/shared'
import {
hasOwn,
makeMap,
isArray,
isObject,
toRawType,
capitalize,
} from '@vue/shared'
import {
ApiProtocol,
ApiProtocols,
ProtocolConstructor,
ProtocolOptions,
} from '../protocols/type'
export const CHOOSE_SIZE_TYPES = ['original', 'compressed']
export const CHOOSE_SOURCE_TYPES = ['album', 'camera']
......@@ -14,6 +28,14 @@ export const HTTP_METHODS = {
CONNECT: 'CONNECT',
}
export const API_TYPE_ON_PROTOCOLS = [
{
name: 'callback',
type: Function,
required: true,
},
]
export function normalizeStrArray(strArr: string[], optionalVal: string[]) {
if (
!isArray(strArr) ||
......@@ -24,3 +46,188 @@ export function normalizeStrArray(strArr: string[], optionalVal: string[]) {
}
return strArr
}
function validateProtocolFail(name: string, msg: string) {
const errMsg = `${name}:fail ${msg}`
if (!__TEST__) {
console.error(errMsg)
}
return {
errMsg,
}
}
function validateProtocol(
name: string,
data: Record<string, any>,
protocol?: ApiProtocol
) {
for (const key in protocol) {
const errMsg = validateProp(
key,
data[key],
protocol[key],
!hasOwn(data, key)
)
if (errMsg) {
return validateProtocolFail(name, errMsg)
}
}
}
export function validateProtocols(
name: string,
args: any[],
protocol?: ApiProtocols
) {
if (!protocol) {
return
}
if (isArray(protocol)) {
const len = protocol.length
const argsLen = args.length
for (let i = 0; i < len; i++) {
const opts = protocol[i]
const data = Object.create(null)
if (argsLen > i) {
data[opts.name!] = args[i]
}
const errMsg = validateProtocol(name, data, { [opts.name!]: opts })
if (errMsg) {
return errMsg
}
}
return
}
return validateProtocol(name, args[0] || Object.create(null), protocol)
}
function validateProp(
name: string,
value: unknown,
prop: ProtocolOptions,
isAbsent: boolean
) {
const { type, required, validator } = prop
// required!
if (required && isAbsent) {
return 'Missing required args: "' + name + '"'
}
// missing but optional
if (value == null && !prop.required) {
return
}
// type check
if (type != null && type !== true) {
let isValid = false
const types = isArray(type) ? type : [type]
const expectedTypes = []
// value is valid as long as one of the specified types match
for (let i = 0; i < types.length && !isValid; i++) {
const { valid, expectedType } = assertType(value, types[i])
expectedTypes.push(expectedType || '')
isValid = valid
}
if (!isValid) {
return getInvalidTypeMessage(name, value, expectedTypes)
}
}
// custom validator
if (validator && !validator(value)) {
return (
'Invalid args: custom validator check failed for args "' + name + '".'
)
}
}
const isSimpleType = /*#__PURE__*/ makeMap(
'String,Number,Boolean,Function,Symbol'
)
type AssertionResult = {
valid: boolean
expectedType: string
}
function assertType(
value: unknown,
type: ProtocolConstructor
): AssertionResult {
let valid
const expectedType = getType(type)
if (isSimpleType(expectedType)) {
const t = typeof value
valid = t === expectedType.toLowerCase()
// for primitive wrapper objects
if (!valid && t === 'object') {
valid = value instanceof type
}
} else if (expectedType === 'Object') {
valid = isObject(value)
} else if (expectedType === 'Array') {
valid = isArray(value)
} else {
if (__PLATFORM__ === 'app-plus') {
// App平台ArrayBuffer等参数跨实例传输,无法通过 instanceof 识别
valid = value instanceof type || toRawType(value) === getType(type)
} else {
valid = value instanceof type
}
}
return {
valid,
expectedType,
}
}
function getInvalidTypeMessage(
name: string,
value: unknown,
expectedTypes: string[]
): string {
let message =
`Invalid args: type check failed for args "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(', ')}`
const expectedType = expectedTypes[0]
const receivedType = toRawType(value)
const expectedValue = styleValue(value, expectedType)
const receivedValue = styleValue(value, receivedType)
// check if we need to specify expected value
if (
expectedTypes.length === 1 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)
) {
message += ` with value ${expectedValue}`
}
message += `, got ${receivedType} `
// check if we need to specify received value
if (isExplicable(receivedType)) {
message += `with value ${receivedValue}.`
}
return message
}
function getType(ctor: ProtocolConstructor): string {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/)
return match ? match[1] : ''
}
function styleValue(value: unknown, type: string): string {
if (type === 'String') {
return `"${value}"`
} else if (type === 'Number') {
return `${Number(value)}`
} else {
return `${value}`
}
}
function isExplicable(type: string): boolean {
const explicitTypes = ['string', 'number', 'boolean']
return explicitTypes.some((elem) => type.toLowerCase() === elem)
}
function isBoolean(...args: string[]): boolean {
return args.some((elem) => elem.toLowerCase() === 'boolean')
}
export type Data = Record<string, unknown>
type DefaultFactory<T> = (protocols: Data) => T | null | undefined
type ProtocolConstructor<T = any> =
export type ProtocolConstructor<T = any> =
| { new (...args: any[]): T & object }
| { (): T }
| ProtocolMethod<T>
......@@ -15,6 +15,7 @@ export interface ApiProtocol {
[name: string]: ProtocolOptions
}
export type ApiProtocols = ApiProtocol | ProtocolOptions[]
export interface ApiOptions {
beforeAll?: (res: unknown) => void
beforeSuccess?: (res: unknown) => void
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`easycom render 1`] = `
"import { resolveComponent as _resolveComponent, createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
import _component_test from '@/components/uni-test.vue'
import _component_test1 from '@/components/test1/test1.vue'
import _component_test2 from '@/uni_modules/plugin/components/test2/test2.vue'
import { CheckboxGroup as _component_v_uni_checkbox_group } from '@dcloudio/uni-h5/dist/uni-h5.esm.js'
import _style_v_uni_checkbox_group from '@dcloudio/uni-h5/style/checkbox-group.css'
export function render(_ctx, _cache, $props, $setup, $data, $options) {
// const _component_test = _resolveComponent(\\"test\\")
// const _component_test1 = _resolveComponent(\\"test1\\")
// const _component_test2 = _resolveComponent(\\"test2\\")
const _component_some_other_comp = _resolveComponent(\\"some-other-comp\\")
// const _component_v_uni_checkbox_group = _resolveComponent(\\"v-uni-checkbox-group\\")
return (_openBlock(), _createBlock(\\"template\\", null, [
_createVNode(_component_test),
_createVNode(_component_test1),
_createVNode(_component_test1),
_createVNode(_component_test2),
_createVNode(_component_some_other_comp),
_createVNode(_component_v_uni_checkbox_group),
_createVNode(_component_v_uni_checkbox_group)
]))
}"
`;
exports[`easycom render with setup 1`] = `
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, openBlock as _openBlock, createBlock as _createBlock } from \\"vue\\"
import _component_test1 from '@/components/test1/test1.vue'
import _component_test2 from '@/uni_modules/plugin/components/test2/test2.vue'
import { CheckboxGroup as _component_v_uni_checkbox_group } from '@dcloudio/uni-h5/dist/uni-h5.esm.js'
import _style_v_uni_checkbox_group from '@dcloudio/uni-h5/style/checkbox-group.css'
export function render(_ctx, _cache, $props, $setup, $data, $options) {
// const _component_test1 = _resolveComponent(\\"test1\\")
// const _component_test2 = _resolveComponent(\\"test2\\")
// const _component_v_uni_checkbox_group = _resolveComponent(\\"v-uni-checkbox-group\\")
return (_openBlock(), _createBlock(\\"template\\", null, [
_createVNode($setup[\\"Test\\"]),
_createVNode(_component_test1),
_createVNode(_component_test1),
_createVNode(_component_test2),
_createVNode($setup[\\"SomeOtherComp\\"]),
_createVNode(_component_v_uni_checkbox_group),
_createVNode(_component_v_uni_checkbox_group)
]))
}"
`;
......@@ -239,7 +239,7 @@ var keyboard = {
},
watch: {
focus(val) {
if (val && true) {
if (val && false) {
this.showSoftKeybord();
}
}
......@@ -260,10 +260,6 @@ var keyboard = {
};
UniViewJSBridge.subscribe("hideKeyboard", this.hideKeyboardTemp);
document.addEventListener("click", iosHideKeyboard, false);
{
this.setSoftinputNavBar();
this.setSoftinputTemporary();
}
});
el.addEventListener("blur", this.onKeyboardHide.bind(this));
},
......@@ -319,9 +315,6 @@ var keyboard = {
onKeyboardHide() {
UniViewJSBridge.unsubscribe("hideKeyboard", this.hideKeyboardTemp);
document.removeEventListener("click", iosHideKeyboard, false);
{
this.resetSoftinputNavBar();
}
if (String(navigator.vendor).indexOf("Apple") === 0) {
document.documentElement.scrollTo(document.documentElement.scrollLeft, document.documentElement.scrollTop);
}
......@@ -3036,39 +3029,6 @@ STD.prototype.reconfigure = function(e2, t2, n) {
this._springY.reconfigure(e2, t2, n);
this._springScale.reconfigure(e2, t2, n);
};
let view;
let pullToRefreshStyle;
let disabled;
const lastAction = {};
function disableScrollBounce({disable}) {
function exec() {
if (!view) {
view = plus.webview.currentWebview();
}
if (!disabled) {
pullToRefreshStyle = (view.getStyle() || {}).pullToRefresh || {};
}
disabled = disable;
if (pullToRefreshStyle.support) {
view.setPullToRefresh(Object.assign({}, pullToRefreshStyle, {
support: !disable
}));
}
}
const time = Date.now();
if (disable === lastAction.disable && time - lastAction.time < 20) {
return;
}
lastAction.disable = disable;
lastAction.time = time;
plusReady(() => {
if (plus.os.name === "iOS") {
setTimeout(exec, 20);
} else {
exec();
}
});
}
var index_vue_vue_type_style_index_0_lang$7 = "\nuni-movable-view {\n display: inline-block;\n width: 10px;\n height: 10px;\n top: 0px;\n left: 0px;\n position: absolute;\n cursor: grab;\n}\nuni-movable-view[hidden] {\n display: none;\n}\n";
var requesting = false;
function _requestAnimationFrame(e2) {
......@@ -3332,9 +3292,6 @@ const _sfc_main$a = {
__handleTouchStart: function() {
if (!this._isScaling) {
if (!this.disabled) {
disableScrollBounce({
disable: true
});
if (this._FA) {
this._FA.cancel();
}
......@@ -3427,9 +3384,6 @@ const _sfc_main$a = {
__handleTouchEnd: function() {
var self = this;
if (!this._isScaling && !this.disabled && this._isTouching) {
disableScrollBounce({
disable: false
});
this.$el.style.willChange = "auto";
this._isTouching = false;
if (!this._checkCanMove && !this._revise("out-of-bounds") && this.inertia) {
......@@ -5254,9 +5208,6 @@ const _sfc_main$h = {
};
this.__handleTouchStart = function(event2) {
if (event2.touches.length === 1) {
disableScrollBounce({
disable: true
});
needStop = null;
touchStart = {
x: event2.touches[0].pageX,
......@@ -5269,9 +5220,6 @@ const _sfc_main$h = {
};
this.__handleTouchEnd = function(event2) {
touchStart = null;
disableScrollBounce({
disable: false
});
if (self.refresherHeight >= self.refresherThreshold) {
self._setRefreshState("refreshing");
} else {
......
......@@ -8,7 +8,8 @@ import { isCustomElement } from '../uni-shared'
export default defineConfig({
root: '.',
define: {
__PLATFORM__: JSON.stringify('app-plus'),
__TEST__: false,
__PLATFORM__: JSON.stringify('h5'),
},
plugins: [
vue({
......
var __assign = Object.assign;
import {isFunction, extend, isPlainObject, isPromise, isArray, hasOwn, hyphenate} from "@vue/shared";
import {isFunction, extend, isPlainObject, isArray, hasOwn, isObject, capitalize, toRawType, makeMap as makeMap$1, isPromise, hyphenate} from "@vue/shared";
import {injectHook, openBlock, createBlock, createVNode, Fragment, renderList, toDisplayString, createCommentVNode, createTextVNode, Transition, withCtx, withModifiers, withDirectives, vShow, resolveComponent, KeepAlive, resolveDynamicComponent, mergeProps, toHandlers, renderSlot, vModelDynamic, vModelText} from "vue";
import {TABBAR_HEIGHT, plusReady, debounce, NAVBAR_HEIGHT, COMPONENT_NAME_PREFIX, isCustomElement} from "@dcloudio/uni-shared";
import {createRouter, createWebHistory, createWebHashHistory} from "vue-router";
......@@ -1283,6 +1283,129 @@ function decode(base64) {
}
return arraybuffer;
}
function validateProtocolFail(name, msg) {
const errMsg = `${name}:fail ${msg}`;
{
console.error(errMsg);
}
return {
errMsg
};
}
function validateProtocol(name, data, protocol) {
for (const key in protocol) {
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
if (errMsg) {
return validateProtocolFail(name, errMsg);
}
}
}
function validateProtocols(name, args, protocol) {
if (!protocol) {
return;
}
if (isArray(protocol)) {
const len = protocol.length;
const argsLen = args.length;
for (let i = 0; i < len; i++) {
const opts = protocol[i];
const data = Object.create(null);
if (argsLen > i) {
data[opts.name] = args[i];
}
const errMsg = validateProtocol(name, data, {[opts.name]: opts});
if (errMsg) {
return errMsg;
}
}
return;
}
return validateProtocol(name, args[0] || Object.create(null), protocol);
}
function validateProp(name, value, prop, isAbsent) {
const {type, required, validator} = prop;
if (required && isAbsent) {
return 'Missing required args: "' + name + '"';
}
if (value == null && !prop.required) {
return;
}
if (type != null && type !== true) {
let isValid = false;
const types = isArray(type) ? type : [type];
const expectedTypes = [];
for (let i = 0; i < types.length && !isValid; i++) {
const {valid, expectedType} = assertType(value, types[i]);
expectedTypes.push(expectedType || "");
isValid = valid;
}
if (!isValid) {
return getInvalidTypeMessage(name, value, expectedTypes);
}
}
if (validator && !validator(value)) {
return 'Invalid args: custom validator check failed for args "' + name + '".';
}
}
const isSimpleType = /* @__PURE__ */ makeMap$1("String,Number,Boolean,Function,Symbol");
function assertType(value, type) {
let valid;
const expectedType = getType(type);
if (isSimpleType(expectedType)) {
const t2 = typeof value;
valid = t2 === expectedType.toLowerCase();
if (!valid && t2 === "object") {
valid = value instanceof type;
}
} else if (expectedType === "Object") {
valid = isObject(value);
} else if (expectedType === "Array") {
valid = isArray(value);
} else {
{
valid = value instanceof type;
}
}
return {
valid,
expectedType
};
}
function getInvalidTypeMessage(name, value, expectedTypes) {
let message = `Invalid args: type check failed for args "${name}". Expected ${expectedTypes.map(capitalize).join(", ")}`;
const expectedType = expectedTypes[0];
const receivedType = toRawType(value);
const expectedValue = styleValue(value, expectedType);
const receivedValue = styleValue(value, receivedType);
if (expectedTypes.length === 1 && isExplicable(expectedType) && !isBoolean(expectedType, receivedType)) {
message += ` with value ${expectedValue}`;
}
message += `, got ${receivedType} `;
if (isExplicable(receivedType)) {
message += `with value ${receivedValue}.`;
}
return message;
}
function getType(ctor) {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
return match ? match[1] : "";
}
function styleValue(value, type) {
if (type === "String") {
return `"${value}"`;
} else if (type === "Number") {
return `${Number(value)}`;
} else {
return `${value}`;
}
}
function isExplicable(type) {
const explicitTypes = ["string", "number", "boolean"];
return explicitTypes.some((elem) => type.toLowerCase() === elem);
}
function isBoolean(...args) {
return args.some((elem) => elem.toLowerCase() === "boolean");
}
function tryCatch(fn) {
return function() {
try {
......@@ -1407,10 +1530,6 @@ const API_TYPE_ON = 0;
const API_TYPE_TASK = 1;
const API_TYPE_SYNC = 2;
const API_TYPE_ASYNC = 3;
function validateProtocol(name, args, protocol) {
console.log("validateProtocol", name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
return args;
}
......@@ -1434,9 +1553,13 @@ function wrapperAsyncApi(name, fn, options) {
}
function wrapperApi(fn, name, protocol, options) {
return function(...args) {
if (!(process.env.NODE_ENV !== "production" && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args));
if (process.env.NODE_ENV !== "production") {
const errMsg = validateProtocols(name, args, protocol);
if (errMsg) {
return errMsg;
}
}
return fn.apply(null, formatApiArgs(args));
};
}
function createSyncApi(name, fn, protocol, options) {
......
......@@ -16,6 +16,7 @@ export default defineConfig({
define: {
global: 'window',
__DEV__: `(process.env.NODE_ENV !== 'production')`,
__TEST__: false,
__PLATFORM__: JSON.stringify('h5'),
},
resolve: {
......
import { isPlainObject, isFunction, isArray, isPromise, hasOwn, isString } from '@vue/shared';
import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared';
function validateProtocolFail(name, msg) {
const errMsg = `${name}:fail ${msg}`;
{
console.error(errMsg);
}
return {
errMsg,
};
}
function validateProtocol(name, data, protocol) {
for (const key in protocol) {
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
if (errMsg) {
return validateProtocolFail(name, errMsg);
}
}
}
function validateProtocols(name, args, protocol) {
if (!protocol) {
return;
}
if (isArray(protocol)) {
const len = protocol.length;
const argsLen = args.length;
for (let i = 0; i < len; i++) {
const opts = protocol[i];
const data = Object.create(null);
if (argsLen > i) {
data[opts.name] = args[i];
}
const errMsg = validateProtocol(name, data, { [opts.name]: opts });
if (errMsg) {
return errMsg;
}
}
return;
}
return validateProtocol(name, args[0] || Object.create(null), protocol);
}
function validateProp(name, value, prop, isAbsent) {
const { type, required, validator } = prop;
// required!
if (required && isAbsent) {
return 'Missing required args: "' + name + '"';
}
// missing but optional
if (value == null && !prop.required) {
return;
}
// type check
if (type != null && type !== true) {
let isValid = false;
const types = isArray(type) ? type : [type];
const expectedTypes = [];
// value is valid as long as one of the specified types match
for (let i = 0; i < types.length && !isValid; i++) {
const { valid, expectedType } = assertType(value, types[i]);
expectedTypes.push(expectedType || '');
isValid = valid;
}
if (!isValid) {
return getInvalidTypeMessage(name, value, expectedTypes);
}
}
// custom validator
if (validator && !validator(value)) {
return ('Invalid args: custom validator check failed for args "' + name + '".');
}
}
const isSimpleType = /*#__PURE__*/ makeMap('String,Number,Boolean,Function,Symbol');
function assertType(value, type) {
let valid;
const expectedType = getType(type);
if (isSimpleType(expectedType)) {
const t = typeof value;
valid = t === expectedType.toLowerCase();
// for primitive wrapper objects
if (!valid && t === 'object') {
valid = value instanceof type;
}
}
else if (expectedType === 'Object') {
valid = isObject(value);
}
else if (expectedType === 'Array') {
valid = isArray(value);
}
else {
{
valid = value instanceof type;
}
}
return {
valid,
expectedType,
};
}
function getInvalidTypeMessage(name, value, expectedTypes) {
let message = `Invalid args: type check failed for args "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(', ')}`;
const expectedType = expectedTypes[0];
const receivedType = toRawType(value);
const expectedValue = styleValue(value, expectedType);
const receivedValue = styleValue(value, receivedType);
// check if we need to specify expected value
if (expectedTypes.length === 1 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)) {
message += ` with value ${expectedValue}`;
}
message += `, got ${receivedType} `;
// check if we need to specify received value
if (isExplicable(receivedType)) {
message += `with value ${receivedValue}.`;
}
return message;
}
function getType(ctor) {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
return match ? match[1] : '';
}
function styleValue(value, type) {
if (type === 'String') {
return `"${value}"`;
}
else if (type === 'Number') {
return `${Number(value)}`;
}
else {
return `${value}`;
}
}
function isExplicable(type) {
const explicitTypes = ['string', 'number', 'boolean'];
return explicitTypes.some((elem) => type.toLowerCase() === elem);
}
function isBoolean(...args) {
return args.some((elem) => elem.toLowerCase() === 'boolean');
}
function tryCatch(fn) {
return function () {
......@@ -113,10 +253,6 @@ const API_TYPE_ON = 0;
const API_TYPE_TASK = 1;
const API_TYPE_SYNC = 2;
const API_TYPE_ASYNC = 3;
function validateProtocol(name, args, protocol) {
console.log('validateProtocol', name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
return args;
}
......@@ -140,9 +276,13 @@ function wrapperAsyncApi(name, fn, options) {
}
function wrapperApi(fn, name, protocol, options) {
return function (...args) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args));
if ((process.env.NODE_ENV !== 'production')) {
const errMsg = validateProtocols(name, args, protocol);
if (errMsg) {
return errMsg;
}
}
return fn.apply(null, formatApiArgs(args));
};
}
function createSyncApi(name, fn, protocol, options) {
......
import { isPlainObject, isFunction, isArray, isPromise, hasOwn, isString } from '@vue/shared';
import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared';
function validateProtocolFail(name, msg) {
const errMsg = `${name}:fail ${msg}`;
{
console.error(errMsg);
}
return {
errMsg,
};
}
function validateProtocol(name, data, protocol) {
for (const key in protocol) {
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
if (errMsg) {
return validateProtocolFail(name, errMsg);
}
}
}
function validateProtocols(name, args, protocol) {
if (!protocol) {
return;
}
if (isArray(protocol)) {
const len = protocol.length;
const argsLen = args.length;
for (let i = 0; i < len; i++) {
const opts = protocol[i];
const data = Object.create(null);
if (argsLen > i) {
data[opts.name] = args[i];
}
const errMsg = validateProtocol(name, data, { [opts.name]: opts });
if (errMsg) {
return errMsg;
}
}
return;
}
return validateProtocol(name, args[0] || Object.create(null), protocol);
}
function validateProp(name, value, prop, isAbsent) {
const { type, required, validator } = prop;
// required!
if (required && isAbsent) {
return 'Missing required args: "' + name + '"';
}
// missing but optional
if (value == null && !prop.required) {
return;
}
// type check
if (type != null && type !== true) {
let isValid = false;
const types = isArray(type) ? type : [type];
const expectedTypes = [];
// value is valid as long as one of the specified types match
for (let i = 0; i < types.length && !isValid; i++) {
const { valid, expectedType } = assertType(value, types[i]);
expectedTypes.push(expectedType || '');
isValid = valid;
}
if (!isValid) {
return getInvalidTypeMessage(name, value, expectedTypes);
}
}
// custom validator
if (validator && !validator(value)) {
return ('Invalid args: custom validator check failed for args "' + name + '".');
}
}
const isSimpleType = /*#__PURE__*/ makeMap('String,Number,Boolean,Function,Symbol');
function assertType(value, type) {
let valid;
const expectedType = getType(type);
if (isSimpleType(expectedType)) {
const t = typeof value;
valid = t === expectedType.toLowerCase();
// for primitive wrapper objects
if (!valid && t === 'object') {
valid = value instanceof type;
}
}
else if (expectedType === 'Object') {
valid = isObject(value);
}
else if (expectedType === 'Array') {
valid = isArray(value);
}
else {
{
valid = value instanceof type;
}
}
return {
valid,
expectedType,
};
}
function getInvalidTypeMessage(name, value, expectedTypes) {
let message = `Invalid args: type check failed for args "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(', ')}`;
const expectedType = expectedTypes[0];
const receivedType = toRawType(value);
const expectedValue = styleValue(value, expectedType);
const receivedValue = styleValue(value, receivedType);
// check if we need to specify expected value
if (expectedTypes.length === 1 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)) {
message += ` with value ${expectedValue}`;
}
message += `, got ${receivedType} `;
// check if we need to specify received value
if (isExplicable(receivedType)) {
message += `with value ${receivedValue}.`;
}
return message;
}
function getType(ctor) {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
return match ? match[1] : '';
}
function styleValue(value, type) {
if (type === 'String') {
return `"${value}"`;
}
else if (type === 'Number') {
return `${Number(value)}`;
}
else {
return `${value}`;
}
}
function isExplicable(type) {
const explicitTypes = ['string', 'number', 'boolean'];
return explicitTypes.some((elem) => type.toLowerCase() === elem);
}
function isBoolean(...args) {
return args.some((elem) => elem.toLowerCase() === 'boolean');
}
function tryCatch(fn) {
return function () {
......@@ -113,10 +253,6 @@ const API_TYPE_ON = 0;
const API_TYPE_TASK = 1;
const API_TYPE_SYNC = 2;
const API_TYPE_ASYNC = 3;
function validateProtocol(name, args, protocol) {
console.log('validateProtocol', name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
return args;
}
......@@ -140,9 +276,13 @@ function wrapperAsyncApi(name, fn, options) {
}
function wrapperApi(fn, name, protocol, options) {
return function (...args) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args));
if ((process.env.NODE_ENV !== 'production')) {
const errMsg = validateProtocols(name, args, protocol);
if (errMsg) {
return errMsg;
}
}
return fn.apply(null, formatApiArgs(args));
};
}
function createSyncApi(name, fn, protocol, options) {
......
import { isPlainObject, isFunction, isArray, isPromise, hasOwn, isString } from '@vue/shared';
import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared';
function validateProtocolFail(name, msg) {
const errMsg = `${name}:fail ${msg}`;
{
console.error(errMsg);
}
return {
errMsg,
};
}
function validateProtocol(name, data, protocol) {
for (const key in protocol) {
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
if (errMsg) {
return validateProtocolFail(name, errMsg);
}
}
}
function validateProtocols(name, args, protocol) {
if (!protocol) {
return;
}
if (isArray(protocol)) {
const len = protocol.length;
const argsLen = args.length;
for (let i = 0; i < len; i++) {
const opts = protocol[i];
const data = Object.create(null);
if (argsLen > i) {
data[opts.name] = args[i];
}
const errMsg = validateProtocol(name, data, { [opts.name]: opts });
if (errMsg) {
return errMsg;
}
}
return;
}
return validateProtocol(name, args[0] || Object.create(null), protocol);
}
function validateProp(name, value, prop, isAbsent) {
const { type, required, validator } = prop;
// required!
if (required && isAbsent) {
return 'Missing required args: "' + name + '"';
}
// missing but optional
if (value == null && !prop.required) {
return;
}
// type check
if (type != null && type !== true) {
let isValid = false;
const types = isArray(type) ? type : [type];
const expectedTypes = [];
// value is valid as long as one of the specified types match
for (let i = 0; i < types.length && !isValid; i++) {
const { valid, expectedType } = assertType(value, types[i]);
expectedTypes.push(expectedType || '');
isValid = valid;
}
if (!isValid) {
return getInvalidTypeMessage(name, value, expectedTypes);
}
}
// custom validator
if (validator && !validator(value)) {
return ('Invalid args: custom validator check failed for args "' + name + '".');
}
}
const isSimpleType = /*#__PURE__*/ makeMap('String,Number,Boolean,Function,Symbol');
function assertType(value, type) {
let valid;
const expectedType = getType(type);
if (isSimpleType(expectedType)) {
const t = typeof value;
valid = t === expectedType.toLowerCase();
// for primitive wrapper objects
if (!valid && t === 'object') {
valid = value instanceof type;
}
}
else if (expectedType === 'Object') {
valid = isObject(value);
}
else if (expectedType === 'Array') {
valid = isArray(value);
}
else {
{
valid = value instanceof type;
}
}
return {
valid,
expectedType,
};
}
function getInvalidTypeMessage(name, value, expectedTypes) {
let message = `Invalid args: type check failed for args "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(', ')}`;
const expectedType = expectedTypes[0];
const receivedType = toRawType(value);
const expectedValue = styleValue(value, expectedType);
const receivedValue = styleValue(value, receivedType);
// check if we need to specify expected value
if (expectedTypes.length === 1 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)) {
message += ` with value ${expectedValue}`;
}
message += `, got ${receivedType} `;
// check if we need to specify received value
if (isExplicable(receivedType)) {
message += `with value ${receivedValue}.`;
}
return message;
}
function getType(ctor) {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
return match ? match[1] : '';
}
function styleValue(value, type) {
if (type === 'String') {
return `"${value}"`;
}
else if (type === 'Number') {
return `${Number(value)}`;
}
else {
return `${value}`;
}
}
function isExplicable(type) {
const explicitTypes = ['string', 'number', 'boolean'];
return explicitTypes.some((elem) => type.toLowerCase() === elem);
}
function isBoolean(...args) {
return args.some((elem) => elem.toLowerCase() === 'boolean');
}
function tryCatch(fn) {
return function () {
......@@ -113,10 +253,6 @@ const API_TYPE_ON = 0;
const API_TYPE_TASK = 1;
const API_TYPE_SYNC = 2;
const API_TYPE_ASYNC = 3;
function validateProtocol(name, args, protocol) {
console.log('validateProtocol', name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
return args;
}
......@@ -140,9 +276,13 @@ function wrapperAsyncApi(name, fn, options) {
}
function wrapperApi(fn, name, protocol, options) {
return function (...args) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args));
if ((process.env.NODE_ENV !== 'production')) {
const errMsg = validateProtocols(name, args, protocol);
if (errMsg) {
return errMsg;
}
}
return fn.apply(null, formatApiArgs(args));
};
}
function createSyncApi(name, fn, protocol, options) {
......
import { isPlainObject, isFunction, isArray, isPromise, hasOwn, isString } from '@vue/shared';
import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared';
function validateProtocolFail(name, msg) {
const errMsg = `${name}:fail ${msg}`;
{
console.error(errMsg);
}
return {
errMsg,
};
}
function validateProtocol(name, data, protocol) {
for (const key in protocol) {
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
if (errMsg) {
return validateProtocolFail(name, errMsg);
}
}
}
function validateProtocols(name, args, protocol) {
if (!protocol) {
return;
}
if (isArray(protocol)) {
const len = protocol.length;
const argsLen = args.length;
for (let i = 0; i < len; i++) {
const opts = protocol[i];
const data = Object.create(null);
if (argsLen > i) {
data[opts.name] = args[i];
}
const errMsg = validateProtocol(name, data, { [opts.name]: opts });
if (errMsg) {
return errMsg;
}
}
return;
}
return validateProtocol(name, args[0] || Object.create(null), protocol);
}
function validateProp(name, value, prop, isAbsent) {
const { type, required, validator } = prop;
// required!
if (required && isAbsent) {
return 'Missing required args: "' + name + '"';
}
// missing but optional
if (value == null && !prop.required) {
return;
}
// type check
if (type != null && type !== true) {
let isValid = false;
const types = isArray(type) ? type : [type];
const expectedTypes = [];
// value is valid as long as one of the specified types match
for (let i = 0; i < types.length && !isValid; i++) {
const { valid, expectedType } = assertType(value, types[i]);
expectedTypes.push(expectedType || '');
isValid = valid;
}
if (!isValid) {
return getInvalidTypeMessage(name, value, expectedTypes);
}
}
// custom validator
if (validator && !validator(value)) {
return ('Invalid args: custom validator check failed for args "' + name + '".');
}
}
const isSimpleType = /*#__PURE__*/ makeMap('String,Number,Boolean,Function,Symbol');
function assertType(value, type) {
let valid;
const expectedType = getType(type);
if (isSimpleType(expectedType)) {
const t = typeof value;
valid = t === expectedType.toLowerCase();
// for primitive wrapper objects
if (!valid && t === 'object') {
valid = value instanceof type;
}
}
else if (expectedType === 'Object') {
valid = isObject(value);
}
else if (expectedType === 'Array') {
valid = isArray(value);
}
else {
{
valid = value instanceof type;
}
}
return {
valid,
expectedType,
};
}
function getInvalidTypeMessage(name, value, expectedTypes) {
let message = `Invalid args: type check failed for args "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(', ')}`;
const expectedType = expectedTypes[0];
const receivedType = toRawType(value);
const expectedValue = styleValue(value, expectedType);
const receivedValue = styleValue(value, receivedType);
// check if we need to specify expected value
if (expectedTypes.length === 1 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)) {
message += ` with value ${expectedValue}`;
}
message += `, got ${receivedType} `;
// check if we need to specify received value
if (isExplicable(receivedType)) {
message += `with value ${receivedValue}.`;
}
return message;
}
function getType(ctor) {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
return match ? match[1] : '';
}
function styleValue(value, type) {
if (type === 'String') {
return `"${value}"`;
}
else if (type === 'Number') {
return `${Number(value)}`;
}
else {
return `${value}`;
}
}
function isExplicable(type) {
const explicitTypes = ['string', 'number', 'boolean'];
return explicitTypes.some((elem) => type.toLowerCase() === elem);
}
function isBoolean(...args) {
return args.some((elem) => elem.toLowerCase() === 'boolean');
}
function tryCatch(fn) {
return function () {
......@@ -113,10 +253,6 @@ const API_TYPE_ON = 0;
const API_TYPE_TASK = 1;
const API_TYPE_SYNC = 2;
const API_TYPE_ASYNC = 3;
function validateProtocol(name, args, protocol) {
console.log('validateProtocol', name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
return args;
}
......@@ -140,9 +276,13 @@ function wrapperAsyncApi(name, fn, options) {
}
function wrapperApi(fn, name, protocol, options) {
return function (...args) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args));
if ((process.env.NODE_ENV !== 'production')) {
const errMsg = validateProtocols(name, args, protocol);
if (errMsg) {
return errMsg;
}
}
return fn.apply(null, formatApiArgs(args));
};
}
function createSyncApi(name, fn, protocol, options) {
......
import { isPlainObject, isFunction, isArray, isPromise, hasOwn, isString } from '@vue/shared';
import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared';
function validateProtocolFail(name, msg) {
const errMsg = `${name}:fail ${msg}`;
{
console.error(errMsg);
}
return {
errMsg,
};
}
function validateProtocol(name, data, protocol) {
for (const key in protocol) {
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
if (errMsg) {
return validateProtocolFail(name, errMsg);
}
}
}
function validateProtocols(name, args, protocol) {
if (!protocol) {
return;
}
if (isArray(protocol)) {
const len = protocol.length;
const argsLen = args.length;
for (let i = 0; i < len; i++) {
const opts = protocol[i];
const data = Object.create(null);
if (argsLen > i) {
data[opts.name] = args[i];
}
const errMsg = validateProtocol(name, data, { [opts.name]: opts });
if (errMsg) {
return errMsg;
}
}
return;
}
return validateProtocol(name, args[0] || Object.create(null), protocol);
}
function validateProp(name, value, prop, isAbsent) {
const { type, required, validator } = prop;
// required!
if (required && isAbsent) {
return 'Missing required args: "' + name + '"';
}
// missing but optional
if (value == null && !prop.required) {
return;
}
// type check
if (type != null && type !== true) {
let isValid = false;
const types = isArray(type) ? type : [type];
const expectedTypes = [];
// value is valid as long as one of the specified types match
for (let i = 0; i < types.length && !isValid; i++) {
const { valid, expectedType } = assertType(value, types[i]);
expectedTypes.push(expectedType || '');
isValid = valid;
}
if (!isValid) {
return getInvalidTypeMessage(name, value, expectedTypes);
}
}
// custom validator
if (validator && !validator(value)) {
return ('Invalid args: custom validator check failed for args "' + name + '".');
}
}
const isSimpleType = /*#__PURE__*/ makeMap('String,Number,Boolean,Function,Symbol');
function assertType(value, type) {
let valid;
const expectedType = getType(type);
if (isSimpleType(expectedType)) {
const t = typeof value;
valid = t === expectedType.toLowerCase();
// for primitive wrapper objects
if (!valid && t === 'object') {
valid = value instanceof type;
}
}
else if (expectedType === 'Object') {
valid = isObject(value);
}
else if (expectedType === 'Array') {
valid = isArray(value);
}
else {
{
valid = value instanceof type;
}
}
return {
valid,
expectedType,
};
}
function getInvalidTypeMessage(name, value, expectedTypes) {
let message = `Invalid args: type check failed for args "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(', ')}`;
const expectedType = expectedTypes[0];
const receivedType = toRawType(value);
const expectedValue = styleValue(value, expectedType);
const receivedValue = styleValue(value, receivedType);
// check if we need to specify expected value
if (expectedTypes.length === 1 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)) {
message += ` with value ${expectedValue}`;
}
message += `, got ${receivedType} `;
// check if we need to specify received value
if (isExplicable(receivedType)) {
message += `with value ${receivedValue}.`;
}
return message;
}
function getType(ctor) {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
return match ? match[1] : '';
}
function styleValue(value, type) {
if (type === 'String') {
return `"${value}"`;
}
else if (type === 'Number') {
return `${Number(value)}`;
}
else {
return `${value}`;
}
}
function isExplicable(type) {
const explicitTypes = ['string', 'number', 'boolean'];
return explicitTypes.some((elem) => type.toLowerCase() === elem);
}
function isBoolean(...args) {
return args.some((elem) => elem.toLowerCase() === 'boolean');
}
function tryCatch(fn) {
return function () {
......@@ -113,10 +253,6 @@ const API_TYPE_ON = 0;
const API_TYPE_TASK = 1;
const API_TYPE_SYNC = 2;
const API_TYPE_ASYNC = 3;
function validateProtocol(name, args, protocol) {
console.log('validateProtocol', name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
return args;
}
......@@ -140,9 +276,13 @@ function wrapperAsyncApi(name, fn, options) {
}
function wrapperApi(fn, name, protocol, options) {
return function (...args) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args));
if ((process.env.NODE_ENV !== 'production')) {
const errMsg = validateProtocols(name, args, protocol);
if (errMsg) {
return errMsg;
}
}
return fn.apply(null, formatApiArgs(args));
};
}
function createSyncApi(name, fn, protocol, options) {
......
import { isPlainObject, isFunction, isArray, isPromise, hasOwn, isString } from '@vue/shared';
import { isArray, hasOwn, isObject, capitalize, toRawType, makeMap, isPlainObject, isFunction, isPromise, isString } from '@vue/shared';
function validateProtocolFail(name, msg) {
const errMsg = `${name}:fail ${msg}`;
{
console.error(errMsg);
}
return {
errMsg,
};
}
function validateProtocol(name, data, protocol) {
for (const key in protocol) {
const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key));
if (errMsg) {
return validateProtocolFail(name, errMsg);
}
}
}
function validateProtocols(name, args, protocol) {
if (!protocol) {
return;
}
if (isArray(protocol)) {
const len = protocol.length;
const argsLen = args.length;
for (let i = 0; i < len; i++) {
const opts = protocol[i];
const data = Object.create(null);
if (argsLen > i) {
data[opts.name] = args[i];
}
const errMsg = validateProtocol(name, data, { [opts.name]: opts });
if (errMsg) {
return errMsg;
}
}
return;
}
return validateProtocol(name, args[0] || Object.create(null), protocol);
}
function validateProp(name, value, prop, isAbsent) {
const { type, required, validator } = prop;
// required!
if (required && isAbsent) {
return 'Missing required args: "' + name + '"';
}
// missing but optional
if (value == null && !prop.required) {
return;
}
// type check
if (type != null && type !== true) {
let isValid = false;
const types = isArray(type) ? type : [type];
const expectedTypes = [];
// value is valid as long as one of the specified types match
for (let i = 0; i < types.length && !isValid; i++) {
const { valid, expectedType } = assertType(value, types[i]);
expectedTypes.push(expectedType || '');
isValid = valid;
}
if (!isValid) {
return getInvalidTypeMessage(name, value, expectedTypes);
}
}
// custom validator
if (validator && !validator(value)) {
return ('Invalid args: custom validator check failed for args "' + name + '".');
}
}
const isSimpleType = /*#__PURE__*/ makeMap('String,Number,Boolean,Function,Symbol');
function assertType(value, type) {
let valid;
const expectedType = getType(type);
if (isSimpleType(expectedType)) {
const t = typeof value;
valid = t === expectedType.toLowerCase();
// for primitive wrapper objects
if (!valid && t === 'object') {
valid = value instanceof type;
}
}
else if (expectedType === 'Object') {
valid = isObject(value);
}
else if (expectedType === 'Array') {
valid = isArray(value);
}
else {
{
valid = value instanceof type;
}
}
return {
valid,
expectedType,
};
}
function getInvalidTypeMessage(name, value, expectedTypes) {
let message = `Invalid args: type check failed for args "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(', ')}`;
const expectedType = expectedTypes[0];
const receivedType = toRawType(value);
const expectedValue = styleValue(value, expectedType);
const receivedValue = styleValue(value, receivedType);
// check if we need to specify expected value
if (expectedTypes.length === 1 &&
isExplicable(expectedType) &&
!isBoolean(expectedType, receivedType)) {
message += ` with value ${expectedValue}`;
}
message += `, got ${receivedType} `;
// check if we need to specify received value
if (isExplicable(receivedType)) {
message += `with value ${receivedValue}.`;
}
return message;
}
function getType(ctor) {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
return match ? match[1] : '';
}
function styleValue(value, type) {
if (type === 'String') {
return `"${value}"`;
}
else if (type === 'Number') {
return `${Number(value)}`;
}
else {
return `${value}`;
}
}
function isExplicable(type) {
const explicitTypes = ['string', 'number', 'boolean'];
return explicitTypes.some((elem) => type.toLowerCase() === elem);
}
function isBoolean(...args) {
return args.some((elem) => elem.toLowerCase() === 'boolean');
}
function tryCatch(fn) {
return function () {
......@@ -113,10 +253,6 @@ const API_TYPE_ON = 0;
const API_TYPE_TASK = 1;
const API_TYPE_SYNC = 2;
const API_TYPE_ASYNC = 3;
function validateProtocol(name, args, protocol) {
console.log('validateProtocol', name, args, protocol);
return true;
}
function formatApiArgs(args, options) {
return args;
}
......@@ -140,9 +276,13 @@ function wrapperAsyncApi(name, fn, options) {
}
function wrapperApi(fn, name, protocol, options) {
return function (...args) {
if (!((process.env.NODE_ENV !== 'production') && protocol && !validateProtocol(name, args, protocol))) {
return fn.apply(null, formatApiArgs(args));
if ((process.env.NODE_ENV !== 'production')) {
const errMsg = validateProtocols(name, args, protocol);
if (errMsg) {
return errMsg;
}
}
return fn.apply(null, formatApiArgs(args));
};
}
function createSyncApi(name, fn, protocol, options) {
......
import path from 'path'
import { parse, compileTemplate, compileScript } from '@vue/compiler-sfc'
import { initEasycom, matchEasycom, vueCompilerOptions } from '../src'
import { initEasycom, matchEasycom } from '../src/easycom'
const rootDir = path.resolve(__dirname, 'example')
const dirs = [
......@@ -10,22 +8,6 @@ const dirs = [
path.resolve(__dirname, 'example/uni_modules/plugin/components'),
]
const template = `<template><test/><test1/><test1/><test2/><some-other-comp/><checkbox-group/><checkbox-group/></template>`
const sfcParseRes = parse(`${template}<script>export default {}</script>`)
const sfcParseResWithSetup = parse(
`${template}<script setup>import SomeOtherComp from './some-other-comp';import Test from './test';</script>`
)
const id = 'test'
const sfcScriptCompileOptions = { id }
const sfcScriptBlock = compileScript(
sfcParseRes.descriptor,
sfcScriptCompileOptions
)
const sfcScriptBlockWithSetup = compileScript(
sfcParseResWithSetup.descriptor,
sfcScriptCompileOptions
)
describe('easycom', () => {
test('initEasycom with dirs', () => {
expect(initEasycom({ dirs, rootDir })).toEqual([
......@@ -83,55 +65,4 @@ describe('easycom', () => {
])
expect(matchEasycom('test')).toBe('@/components/uni-test.vue')
})
test('render', () => {
initEasycom({
dirs,
rootDir,
custom: { '^test$': '@/components/uni-test.vue' },
})
const { code } = compileTemplate(
Object.assign(sfcParseRes.descriptor, {
id,
compilerOptions: Object.assign(
{
bindingMetadata: sfcScriptBlock.bindings,
},
vueCompilerOptions
),
})
)
expect(code).toMatch(` // const _component_test = _resolveComponent("test")
// const _component_test1 = _resolveComponent("test1")
// const _component_test2 = _resolveComponent("test2")
const _component_some_other_comp = _resolveComponent("some-other-comp")
// const _component_v_uni_checkbox_group = _resolveComponent("v-uni-checkbox-group")`)
expect(code)
.toMatch(`import _component_test from '@/components/uni-test.vue'
import _component_test1 from '@/components/test1/test1.vue'
import _component_test2 from '@/uni_modules/plugin/components/test2/test2.vue'
import { CheckboxGroup as _component_v_uni_checkbox_group } from '@dcloudio/uni-h5/dist/uni-h5.esm.js'
import _style_v_uni_checkbox_group from '@dcloudio/uni-h5/style/checkbox-group.css'`)
expect(code).toMatchSnapshot()
})
test('render with setup', () => {
initEasycom({
dirs,
rootDir,
custom: { '^test$': '@/components/uni-test.vue' },
})
const { code } = compileTemplate(
Object.assign(sfcParseResWithSetup.descriptor, {
id,
compilerOptions: Object.assign(
{ bindingMetadata: sfcScriptBlockWithSetup.bindings },
vueCompilerOptions
),
})
)
expect(code)
.toMatch(` // const _component_test1 = _resolveComponent("test1")
// const _component_test2 = _resolveComponent("test2")
// const _component_v_uni_checkbox_group = _resolveComponent("v-uni-checkbox-group")`)
expect(code).toMatchSnapshot()
})
})
......@@ -12,7 +12,7 @@ if (!process.env.TARGET) {
const packagesDir = path.resolve(__dirname, 'packages')
const packageDir = path.resolve(packagesDir, process.env.TARGET)
const resolve = p => path.resolve(packageDir, p)
const resolve = (p) => path.resolve(packageDir, p)
const pkg = require(resolve(`package.json`))
// ensure TS checks only once for each build
......@@ -20,14 +20,14 @@ let hasTSChecked = false
const configs = []
const buildOptions = require(resolve(`build.json`))
Object.keys(buildOptions.input).forEach(name => {
Object.keys(buildOptions.input).forEach((name) => {
const files = buildOptions.input[name]
if (Array.isArray(files)) {
files.forEach(file => {
files.forEach((file) => {
configs.push(
createConfig(name, {
file: resolve(file),
format: file.includes('.cjs.') ? 'cjs' : 'es'
format: file.includes('.cjs.') ? 'cjs' : 'es',
})
)
})
......@@ -35,7 +35,7 @@ Object.keys(buildOptions.input).forEach(name => {
configs.push(
createConfig(name, {
file: resolve(buildOptions.input[name]),
format: (buildOptions.output && buildOptions.output.format) || `es`
format: (buildOptions.output && buildOptions.output.format) || `es`,
})
)
}
......@@ -53,10 +53,10 @@ function createConfig(entryFile, output, plugins = []) {
compilerOptions: {
sourceMap: output.sourcemap,
declaration: shouldEmitDeclarations,
declarationMap: shouldEmitDeclarations
declarationMap: shouldEmitDeclarations,
},
exclude: ['**/__tests__', 'test-dts']
}
exclude: ['**/__tests__', 'test-dts'],
},
})
// we only need to check TS and generate declarations once for each build.
......@@ -68,7 +68,7 @@ function createConfig(entryFile, output, plugins = []) {
'@vue/shared',
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
...(buildOptions.external || [])
...(buildOptions.external || []),
]
return {
......@@ -79,11 +79,11 @@ function createConfig(entryFile, output, plugins = []) {
nodeResolve(),
commonjs(),
json({
namedExports: false
namedExports: false,
}),
tsPlugin,
createReplacePlugin(buildOptions),
...plugins
...plugins,
],
output,
onwarn: (msg, warn) => {
......@@ -101,8 +101,8 @@ function createConfig(entryFile, output, plugins = []) {
return true
}
return false
}
}
},
},
}
}
......@@ -112,13 +112,14 @@ function createAliasPlugin(buildOptions) {
function createReplacePlugin(buildOptions) {
const replacements = {
__DEV__: `(process.env.NODE_ENV !== 'production')`
__DEV__: `(process.env.NODE_ENV !== 'production')`,
__TEST__: false,
}
if (buildOptions.replacements) {
Object.assign(replacements, buildOptions.replacements)
}
Object.keys(replacements).forEach(key => {
Object.keys(replacements).forEach((key) => {
if (key in process.env) {
replacements[key] = process.env[key]
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册