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