Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

http-api package

Andras Bacsai5cfd2d65553f
diff --git a/packages/http-api/LICENSE b/packages/http-api/LICENSE
new file mode 100644
index 0000000000..1f6edad9a7
--- /dev/null
+++ b/packages/http-api/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 Webstudio
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/packages/http-api/package.json b/packages/http-api/package.json
new file mode 100644
index 0000000000..2e2fa66088
--- /dev/null
+++ b/packages/http-api/package.json
@@ -0,0 +1,32 @@
+{
+ "name": "@webstudio-is/http-api",
+ "version": "0.0.1",
+ "description": "Webstudio HTTP API",
+ "author": "Webstudio <[email protected]>",
+ "homepage": "https://webstudio.is",
+ "scripts": {
+ "dev": "tsup src/index.ts --format esm,cjs --watch --out-dir=lib",
+ "build": "rm -fr lib && tsc",
+ "typecheck": "tsc --noEmit",
+ "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0",
+ "checks": "yarn typecheck && yarn lint"
+ },
+ "devDependencies": {
+ "@types/isomorphic-fetch": "^0.0.36",
+ "tsup": "^6.1.3",
+ "typescript": "^4.6.2"
+ },
+ "peerDependencies": {},
+ "dependencies": {
+ "isomorphic-fetch": "^3.0.0"
+ },
+ "main": "lib/index.js",
+ "types": "lib/index.d.ts",
+ "files": [
+ "lib/*",
+ "README.md",
+ "!*.test.*"
+ ],
+ "license": "MIT",
+ "private": false
+}
diff --git a/packages/http-api/src/index.d.ts b/packages/http-api/src/index.d.ts
new file mode 100644
index 0000000000..3185eb209b
--- /dev/null
+++ b/packages/http-api/src/index.d.ts
@@ -0,0 +1,7 @@
+export type IncludeTypes = "tree" | "props" | "breakpoints";
+export type Includes<T> = { [include in IncludeTypes]: T };
+export type Project = {
+ tree?: string;
+ props?: string;
+ breakpoints?: string;
+};
diff --git a/packages/http-api/src/index.ts b/packages/http-api/src/index.ts
new file mode 100644
index 0000000000..3093ca45ed
--- /dev/null
+++ b/packages/http-api/src/index.ts
@@ -0,0 +1,34 @@
+import isoMorphicFetch from "isomorphic-fetch";
+import type { Includes, Project } from "./index.d";
+
+const protocol = process.env.NODE_ENV === "production" ? "https" : "http";
+
+const fetch = async (url: string, options?: any) => {
+ const response = await isoMorphicFetch(url, options);
+ const contentType = response.headers.get("content-type");
+ if (contentType && contentType.indexOf("application/json") !== -1) {
+ return response.json();
+ } else {
+ return response.text();
+ }
+};
+
+const loadProjectFromRest = async (
+ host: string,
+ projectId: string,
+ include: Includes<boolean> = { tree: true, props: true, breakpoints: true }
+): Promise<Project> => {
+ const domain = `${protocol}://${host}`;
+ const [tree, props, breakpoints] = await Promise.all([
+ include.tree && fetch(`${domain}/rest/tree/${projectId}`),
+ include.props && fetch(`${domain}/rest/props/${projectId}`),
+ include.breakpoints && fetch(`${domain}/rest/breakpoints/${projectId}`),
+ ]);
+ return {
+ tree,
+ props,
+ breakpoints,
+ };
+};
+
+export { loadProjectFromRest };
diff --git a/packages/http-api/tsconfig.json b/packages/http-api/tsconfig.json
new file mode 100644
index 0000000000..eb77362863
--- /dev/null
+++ b/packages/http-api/tsconfig.json
@@ -0,0 +1,17 @@
+// Extends can't use node's resolution rn https://github.com/dividab/tsconfig-paths/issues/209#issuecomment-1177261091
+{
+ "extends": "../../node_modules/@webstudio-is/tsconfig/base.json",
+ "include": ["./src/**/*.ts", "./src/**/*.tsx"],
+ "compilerOptions": {
+ "lib": ["DOM", "DOM.Iterable", "ES2020"],
+ "module": "commonjs",
+ "jsx": "react-jsx",
+ "target": "ES2020",
+ "strict": true,
+ "baseUrl": ".",
+ "paths": {
+ "~/*": ["./src/*"]
+ },
+ "outDir": "./lib"
+ }
+}
diff --git a/yarn.lock b/yarn.lock
index 22cf874352..c9c32a8cf1 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5030,6 +5030,11 @@
resolved "https://registry.yarnpkg.com/@types/is-function/-/is-function-1.0.1.tgz#2d024eace950c836d9e3335a66b97960ae41d022"
integrity sha512-A79HEEiwXTFtfY+Bcbo58M2GRYzCr9itHWzbzHVFNEYCcoU/MMGwYYf721gBrnhpj1s6RGVVha/IgNFnR0Iw/Q==
+"@types/isomorphic-fetch@^0.0.36":
+ version "0.0.36"
+ resolved "https://registry.yarnpkg.com/@types/isomorphic-fetch/-/isomorphic-fetch-0.0.36.tgz#3a6d8f63974baf26b10fd1314db910633108a769"
+ integrity sha512-ulw4d+vW1HKn4oErSmNN2HYEcHGq0N1C5exlrMM0CRqX1UUpFhGb5lwiom5j9KN3LBJJDLRmYIZz1ghm7FIzZw==
+
"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz#8467d4b3c087805d63580480890791277ce35c44"
@@ -11275,6 +11280,14 @@ isobject@^4.0.0:
resolved "https://registry.yarnpkg.com/isobject/-/isobject-4.0.0.tgz#3f1c9155e73b192022a80819bacd0343711697b0"
integrity sha512-S/2fF5wH8SJA/kmwr6HYhK/RI/OkhD84k8ntalo0iJjZikgq1XFvR5M8NPT1x5F7fBwCG3qHfnzeP/Vh/ZxCUA==
+isomorphic-fetch@^3.0.0:
+ version "3.0.0"
+ resolved "https://registry.yarnpkg.com/isomorphic-fetch/-/isomorphic-fetch-3.0.0.tgz#0267b005049046d2421207215d45d6a262b8b8b4"
+ integrity sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==
+ dependencies:
+ node-fetch "^2.6.1"
+ whatwg-fetch "^3.4.1"
+
isomorphic-unfetch@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/isomorphic-unfetch/-/isomorphic-unfetch-3.1.0.tgz#87341d5f4f7b63843d468438128cb087b7c3e98f"
@@ -17733,6 +17746,11 @@ whatwg-encoding@^1.0.5:
dependencies:
iconv-lite "0.4.24"
+whatwg-fetch@^3.4.1:
+ version "3.6.2"
+ resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.6.2.tgz#dced24f37f2624ed0281725d51d0e2e3fe677f8c"
+ integrity sha512-bJlen0FcuU/0EMLrdbJ7zOnW6ITZLrZMIarMUVmdKtsGvZna8vxKYaexICWPfZ8qwf9fzNq+UEIZrnSaApt6RA==
+
whatwg-mimetype@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"