@lcap/asl
Version:
NetEase Application Specific Language
83 lines (74 loc) • 2.21 kB
text/typescript
export * from './string';
export * from './traverse';
export * from './logger';
import { traverse } from './traverse';
import { isBasicType } from '../';
/**
* 用 lodash 的 throttle 好像有点问题,先临时做个处理
* @param func
* @param wait
* @param options
*/
export const throttle = (func: Function, wait: number, options: any) => func;
export function clearObject(obj: any) {
Object.keys(obj).forEach((key) => {
if (obj[key] === undefined || obj[key] === null)
delete obj[key];
});
}
export function clearObjectDeep(obj: any) {
traverse((current) => {
Object.keys(current.node).forEach((key) => {
if (current.node[key] === null)
delete current.node[key];
});
}, { node: obj }, { mode: 'anyObject' });
}
export const waitPromise = (wait: number) => new Promise((res) => setTimeout(() => res(wait), wait));
export interface ParamStruts {
name: string;
schema: any;
in: string;
required: boolean;
defaultValue: any;
}
export interface resolvedParameter {
parameters: string;
requestBody: string;
}
interface paramMap <T>{
[id: string]: T;
}
export function resolveParameter(params: Array<ParamStruts>): resolvedParameter {
const parameters: paramMap<ParamStruts> = {};
const requestBody = {
content: {
'application/json': {},
},
description: { name: '' },
required: false,
};
let hasBody = false;
let hasParam = false;
params.forEach((p) => {
if (p.in === 'body') {
hasBody = true;
requestBody.description.name = p.name;
if (!isBasicType(p.schema)) {
requestBody.content['application/json'] = p;
} else {
requestBody.content['application/json'] = {
schema: p.schema,
};
}
requestBody.required = true;
} else {
hasParam = true;
parameters[p.name] = p;
}
});
return {
parameters: hasParam ? JSON.stringify(parameters) : '',
requestBody: hasBody ? JSON.stringify(requestBody) : '',
};
}