本文整理匯總了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,
},
});
}