UNPKG

@girin/framework

Version:

Core modules for Girin: GraphQL server framework

99 lines 4.26 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const http = __importStar(require("http")); const path_1 = require("path"); const os_1 = require("os"); const crypto_1 = require("crypto"); const form_data_1 = __importDefault(require("form-data")); const HttpServer_1 = require("../core/HttpServer"); class TestHttpServer extends HttpServer_1.HttpServer { constructor(configs) { const randomStr = crypto_1.randomBytes(12).toString('hex'); const socketPath = path_1.join(os_1.tmpdir(), randomStr); const port = configs && configs.port; const host = configs && configs.host; const listen = (host && port) ? { host, port } : { path: socketPath }; super({ listen }); this.requestOptions = (host && port) ? { host, port } : { socketPath }; } getClient() { return new TestClient(this.requestOptions); } } exports.TestHttpServer = TestHttpServer; class TestClient { constructor(requestOptions) { this.requestOptions = requestOptions; } request(method, path, body, headers = {}) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const req = http.request(Object.assign({}, this.requestOptions, { method, headers, path })); req.once('error', reject); req.once('response', (res) => __awaiter(this, void 0, void 0, function* () { const bufs = []; res.on('data', buf => { bufs.push(buf); }); res.on('end', () => resolve({ res, body: Buffer.concat(bufs) })); })); if (!body) { req.end(); } else if (typeof body.pipe === 'function') { body.pipe(req); body.once('finish', () => req.end()); } else { req.write(body); req.end(); } }); }); } sendQuery(data, headers = {}) { return __awaiter(this, void 0, void 0, function* () { const { body } = yield this.request('POST', '/graphql', JSON.stringify(data), Object.assign({ 'Content-Type': 'application/json' }, headers)); return JSON.parse(body.toString()); }); } sendQueryWithFiles(data, headers = {}) { return __awaiter(this, void 0, void 0, function* () { const form = new form_data_1.default(); const { query, map, variables, files } = data; form.append('operations', JSON.stringify({ query, variables })); form.append('map', JSON.stringify(map)); if (files) { files.forEach((file, idx) => { form.append(String(idx), file); }); } const { body } = yield this.request('POST', '/graphql', form, Object.assign({}, form.getHeaders(), headers)); return JSON.parse(body.toString()); }); } } exports.TestClient = TestClient; //# sourceMappingURL=TestHttpServer.js.map