-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathfastify.ts
45 lines (40 loc) · 1.5 KB
/
fastify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { FastifyReply, FastifyRequest } from "fastify";
import { OAuthException } from "../exceptions/oauth.exception.js";
import { OAuthRequest } from "../requests/request.js";
import { OAuthResponse } from "../responses/response.js";
import { isOAuthError } from "../utils/errors.js";
export function responseFromFastify(res: FastifyReply): OAuthResponse {
return new OAuthResponse({
headers: <Record<string, unknown>>(<unknown>res.headers) ?? {},
});
}
export function requestFromFastify(req: FastifyRequest): OAuthRequest {
return new OAuthRequest({
query: <Record<string, unknown>>req.query ?? {},
body: <Record<string, unknown>>req.body ?? {},
headers: <Record<string, unknown>>req.headers ?? {},
});
}
// @todo v4.0 flip these to always be Fastify as first arg, OAuth as second. Then update Docs
export function handleFastifyError(e: unknown | OAuthException, res: FastifyReply): void {
if (isOAuthError(e)) {
res.status(e.status).send({
status: e.status,
message: e.message,
error: e.errorType,
error_description: e.errorDescription ?? e.error,
});
return;
}
throw e;
}
export function handleFastifyReply(res: FastifyReply, response: OAuthResponse): void {
if (response.status === 302) {
if (!response.headers.location) throw new Error("missing redirect location");
res.headers(response.headers);
res.redirect(response.headers.location);
} else {
res.headers(response.headers);
res.status(response.status).send(response.body);
}
}