Improve SDK generation and wrap, promisify and simplify SDK client configuration
This commit is contained in:
parent
e7e800d91c
commit
08ae3e7ddc
|
@ -1,4 +1,4 @@
|
|||
src
|
||||
sdk
|
||||
docs
|
||||
node_modules
|
||||
dist
|
|
@ -4,3 +4,30 @@ 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.
|
||||
|
||||
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)
|
||||
}
|
||||
}))
|
||||
```
|
|
@ -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"
|
||||
|
|
|
@ -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: [
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
|
@ -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==
|
||||
|
|
Loading…
Reference in New Issue