本文整理匯總了TypeScript中hapi.ResponseToolkit類的典型用法代碼示例。如果您正苦於以下問題:TypeScript ResponseToolkit類的具體用法?TypeScript ResponseToolkit怎麽用?TypeScript ResponseToolkit使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ResponseToolkit類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: async
handler: async (request: Request, h: ResponseToolkit) => {
try {
const { method } = request;
const query =
method === 'post'
? (request.payload as Record<string, any>)
: (request.query as Record<string, any>);
const graphQLResponse = await runHttpQuery([request], {
method: method.toUpperCase(),
options: options.graphQLOptions,
query,
});
return h.response(graphQLResponse).type('application/json');
} catch (error) {
if (error.isGraphQLError === true) {
return h
.response(error.message)
.code(error.statusCode)
.type('application/json');
}
return h.response(error).type('application/json');
}
},
示例2: interceptAuth
return async function interceptAuth(
req: Request,
h: ResponseToolkit
): Promise<Lifecycle.ReturnValue> {
try {
const result = await fn(req, sessionStorage.asScoped(req), toolkit);
if (AuthResult.isValidResult(result)) {
if (result.isAuthenticated()) {
return h.authenticated({ credentials: result.payload });
}
if (result.isRedirected()) {
return h.redirect(result.payload).takeover();
}
if (result.isRejected()) {
const { error, statusCode } = result.payload;
return Boom.boomify(error, { statusCode });
}
}
throw new Error(
`Unexpected result from Authenticate. Expected AuthResult, but given: ${result}.`
);
} catch (error) {
return Boom.internal(error.message, { statusCode: 500 });
}
};
示例3: interceptRequest
return async function interceptRequest(
req: Request,
h: ResponseToolkit
): Promise<Lifecycle.ReturnValue> {
try {
const result = await fn(KibanaRequest.from(req, undefined), toolkit);
if (OnRequestResult.isValidResult(result)) {
if (result.isNext()) {
return h.continue;
}
if (result.isRedirected()) {
return h.redirect(result.payload).takeover();
}
if (result.isRejected()) {
const { error, statusCode } = result.payload;
return Boom.boomify(error, { statusCode });
}
}
throw new Error(
`Unexpected result from OnRequest. Expected OnRequestResult, but given: ${result}.`
);
} catch (error) {
return Boom.internal(error.message, { statusCode: 500 });
}
};
示例4:
const handler = (request: Request, h: ResponseToolkit) => {
const context = {
title: 'Views Example',
message: 'Hello, World',
};
return h.view('hello', context);
};
示例5: interceptRequest
return async function interceptRequest(
request: Request,
h: ResponseToolkit
): Promise<Lifecycle.ReturnValue> {
try {
const result = await fn(KibanaRequest.from(request, undefined), {
next: OnRequestResult.next,
redirected: OnRequestResult.redirected,
rejected: OnRequestResult.rejected,
setUrl: (newUrl: string | Url) => {
request.setUrl(newUrl);
// We should update raw request as well since it can be proxied to the old platform
request.raw.req.url = typeof newUrl === 'string' ? newUrl : newUrl.href;
},
});
if (OnRequestResult.isValidResult(result)) {
if (result.isNext()) {
return h.continue;
}
if (result.isRedirected()) {
return h.redirect(result.payload).takeover();
}
if (result.isRejected()) {
const { error, statusCode } = result.payload;
return Boom.boomify(error, { statusCode });
}
}
throw new Error(
`Unexpected result from OnRequest. Expected OnRequestResult, but given: ${result}.`
);
} catch (error) {
return Boom.internal(error.message, { statusCode: 500 });
}
};
示例6:
authenticate: (request: Request, h: ResponseToolkit) => {
const authorization = request.headers.authorization;
if (!authorization) {
throw Boom.unauthorized(null, 'Custom');
}
return h.authenticated({ credentials: { user: 'john' } });
}
示例7: async
handler: async (request: Request, h: ResponseToolkit) => {
try {
const query =
request.method === 'post'
? (request.payload as Record<string, any>) // eslint-disable-line @typescript-eslint/no-explicit-any
: (request.query as Record<string, any>); // eslint-disable-line @typescript-eslint/no-explicit-any
const gqlResponse = await runHttpQuery([request], {
method: request.method.toUpperCase(),
options: options.graphqlOptions,
query,
});
return h.response(gqlResponse).type('application/json');
} catch (error) {
if ('HttpQueryError' !== error.name) {
const queryError = Boom.boomify(error);
queryError.output.payload.message = error.message;
return queryError;
}
if (error.isGraphQLError === true) {
return h
.response(error.message)
.code(error.statusCode)
.type('application/json');
}
const genericError = new Boom(error.message, { statusCode: error.statusCode });
if (error.headers) {
Object.keys(error.headers).forEach(header => {
genericError.output.headers[header] = error.headers[header];
});
}
// Boom hides the error when status code is 500
genericError.output.payload.message = error.message;
throw genericError;
}
},
示例8: formatUrl
this.server.ext('onRequest', (request: Request, responseToolkit: ResponseToolkit) => {
return responseToolkit
.redirect(
formatUrl({
hostname: config.host,
pathname: request.url.pathname,
port: config.port,
protocol: 'https',
search: request.url.search,
})
)
.takeover();
});
示例9: handler
async function handler(exportTypeId: any, jobParams: any, request: Request, h: ResponseToolkit) {
// @ts-ignore
const user = request.pre.user;
const headers = request.headers;
const job = await enqueueJob(exportTypeId, jobParams, user, headers, request);
// return the queue's job information
const jobJson = job.toJSON();
return h
.response({
path: `${DOWNLOAD_BASE_URL}/${jobJson.id}`,
job: jobJson,
})
.type('application/json');
}