|
| 1 | +import{readYaml}from"../../common/util"; |
| 2 | +import_from"lodash"; |
| 3 | +importpathfrom"path"; |
| 4 | +import{OpenAPIV3,OpenAPI}from"openapi-types"; |
| 5 | +import{ConfigToType,DataSourcePlugin}from"lowcoder-sdk/dataSource"; |
| 6 | +import{runOpenApi}from"../openApi"; |
| 7 | +import{parseOpenApi,ParseOpenApiOptions}from"../openApi/parse"; |
| 8 | + |
| 9 | +importspecfrom'./lowcoder.spec.json'; |
| 10 | + |
| 11 | +constdataSourceConfig={ |
| 12 | +type:"dataSource", |
| 13 | +params:[ |
| 14 | +{ |
| 15 | +key:"serverURL", |
| 16 | +type:"textInput", |
| 17 | +label:"Lowcoder API Service URL", |
| 18 | +rules:[{required:true}], |
| 19 | +placeholder:"https://<your-lowcoder-api-service>:port", |
| 20 | +tooltip:"Input the server url of your self-hosting instance or api-service.lowcoder.cloud if you are running your apps on the free public Community Edition Cloud Service.", |
| 21 | +}, |
| 22 | +{ |
| 23 | +"type":"groupTitle", |
| 24 | +"key":"API Key", |
| 25 | +"label":"Api Key Auth" |
| 26 | +}, |
| 27 | +{ |
| 28 | +type:"password", |
| 29 | +key:"bearerAuth.value", |
| 30 | +label:"Authorization", |
| 31 | +"tooltip":"API Key Authentication with a Bearer token. Copy your API Key here. (e.g. 'Bearer eyJhbGciO...')", |
| 32 | +"placeholder":"API Key Authentication with a Bearer token. Copy your API Key here. (e.g. 'Bearer eyJhbGciO...')" |
| 33 | +} |
| 34 | +] |
| 35 | +}asconst; |
| 36 | + |
| 37 | +constparseOptions:ParseOpenApiOptions={ |
| 38 | +actionLabel:(method:string,path:string,operation:OpenAPI.Operation)=>{ |
| 39 | +return_.upperFirst(operation.operationId||""); |
| 40 | +}, |
| 41 | +}; |
| 42 | + |
| 43 | +typeDataSourceConfigType=ConfigToType<typeofdataSourceConfig>; |
| 44 | + |
| 45 | +constlowcoderPlugin:DataSourcePlugin<any,DataSourceConfigType>={ |
| 46 | +id:"lowcoder", |
| 47 | +name:"Lowcoder API", |
| 48 | +icon:"lowcoder.svg", |
| 49 | +category:"api", |
| 50 | + dataSourceConfig, |
| 51 | +queryConfig:async()=>{ |
| 52 | +const{ actions, categories}=awaitparseOpenApi(specasunknownasOpenAPI.Document,parseOptions); |
| 53 | +return{ |
| 54 | +type:"query", |
| 55 | +label:"Action", |
| 56 | +categories:{ |
| 57 | +label:"Resources", |
| 58 | +items:categories, |
| 59 | +}, |
| 60 | + actions, |
| 61 | +}; |
| 62 | +}, |
| 63 | +run:function(actionData,dataSourceConfig):Promise<any>{ |
| 64 | +const{ serverURL, ...otherDataSourceConfig}=dataSourceConfig; |
| 65 | +console.log("Lowcoder API Plugin: run",serverURL,otherDataSourceConfig); |
| 66 | +construnApiDsConfig={ |
| 67 | +url:"", |
| 68 | +serverURL:serverURL, |
| 69 | +dynamicParamsConfig:otherDataSourceConfig, |
| 70 | +}; |
| 71 | +returnrunOpenApi(actionData,runApiDsConfig,specasOpenAPIV3.Document); |
| 72 | +}, |
| 73 | +}; |
| 74 | + |
| 75 | +exportdefaultlowcoderPlugin; |