From 08ae3e7ddcb794f8b72702ba60914a556460d096 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 15 Sep 2022 15:17:19 +0100 Subject: [PATCH] Improve SDK generation and wrap, promisify and simplify SDK client configuration --- packages/sdk/.gitignore | 2 +- packages/sdk/README.md | 29 +++++++++++++++- packages/sdk/package.json | 5 ++- packages/sdk/rollup.config.js | 10 +----- packages/sdk/scripts/generate-sdk.sh | 11 +++--- packages/sdk/src/index.js | 51 ++++++++++++++++++++++++++++ packages/sdk/yarn.lock | 9 ++++- 7 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 packages/sdk/src/index.js diff --git a/packages/sdk/.gitignore b/packages/sdk/.gitignore index eb7de7eef4..43e879ac90 100644 --- a/packages/sdk/.gitignore +++ b/packages/sdk/.gitignore @@ -1,4 +1,4 @@ -src +sdk docs node_modules dist \ No newline at end of file diff --git a/packages/sdk/README.md b/packages/sdk/README.md index 6b0fac862c..64b4c0538d 100644 --- a/packages/sdk/README.md +++ b/packages/sdk/README.md @@ -3,4 +3,31 @@ JS SDK for the Budibase Public API. This SDK is generated by [swagger-codegen](https://github.com/swagger-api/swagger-codegen). -Docker is used to run the generator, so Java is not required. Docker is the only requirement to generate the SDK. \ No newline at end of file +Docker is used to run the generator, so Java is not required. Docker is the only requirement to generate the SDK. + +The generated code will only run in a browser. It is not currently useable in a NodeJS environment. + +## Example usage +```js +import { configure, ApplicationsApi } from "@budibase/sdk" + +// Configure the API client +configure({ + apiKey: "my-api-key", + host: "https://my.budibase.app" +}) + +// Search for an app. +// We can use the promisified version... +const res = await ApplicationsApi.applicationsSearchPost({ name: "foo" }) +console.log("Applications:", res.data) + +// ...or the callback version +ApplicationsApi.applicationsSearchPost({ name: "foo" }, ((error, data) => { + if (error) { + console.error("Failed to search:", error) + } else { + console.log("Applications:", data.data) + } +})) +``` \ No newline at end of file diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 01695f7cbf..65b57dacf3 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -4,13 +4,12 @@ "description": "Budibase Public API SDK", "author": "Budibase", "license": "MPL-2.0", - "main": "dist/sdk.cjs", "module": "dist/sdk.mjs", "type": "module", "scripts": { "generate": "cd scripts && bash generate-sdk.sh", - "build": "rollup -c", - "dev:server": "rollup -c" + "build": "yarn run generate && rollup -c", + "dev:builder": "yarn run build" }, "dependencies": { "superagent": "^5.3.0" diff --git a/packages/sdk/rollup.config.js b/packages/sdk/rollup.config.js index 948b10f9f4..b9c1543c42 100644 --- a/packages/sdk/rollup.config.js +++ b/packages/sdk/rollup.config.js @@ -1,22 +1,14 @@ import commonjs from "@rollup/plugin-commonjs" import resolve from "@rollup/plugin-node-resolve" -import { terser } from "rollup-plugin-terser" import nodePolyfills from "rollup-plugin-polyfill-node" -const production = !process.env.ROLLUP_WATCH - export default { input: "src/index.js", output: [ { sourcemap: false, format: "esm", - file: `./dist/sdk.mjs`, - }, - { - sourcemap: false, - format: "cjs", - file: `./dist/sdk.cjs`, + file: "./dist/sdk.mjs", }, ], plugins: [ diff --git a/packages/sdk/scripts/generate-sdk.sh b/packages/sdk/scripts/generate-sdk.sh index 98dbb01b01..88fbde906a 100755 --- a/packages/sdk/scripts/generate-sdk.sh +++ b/packages/sdk/scripts/generate-sdk.sh @@ -7,11 +7,8 @@ fi if [[ -d "generated" ]]; then rm -r generated fi -if [[ -d "../docs" ]]; then - rm -r ../docs -fi -if [[ -d "../src" ]]; then - rm -r ../src +if [[ -d "../sdk" ]]; then + rm -r ../sdk fi # Generate new SDK @@ -25,10 +22,10 @@ docker run --rm \ -i /openapi.yml \ -l javascript \ -o /generated + --additional-properties useES6=false # Use a subset of the generated files -mv generated/docs ../ -mv generated/src ../ +mv generated/src ../sdk # Cleanup if [[ -f "openapi.yaml" ]]; then diff --git a/packages/sdk/src/index.js b/packages/sdk/src/index.js new file mode 100644 index 0000000000..c8948484a9 --- /dev/null +++ b/packages/sdk/src/index.js @@ -0,0 +1,51 @@ +import * as BudibaseApi from "../sdk" + +let ApiClient = new BudibaseApi.ApiClient() + +// Default to current host +ApiClient.basePath = "/api/public/v1" + +/** + * Configures the Budibase Public API SDK + * @param apiKey the user's API key + * @param host the Budibase server host + */ +export const configure = ({ apiKey, host }) => { + ApiClient.authentications["ApiKeyAuth"].apiKey = apiKey + ApiClient.basePath = `${host || ""}/api/public/v1` +} + +/** + * Promisifies a generated API SDK and turns it into a more traditional async + * function. + * @param apiName the name of the generated API SDK to promisify + */ +const promisify = apiName => { + // Construct an instance of the generated API + let api = new BudibaseApi[apiName](ApiClient) + + // Patch each API endpoint and promisify it + let fns = Object.getOwnPropertyNames(Object.getPrototypeOf(api)) + fns = fns.filter(x => x !== "constructor") + fns.forEach(fn => { + const generated = api[fn].bind(api) + api[fn] = async (...params) => { + return new Promise((resolve, reject) => { + generated(...params, (error, data) => { + if (error) { + reject(error) + } else { + resolve(data) + } + }) + }) + } + }) + return api +} + +export const ApplicationsApi = promisify("ApplicationsApi") +export const QueriesApi = new BudibaseApi.QueriesApi(ApiClient) +export const RowsApi = promisify("RowsApi") +export const TablesApi = promisify("TablesApi") +export const UsersApi = promisify("UsersApi") diff --git a/packages/sdk/yarn.lock b/packages/sdk/yarn.lock index 4e8d6d718f..757430d6b7 100644 --- a/packages/sdk/yarn.lock +++ b/packages/sdk/yarn.lock @@ -85,6 +85,13 @@ estree-walker "^2.0.1" magic-string "^0.25.7" +"@rollup/plugin-json@^4.1.0": + version "4.1.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-4.1.0.tgz#54e09867ae6963c593844d8bd7a9c718294496f3" + integrity sha512-yfLbTdNS6amI/2OpmbiBoW12vngr5NW2jCJVZSBEz+H5KfUJZ2M7sDjk0U6GOOdCWFVScShte29o9NezJ53TPw== + dependencies: + "@rollup/pluginutils" "^3.0.8" + "@rollup/plugin-node-resolve@^11.2.1": version "11.2.1" resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-11.2.1.tgz#82aa59397a29cd4e13248b106e6a4a1880362a60" @@ -97,7 +104,7 @@ is-module "^1.0.0" resolve "^1.19.0" -"@rollup/pluginutils@^3.1.0": +"@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.1.0": version "3.1.0" resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b" integrity sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==