UNPKG

@bapp/auto-api-client

Version:

Client to consume BAPP auto API, based on content types with type inference

84 lines (67 loc) 1.73 kB
#!/usr/bin/env node import fs from "fs"; import axios from "axios"; import { compile } from "json-schema-to-typescript"; const [, , host, outputPath] = process.argv; if (!host) throw new Error("No host uri was passed"); if (!outputPath) throw new Error("No outputPath was passed"); try { new URL(host); } catch (error) { throw new Error("Invalid host uri"); } const requestHostSchema = async (host) => { console.info("Requesting host schema..."); const res = await axios.post( "/tasks", { code: "bapp_framework.UIContentTypes", }, { baseURL: host, } ); return res.data; }; const generateHostSchema = async (host) => { const hostSchema = await requestHostSchema(host); return { properties: { ContentType: { type: "string", enum: Object.keys(hostSchema.content_types), }, TaskCode: { type: "string", enum: Object.keys(hostSchema.tasks), }, entities: { type: "object", additionalProperties: { type: "object", properties: { filter: { type: "object", additionalProperties: {}, }, body: {}, response: {}, }, }, }, }, required: ["ContentType", "TaskCode"], }; }; const init = async () => { const hostSchema = await generateHostSchema(host); console.info("Compiling host schema to Typescript types..."); const types = await compile(hostSchema, "Mapping", { unknownAny: false, additionalProperties: false, }); console.info("Writting generated file..."); await fs.promises.writeFile(outputPath, types); console.info("Finished!"); }; init();