@atlaskit/profilecard
Version:
A React component to display a card with user information.
47 lines (45 loc) • 2.2 kB
JavaScript
import { HttpError } from '../util/errors';
import { handleAGGErrors, handleDirectoryGraphQLErrors } from './errorUtils';
const buildHeaders = () => {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return headers;
};
const id = headers => headers;
/**
* @param {string} serviceUrl - GraphQL service endpoint
* @param {Query} query - GraphQL query
* @param {HeaderProcessor} processHeaders - a function to add extra headers to the request
*/
export async function directoryGraphqlQuery(serviceUrl, query, processHeaders = id) {
return graphQLQuery(serviceUrl, query, processHeaders, handleDirectoryGraphQLErrors);
}
/**
* @param {string} serviceUrl - GraphQL service endpoint
* @param {Query} query - GraphQL query
* @param {HeaderProcessor} processHeaders - a function to add extra headers to the request
*/
export async function AGGQuery(serviceUrl, query, processHeaders = id) {
return graphQLQuery(serviceUrl, query, processHeaders, handleAGGErrors);
}
async function graphQLQuery(serviceUrl, query, processHeaders = id, handleErrors) {
var _response$headers;
const headers = processHeaders(buildHeaders());
const response = await fetch(new Request(serviceUrl, {
method: 'POST',
credentials: 'include',
mode: 'cors',
headers,
body: JSON.stringify(query)
}));
const traceIdFromHeaders = response === null || response === void 0 ? void 0 : (_response$headers = response.headers) === null || _response$headers === void 0 ? void 0 : _response$headers.get('atl-traceid');
if (!response.ok) {
throw new HttpError(response.status, response.statusText, traceIdFromHeaders);
}
const json = await response.json();
if (json.errors) {
var _json$extensions$gate, _json$extensions, _json$extensions$gate2;
handleErrors(json.errors, (_json$extensions$gate = (_json$extensions = json.extensions) === null || _json$extensions === void 0 ? void 0 : (_json$extensions$gate2 = _json$extensions.gateway) === null || _json$extensions$gate2 === void 0 ? void 0 : _json$extensions$gate2.request_id) !== null && _json$extensions$gate !== void 0 ? _json$extensions$gate : traceIdFromHeaders);
}
return json.data;
}