本文整理汇总了TypeScript中microrouter.post函数的典型用法代码示例。如果您正苦于以下问题:TypeScript post函数的具体用法?TypeScript post怎么用?TypeScript post使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了post函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createApp
function createApp(options: CreateAppOptions) {
const graphqlOptions = (options && options.graphqlOptions) || { schema };
const graphiqlOptions = (options && options.graphiqlOptions) || {
endpointURL: '/graphql',
};
const graphqlHandler = microGraphql(graphqlOptions);
const graphiqlHandler = microGraphiql(graphiqlOptions);
return micro(
router(
get('/graphql', graphqlHandler),
post('/graphql', graphqlHandler),
put('/graphql', graphqlHandler),
patch('/graphql', graphqlHandler),
del('/graphql', graphqlHandler),
head('/graphql', graphqlHandler),
opts('/graphql', graphqlHandler),
get('/graphiql', graphiqlHandler),
post('/graphiql', graphiqlHandler),
put('/graphiql', graphiqlHandler),
patch('/graphiql', graphiqlHandler),
del('/graphiql', graphiqlHandler),
head('/graphiql', graphiqlHandler),
opts('/graphiql', graphiqlHandler),
(req, res) => send(res, 404, 'not found'),
),
);
}
示例2: catch
});
} catch (e) {
console.log(e);
send(res, e.httpStatus || 500, e.message || null);
}
}
),
post(
//@ts-ignore UrlPattern is allowed as a parameter to micro-router methods
new UrlPattern("/api/user/:username/delete", usernameRegex),
async (req: ServerRequest, res: ServerResponse) => {
try {
const body = (await json(req)) as { password: string };
await deleteProfile(
decodeURIComponent(req.params.username),
body.password,
await getToken(req)
);
send(res, 200);
} catch (e) {
console.log(e);
send(res, e.httpStatus || 500, e.message || null);
}
}
),
post(
//@ts-ignore UrlPattern is allowed as a parameter to micro-router methods
new UrlPattern("/api/user/:username/changepass", usernameRegex),
async (req: ServerRequest, res: ServerResponse) => {
try {
const body = (await json(req)) as {
password: string;
示例3: post
import { post, ServerRequest, ServerResponse } from "microrouter";
import { send, json } from "micro";
import { uploadProfile } from "../database";
import { getToken } from "../utils";
export const routes = [
post("/loadorder", async (req: ServerRequest, res: ServerResponse) => {
try {
const body = (await json(req)) as Modwatch.Profile;
const profile = {
...body,
timestamp: new Date()
};
send(res, 201, await uploadProfile(profile, getToken(req)));
} catch (e) {
console.log(e);
send(res, e.httpStatus || 500, e.message || null);
}
})
];
示例4: post
import { post, ServerRequest, ServerResponse } from "microrouter";
import { send, json } from "micro";
import { getProfile } from "../database";
import { verifyToken, validPassword, generateToken } from "../utils";
export const routes = [
post("/auth/checkToken", async (req: ServerRequest, res: ServerResponse) => {
try {
const body = (await json(req)) as { token: string; username: string };
await verifyToken(body.token);
send(res, 200, { token: body.token });
} catch (e) {
console.log(e);
send(res, e.httpStatus || 500, e.message || null);
}
}),
post("/auth/signin", async (req: ServerRequest, res: ServerResponse) => {
try {
const body = (await json(req)) as { username: string; password: string };
const profile = await getProfile({ username: body.username });
await validPassword(body.password, profile.password);
const token = await generateToken(body.username, body.password);
send(res, 200, { token });
} catch (e) {
console.log(e);
send(res, e.httpStatus || 500, e.message || null);
}
})
];
示例5: post
post("/oauth/authorize", async (req: ServerRequest, res: ServerResponse) => {
const query = serialize(req.query);
try {
const body = (await parse(req)) as Modwatch.Profile;
const scopes = body.scopes || [];
if (clients.every(c => req.query.redirect_uri.indexOf(c) !== 0)) {
res.statusCode = 301;
res.setHeader(
"Location",
`/oauth/authorize?${query}&failed=${encodeURIComponent(
"Login must be initiated from a valid client"
)}`
);
res.end();
return;
}
if (["username", "password"].filter(q => !!body[q]).length !== 2) {
res.statusCode = 301;
res.setHeader(
"Location",
`/oauth/authorize?${query}&failed=${encodeURIComponent(
"Username/Password/Scope Required"
)}`
);
res.end();
return;
}
const profile = await getProfile({ username: body.username });
if (!profile) {
throw {
httpStatus: 404,
message: "Profile Not Found"
};
}
const roles = profile.roles ? [].concat(profile.roles).sort() : [];
if (
!(await validPassword(body.password, profile.password)) ||
![]
.concat(scopes)
.sort()
.every((scope, index) => scope === roles[index])
) {
res.statusCode = 301;
res.setHeader(
"Location",
`/oauth/authorize?${query}&failed=${encodeURIComponent(
"Invalid Credentials"
)}`
);
res.end();
return;
}
const iat = new Date().getTime();
const token = {
iss: req.query.redirect_uri,
aud: "https://api.modwat.ch/",
sub: profile.username,
iat,
exp: iat + 3600 * 1000,
scopes: [...new Set(["user"].concat(scopes).concat(roles))]
};
res.statusCode = 301;
res.setHeader(
"Location",
`${decodeURIComponent(
req.query.redirect_uri
)}oauth/access_token/${encodeURIComponent(
encode(token, process.env.JWTSECRET)
)}/token_type/Bearer/expires_in/3600`
);
res.end();
} catch (e) {
console.log(e);
res.statusCode = 301;
res.setHeader(
"Location",
`/oauth/authorize?${query}&failed=${encodeURIComponent(
"Invalid Credentials"
)}`
);
res.end();
}
}),