本文整理汇总了TypeScript中util.isObject函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isObject函数的具体用法?TypeScript isObject怎么用?TypeScript isObject使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isObject函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it("can overwrite config settings with default settings", function() {
interface ITestSettings {
obj: {
obj: {
str: string;
};
nr: number;
};
bool: boolean;
}
const defaults: DefaultSettings<ITestSettings> = {
obj: ensure.object({
obj: ensure.object({
str: "Hallo world!"
}),
nr: 1
}),
bool: true
};
const settings = mergeDefaults({ obj: "Hallo universe!" }, defaults);
expect(isObject(settings.obj)).to.be.true;
expect(isObject(settings.obj.obj)).to.be.true;
expect(settings.obj.obj.str).to.equal("Hallo world!");
expect(settings.obj.nr).to.equal(1);
expect(settings.bool).to.be.true;
});
示例2: computeData
function computeData(data) {
if (data && util.isObject(data)) {
if (data instanceof Error) {
data = { message: data.message, error: data.stack };
}
if (typeof(data.toLogObj) === 'function') {
return CircularJSON.stringify(snipsecret(data.toLogObj()));
} else {
return CircularJSON.stringify(snipsecret(data));
}
} else {
return data;
}
}
示例3: reportRollbar
/* istanbul ignore next: no crash reporters when testing */
function reportRollbar(ctx: Context, err: Error) {
const request: any = {
headers: ctx.request.headers,
protocol: ctx.request.protocol,
url: ctx.request.url,
method: ctx.request.method,
body: isObject(ctx.request.body) ? JSON.stringify(ctx.request.body) : ctx.request.body,
user_ip: ctx.request.get('x-forwarded-for') || ctx.ip || '0.0.0.0',
};
if (ctx.state.user) {
request.user = {
id: ctx.state.user.id,
email: ctx.state.user.email,
username: ctx.state.user.name || ctx.state.user.username,
};
}
rollbar.error(err, request, ctx.state.request);
}
示例4: log
function log(ctx: Context, start: number, len: number, err: any = null, event: string = null) {
const statusCode = err
? (err.statusCode || err.status || 500)
: (ctx.status || 404);
let length: string;
if ([204, 205, 304].includes(statusCode)) {
length = '';
} else if (len == null) {
length = '-';
} else {
length = bytes(len).toLowerCase();
}
let cache: string = '';
if (ctx.response.headers['x-cache-api']) {
cache = ' [' + ctx.response.headers['x-cache-api'].toLowerCase() + ']';
}
const ip = ctx.request.get('x-forwarded-for') || ctx.ip || '0.0.0.0';
const user = ctx.state.user ? ctx.state.user.name || ctx.state.user.username : '';
let logUserIp: string;
if (user) {
logUserIp = chalk.cyan(user) + ':' + chalk.blueBright(ip);
} else {
logUserIp = chalk.blueBright(ip);
}
const upstream = err ? chalk.redBright('*ERR* ') : event === 'close' ? chalk.yellowBright('*CLOSED* ') : '';
const logStatus = statusStyle[Math.floor(statusCode / 100) * 100] || statusStyle[100];
const logMethod = methodStyle[ctx.method] || methodStyle.GET;
const duration = Date.now() - start;
ctx.state.request.duration = duration;
ctx.state.request.size = len;
// log this
const message = `[${logUserIp}] ${upstream}${logStatus(' ' + statusCode + ' ')} ${logMethod(ctx.method + ' ' + ctx.originalUrl)} ${duration}ms - ${length}${cache}`;
const level = statusCode >= 500 ? 'error' : 'info';
const fullLog = statusCode >= 400 && statusCode !== 404;
const requestHeaders = stripAuthHeaders(ctx.request.headers);
logger.text(ctx.state, level, message);
logger.json(ctx.state, level, {
type: 'access',
message: `${ctx.method} ${ctx.originalUrl} [${ctx.response.status}]`,
level,
request: {
id: ctx.state.request.id,
ip: ctx.state.request.ip,
method: ctx.request.method,
path: ctx.request.url,
headers: fullLog ? Object.keys(requestHeaders)
.filter(header => !['accept', 'connection', 'pragma', 'cache-control', 'host', 'origin'].includes(header))
.reduce((obj: { [key: string]: string }, key: string) => { obj[key] = requestHeaders[key]; return obj; }, {}) : undefined,
body: ctx.request.get('content-type').startsWith('application/json') ? ctx.request.rawBody : undefined,
},
response: {
status: ctx.response.status,
body: fullLog && ctx.response.get('content-type').startsWith('application/json') ? (isObject(ctx.response.body) ? JSON.stringify(ctx.response.body) : ctx.response.body) : undefined,
headers: fullLog ? Object.keys(ctx.response.headers)
.filter(header => !['x-request-id', 'x-user-id', 'x-user-dirty', 'x-cache-api', 'x-response-time', 'x-token-refresh', 'vary', 'access-control-allow-origin', 'access-control-allow-credentials', 'access-control-expose-headers'].includes(header))
.reduce((obj: { [key: string]: string }, key: string) => { obj[key] = ctx.response.headers[key]; return obj; }, {}) : undefined,
duration: ctx.state.request.duration,
size: ctx.state.request.size,
cached: ctx.response.headers['x-cache-api'] ? ctx.response.headers['x-cache-api'] === 'HIT' : undefined,
},
});
}