Use promises when generating public API SDK rather than promisifying after

This commit is contained in:
Andrew Kingston 2022-09-15 15:38:09 +01:00
parent 6e4a525a90
commit 1fb4fcaeeb
3 changed files with 11 additions and 36 deletions

View File

@ -0,0 +1,3 @@
{
"usePromises": true
}

View File

@ -17,12 +17,13 @@ cp ../../server/specs/openapi.yaml ./
docker run --rm \
-v ${PWD}/openapi.yaml:/openapi.yml \
-v ${PWD}/generated:/generated \
-v ${PWD}/config.json:/config.json \
-u $(id -u):$(id -g) \
swaggerapi/swagger-codegen-cli-v3 generate \
-i /openapi.yml \
-l javascript \
-o /generated
--additional-properties useES6=false
-o /generated \
-c /config.json
# Use a subset of the generated files
mv generated/src ../sdk

View File

@ -15,37 +15,8 @@ export const configure = ({ apiKey, host }) => {
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 = promisify("QueriesApi")
export const RowsApi = promisify("RowsApi")
export const TablesApi = promisify("TablesApi")
export const UsersApi = promisify("UsersApi")
export const ApplicationsApi = new BudibaseApi.ApplicationsApi(ApiClient)
export const QueriesApi = new BudibaseApi.QueriesApi(ApiClient)
export const RowsApi = new BudibaseApi.RowsApi(ApiClient)
export const TablesApi = new BudibaseApi.TablesApi(ApiClient)
export const UsersApi = new BudibaseApi.UsersApi(ApiClient)