Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
operation: string,
variables?: { [name: string]: string },
additionalHeaders?: object,
) {
const body = {
query: operation,
variables,
};
const headers = new Headers({ 'Content-Type': 'application/json' });
if (additionalHeaders) {
for (const [name, value] of new Headers(additionalHeaders)) {
headers.append(name, value);
}
}
const request = new Request('http://localhost', {
method: 'POST',
headers,
body: JSON.stringify(body),
});
// create the options and run the operation
const options = this.createGraphQLServerOptions.bind(this);
const response = await graphqlTesting(options)(request);
// parse response and return
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.startsWith('application/json')) {
return response.json();
} else {
return response.text();
}
export function convertNodeHttpToRequest(req: IncomingMessage): Request {
const headers = new Headers();
Object.keys(req.headers).forEach(key => {
const values = req.headers[key]!;
if (Array.isArray(values)) {
values.forEach(value => headers.append(key, value));
} else {
headers.append(key, values);
}
});
return new Request(req.url!, {
headers,
method: req.method,
});
}
if (
(ttlOverride && policy.age() < ttlOverride) ||
(!ttlOverride &&
policy.satisfiesWithoutRevalidation(policyRequestFrom(request)))
) {
const headers = policy.responseHeaders();
return new Response(body, {
url: policy._url,
status: policy._status,
headers,
});
} else {
const revalidationHeaders = policy.revalidationHeaders(
policyRequestFrom(request),
);
const revalidationRequest = new Request(request, {
headers: revalidationHeaders,
});
const revalidationResponse = await this.httpFetch(revalidationRequest);
const { policy: revalidatedPolicy, modified } = policy.revalidatedPolicy(
policyRequestFrom(revalidationRequest),
policyResponseFrom(revalidationResponse),
);
return this.storeResponseAndReturnClone(
new Response(modified ? await revalidationResponse.text() : body, {
url: revalidatedPolicy._url,
status: revalidatedPolicy._status,
headers: revalidatedPolicy.responseHeaders(),
}),
request,