fix: refactor deno client to use another openapi generator #743

This commit is contained in:
Ruben Fiszel
2022-10-15 18:25:26 +02:00
committed by GitHub
parent e7e8a4cf38
commit 9fb4e20e21
9 changed files with 63 additions and 45 deletions
+1 -2
View File
@@ -9,14 +9,13 @@ env:
jobs:
build_deno_and_push_to_repo:
runs-on: ubuntu-latest
container: openapitools/openapi-generator-cli:v6.0.0-beta
steps:
- uses: actions/checkout@v3
- name: generate_deno
run: |
cd deno-client
rm .gitignore
./generate.sh
./build.sh
- name: Pushes to another repository
id: push_directory
uses: cpina/github-action-push-to-another-repository@devel
+3
View File
@@ -0,0 +1,3 @@
# windmill-deno-client
Deno client for Windmill
+10
View File
@@ -0,0 +1,10 @@
#!/bin/bash
set -e
npx --yes openapi-typescript-codegen --input ../backend/openapi.yaml \
--output ./src --useOptions \
&& sed -i '213 i \\ request.referrerPolicy = \"no-referrer\"\n' src/core/request.ts
npx --yes denoify
rm -rf windmill-api
mv deno_dist windmill-api
rm -rf src/
-6
View File
@@ -1,6 +0,0 @@
#!/bin/bash
set -e
/usr/local/bin/docker-entrypoint.sh generate -i ../backend/openapi.yaml -g typescript --additional-properties platform=deno -o windmill-api --skip-validate-spec
sed -i 's/this\.type = "Job";//' windmill-api/models/Job.ts
sed -i -z 's/public static parse(rawData: string, mediaType: string | undefined) {/public static parse(rawData: string, mediaType: string | undefined) {\n if (mediaType === "text\/plain") { return rawData }/' windmill-api/models/ObjectSerializer.ts
+29 -30
View File
@@ -1,10 +1,10 @@
import { ResourceApi, VariableApi, ServerConfiguration, JobApi } from './windmill-api/index.ts'
import { createConfiguration, type Configuration as Configuration } from './windmill-api/configuration.ts'
import { ResourceService, VariableService } from './windmill-api/index.ts'
import { OpenAPI } from './windmill-api/index.ts'
export {
AdminApi, AuditApi, FlowApi, GranularAclApi, GroupApi,
JobApi, ResourceApi, VariableApi, ScriptApi, ScheduleApi, SettingsApi,
UserApi, WorkspaceApi
AdminService, AuditService, FlowService, GranularAclService, GroupService,
JobService, ResourceService, VariableService, ScriptService, ScheduleService, SettingsService,
UserService, WorkspaceService
} from './windmill-api/index.ts'
export { pgSql, pgClient } from './pg.ts'
@@ -14,23 +14,22 @@ export type Email = string
export type Base64 = string
export type Resource<S extends string> = any
type Conf = Configuration & { workspace_id: string }
export const SHARED_FOLDER = '/shared'
export function setClient(token: string, baseUrl: string) {
OpenAPI.WITH_CREDENTIALS = true
OpenAPI.TOKEN = token
OpenAPI.BASE = baseUrl
}
setClient(Deno.env.get("WM_TOKEN") ?? 'no_token', Deno.env.get("BASE_INTERNAL_URL") ?? Deno.env.get("BASE_URL") ?? 'http://localhost:8000')
/**
* Create a client configuration from env variables
* @returns client configuration
*/
export function createConf(): Conf {
const token = Deno.env.get("WM_TOKEN") ?? 'no_token'
const base_url = Deno.env.get("BASE_INTERNAL_URL") ?? 'http://localhost:8000'
return {
...createConfiguration({
baseServer: new ServerConfiguration(`${base_url}/api`, {}),
authMethods: { bearerAuth: { tokenProvider: { getToken() { return token } } } },
}), workspace_id: Deno.env.get("WM_WORKSPACE") ?? 'no_workspace'
}
export function getWorkspace(): string {
return Deno.env.get("WM_WORKSPACE") ?? 'no_workspace'
}
/**
@@ -40,9 +39,9 @@ export function createConf(): Conf {
* @returns resource value
*/
export async function getResource(path: string, undefinedIfEmpty?: boolean): Promise<any> {
const conf = createConf()
const workspace = getWorkspace()
try {
const resource = await new ResourceApi(conf).getResource(conf.workspace_id, path)
const resource = await ResourceService.getResource({ workspace, path })
return await _transformLeaf(resource.value)
} catch (e) {
if (undefinedIfEmpty && e.code === 404) {
@@ -75,12 +74,11 @@ export function getInternalStatePath(suffix?: string): string {
* @param initializeToTypeIfNotExist if the resource does not exist, initialize it with this type
*/
export async function setResource(path: string, value: any, initializeToTypeIfNotExist?: string): Promise<void> {
const conf = createConf()
const resourceApi = new ResourceApi(conf)
if (await resourceApi.existsResource(conf.workspace_id, path)) {
await resourceApi.updateResource(conf.workspace_id, path, { value })
const workspace = getWorkspace()
if (await ResourceService.existsResource({ workspace, path })) {
await ResourceService.updateResource({ workspace, path, requestBody: { value } })
} else if (initializeToTypeIfNotExist) {
await resourceApi.createResource(conf.workspace_id, { path, value, resourceType: initializeToTypeIfNotExist })
await ResourceService.createResource({ workspace, requestBody: { path, value, resource_type: initializeToTypeIfNotExist } })
} else {
throw Error(`Resource at path ${path} does not exist and no type was provided to initialize it`)
}
@@ -109,8 +107,8 @@ export async function getInternalState(suffix?: string): Promise<any> {
* @returns variable value
*/
export async function getVariable(path: string): Promise<string | undefined> {
const conf = createConf()
const variable = await new VariableApi(conf).getVariable(conf.workspace_id, path)
const workspace = getWorkspace()
const variable = await VariableService.getVariable({ workspace, path })
return variable.value
}
@@ -140,10 +138,10 @@ export async function databaseUrlFromResource(path: string): Promise<string> {
}
export async function genNounceAndHmac(conf: Conf, jobId: string) {
export async function genNounceAndHmac(workspace: string, jobId: string) {
const nounce = Math.floor(Math.random() * 4294967295);
const sig = await fetch(Deno.env.get("WM_BASE_URL") +
`/api/w/${conf.workspace_id}/jobs/job_signature/${jobId}/${nounce}?token=${Deno.env.get("WM_TOKEN")}`)
`/api/w/${workspace}/jobs/job_signature/${jobId}/${nounce}?token=${Deno.env.get("WM_TOKEN")}`)
return {
nounce,
signature: await sig.text()
@@ -151,13 +149,14 @@ export async function genNounceAndHmac(conf: Conf, jobId: string) {
}
export async function getResumeEndpoints() {
const conf = createConf();
const workspace = getWorkspace()
const { nounce, signature } = await genNounceAndHmac(
conf,
workspace,
Deno.env.get("WM_JOB_ID") ?? "no_job_id",
);
const url_prefix = Deno.env.get("WM_BASE_URL") +
`/api/w/${conf.workspace_id}/jobs/`;
`/api/w/${workspace}/jobs/`;
function getResumeUrl(op: string) {
return url_prefix +
View File
-7
View File
@@ -1,7 +0,0 @@
{
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "6.2.0"
}
}
+5
View File
@@ -0,0 +1,5 @@
{
"denoify": {
"index": "index.ts"
}
}
+15
View File
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es6",
"module": "ES6",
"lib": ["es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"exclude": [
"node_modules",
"dist"
]
}