@ardatan/graphql-helix
Advanced tools
Comparing version 0.0.3 to 0.0.4
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getErrorResponse = exports.getPushResponse = exports.getMultipartResponse = exports.getRegularResponse = exports.DEFAULT_TRANSFORM_RESULT_FN = void 0; | ||
const w3_mocks_1 = require("./w3-mocks"); | ||
const calculate_byte_length_1 = require("./calculate-byte-length"); | ||
const ReadableStream_1 = require("./w3-ponyfills/ReadableStream"); | ||
const Response_1 = require("./w3-ponyfills/Response"); | ||
const DEFAULT_TRANSFORM_RESULT_FN = (result) => result; | ||
exports.DEFAULT_TRANSFORM_RESULT_FN = DEFAULT_TRANSFORM_RESULT_FN; | ||
function getRegularResponse(executionResult, transformResult = exports.DEFAULT_TRANSFORM_RESULT_FN) { | ||
const headersInit = []; | ||
const transformedResult = transformResult(executionResult); | ||
const responseBody = JSON.stringify(transformedResult); | ||
const contentLength = (0, calculate_byte_length_1.calculateByteLength)(responseBody); | ||
const headersInit = { | ||
"Content-Type": 'application/json', | ||
"Content-Length": contentLength.toString() | ||
}; | ||
const responseInit = { | ||
@@ -13,5 +21,3 @@ headers: headersInit, | ||
}; | ||
const transformedResult = transformResult(executionResult); | ||
const responseBody = JSON.stringify(transformedResult); | ||
return new w3_mocks_1.Response(responseBody, responseInit); | ||
return new Response_1.Response(responseBody, responseInit); | ||
} | ||
@@ -29,3 +35,3 @@ exports.getRegularResponse = getRegularResponse; | ||
}; | ||
const readableStream = new w3_mocks_1.ReadableStream({ | ||
const readableStream = new ReadableStream_1.ReadableStream({ | ||
async start(controller) { | ||
@@ -44,6 +50,7 @@ try { | ||
const chunk = JSON.stringify(transformedResult); | ||
const contentLength = (0, calculate_byte_length_1.calculateByteLength)(chunk); | ||
const data = [ | ||
"", | ||
"Content-Type: application/json; charset=utf-8", | ||
"Content-Length: " + String(chunk.length), | ||
"Content-Length: " + contentLength.toString(), | ||
"", | ||
@@ -63,3 +70,3 @@ chunk, | ||
}); | ||
return new w3_mocks_1.Response(readableStream, responseInit); | ||
return new Response_1.Response(readableStream, responseInit); | ||
} | ||
@@ -77,3 +84,3 @@ exports.getMultipartResponse = getMultipartResponse; | ||
}; | ||
const readableStream = new w3_mocks_1.ReadableStream({ | ||
const readableStream = new ReadableStream_1.ReadableStream({ | ||
async start(controller) { | ||
@@ -99,3 +106,3 @@ try { | ||
}); | ||
return new w3_mocks_1.Response(readableStream, responseInit); | ||
return new Response_1.Response(readableStream, responseInit); | ||
} | ||
@@ -113,3 +120,3 @@ exports.getPushResponse = getPushResponse; | ||
} | ||
return new w3_mocks_1.Response(JSON.stringify(transformResult(payload)), { | ||
return new Response_1.Response(JSON.stringify(transformResult(payload)), { | ||
status, | ||
@@ -116,0 +123,0 @@ headers, |
@@ -5,7 +5,9 @@ "use strict"; | ||
const is_async_iterable_1 = require("./is-async-iterable"); | ||
const w3_mocks_1 = require("./w3-mocks"); | ||
const Request_1 = require("./w3-ponyfills/Request"); | ||
const ReadableStream_1 = require("./w3-ponyfills/ReadableStream"); | ||
const Body_1 = require("./w3-ponyfills/Body"); | ||
async function getNodeRequest(nodeRequest) { | ||
const fullUrl = `${nodeRequest.protocol || "http"}://${nodeRequest.hostname || nodeRequest.headers.host || "localhost"}${nodeRequest.url || '/graphql'}`; | ||
if (nodeRequest.method !== "POST") { | ||
return new w3_mocks_1.Request(fullUrl, { | ||
return new Request_1.Request(fullUrl, { | ||
headers: nodeRequest.headers, | ||
@@ -16,10 +18,21 @@ method: nodeRequest.method, | ||
else if (nodeRequest.body) { | ||
return new w3_mocks_1.Request(fullUrl, { | ||
const request = new Request_1.Request(fullUrl, { | ||
headers: nodeRequest.headers, | ||
method: nodeRequest.method, | ||
body: JSON.stringify(nodeRequest.body), | ||
}); | ||
Object.defineProperties(request, { | ||
json: { | ||
value: async () => nodeRequest.body, | ||
}, | ||
text: { | ||
value: async () => JSON.stringify(nodeRequest.body), | ||
}, | ||
body: { | ||
get: () => new Body_1.Body(JSON.stringify(nodeRequest.body)), | ||
} | ||
}); | ||
return request; | ||
} | ||
else if ((0, is_async_iterable_1.isAsyncIterable)(nodeRequest)) { | ||
const body = new w3_mocks_1.ReadableStream({ | ||
const body = new ReadableStream_1.ReadableStream({ | ||
async start(controller) { | ||
@@ -32,3 +45,3 @@ for await (const chunk of nodeRequest) { | ||
}); | ||
return new w3_mocks_1.Request(fullUrl, { | ||
return new Request_1.Request(fullUrl, { | ||
headers: nodeRequest.headers, | ||
@@ -35,0 +48,0 @@ method: nodeRequest.method, |
{ | ||
"name": "@ardatan/graphql-helix", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A highly evolved GraphQL HTTP Server 🧬", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2333787
74
10627