-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (69 loc) · 2.15 KB
/
index.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { version } from "../../../package.json";
import renderVoyagerPage from "./voyager";
import accepts from "accepts";
/**
* Define helper: determine if GraphiQL can be displayed.
*/
const canDisplayVoyager = request => {
// If `raw` exists, GraphiQL mode is not enabled.
const raw = request.query.raw !== undefined;
// Allowed to show GraphiQL if not requested as raw and this request
// prefers HTML over JSON.
const accept = accepts(request.raw);
return !raw && accept.type(["json", "html"]) === "html";
};
/**
* Define handler
*/
const handler = (options = {}, logUtil) => async (request, reply) => {
try {
// Can we show graphiQL?
const canShowVoyager = canDisplayVoyager(request);
if (options.endpointUrl === undefined) {
logUtil.error("No Endpoint provided");
throw new Error("No Endpoint provided");
}
// If allowed to show GraphiQL, present it instead of JSON.
if (canShowVoyager) {
reply
.code(200)
.type("text/html")
.send(renderVoyagerPage(options));
} else {
// Otherwise, present JSON directly.
reply.code(403).send({ message: "Environment Error: Can't display" });
}
} catch (error) {
// Return error, picking up Boom overrides
const { statusCode = 500 } = error.output;
const errors = error.data || [error];
reply.code(statusCode).send({ errors: "Voyager Error" });
}
};
/**
* Define plugin
*/
function register(fastify, options = {}, next) {
const { route, voyagerOptions } = options;
const logUtil = fastify.log;
if (!route || !voyagerOptions) {
logUtil.error("Invalid Configuration");
throw new Error("Route or Voyager Options were not provided");
}
fastify.route({
method: ["GET", "POST"],
url: route.path,
config: route.config,
handler: handler(voyagerOptions, logUtil),
});
// Done
next();
}
/**
* Define plugin attributes
*/
register.attributes = { name: "voyager-fastify", version };
/**
* Export plugin
*/
export default register;