UNPKG

@codesandbox/api

Version:
98 lines 3.78 kB
import Cookies from "universal-cookie"; import { GQLApi } from "./GQLApi"; import { RESTApi } from "./RESTApi"; import { ServerApi } from "./ServerApi"; import { SessionApi } from "./SessionApi"; import { onRequest, createRESTRequester } from "./utils"; export * as rest from "./RESTTypes"; export * as gql from "./generated_gql"; let globalSession; export { ApiResponseError } from "./utils"; export class CodeSandboxApi { constructor(options) { const baseUrl = options.apiUrl; const apiVersion = options.apiVersion || "v1"; const baseRequestOptions = { clientType: options.clientType, onRequest, getBearerToken: () => this.session.bearerToken, onResponse: (response) => { const cookies = new Cookies(response.cookies); if (cookies.get("signedIn") === "false") { this.session.unauthorizedResponseReceived(); } }, }; const rootRequest = createRESTRequester(Object.assign(Object.assign({}, baseRequestOptions), { baseUrl: baseUrl })); const apiRequest = createRESTRequester(Object.assign(Object.assign({}, baseRequestOptions), { baseUrl: baseUrl + "/api/" + apiVersion })); this.rootApiRequest = createRESTRequester(Object.assign(Object.assign({}, baseRequestOptions), { baseUrl: baseUrl + "/api" })); this.server = new ServerApi({ clientType: options.clientType, }); // Allows multiple instances of CodeSandboxApi with shared // session if (globalSession) { this.session = globalSession; } else { this.session = new SessionApi({ baseUrl, apiRequest, rootApiRequest: this.rootApiRequest, rootRequest, apiVersion, user: options.user, useCliAuthentication: options.useCliAuthentication, }); } this.gql = new GQLApi({ apiUrl: options.apiUrl, request: this.rootApiRequest, session: this.session, }); this.rest = new RESTApi(apiRequest); } createQueryPath(path, query) { if (!query) { return path; } const queryString = Object.keys(query) .map((key) => `${key}=${query[key]}`) .join("&"); if (queryString) { return `${path}?${queryString}`; } return path; } /** * Do a generic GET request to the API */ get(options) { return this.rootApiRequest(Object.assign(Object.assign({ method: "GET" }, options), { path: this.createQueryPath(options.path, options.query) })); } /** * Do a generic POST request to the API */ post(options) { return this.rootApiRequest(Object.assign(Object.assign({ method: "POST" }, options), { path: this.createQueryPath(options.path, options.query) })); } /** * Do a generic PUT request to the API */ put(options) { return this.rootApiRequest(Object.assign(Object.assign({ method: "PUT" }, options), { path: this.createQueryPath(options.path, options.query) })); } /** * Do a generic PATCH request to the API */ patch(options) { return this.rootApiRequest(Object.assign(Object.assign({ method: "PATCH" }, options), { path: this.createQueryPath(options.path, options.query) })); } /** * Do a generic DELETE request to the API */ delete(options) { return this.rootApiRequest(Object.assign(Object.assign({ method: "DELETE" }, options), { path: this.createQueryPath(options.path, options.query) })); } } //# sourceMappingURL=index.js.map