本文整理匯總了TypeScript中@hapi/hapi.Server.inject方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Server.inject方法的具體用法?TypeScript Server.inject怎麽用?TypeScript Server.inject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@hapi/hapi.Server
的用法示例。
在下文中一共展示了Server.inject方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: Server
const server = new Server({
port: 8000,
});
const serverRoute: ServerRoute = {
path: '/',
method: 'GET',
handler(request, h) {
return 'Success!';
}
};
server.route(serverRoute);
server.start();
server.inject('/').then(res => console.log(res.result));
declare module '@hapi/hapi' {
interface ApplicationState {
injectState?: number;
}
}
server.inject({
auth: {
strategy: 'test',
credentials: {
user: {
a: 1,
},
},
示例2: async
const sendRequest = async ({
url,
method = "GET",
}: {
url: string;
method?: string;
}): Promise<Hapi.ServerInjectResponse> => {
const server = new Hapi.Server({ debug: { request: ["*"] } });
await server.register({ plugin });
server.route({
method: "GET",
path: "/",
handler: () => [],
});
// @ts-ignore
server.route({
method: "GET",
path: "/query",
handler: () => [],
options: {
plugins: {
jsonapi: {
validate: {
query: schema,
},
},
},
},
});
// @ts-ignore
server.route({
method: "POST",
path: "/payload",
handler: () => [],
options: {
plugins: {
jsonapi: {
validate: {
payload: schema,
},
},
},
},
});
// @ts-ignore
server.route({
method: "GET",
path: "/params",
handler: () => [],
options: {
plugins: {
jsonapi: {
validate: {
params: schema,
},
},
},
},
});
// @ts-ignore
server.route({
method: "GET",
path: "/headers",
handler: () => [],
options: {
plugins: {
jsonapi: {
validate: {
headers: schema,
},
},
},
},
});
return server.inject({ method, url, payload: {}, headers: { "Content-Type": "application/vnd.api+json" } });
};