本文整理汇总了TypeScript中koa.Context.get方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Context.get方法的具体用法?TypeScript Context.get怎么用?TypeScript Context.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类koa.Context
的用法示例。
在下文中一共展示了Context.get方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: async
export default async (ctx: Context, next: Function) => {
const apiKey = ctx.get('X-API-Key');
if (apiKey === config.appApiKey) {
await next();
} else {
ctx.body = "Access denied. Invalid 'X-API-Key' header was supplied";
ctx.status = 403;
}
};
示例2: async
return async (ctx: Context, next) => {
const startTime = new Date().getTime();
const date = new Date().toISOString();
await next();
const responseTime = new Date().getTime() - startTime;
logger.info({
date,
status: ctx.status,
remoteAddress: ctx.ip,
method: ctx.method,
url: ctx.url,
referrer: ctx.get('Referrer'),
userAgent: ctx.get('user-agent'),
responseTime,
});
};
示例3: async
export default async (ctx: Context, next: Function) => {
const header = 'X-Experience-API-Version';
if (ctx.get(header) && ctx.get(header).substring(0, 3) === config.xApiVersion.substring(0, 3)) {
await next();
} else {
ctx.body = "Invalid 'X-Experience-API-Version' header was supplied";
ctx.status = 400;
}
ctx.set(header, config.xApiVersion);
};
示例4: async
export default async (ctx: Context, next: Function) => {
const idToken = ctx.get('X-Id-Token');
const entityId = ctx.get('X-Entity-Id');
const entityType = ctx.get('X-Entity-Type');
if (!idToken || !entityId || !entityType) {
return reject(ctx);
}
try {
const token: any = decodeJWT(idToken);
const issuer = token.iss;
const identityId = token.unique_name;
if (!isKnownIssuer(issuer)) {
return reject(ctx);
}
let path = config.permissionsEndpoint.coursePath;
let data: any = { courseId: entityId };
if (entityType === constants.entityTypes.learningPath) {
path = config.permissionsEndpoint.learningPathPath;
data = { learningpathId: entityId };
}
let response;
if (issuer === 'localhost') {
response = { body: { data: constants.accessTypes.academy } };
} else {
response = await httpRequestSender.post(`https://${issuer}${path}`, data, {
Authorization: `Bearer ${idToken}`
});
if (!response || !response.body || response.statusCode !== 200) {
return reject(ctx);
}
}
ctx.entityId = entityId;
ctx.entityType = entityType;
ctx.identityId = identityId;
ctx.identityAccessLevel = response.body.data;
} catch (e) {
return reject(ctx);
}
await next();
};
示例5: async
export default async (ctx: Context, next: Function) => {
const accessToken = ctx.get('X-Access-Token');
if (accessToken) {
const info = await httpRequestSender.get(`https://${config.tokensUri}/${accessToken}`, {
'X-Api-Key': config.tokensApiKey
});
if (!info || info.revoked || !info.entityId || !info.entityType || info.scopes.indexOf('read') === -1 || !info.createdBy) {
return reject(ctx);
}
ctx.entityId = info.entityId;
ctx.entityType = info.entityType;
ctx.identityId = info.createdBy;
await next();
return;
}
await idTokenAuth(ctx, next);
};