UNPKG

11 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14 if (k2 === undefined) k2 = k;
15 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
16}) : (function(o, m, k, k2) {
17 if (k2 === undefined) k2 = k;
18 o[k2] = m[k];
19}));
20var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21 Object.defineProperty(o, "default", { enumerable: true, value: v });
22}) : function(o, v) {
23 o["default"] = v;
24});
25var __importStar = (this && this.__importStar) || function (mod) {
26 if (mod && mod.__esModule) return mod;
27 var result = {};
28 if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29 __setModuleDefault(result, mod);
30 return result;
31};
32var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
33 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
34 return new (P || (P = Promise))(function (resolve, reject) {
35 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
36 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
37 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
38 step((generator = generator.apply(thisArg, _arguments || [])).next());
39 });
40};
41var __generator = (this && this.__generator) || function (thisArg, body) {
42 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
43 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
44 function verb(n) { return function (v) { return step([n, v]); }; }
45 function step(op) {
46 if (f) throw new TypeError("Generator is already executing.");
47 while (_) try {
48 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
49 if (y = 0, t) op = [op[0] & 2, t.value];
50 switch (op[0]) {
51 case 0: case 1: t = op; break;
52 case 4: _.label++; return { value: op[1], done: false };
53 case 5: _.label++; y = op[1]; op = [0]; continue;
54 case 7: op = _.ops.pop(); _.trys.pop(); continue;
55 default:
56 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
57 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
58 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
59 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
60 if (t[2]) _.ops.pop();
61 _.trys.pop(); continue;
62 }
63 op = body.call(thisArg, _);
64 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
65 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
66 }
67};
68var __read = (this && this.__read) || function (o, n) {
69 var m = typeof Symbol === "function" && o[Symbol.iterator];
70 if (!m) return o;
71 var i = m.call(o), r, ar = [], e;
72 try {
73 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
74 }
75 catch (error) { e = { error: error }; }
76 finally {
77 try {
78 if (r && !r.done && (m = i["return"])) m.call(i);
79 }
80 finally { if (e) throw e.error; }
81 }
82 return ar;
83};
84var __importDefault = (this && this.__importDefault) || function (mod) {
85 return (mod && mod.__esModule) ? mod : { "default": mod };
86};
87Object.defineProperty(exports, "__esModule", { value: true });
88exports.fetchSchemas = exports.downloadSchema = void 0;
89var chalk_1 = __importDefault(require("chalk"));
90var node_fetch_1 = __importDefault(require("node-fetch"));
91var ora_1 = __importDefault(require("ora"));
92var query = __importStar(require("querystringify"));
93var graphql_1 = require("graphql");
94var buildClientSchema_1 = require("graphql/utilities/buildClientSchema");
95var path_1 = __importDefault(require("path"));
96var fs_1 = __importDefault(require("fs"));
97exports.downloadSchema = function (endpoint, header) { return __awaiter(void 0, void 0, void 0, function () {
98 var download, formatedHeaders, schema, e_1;
99 return __generator(this, function (_a) {
100 switch (_a.label) {
101 case 0:
102 download = ora_1.default("\u2B07\uFE0F Downloading schemas from " + chalk_1.default.blue(endpoint)).start();
103 _a.label = 1;
104 case 1:
105 _a.trys.push([1, 3, , 4]);
106 formatedHeaders = getHeadersFromInput(header);
107 formatedHeaders = __assign(__assign({}, formatedHeaders), { 'Content-Type': 'application/json' });
108 return [4, getRemoteSchema(endpoint, {
109 method: 'POST',
110 json: true,
111 headers: formatedHeaders,
112 })];
113 case 2:
114 schema = _a.sent();
115 if (schema.status === 'err') {
116 download.fail();
117 return [2, Promise.reject(schema.message)];
118 }
119 else {
120 download.succeed("\uD83D\uDCE5 Schemas successfully downloaded from " + chalk_1.default.blue(endpoint));
121 return [2, schema.schema];
122 }
123 return [3, 4];
124 case 3:
125 e_1 = _a.sent();
126 download.fail();
127 return [2, Promise.reject("\u2757\uFE0F Unable to request from the GraphQL Api, please verify that the server is online \n " + chalk_1.default.yellow("Maybe the endpoint is not a GraphQL Api, try to add " + chalk_1.default.green(possibleGraphQLSuffix.join(', ')) + " at the end of your url") + "\n ")];
128 case 4: return [2];
129 }
130 });
131}); };
132function getHeadersFromInput(header) {
133 var _a;
134 switch (typeof header) {
135 case 'string': {
136 var keys = query.parse(header);
137 var key = Object.keys(keys)[0];
138 return _a = {}, _a[key] = keys[key], _a;
139 }
140 case 'object': {
141 return header.map(function (header) {
142 var _a;
143 var keys = query.parse(header);
144 var key = Object.keys(keys)[0];
145 return _a = {}, _a[key] = keys[key], _a;
146 });
147 }
148 default: {
149 return {};
150 }
151 }
152}
153function getRemoteSchema(endpoint, options) {
154 return __awaiter(this, void 0, void 0, function () {
155 var _a, data, errors, schema, err_1;
156 return __generator(this, function (_b) {
157 switch (_b.label) {
158 case 0:
159 _b.trys.push([0, 2, , 3]);
160 return [4, node_fetch_1.default(endpoint, {
161 method: options.method,
162 headers: options.headers,
163 body: JSON.stringify({ query: graphql_1.getIntrospectionQuery() }),
164 }).then(function (res) { return res.json(); })];
165 case 1:
166 _a = _b.sent(), data = _a.data, errors = _a.errors;
167 if (errors) {
168 return [2, { status: 'err', message: JSON.stringify(errors, null, 2) }];
169 }
170 if (options.json) {
171 return [2, {
172 status: 'ok',
173 schema: JSON.stringify(data, null, 2),
174 }];
175 }
176 else {
177 schema = buildClientSchema_1.buildClientSchema(data);
178 return [2, {
179 status: 'ok',
180 schema: graphql_1.printSchema(schema),
181 }];
182 }
183 return [3, 3];
184 case 2:
185 err_1 = _b.sent();
186 return [2, Promise.reject(err_1)];
187 case 3: return [2];
188 }
189 });
190 });
191}
192var possibleGraphQLSuffix = ['/graphql', '/api', '/__graphql', '/__api'];
193function fetchSchemas(_a) {
194 var endpoint = _a.endpoint, header = _a.header, json = _a.json, download = _a.download;
195 return __awaiter(this, void 0, void 0, function () {
196 var graphqlRegxp, _b, result, JSONschema, outputfile, e_2, e_3;
197 return __generator(this, function (_c) {
198 switch (_c.label) {
199 case 0:
200 _c.trys.push([0, 8, , 9]);
201 if (!endpoint) return [3, 6];
202 graphqlRegxp = /[^/]+(?=\/$|$)/;
203 _b = __read(graphqlRegxp.exec(endpoint), 1), result = _b[0];
204 return [4, exports.downloadSchema(endpoint, header)];
205 case 1:
206 JSONschema = _c.sent();
207 if (!download) return [3, 5];
208 outputfile = path_1.default.resolve(process.cwd(), download);
209 _c.label = 2;
210 case 2:
211 _c.trys.push([2, 4, , 5]);
212 return [4, fs_1.default.writeFileSync(outputfile, JSONschema)];
213 case 3:
214 _c.sent();
215 console.log(chalk_1.default.green("Graphql intropesction schema saved at " + download));
216 return [3, 5];
217 case 4:
218 e_2 = _c.sent();
219 console.error(e_2);
220 return [3, 5];
221 case 5: return [2, JSON.parse(JSONschema)];
222 case 6:
223 if (json) {
224 return [2, require(path_1.default.resolve(process.cwd(), json))];
225 }
226 else {
227 return [2, null];
228 }
229 _c.label = 7;
230 case 7: return [3, 9];
231 case 8:
232 e_3 = _c.sent();
233 return [2, Promise.reject(e_3)];
234 case 9: return [2];
235 }
236 });
237 });
238}
239exports.fetchSchemas = fetchSchemas;