UNPKG

@codesandbox/api

Version:
41 lines 1.64 kB
import Cookies from "universal-cookie"; import { COOKIES } from "./constants"; export class ServerApi { constructor(options) { this.clientType = options.clientType; } /** * On server side we need to produce headers passed to any REST/GQL request based * on client headers and cookies. As we do not want to create a CodeSandboxApi instance * for every request, we rather allow to create these headers and pass them to each request */ createServerRequestHeaders(clientHeaders, clientCookies) { const headers = {}; if (clientCookies.devJwt) { headers.Authorization = `Bearer ${clientCookies.devJwt}`; } if (clientHeaders.cookie) { headers.Cookie = clientHeaders.cookie; } // Pass the country header in the request injected by Cloudflare to the API server // This is required by global scheduler to decide the cluster for scheduling const countryFromIp = clientHeaders["x-forwarded-cf-ipcountry"] || clientHeaders["cf-ipcountry"]; if (countryFromIp) { headers["x-forwarded-cf-ipcountry"] = countryFromIp; } headers["x-codesandbox-client"] = this.clientType; return headers; } /** * Expect to get the headers created by "createServerRequestHeaders" */ hasSignedIn(serverRequestHeaders) { if (serverRequestHeaders.Cookie) { const cookies = new Cookies(serverRequestHeaders.Cookie); return Boolean(cookies.get(COOKIES.SIGNED_IN)); } return false; } } //# sourceMappingURL=ServerApi.js.map