From 9fb4e20e21efee902291c1a429ca7d99b8ea72b8 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sat, 15 Oct 2022 18:25:26 +0200 Subject: [PATCH] fix: refactor deno client to use another openapi generator #743 --- .github/workflows/deno_on_release.yml | 3 +- deno-client/README.md | 3 ++ deno-client/build.sh | 10 +++++ deno-client/generate.sh | 6 --- deno-client/mod.ts | 59 +++++++++++++-------------- deno-client/openapi-bundled.yaml | 0 deno-client/openapitools.json | 7 ---- deno-client/package.json | 5 +++ deno-client/tsconfig.json | 15 +++++++ 9 files changed, 63 insertions(+), 45 deletions(-) create mode 100644 deno-client/README.md create mode 100755 deno-client/build.sh delete mode 100755 deno-client/generate.sh delete mode 100644 deno-client/openapi-bundled.yaml delete mode 100644 deno-client/openapitools.json create mode 100644 deno-client/package.json create mode 100644 deno-client/tsconfig.json diff --git a/.github/workflows/deno_on_release.yml b/.github/workflows/deno_on_release.yml index f194b96567..4eecb0934b 100644 --- a/.github/workflows/deno_on_release.yml +++ b/.github/workflows/deno_on_release.yml @@ -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 diff --git a/deno-client/README.md b/deno-client/README.md new file mode 100644 index 0000000000..4c6682d476 --- /dev/null +++ b/deno-client/README.md @@ -0,0 +1,3 @@ +# windmill-deno-client + +Deno client for Windmill \ No newline at end of file diff --git a/deno-client/build.sh b/deno-client/build.sh new file mode 100755 index 0000000000..219d610cd6 --- /dev/null +++ b/deno-client/build.sh @@ -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/ \ No newline at end of file diff --git a/deno-client/generate.sh b/deno-client/generate.sh deleted file mode 100755 index a087e72118..0000000000 --- a/deno-client/generate.sh +++ /dev/null @@ -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 diff --git a/deno-client/mod.ts b/deno-client/mod.ts index 49935e0414..4bf1f552ef 100644 --- a/deno-client/mod.ts +++ b/deno-client/mod.ts @@ -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 = 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 { - 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 { - 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 { * @returns variable value */ export async function getVariable(path: string): Promise { - 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 { } -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 + diff --git a/deno-client/openapi-bundled.yaml b/deno-client/openapi-bundled.yaml deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/deno-client/openapitools.json b/deno-client/openapitools.json deleted file mode 100644 index 27e6d53bb8..0000000000 --- a/deno-client/openapitools.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json", - "spaces": 2, - "generator-cli": { - "version": "6.2.0" - } -} diff --git a/deno-client/package.json b/deno-client/package.json new file mode 100644 index 0000000000..c2b20a2598 --- /dev/null +++ b/deno-client/package.json @@ -0,0 +1,5 @@ +{ + "denoify": { + "index": "index.ts" + } +} \ No newline at end of file diff --git a/deno-client/tsconfig.json b/deno-client/tsconfig.json new file mode 100644 index 0000000000..6aee05753b --- /dev/null +++ b/deno-client/tsconfig.json @@ -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" + ] + } \ No newline at end of file